diff --git a/src/io/github/sammers/pla/logic/Calculator.java b/src/io/github/sammers/pla/logic/Calculator.java index be5aa864..91180617 100644 --- a/src/io/github/sammers/pla/logic/Calculator.java +++ b/src/io/github/sammers/pla/logic/Calculator.java @@ -9,6 +9,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.time.Duration; +import java.time.ZoneId; +import java.time.ZonedDateTime; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Function; @@ -64,6 +67,7 @@ public static Maybe calcDiffAndCombine(String bracket, String regi } }); } + public static SnapshotDiff calculateDiff(Snapshot oldChars, Snapshot newChars, String bracket) { return calculateDiff(oldChars, newChars, bracket, true); } @@ -83,7 +87,7 @@ public static SnapshotDiff calculateDiff(Snapshot oldChars, Snapshot newChars, S } CharAndDiff e; if (oldChar == null) { - if(newIsZero) { + if (newIsZero) { e = new CharAndDiff(newChar, new Diff(0L, 0L, 0L, 0L, newChars.timestamp())); } else { e = new CharAndDiff(newChar, @@ -157,7 +161,7 @@ public static double pPresence(List chars, int total) { public static Meta calculateMeta(SnapshotDiff snapshot, String role, String bracket, double... ratios) { Set acceptedSpecs; - if (role.equals("all")){ + if (role.equals("all")) { acceptedSpecs = Spec.ALL_SPECS; } else if (role.equals("dps")) { acceptedSpecs = Spec.DPS_SPECS; @@ -177,9 +181,9 @@ public static Meta calculateMeta(SnapshotDiff snapshot, String role, String brac .sorted(Comparator.comparing((CharAndDiff o) -> o.character().rating()).reversed()).toList(); if (bracket.equals("shuffle")) { int maxMinRating = totalSortedRoleList.stream().collect(Collectors.groupingBy(character -> character.character().fullSpec(), Collectors.toList())) - .entrySet().stream() - .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().stream().mapToLong(c -> c.character().rating()).min().orElse(0))) - .values().stream().mapToInt(Long::intValue).max().orElse(0); + .entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().stream().mapToLong(c -> c.character().rating()).min().orElse(0))) + .values().stream().mapToInt(Long::intValue).max().orElse(0); totalSortedRoleList = totalSortedRoleList.stream().filter(c -> c.character().rating() >= maxMinRating).toList(); } LinkedList fsrtList = new LinkedList<>(totalSortedRoleList); @@ -217,20 +221,20 @@ public static Meta calculateMeta(SnapshotDiff snapshot, String role, String brac } List specs = diviedLists.stream().flatMap(List::stream).collect(Collectors.groupingBy(Spec::specName, Collectors.toList())) .entrySet().stream().map(entry -> { - Map res = new HashMap<>(); - for(Spec spec : entry.getValue()) { - res.putAll(spec.winRates()); - } - for (Double ratio: ratios) { - if(!res.containsKey(String.format("%.3f_win_rate", ratio))) { - res.put(String.format("%.3f_win_rate", ratio), 0.0); + Map res = new HashMap<>(); + for (Spec spec : entry.getValue()) { + res.putAll(spec.winRates()); } - if(!res.containsKey(String.format("%.3f_presence", ratio))) { - res.put(String.format("%.3f_presence", ratio), 0.0); + for (Double ratio : ratios) { + if (!res.containsKey(String.format("%.3f_win_rate", ratio))) { + res.put(String.format("%.3f_win_rate", ratio), 0.0); + } + if (!res.containsKey(String.format("%.3f_presence", ratio))) { + res.put(String.format("%.3f_presence", ratio), 0.0); + } } - } return new Spec(entry.getKey(), res); - }).toList(); + }).toList(); return new Meta(Map.of(), sizing, specs); } @@ -248,7 +252,7 @@ public static void indexCharAlts(Map> alts, WowAPI if (key == -1) { return null; } - if (value == null) { + if (value == null) { value = new TreeSet<>(Comparator.comparing(WowAPICharacter::id)); } value.add(character); @@ -270,4 +274,20 @@ public static String realmCalc(String realm) { String withCapFirst = realm.substring(0, 1).toUpperCase() + realm.substring(1).toLowerCase(); return withCapFirst.replace(" ", "-"); } + + public static int minutesTillNextHour() { + ZoneId zone = ZoneId.systemDefault(); + ZonedDateTime now = ZonedDateTime.now(zone); + ZonedDateTime nextHour = now.withMinute(0).withSecond(0).withNano(0).plusHours(1).plusMinutes(1); + Duration duration = Duration.between(now, nextHour); + return (int) duration.toMinutes(); + } + + public static int minutesTill5am() { + ZoneId zone = ZoneId.systemDefault(); + ZonedDateTime now = ZonedDateTime.now(zone); + ZonedDateTime nextHour = now.withMinute(0).withSecond(0).withHour(5).plusDays(1); + Duration duration = Duration.between(now, nextHour); + return (int) duration.toMinutes(); + } } diff --git a/src/io/github/sammers/pla/logic/CharUpdater.java b/src/io/github/sammers/pla/logic/CharUpdater.java index 5beb4700..a0b719ec 100644 --- a/src/io/github/sammers/pla/logic/CharUpdater.java +++ b/src/io/github/sammers/pla/logic/CharUpdater.java @@ -111,11 +111,12 @@ public Completable updateChars(List nickNames, String region) { } public Completable updateChar(String region, String nickName) { - return api.character(region, nickName).flatMapCompletable(wowAPICharacter -> { - Calculator.indexCharAlts(altsCache, wowAPICharacter); - characterCache.put(nickName, wowAPICharacter); - charSearchIndex.insertNickNames(new SearchResult(nickName, region, wowAPICharacter.clazz())); - return db.upsertCharacter(wowAPICharacter).ignoreElement(); - }).onErrorComplete(); + return Completable.defer(() -> api.character(region, nickName).flatMapCompletable(wowAPICharacter -> { + Calculator.indexCharAlts(altsCache, wowAPICharacter); + characterCache.put(nickName, wowAPICharacter); + charSearchIndex.insertNickNames(new SearchResult(nickName, region, wowAPICharacter.clazz())); + return db.upsertCharacter(wowAPICharacter).ignoreElement(); + }).onErrorComplete() + ); } } diff --git a/src/io/github/sammers/pla/logic/Ladder.java b/src/io/github/sammers/pla/logic/Ladder.java index e94383e7..054e2c0a 100644 --- a/src/io/github/sammers/pla/logic/Ladder.java +++ b/src/io/github/sammers/pla/logic/Ladder.java @@ -36,6 +36,8 @@ import static io.github.sammers.pla.Main.VTHREAD_SCHEDULER; import static io.github.sammers.pla.blizzard.BlizzardAPI.realRegion; +import static io.github.sammers.pla.logic.Calculator.minutesTill5am; +import static io.github.sammers.pla.logic.Calculator.minutesTillNextHour; public class Ladder { @@ -161,7 +163,7 @@ public void start() { runDataUpdater(US, runDataUpdater(EU, Observable.defer(() -> { - int initialDelay = minutesTillNextHour(); + int initialDelay = Calculator.minutesTillNextHour(); return Observable.interval(initialDelay, 60, TimeUnit.MINUTES) .observeOn(VTHREAD_SCHEDULER) .subscribeOn(VTHREAD_SCHEDULER); @@ -184,8 +186,8 @@ private Observable runDataUpdater(String region, Observable tic .flatMapSingle(tick -> twoVTwo(region)) .flatMapSingle(tick -> battlegrounds(region)) .flatMapSingle(tick -> shuffle(region)) - .flatMapSingle(tick -> calculateMeta(region).andThen(Single.just(tick))) .flatMapSingle(tick -> loadCutoffs(region).andThen(Single.just(tick))) + .flatMapSingle(tick -> calculateMeta(region).andThen(Single.just(tick))) .flatMapSingle(tick -> { log.info("Data updater for " + region + " has been finished"); return Single.just(tick); @@ -587,7 +589,7 @@ public Completable loadWowCharApiData(String region) { List list = characters.stream().map(charz -> new SearchResult(charz.fullName(), charz.region(), charz.clazz())).toList(); charSearchIndex.insertNickNames(list); log.info("Character data size={} for region={} has been loaded to cache in {} ms", characters.size(), region, (System.nanoTime() - tick) / 1000000); -// VTHREAD_SCHEDULER.schedulePeriodicallyDirect(() -> charUpdater.updateCharsInfinite(region).subscribe(), 0, 24, TimeUnit.HOURS); + VTHREAD_SCHEDULER.schedulePeriodicallyDirect(() -> charUpdater.updateCharsInfinite(region).subscribe(), minutesTill5am(), 24 * 60, TimeUnit.MINUTES); emitter.onComplete(); }); })); @@ -778,12 +780,4 @@ public Completable newDataOnBracket(String bracket, String region, Snapshot newC return Completable.complete(); } } - - public static int minutesTillNextHour() { - ZoneId zone = ZoneId.systemDefault(); - ZonedDateTime now = ZonedDateTime.now(zone); - ZonedDateTime nextHour = now.withMinute(0).withSecond(0).withNano(0).plusHours(1).plusMinutes(1); - Duration duration = Duration.between(now, nextHour); - return (int) duration.toMinutes(); - } }