api: change how some data points are got for histories
Some checks failed
Deploy API / docker (17, 3.8.5) (push) Failing after 31s

This commit is contained in:
Lee 2024-08-05 04:15:01 +01:00
parent 7b0c9f54ff
commit 98223a3293
5 changed files with 105 additions and 14 deletions

@ -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.
*/

@ -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
}
}

@ -106,7 +106,7 @@ public interface ScoreRepository extends MongoRepository<Score, String> {
"{ $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<Score, String> {
"{ $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);
}

@ -58,10 +58,10 @@ public class PlatformService {
/**
* Updates the platform players.
* <p>
* This method is scheduled to run every day at midnight.
* This method is scheduled to run every day at 23:59.
* </p>
*/
@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) {

@ -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
}