Skip to content

Commit

Permalink
releaseb improve char updater
Browse files Browse the repository at this point in the history
  • Loading branch information
Sammers21 committed Jan 4, 2024
1 parent 726bdae commit 0a46c21
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/io/github/sammers/pla/logic/CharUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -101,24 +102,34 @@ public Completable updateCharacters(String region,
.sorted(Comparator.comparingLong(entry -> -entry.getValue().getValue1()))
.map(Map.Entry::getKey)
.toList();
List<Pair<String, String>> 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
// 3. repeat until both lists are empty
List<Pair<String, String>> merged = new ArrayList<>(newCharsSorted.size() + existingSorted.size());
Iterator<Pair<String, String>> newCharsIt = newCharsSorted.iterator();
Iterator<Pair<String, String>> existingIt = existingSorted.iterator();
Iterator<Pair<String, String>> randomNotUpdatedIt = randomNotUpdatedChars.iterator();
while (newCharsIt.hasNext() || existingIt.hasNext()) {
if (newCharsIt.hasNext()) {
merged.add(newCharsIt.next());
}
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()
Expand Down

0 comments on commit 0a46c21

Please sign in to comment.