From 98223a32934ea91fadfc09ae3a15257d3ed33a45 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 5 Aug 2024 04:15:01 +0100 Subject: [PATCH] api: change how some data points are got for histories --- .../model/user/history/HistoryPoint.java | 9 ++++ .../platform/impl/ScoreSaberPlatform.java | 23 +++++----- .../repository/ScoreRepository.java | 43 ++++++++++++++++++- .../fascinated/services/PlatformService.java | 4 +- .../cc/fascinated/services/ScoreService.java | 40 +++++++++++++++++ 5 files changed, 105 insertions(+), 14 deletions(-) diff --git a/API/src/main/java/cc/fascinated/model/user/history/HistoryPoint.java b/API/src/main/java/cc/fascinated/model/user/history/HistoryPoint.java index 22652aa..3028da2 100644 --- a/API/src/main/java/cc/fascinated/model/user/history/HistoryPoint.java +++ b/API/src/main/java/cc/fascinated/model/user/history/HistoryPoint.java @@ -9,6 +9,8 @@ import lombok.Setter; @Getter @Setter public class HistoryPoint { + // These are data points that are provided by ScoreSaber, and may not always be here + /** * The rank of the player. */ @@ -24,6 +26,8 @@ public class HistoryPoint { */ private Double pp; + // Below are data points that are provided by us, therefore they will always be here + /** * Play count of all the player's scores. */ @@ -34,6 +38,11 @@ public class HistoryPoint { */ private Integer totalRankedPlayCount; + /** + * Play count of all the player's unranked scores. + */ + private Integer totalUnrankedPlayCount; + /** * Play count for this day's unranked scores. */ diff --git a/API/src/main/java/cc/fascinated/platform/impl/ScoreSaberPlatform.java b/API/src/main/java/cc/fascinated/platform/impl/ScoreSaberPlatform.java index c2b2ae1..06484da 100644 --- a/API/src/main/java/cc/fascinated/platform/impl/ScoreSaberPlatform.java +++ b/API/src/main/java/cc/fascinated/platform/impl/ScoreSaberPlatform.java @@ -48,9 +48,14 @@ public class ScoreSaberPlatform extends Platform { @NonNull private final UserService userService; + /** + * The score service to use + */ + @NonNull + private final ScoreService scoreService; @Autowired - public ScoreSaberPlatform(@NonNull ScoreSaberService scoreSaberService, @NonNull UserService userService) { + public ScoreSaberPlatform(@NonNull ScoreSaberService scoreSaberService, @NonNull UserService userService, @NonNull ScoreService scoreService) { super(Platforms.SCORESABER, 1, Map.of( 1, new CurvePoint[]{ new CurvePoint(0, 0), @@ -94,6 +99,7 @@ public class ScoreSaberPlatform extends Platform { )); this.scoreSaberService = scoreSaberService; this.userService = userService; + this.scoreService = scoreService; } /** @@ -140,16 +146,13 @@ public class ScoreSaberPlatform extends Platform { public void trackPlayerMetrics() { Date date = DateUtils.getMidnightToday(); for (User user : this.userService.getUsers(false)) { - if (!user.isLinkedAccount()) { // Check if the user has linked their account - continue; - } - ScoreSaberAccountToken account = scoreSaberService.getAccount(user); // Get the account from the ScoreSaber API HistoryPoint history = user.getHistory().getHistoryForDate(date); - history.setPp(account.getPp()); - history.setRank(account.getRank()); - history.setCountryRank(account.getCountryRank()); - history.setTotalPlayCount(account.getScoreStats().getTotalPlayCount()); - history.setTotalRankedPlayCount((int) account.getScoreStats().getTotalRankedScore()); + if (user.isLinkedAccount()) { // Check if the user has linked their account + ScoreSaberAccountToken account = scoreSaberService.getAccount(user); // Get the account from the ScoreSaber API + history.setPp(account.getPp()); + history.setRank(account.getRank()); + history.setCountryRank(account.getCountryRank()); + } this.userService.saveUser(user); // Save the user } } diff --git a/API/src/main/java/cc/fascinated/repository/ScoreRepository.java b/API/src/main/java/cc/fascinated/repository/ScoreRepository.java index 928181d..6190f8a 100644 --- a/API/src/main/java/cc/fascinated/repository/ScoreRepository.java +++ b/API/src/main/java/cc/fascinated/repository/ScoreRepository.java @@ -106,7 +106,7 @@ public interface ScoreRepository extends MongoRepository { "{ $match: { platform: ?0 } }", "{ $count: 'total' }" }) - int getTotalScores(@NonNull Platform.Platforms platform); + Integer getTotalScores(@NonNull Platform.Platforms platform); /** * Gets the total ranked scores for the platform. @@ -118,5 +118,44 @@ public interface ScoreRepository extends MongoRepository { "{ $match: { platform: ?0, pp: { $gt: 0 } } }", "{ $count: 'total' }" }) - int getTotalRankedScores(@NonNull Platform.Platforms platform); + Integer getTotalRankedScores(@NonNull Platform.Platforms platform); + + /** + * Gets the total player scores for the platform. + * + * @param platform the platform to get the scores from + * @param playerId the player id to get the scores from + * @return the total player scores + */ + @Aggregation(pipeline = { + "{ $match: { platform: ?0, playerId: ?1 } }", + "{ $count: 'total' }" + }) + Integer getTotalPlayerScores(@NonNull Platform.Platforms platform, @NonNull String playerId); + + /** + * Gets the total player ranked scores for the platform. + * + * @param platform the platform to get the scores from + * @param playerId the player id to get the scores from + * @return the total player ranked scores + */ + @Aggregation(pipeline = { + "{ $match: { platform: ?0, playerId: ?1, pp: { $gt: 0 } } }", + "{ $count: 'total' }" + }) + Integer getTotalPlayerRankedScores(@NonNull Platform.Platforms platform, @NonNull String playerId); + + /** + * Gets the total player unranked scores for the platform. + * + * @param platform the platform to get the scores from + * @param playerId the player id to get the scores from + * @return the total player unranked scores + */ + @Aggregation(pipeline = { + "{ $match: { platform: ?0, playerId: ?1, pp: { $eq: null } } }", + "{ $count: 'total' }" + }) + Integer getTotalPlayerUnrankedScores(@NonNull Platform.Platforms platform, @NonNull String playerId); } diff --git a/API/src/main/java/cc/fascinated/services/PlatformService.java b/API/src/main/java/cc/fascinated/services/PlatformService.java index 5734cef..321ff3f 100644 --- a/API/src/main/java/cc/fascinated/services/PlatformService.java +++ b/API/src/main/java/cc/fascinated/services/PlatformService.java @@ -58,10 +58,10 @@ public class PlatformService { /** * Updates the platform players. *

- * This method is scheduled to run every day at midnight. + * This method is scheduled to run every day at 23:59. *

*/ - @Scheduled(cron = "0 0 0 * * *") + @Scheduled(cron = "0 59 23 * * *") public void updatePlayerMetrics() { log.info("Updating %s platform player metrics...".formatted(this.platforms.size())); for (Platform platform : this.platforms) { diff --git a/API/src/main/java/cc/fascinated/services/ScoreService.java b/API/src/main/java/cc/fascinated/services/ScoreService.java index 1d09b8c..bd21188 100644 --- a/API/src/main/java/cc/fascinated/services/ScoreService.java +++ b/API/src/main/java/cc/fascinated/services/ScoreService.java @@ -148,6 +148,42 @@ public class ScoreService { ); } + /** + * Gets the total scores for the platform and user. + * + * @param platform The platform to get the scores from. + * @param user The user to get the scores from. + * @return The total scores. + */ + public int getTotalScores(@NonNull Platform.Platforms platform, @NonNull User user) { + Integer totalPlayerScores = scoreRepository.getTotalPlayerScores(platform, user.getSteamId()); + return totalPlayerScores == null ? 0 : totalPlayerScores; + } + + /** + * Gets the total ranked scores for the platform and user. + * + * @param platform The platform to get the scores from. + * @param user The user to get the scores from. + * @return The total ranked scores. + */ + public int getTotalRankedScores(@NonNull Platform.Platforms platform, @NonNull User user) { + Integer totalPlayerRankedScores = scoreRepository.getTotalPlayerRankedScores(platform, user.getSteamId()); + return totalPlayerRankedScores == null ? 0 : totalPlayerRankedScores; + } + + /** + * Gets the total unranked scores for the platform and user. + * + * @param platform The platform to get the scores from. + * @param user The user to get the scores from. + * @return The total unranked scores. + */ + public int getTotalUnrankedScores(@NonNull Platform.Platforms platform, @NonNull User user) { + Integer totalPlayerUnrankedScores = scoreRepository.getTotalPlayerUnrankedScores(platform, user.getSteamId()); + return totalPlayerUnrankedScores == null ? 0 : totalPlayerUnrankedScores; + } + /** * Tracks a ScoreSaber score. * @@ -274,6 +310,10 @@ public class ScoreService { } else { todayHistory.incrementUnrankedPlayCount(); } + Platform.Platforms platform = score.getPlatform(); + todayHistory.setTotalPlayCount(this.getTotalScores(platform, user)); // Get the total scores for the platform + todayHistory.setTotalRankedPlayCount(this.getTotalRankedScores(platform, user)); // Get the total ranked scores for the platform + todayHistory.setTotalUnrankedPlayCount(this.getTotalUnrankedScores(platform, user)); // Get the total unranked scores for the platform userService.saveUser(user); // Save the user scoreRepository.save(score); // Save the score }