cleanup score history
Some checks failed
Deploy API / docker (17, 3.8.5) (push) Failing after 30s

This commit is contained in:
Lee 2024-08-05 05:48:01 +01:00
parent f75d22fa58
commit 6cb86f843d
7 changed files with 18 additions and 132 deletions

@ -74,45 +74,10 @@ public class ScoresController {
@ResponseBody @ResponseBody
@GetMapping(value = "/history/{platform}/{playerId}/{leaderboardId}") @GetMapping(value = "/history/{platform}/{playerId}/{leaderboardId}")
public ResponseEntity<?> getScoreHistory(@PathVariable String platform, @PathVariable String playerId, @PathVariable String leaderboardId) { public ResponseEntity<?> getScoreHistory(@PathVariable String platform, @PathVariable String playerId, @PathVariable String leaderboardId) {
return ResponseEntity.ok(scoreService.getPreviousScore( return ResponseEntity.ok(scoreService.getScoreHistory(
Platform.Platforms.getPlatform(platform), Platform.Platforms.getPlatform(platform),
userService.getUser(playerId), userService.getUser(playerId),
leaderboardId leaderboardId
).getRight());
}
/**
* A GET mapping to retrieve the score
* history for a leaderboard
*
* @param platform the platform to get the history from
* @return the score history
* @throws BadRequestException if there were no history found
*/
@ResponseBody
@GetMapping(value = "/history/{platform}/{playerId}")
public ResponseEntity<?> getScoreHistory(@PathVariable String platform, @PathVariable String playerId) {
return ResponseEntity.ok(scoreService.getImprovedScores(
Platform.Platforms.getPlatform(platform),
userService.getUser(playerId),
30
));
}
/**
* A GET mapping to retrieve the score
* history for a leaderboard
*
* @param platform the platform to get the history from
* @return the score history
* @throws BadRequestException if there were no history found
*/
@ResponseBody
@GetMapping(value = "/improved/best/{platform}")
public ResponseEntity<?> getBestImprovedScores(@PathVariable String platform) {
return ResponseEntity.ok(scoreService.getBestImprovedScores(
Platform.Platforms.getPlatform(platform),
30
)); ));
} }
} }

@ -7,12 +7,10 @@ import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Reference;
import org.springframework.data.mongodb.core.index.Indexed; import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Document;
import java.util.Date; import java.util.Date;
import java.util.List;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
@ -110,12 +108,6 @@ public class Score {
*/ */
private final DeviceInformation deviceInformation; private final DeviceInformation deviceInformation;
/**
* The score history for map the score was set on.
*/
@Reference
private List<Score> previousScores;
/** /**
* The timestamp of when the score was set. * The timestamp of when the score was set.
*/ */
@ -157,15 +149,6 @@ public class Score {
return modifiers == null ? new String[0] : modifiers; return modifiers == null ? new String[0] : modifiers;
} }
/**
* Gets the previous scores of the score.
*
* @return the previous scores
*/
public List<Score> getPreviousScores() {
return previousScores == null ? List.of() : previousScores;
}
/** /**
* Gets if the score is ranked. * Gets if the score is ranked.
* *

@ -6,7 +6,6 @@ import cc.fascinated.platform.Platform;
import lombok.Getter; import lombok.Getter;
import java.util.Date; import java.util.Date;
import java.util.List;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
@ -30,9 +29,9 @@ public class ScoreSaberScore extends Score {
public ScoreSaberScore(long id, String playerId, Platform.Platforms platform, String platformScoreId, String leaderboardId, int rank, public ScoreSaberScore(long id, String playerId, Platform.Platforms platform, String platformScoreId, String leaderboardId, int rank,
double accuracy, Double pp, int score, String[] modifiers, Integer misses, Integer badCuts, DeviceInformation deviceInformation, double accuracy, Double pp, int score, String[] modifiers, Integer misses, Integer badCuts, DeviceInformation deviceInformation,
List<Score> previousScores, Date timestamp, Double weight, double multiplier, int maxCombo) { Date timestamp, Double weight, double multiplier, int maxCombo) {
super(id, playerId, platform, platformScoreId, leaderboardId, rank, accuracy, pp, score, modifiers, misses, super(id, playerId, platform, platformScoreId, leaderboardId, rank, accuracy, pp, score, modifiers, misses,
badCuts, deviceInformation, previousScores, timestamp); badCuts, deviceInformation, timestamp);
this.weight = weight; this.weight = weight;
this.multiplier = multiplier; this.multiplier = multiplier;
this.maxCombo = maxCombo; this.maxCombo = maxCombo;

@ -2,13 +2,11 @@ package cc.fascinated.model.score.impl.scoresaber;
import cc.fascinated.model.leaderboard.Leaderboard; import cc.fascinated.model.leaderboard.Leaderboard;
import cc.fascinated.model.score.DeviceInformation; import cc.fascinated.model.score.DeviceInformation;
import cc.fascinated.model.score.Score;
import cc.fascinated.model.user.UserDTO; import cc.fascinated.model.user.UserDTO;
import cc.fascinated.platform.Platform; import cc.fascinated.platform.Platform;
import lombok.Getter; import lombok.Getter;
import java.util.Date; import java.util.Date;
import java.util.List;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
@ -27,10 +25,9 @@ public class ScoreSaberScoreResponse extends ScoreSaberScore {
public ScoreSaberScoreResponse(long id, String playerId, Platform.Platforms platform, String platformScoreId, String leaderboardId, int rank, public ScoreSaberScoreResponse(long id, String playerId, Platform.Platforms platform, String platformScoreId, String leaderboardId, int rank,
double accuracy, double pp, int score, String[] modifiers, int misses, int badCuts, DeviceInformation deviceInformation, double accuracy, double pp, int score, String[] modifiers, int misses, int badCuts, DeviceInformation deviceInformation,
List<Score> previousScores, Date timestamp, double weight, double multiplier, int maxCombo, UserDTO user, Date timestamp, double weight, double multiplier, int maxCombo, UserDTO user, Leaderboard leaderboard) {
Leaderboard leaderboard) {
super(id, playerId, platform, platformScoreId, leaderboardId, rank, accuracy, pp, score, modifiers, misses, badCuts, super(id, playerId, platform, platformScoreId, leaderboardId, rank, accuracy, pp, score, modifiers, misses, badCuts,
deviceInformation, previousScores, timestamp, weight, multiplier, maxCombo); deviceInformation, timestamp, weight, multiplier, maxCombo);
this.user = user; this.user = user;
this.leaderboard = leaderboard; this.leaderboard = leaderboard;
} }

@ -65,7 +65,7 @@ public class HistoryPoint {
* eg: if the user doesn't have their data tracked by * eg: if the user doesn't have their data tracked by
* {@link Platform#trackPlayerMetrics()} then some * {@link Platform#trackPlayerMetrics()} then some
* data will be inaccurate or missing. * data will be inaccurate or missing.
* This will only affect rank, countryRank, and pp * This will only affect {@link #rank}, {@link #countryRank}, and {@link #pp}.
* </p> * </p>
* *
* @return true if the data is possibly inaccurate, false otherwise * @return true if the data is possibly inaccurate, false otherwise

@ -13,7 +13,7 @@ import java.util.List;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
public interface ScoreRepository extends MongoRepository<Score, String> { public interface ScoreRepository extends MongoRepository<Score, Long> {
/** /**
* Gets the top ranked scores from the platform. * Gets the top ranked scores from the platform.
* *
@ -78,11 +78,10 @@ public interface ScoreRepository extends MongoRepository<Score, String> {
* @return the score * @return the score
*/ */
@Aggregation(pipeline = { @Aggregation(pipeline = {
"{ $match: { platform: ?0, leaderboardId: ?1, playerId: ?2 } }", "{ $match: { platform: ?0, playerId: ?1, leaderboardId: ?2 } }",
"{ $sort: { pp: -1 } }", "{ $sort: { timestamp: -1 } }",
"{ $limit: 1 }"
}) })
Score findScore(@NonNull Platform.Platforms platform, @NonNull String playerId, @NonNull String leaderboardId); List<Score> findScores(@NonNull Platform.Platforms platform, @NonNull String playerId, @NonNull String leaderboardId);
/** /**
* Updates a scores pp value. * Updates a scores pp value.

@ -3,7 +3,6 @@ package cc.fascinated.services;
import cc.fascinated.common.DateUtils; import cc.fascinated.common.DateUtils;
import cc.fascinated.common.EnumUtils; import cc.fascinated.common.EnumUtils;
import cc.fascinated.common.MathUtils; import cc.fascinated.common.MathUtils;
import cc.fascinated.common.Tuple;
import cc.fascinated.model.leaderboard.Leaderboard; import cc.fascinated.model.leaderboard.Leaderboard;
import cc.fascinated.model.score.DeviceInformation; import cc.fascinated.model.score.DeviceInformation;
import cc.fascinated.model.score.Score; import cc.fascinated.model.score.Score;
@ -101,7 +100,6 @@ public class ScoreService {
score.getMisses(), score.getMisses(),
score.getBadCuts(), score.getBadCuts(),
score.getDeviceInformation(), score.getDeviceInformation(),
score.getPreviousScores(),
score.getTimestamp(), score.getTimestamp(),
scoreSaberScore.getWeight(), scoreSaberScore.getWeight(),
scoreSaberScore.getMultiplier(), scoreSaberScore.getMultiplier(),
@ -194,18 +192,6 @@ public class ScoreService {
ScoreSaberScoreToken score = token.getScore(); ScoreSaberScoreToken score = token.getScore();
User user = userService.getUser(score.getLeaderboardPlayerInfo().getId()); User user = userService.getUser(score.getLeaderboardPlayerInfo().getId());
Tuple<Score, List<Score>> previousScoreTuple = this.getPreviousScore(Platform.Platforms.SCORESABER, user, leaderboard.getId());
List<Score> previousScores = previousScoreTuple.getRight();
Score previousScore = previousScoreTuple.getLeft();
boolean previousScoreExists = previousScore != null && (previousScores != null && !previousScores.isEmpty());
if (previousScoreExists) { // There is a previous score
// Delete the previous score
scoreRepository.delete(previousScore);
} else {
// There are no previous scores, so set it to null to save data
previousScores = null;
}
double accuracy = leaderboard.getMaxScore() != 0 ? ((double) score.getBaseScore() / leaderboard.getMaxScore()) * 100 : 0; double accuracy = leaderboard.getMaxScore() != 0 ? ((double) score.getBaseScore() / leaderboard.getMaxScore()) * 100 : 0;
if (accuracy == 0) { if (accuracy == 0) {
log.warn("[Scoresaber] Leaderboard '{}' has a max score of 0, unable to calculate accuracy :(", leaderboard.getId()); log.warn("[Scoresaber] Leaderboard '{}' has a max score of 0, unable to calculate accuracy :(", leaderboard.getId());
@ -231,71 +217,29 @@ public class ScoreService {
score.getDeviceControllerLeft() == null ? DeviceController.UNKNOWN : DeviceController.getByName(score.getDeviceControllerLeft()), score.getDeviceControllerLeft() == null ? DeviceController.UNKNOWN : DeviceController.getByName(score.getDeviceControllerLeft()),
score.getDeviceControllerRight() == null ? DeviceController.UNKNOWN : DeviceController.getByName(score.getDeviceControllerRight()) score.getDeviceControllerRight() == null ? DeviceController.UNKNOWN : DeviceController.getByName(score.getDeviceControllerRight())
), ),
previousScores,
DateUtils.getDateFromIsoString(score.getTimeSet()), DateUtils.getDateFromIsoString(score.getTimeSet()),
score.getWeight() == 0 ? null : score.getWeight(), // no weight, set to null to save data score.getWeight() == 0 ? null : score.getWeight(), // no weight, set to null to save data
score.getMultiplier(), score.getMultiplier(),
score.getMaxCombo() score.getMaxCombo()
); );
this.saveScore(user, scoreSaberScore); this.saveScore(user, scoreSaberScore);
this.logScore(Platform.Platforms.SCORESABER, Leaderboard.getFromScoreSaberToken(leaderboard), scoreSaberScore, user, this.logScore(Platform.Platforms.SCORESABER, Leaderboard.getFromScoreSaberToken(leaderboard), scoreSaberScore, user);
previousScoreExists && previousScore.getScore() < scoreSaberScore.getScore());
if (scoreSaberScore.getDeviceInformation().containsUnknownDevices()) {
log.warn(" - Score contains unknown device: hmd: {}, controller left: {}, controller right: {}",
score.getDeviceHmd(),
score.getDeviceControllerLeft(),
score.getDeviceControllerRight()
);
}
} }
/** /**
* Gets the previous score of a user. * Gets the previous scores for a leaderboard.
* *
* @param platform The platform to get the score from. * @param platform The platform to get the score from.
* @param user The user to get the score from. * @param user The user to get the score from.
* @param leaderboardId The leaderboard id to get the score from. * @param leaderboardId The leaderboard id to get the score from.
* @return The previous score. * @return The previous score.
*/ */
public Tuple<Score, List<Score>> getPreviousScore(@NonNull Platform.Platforms platform, @NonNull User user, @NonNull String leaderboardId) { public @NonNull List<Score> getScoreHistory(@NonNull Platform.Platforms platform, @NonNull User user, @NonNull String leaderboardId) {
Score score = this.scoreRepository.findScore(platform, user.getSteamId(), leaderboardId); List<Score> foundScores = new ArrayList<>(this.scoreRepository.findScores(platform, user.getSteamId(), leaderboardId));
List<Score> previousScores = new ArrayList<>();
if (score == null) { // There is a previous score
return new Tuple<>(null, previousScores);
}
if (score.getPreviousScores() != null) {
previousScores.addAll(score.getPreviousScores()); // Add the previous scores
}
score.setPreviousScores(null); // Clear the previous scores
previousScores.add(score); // Add the previous score
// Sort previous scores by timestamp (newest -> oldest) // Sort previous scores by timestamp (newest -> oldest)
previousScores.sort(Comparator.comparing(Score::getTimestamp).reversed()); foundScores.sort(Comparator.comparing(Score::getTimestamp).reversed());
return foundScores;
return new Tuple<>(score, previousScores);
}
/**
* Gets the improved scores of a user.
*
* @param platform The platform to get the scores from.
* @param user The user to get the scores from.
* @param days The amount of days in the past to get the scores from.
* @return The improved scores.
*/
public List<Score> getImprovedScores(@NonNull Platform.Platforms platform, @NonNull User user, int days) {
return scoreRepository.getUserImprovedScores(platform, user.getSteamId(), DateUtils.getDaysAgo(days));
}
/**
* Gets the best improved scores from the platform.
*
* @param platform The platform to get the scores from.
* @param days The amount of days in the past to get the scores from.
* @return The scores.
*/
public List<Score> getBestImprovedScores(@NonNull Platform.Platforms platform, int days) {
return scoreRepository.getBestImprovedScores(platform, DateUtils.getDaysAgo(days));
} }
/** /**
@ -326,13 +270,12 @@ public class ScoreService {
* @param user The user who set the score. * @param user The user who set the score.
*/ */
private void logScore(@NonNull Platform.Platforms platform, @NonNull Leaderboard leaderboard, @NonNull Score score, private void logScore(@NonNull Platform.Platforms platform, @NonNull Leaderboard leaderboard, @NonNull Score score,
@NonNull User user, boolean improvedScore) { @NonNull User user) {
String platformName = EnumUtils.getEnumName(platform); String platformName = EnumUtils.getEnumName(platform);
boolean isRanked = score.getPp() != 0; boolean isRanked = score.getPp() != 0;
log.info("[{}] {}Tracked{} Score! id: {}, acc: {}%, {} score id: {},{} leaderboard: {}, difficulty: {}, player: {} ({})", log.info("[{}] Tracked{} Score! id: {}, acc: {}%, {} score id: {},{} leaderboard: {}, difficulty: {}, player: {} ({})",
platformName, platformName,
improvedScore ? "Improved " : "",
isRanked ? " Ranked" : "", isRanked ? " Ranked" : "",
score.getId(), score.getId(),
MathUtils.format(score.getAccuracy(), 2), MathUtils.format(score.getAccuracy(), 2),