From 0a46c218ea076704a77f7170f423e6ed576e0a12 Mon Sep 17 00:00:00 2001 From: Sammers21 Date: Thu, 4 Jan 2024 05:12:13 +0300 Subject: [PATCH] releaseb improve char updater --- src/io/github/sammers/pla/logic/CharUpdater.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/io/github/sammers/pla/logic/CharUpdater.java b/src/io/github/sammers/pla/logic/CharUpdater.java index be99f6aa..84e01cb3 100644 --- a/src/io/github/sammers/pla/logic/CharUpdater.java +++ b/src/io/github/sammers/pla/logic/CharUpdater.java @@ -21,6 +21,7 @@ import java.time.temporal.ChronoUnit; import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Stream; @@ -101,6 +102,12 @@ public Completable updateCharacters(String region, .sorted(Comparator.comparingLong(entry -> -entry.getValue().getValue1())) .map(Map.Entry::getKey) .toList(); + List> randomNotUpdatedChars = characterCache.values().stream() + .filter(wowAPICharacter -> tick - wowAPICharacter.lastUpdatedUTCms() > units.toMillis(timeWithoutUpdateMin)) + .sorted((o1, o2) -> ThreadLocalRandom.current().nextInt(-1, 2)) + .map(wowAPICharacter -> Pair.with(wowAPICharacter.name(), wowAPICharacter.realm())) + .limit(10_000) + .toList(); // merge in a following way: // 1. put one from newCharsSorted // 2. put one from existingSorted @@ -108,6 +115,7 @@ public Completable updateCharacters(String region, List> merged = new ArrayList<>(newCharsSorted.size() + existingSorted.size()); Iterator> newCharsIt = newCharsSorted.iterator(); Iterator> existingIt = existingSorted.iterator(); + Iterator> randomNotUpdatedIt = randomNotUpdatedChars.iterator(); while (newCharsIt.hasNext() || existingIt.hasNext()) { if (newCharsIt.hasNext()) { merged.add(newCharsIt.next()); @@ -115,10 +123,13 @@ public Completable updateCharacters(String region, if (existingIt.hasNext()) { merged.add(existingIt.next()); } + if (randomNotUpdatedIt.hasNext()) { + merged.add(randomNotUpdatedIt.next()); + } } // merged into list of nickNames - log.info("There is {} new chars and {} existing chars in region {} that need to be updated. We have {} {} to do it", - newCharsSorted.size(), existingSorted.size(), region, timeout, timeoutUnits.name()); + log.info("There is {} new chars, {} existing chars and {} random not updated chars in region {} that need to be updated. We have {} {} to do it", + newCharsSorted.size(), existingSorted.size(), randomNotUpdatedChars.size(), region, timeout, timeoutUnits.name()); // transform merged list into list of completables return Flowable.fromIterable( merged.stream()