API: fix stats
Some checks failed
Deploy API / docker (17, 3.8.5) (push) Has been cancelled

This commit is contained in:
Lee 2024-08-02 00:38:08 +01:00
parent d4e51d1517
commit 6a1a2dc2c4
6 changed files with 89 additions and 18 deletions

@ -2,6 +2,7 @@ package cc.fascinated.model.score.impl.scoresaber;
import cc.fascinated.model.leaderboard.Leaderboard; import cc.fascinated.model.leaderboard.Leaderboard;
import cc.fascinated.model.user.User; import cc.fascinated.model.user.User;
import cc.fascinated.model.user.UserDTO;
import cc.fascinated.platform.Platform; import cc.fascinated.platform.Platform;
import lombok.Getter; import lombok.Getter;
@ -15,7 +16,7 @@ public class ScoreSaberScoreResponse extends ScoreSaberScore {
/** /**
* The user that set the score. * The user that set the score.
*/ */
private final User user; private final UserDTO user;
/** /**
* The leaderboard the score was set on. * The leaderboard the score was set on.
@ -24,7 +25,7 @@ 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, Date timestamp, double accuracy, double pp, int score, String[] modifiers, int misses, int badCuts, Date timestamp,
double weight, double multiplier, int maxCombo, User user, Leaderboard leaderboard) { double weight, double multiplier, int maxCombo, UserDTO user, 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,
timestamp, weight, multiplier, maxCombo); timestamp, weight, multiplier, maxCombo);
this.user = user; this.user = user;

@ -1,5 +1,6 @@
package cc.fascinated.model.user; package cc.fascinated.model.user;
import cc.fascinated.common.DateUtils;
import cc.fascinated.model.user.statistic.Statistic; import cc.fascinated.model.user.statistic.Statistic;
import cc.fascinated.platform.Platform; import cc.fascinated.platform.Platform;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
@ -9,6 +10,7 @@ import org.springframework.data.annotation.Id;
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.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -23,6 +25,8 @@ import java.util.UUID;
@ToString @ToString
@Document("user") @Document("user")
public class User { public class User {
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
/** /**
* The ID of the user. * The ID of the user.
*/ */
@ -64,8 +68,21 @@ public class User {
/** /**
* The user's statistic history. * The user's statistic history.
*/ */
@JsonIgnore public Map<Platform.Platforms, Map<String, Statistic>> statistics;
public Map<Platform.Platforms, Map<Date, Statistic>> statistics;
/**
* The user's statistic history.
*/
public Map<Platform.Platforms, Map<Date, Statistic>> getStatistics() {
if (this.statistics == null) {
this.statistics = new HashMap<>();
}
Map<Platform.Platforms, Map<Date, Statistic>> toReturn = new HashMap<>();
for (Platform.Platforms platform : statistics.keySet()) {
toReturn.put(platform, getStatistic(platform));
}
return toReturn;
}
/** /**
* Gets the statistics for a platform. * Gets the statistics for a platform.
@ -73,11 +90,17 @@ public class User {
* @param platform the platform to get the statistics for * @param platform the platform to get the statistics for
* @return the statistics * @return the statistics
*/ */
public Map<Date, Statistic> getStatistics(@NonNull Platform.Platforms platform) { @SneakyThrows
public Map<Date, Statistic> getStatistic(@NonNull Platform.Platforms platform) {
if (this.statistics == null) { if (this.statistics == null) {
this.statistics = new HashMap<>(); this.statistics = new HashMap<>();
} }
return this.statistics.computeIfAbsent(platform, k -> new HashMap<>()); Map<String, Statistic> statisticMap = this.statistics.computeIfAbsent(platform, k -> new HashMap<>());
Map<Date, Statistic> statistics = new HashMap<>();
for (Map.Entry<String, Statistic> entry : statisticMap.entrySet()) {
statistics.put(DATE_FORMAT.parse(entry.getKey()), entry.getValue());
}
return statistics;
} }
/** /**
@ -88,6 +111,20 @@ public class User {
* @param statistic the statistic to add * @param statistic the statistic to add
*/ */
public void addStatistic(@NonNull Platform.Platforms platform, @NonNull Date date, @NonNull Statistic statistic) { public void addStatistic(@NonNull Platform.Platforms platform, @NonNull Date date, @NonNull Statistic statistic) {
this.getStatistics(platform).put(date, statistic); if (this.statistics == null) {
this.statistics = new HashMap<>();
}
Map<String, Statistic> statisticMap = this.statistics.computeIfAbsent(platform, k -> new HashMap<>());
statisticMap.put(String.valueOf(date.toString()), statistic);
}
/**
* Gets the user as a DTO.
*
* @return the user as a DTO
*/
@JsonIgnore
public UserDTO getAsDTO() {
return new UserDTO(this.id, this.username, this.steamId, this.scoresaberAccount);
} }
} }

@ -0,0 +1,40 @@
package cc.fascinated.model.user;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import java.util.UUID;
/**
* @author Fascinated (fascinated7)
*/
@AllArgsConstructor
@Getter
public class UserDTO {
/**
* The ID of the user.
*/
private final UUID id;
/**
* The username of the user.
* <p>
* Usually their Steam name.
* </p>
*/
private String username;
/**
* The ID of the users steam profile.
*/
private String steamId;
/**
* The user's ScoreSaber account.
*/
public ScoreSaberAccount scoresaberAccount;
}

@ -1,10 +0,0 @@
package cc.fascinated.services;
import org.springframework.stereotype.Service;
/**
* @author Fascinated (fascinated7)
*/
@Service
public class PlatformMetricsService {
}

@ -38,6 +38,8 @@ public class PlatformService {
log.info("Registering platforms..."); log.info("Registering platforms...");
registerPlatform(context.getBean(ScoreSaberPlatform.class)); registerPlatform(context.getBean(ScoreSaberPlatform.class));
log.info("Loaded %s platforms.".formatted(this.platforms.size())); log.info("Loaded %s platforms.".formatted(this.platforms.size()));
this.updatePlayerMetrics();
} }
/** /**

@ -12,6 +12,7 @@ import cc.fascinated.model.token.ScoreSaberLeaderboardToken;
import cc.fascinated.model.token.ScoreSaberPlayerScoreToken; import cc.fascinated.model.token.ScoreSaberPlayerScoreToken;
import cc.fascinated.model.token.ScoreSaberScoreToken; import cc.fascinated.model.token.ScoreSaberScoreToken;
import cc.fascinated.model.user.User; import cc.fascinated.model.user.User;
import cc.fascinated.model.user.UserDTO;
import cc.fascinated.platform.Platform; import cc.fascinated.platform.Platform;
import cc.fascinated.repository.ScoreRepository; import cc.fascinated.repository.ScoreRepository;
import lombok.NonNull; import lombok.NonNull;
@ -77,7 +78,7 @@ public class ScoreService {
List<ScoreSaberScoreResponse> scores = new ArrayList<>(); List<ScoreSaberScoreResponse> scores = new ArrayList<>();
for (Score score : trackedScores) { for (Score score : trackedScores) {
ScoreSaberScore scoreSaberScore = (ScoreSaberScore) score; ScoreSaberScore scoreSaberScore = (ScoreSaberScore) score;
User user = scoresOnly ? null : userService.getUser(score.getPlayerId()); UserDTO user = scoresOnly ? null : userService.getUser(score.getPlayerId()).getAsDTO();
Leaderboard leaderboard = scoresOnly ? null : Leaderboard.getFromScoreSaberToken(scoreSaberService.getLeaderboard(score.getLeaderboardId())); Leaderboard leaderboard = scoresOnly ? null : Leaderboard.getFromScoreSaberToken(scoreSaberService.getLeaderboard(score.getLeaderboardId()));
scores.add(new ScoreSaberScoreResponse( scores.add(new ScoreSaberScoreResponse(