From 58bdc6f41404abd5a6267f0ff8092f487827a3b1 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 1 Aug 2024 02:13:34 +0100 Subject: [PATCH] add missing leaderboards --- .../platform/impl/ScoreSaberPlatform.java | 42 ++++++++++--------- .../fascinated/services/PlatformService.java | 29 +++++++------ 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/main/java/cc/fascinated/platform/impl/ScoreSaberPlatform.java b/src/main/java/cc/fascinated/platform/impl/ScoreSaberPlatform.java index 2c26d2f..2d7a35e 100644 --- a/src/main/java/cc/fascinated/platform/impl/ScoreSaberPlatform.java +++ b/src/main/java/cc/fascinated/platform/impl/ScoreSaberPlatform.java @@ -33,7 +33,7 @@ public class ScoreSaberPlatform extends Platform { /** * Delay in ms for requests per minute. */ - private static final long UPDATE_DELAY = 1000L / 150L; // 150 requests per minute + private static final long UPDATE_DELAY = 1000L / 250L; // 150 requests per minute /** * The base multiplier for stars. @@ -192,21 +192,31 @@ public class ScoreSaberPlatform extends Platform { public void updateLeaderboards() { // TODO: PUSH THIS List scores = this.trackedScoreService.getTrackedScores(this.getPlatform(), true); - Map rankedLeaderboards = new HashMap<>(); + Map leaderboards = new HashMap<>(); for (ScoreSaberLeaderboardPageToken rankedLeaderboard : this.scoreSaberService.getRankedLeaderboards()) { for (ScoreSaberLeaderboardToken leaderboard : rankedLeaderboard.getLeaderboards()) { - rankedLeaderboards.put(leaderboard.getId(), leaderboard); + leaderboards.put(leaderboard.getId(), leaderboard); } } + + // Add any missing leaderboards + for (TrackedScore score : scores) { + if (leaderboards.containsKey(score.getLeaderboardId())) { + continue; + } + + ScoreSaberLeaderboardToken leaderboard = this.scoreSaberService.getLeaderboard(score.getLeaderboardId(), true); + leaderboards.put(leaderboard.getId(), leaderboard); + } + log.info("Updating {} leaderboards for platform '{}'", - rankedLeaderboards.size(), + leaderboards.size(), this.getPlatform().getPlatformName() ); // Update the leaderboards int finished = 0; - - for (Map.Entry leaderboardEntry : rankedLeaderboards.entrySet()) { + for (Map.Entry leaderboardEntry : leaderboards.entrySet()) { String id = leaderboardEntry.getKey(); ScoreSaberLeaderboardToken leaderboard = leaderboardEntry.getValue(); @@ -220,11 +230,6 @@ public class ScoreSaberPlatform extends Platform { } try { - if (leaderboard == null) { - log.warn("Failed to update leaderboard '{}' for platform '{}'", id, this.getPlatform().getPlatformName()); - continue; - } - List toUpdate = scores.stream().filter(score -> { if (!score.getLeaderboardId().equals(id)) { // Check if the leaderboard ID matches return false; @@ -246,14 +251,13 @@ public class ScoreSaberPlatform extends Platform { } finished++; - log.info("Updated leaderboard '{}' for platform '{}', changed {} scores. ({}/{})", - leaderboard.getSongName(), - this.getPlatform().getPlatformName(), - toUpdate.size(), - - finished, - rankedLeaderboards.size() - ); + if (finished % 100 == 0) { + log.info("Updated {}/{} leaderboards for platform '{}'", + finished, + leaderboards.size(), + this.getPlatform().getPlatformName() + ); + } } catch (Exception ex) { log.error("An error occurred while updating leaderboard '{}'", id, ex); } diff --git a/src/main/java/cc/fascinated/services/PlatformService.java b/src/main/java/cc/fascinated/services/PlatformService.java index 4350750..0f03d0b 100644 --- a/src/main/java/cc/fascinated/services/PlatformService.java +++ b/src/main/java/cc/fascinated/services/PlatformService.java @@ -30,16 +30,8 @@ public class PlatformService { */ private final List platforms = new ArrayList<>(); - /** - * The tracked score repository to use. - */ - @NonNull - private final TrackedScoreRepository trackedScoreRepository; - @Autowired - public PlatformService(@NonNull ApplicationContext context, @NonNull TrackedScoreRepository trackedScoreRepository) { - this.trackedScoreRepository = trackedScoreRepository; - + public PlatformService(@NonNull ApplicationContext context) { log.info("Registering platforms..."); registerPlatform(context.getBean(ScoreSaberPlatform.class)); log.info("Loaded %s platforms.".formatted(this.platforms.size())); @@ -123,17 +115,24 @@ public class PlatformService { Document finalDocument = document; EXECUTOR_SERVICE.execute(() -> { platform.updateLeaderboards(); - // Update the document - finalDocument.put("currentCurveVersion", platform.getCurrentCurveVersion()); - MongoService.INSTANCE.getPlatformsCollection().replaceOne(Filters.eq("_id", platform.getPlatform().getPlatformName()), finalDocument); + this.savePlatform(platform, finalDocument); }); // Update the leaderboards } else { - // Update the document - document.put("currentCurveVersion", platform.getCurrentCurveVersion()); - MongoService.INSTANCE.getPlatformsCollection().replaceOne(Filters.eq("_id", platform.getPlatform().getPlatformName()), document); + this.savePlatform(platform, document); } } + /** + * Saves the platform. + * + * @param platform the platform to save + * @param document the document to save + */ + public void savePlatform(Platform platform, Document document) { + document.put("currentCurveVersion", platform.getCurrentCurveVersion()); + MongoService.INSTANCE.getPlatformsCollection().replaceOne(Filters.eq("_id", platform.getPlatform().getPlatformName()), document); + } + /** * Gets the ScoreSaber platform. *