Compare commits
20 Commits
309a2211f4
...
renovate/m
Author | SHA1 | Date | |
---|---|---|---|
ea407865a4 | |||
9771b04589 | |||
3a60a8050b | |||
8d1ef26183 | |||
d96a38a996 | |||
6c63534988 | |||
f2f45ffa87 | |||
0b5f083366 | |||
ddaa1e7c97 | |||
c0d2781fd0 | |||
e7ee0ef4af | |||
80331ba972 | |||
f5b8aa82c7 | |||
e5fbb3d44c | |||
54b20cf016 | |||
bc8a9f6fdc | |||
ae19233ddf | |||
2b8b135cf8 | |||
4cd6c27b2b | |||
b08961adf8 |
@ -1,5 +1,5 @@
|
||||
# Stage 1: Build the application
|
||||
FROM maven:3.9.6-eclipse-temurin-17-alpine AS builder
|
||||
FROM maven:3.9.8-eclipse-temurin-17-alpine AS builder
|
||||
|
||||
# Set the working directory
|
||||
WORKDIR /home/container
|
||||
|
10
pom.xml
10
pom.xml
@ -80,7 +80,15 @@
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.9.1</version>
|
||||
<version>2.10.1</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Sentry -->
|
||||
<dependency>
|
||||
<groupId>io.sentry</groupId>
|
||||
<artifactId>sentry-spring-boot-starter-jakarta</artifactId>
|
||||
<version>7.8.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Websockets -->
|
||||
|
@ -1,5 +1,8 @@
|
||||
package cc.fascinated.backend.common;
|
||||
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
@UtilityClass
|
||||
public class MathUtils {
|
||||
|
||||
/**
|
||||
|
@ -1,5 +1,8 @@
|
||||
package cc.fascinated.backend.common;
|
||||
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
@UtilityClass
|
||||
public class Timer {
|
||||
|
||||
/**
|
||||
|
@ -2,6 +2,7 @@ package cc.fascinated.backend.exception;
|
||||
|
||||
import cc.fascinated.backend.model.response.ErrorResponse;
|
||||
import io.micrometer.common.lang.NonNull;
|
||||
import io.sentry.Sentry;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
@ -39,6 +40,7 @@ public final class ExceptionControllerAdvice {
|
||||
}
|
||||
if (status == null) { // Fallback to 500
|
||||
status = HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
Sentry.captureException(ex); // Capture the exception
|
||||
}
|
||||
return new ResponseEntity<>(new ErrorResponse(status, message), status);
|
||||
}
|
||||
|
@ -10,6 +10,11 @@ public class Leaderboard {
|
||||
*/
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* The version of the leaderboard.
|
||||
*/
|
||||
private int curveVersion;
|
||||
|
||||
/**
|
||||
* The curve of the leaderboard.
|
||||
*/
|
||||
|
@ -24,7 +24,7 @@ public class ScoreSaberLeaderboard extends Leaderboard {
|
||||
private final double weightCoefficient = 0.965;
|
||||
|
||||
public ScoreSaberLeaderboard() {
|
||||
super("ScoreSaber", new LeaderboardCurvePoint[] {
|
||||
super("ScoreSaber", 1, new LeaderboardCurvePoint[] {
|
||||
new LeaderboardCurvePoint(1.0, 5.367394282890631),
|
||||
new LeaderboardCurvePoint(0.9995, 5.019543595874787),
|
||||
new LeaderboardCurvePoint(0.999, 4.715470646416203),
|
||||
|
@ -163,6 +163,9 @@ public class Account {
|
||||
* @return The bio.
|
||||
*/
|
||||
public static Bio fromRaw(String raw) {
|
||||
if (raw == null || raw.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return new Bio(
|
||||
raw.split("\n"),
|
||||
raw.replaceAll("<[^>]*>", "").split("\n")
|
||||
|
@ -1,6 +1,7 @@
|
||||
package cc.fascinated.backend.model.score;
|
||||
|
||||
import cc.fascinated.backend.common.DateUtils;
|
||||
import cc.fascinated.backend.leaderboard.impl.ScoreSaberLeaderboard;
|
||||
import cc.fascinated.backend.model.token.ScoreSaberLeaderboardToken;
|
||||
import cc.fascinated.backend.model.token.ScoreSaberPlayerScoreToken;
|
||||
import cc.fascinated.backend.model.token.ScoreSaberScoreToken;
|
||||
@ -8,8 +9,6 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mongodb.core.index.CompoundIndex;
|
||||
import org.springframework.data.mongodb.core.index.Indexed;
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -20,7 +19,7 @@ import java.util.List;
|
||||
* A score for an account.
|
||||
*/
|
||||
@Document
|
||||
@AllArgsConstructor @Getter @Setter
|
||||
@Getter @Setter
|
||||
public class Score {
|
||||
/**
|
||||
* The id for this score.
|
||||
@ -128,11 +127,45 @@ public class Score {
|
||||
*/
|
||||
private String leaderboardId;
|
||||
|
||||
/**
|
||||
* The curve version for this score.
|
||||
*/
|
||||
private Integer curveVersion;
|
||||
|
||||
/**
|
||||
* The difficulty this score was set on.
|
||||
*/
|
||||
private Difficulty difficulty;
|
||||
|
||||
public Score(String id, int rank, int baseScore, int modifiedScore, Double pp, int weight, List<String> modifiers,
|
||||
int multiplier, int badCuts, int missedNotes, int maxCombo, boolean fullCombo, int hmd, Date timeSet,
|
||||
boolean hasReplay, String deviceHmd, String deviceControllerLeft, String deviceControllerRight,
|
||||
List<Score> previousScores, String accountId, String leaderboardId, Integer curveVersion, Difficulty difficulty) {
|
||||
this.id = id;
|
||||
this.rank = rank;
|
||||
this.baseScore = baseScore;
|
||||
this.modifiedScore = modifiedScore;
|
||||
this.pp = pp;
|
||||
this.weight = weight;
|
||||
this.modifiers = modifiers;
|
||||
this.multiplier = multiplier;
|
||||
this.badCuts = badCuts;
|
||||
this.missedNotes = missedNotes;
|
||||
this.maxCombo = maxCombo;
|
||||
this.fullCombo = fullCombo;
|
||||
this.hmd = hmd;
|
||||
this.timeSet = timeSet;
|
||||
this.hasReplay = hasReplay;
|
||||
this.deviceHmd = deviceHmd;
|
||||
this.deviceControllerLeft = deviceControllerLeft;
|
||||
this.deviceControllerRight = deviceControllerRight;
|
||||
this.previousScores = previousScores;
|
||||
this.accountId = accountId;
|
||||
this.leaderboardId = leaderboardId;
|
||||
this.curveVersion = curveVersion == null ? ScoreSaberLeaderboard.INSTANCE.getCurveVersion() : curveVersion;
|
||||
this.difficulty = difficulty;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a score from the given token.
|
||||
*
|
||||
@ -171,6 +204,7 @@ public class Score {
|
||||
new ArrayList<>(),
|
||||
playerId,
|
||||
leaderboard.getId(),
|
||||
ScoreSaberLeaderboard.INSTANCE.getCurveVersion(), // Get the curve version from the leaderboard.
|
||||
Difficulty.fromId(leaderboard.getDifficulty().getDifficulty())
|
||||
);
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ public interface ScoreRepository extends MongoRepository<Score, String> {
|
||||
* @return The scores for the account.
|
||||
*/
|
||||
@Query("{ 'accountId' : ?0 }")
|
||||
List<Score> getScoresForAccount(String accountId);
|
||||
List<Score> getScores(String accountId);
|
||||
|
||||
/**
|
||||
* Gets the ranked scores for an account.
|
||||
@ -27,7 +27,7 @@ public interface ScoreRepository extends MongoRepository<Score, String> {
|
||||
* @return The ranked scores for the account.
|
||||
*/
|
||||
@Query(value = "{ 'accountId' : ?0, 'pp' : { $gt : 0 } }", sort = "{ 'pp' : -1 }")
|
||||
List<Score> getRankedScoresForAccount(String accountId);
|
||||
List<Score> getRankedScores(String accountId);
|
||||
|
||||
/**
|
||||
* Gets the scores sorted by the newest for an account.
|
||||
@ -37,4 +37,14 @@ public interface ScoreRepository extends MongoRepository<Score, String> {
|
||||
*/
|
||||
@Query(value = "{ 'accountId' : ?0 }", sort = "{ 'timeSet' : -1 }")
|
||||
List<Score> getScoresSortedByNewest(String accountId);
|
||||
|
||||
/**
|
||||
* Gets the scores for an account and a leaderboard.
|
||||
*
|
||||
* @param accountId The id of the account.
|
||||
* @param leaderboardId The id of the leaderboard.
|
||||
* @return The scores for the leaderboard.
|
||||
*/
|
||||
@Query(value = "{ 'accountId' : ?0, 'leaderboardId' : ?1 }", sort = "{ 'timeSet' : -1 }")
|
||||
List<Score> getScoreForLeaderboard(String accountId, String leaderboardId);
|
||||
}
|
||||
|
@ -70,7 +70,6 @@ public class AccountService {
|
||||
Optional<Account> optionalAccount = accountRepository.findById(id);
|
||||
if (optionalAccount.isEmpty()) {
|
||||
log.info("Account '{}' not found in the database. Fetching from ScoreSaber API.", id);
|
||||
|
||||
return updateAccount(Account.fromToken(scoreSaberService.getAccount(id)));
|
||||
}
|
||||
log.info("Account '{}' found in the database.", id);
|
||||
@ -95,15 +94,19 @@ public class AccountService {
|
||||
|
||||
// Update the account with the new token.
|
||||
account = Account.fromToken(accountToken);
|
||||
accountRepository.save(account);
|
||||
|
||||
// Don't fetch new scores if the account is inactive or banned.
|
||||
if (account.isInactive() || account.isBanned()) {
|
||||
return account;
|
||||
}
|
||||
|
||||
// Fetch the scores for the account.
|
||||
scoreSaberService.updateScores(account);
|
||||
|
||||
// Set the raw pp per +1 global pp
|
||||
double rawPerGlobalPP = ScoreSaberLeaderboard.INSTANCE.getRawPerGlobalPP(scoreRepository.getRankedScoresForAccount(id), 1);
|
||||
double rawPerGlobalPP = ScoreSaberLeaderboard.INSTANCE.getRawPerGlobalPP(scoreRepository.getRankedScores(id), 1);
|
||||
account.setRawPerGlobalPerformancePoints(rawPerGlobalPP);
|
||||
|
||||
// Save the account to the database.
|
||||
return accountRepository.save(account);
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import cc.fascinated.backend.Main;
|
||||
import cc.fascinated.backend.common.DateUtils;
|
||||
import cc.fascinated.backend.common.Timer;
|
||||
import cc.fascinated.backend.common.WebRequest;
|
||||
import cc.fascinated.backend.exception.impl.BadRequestException;
|
||||
import cc.fascinated.backend.exception.impl.RateLimitException;
|
||||
import cc.fascinated.backend.exception.impl.ResourceNotFoundException;
|
||||
import cc.fascinated.backend.model.account.Account;
|
||||
@ -79,6 +80,12 @@ public class ScoreSaberService extends TextWebSocketHandler {
|
||||
log.info("Account with id '{}' not found.", id);
|
||||
throw new ResourceNotFoundException("Account with id '%s' not found.".formatted(id));
|
||||
}
|
||||
if (account.isBanned()) {
|
||||
throw new BadRequestException("Account with id '%s' is banned.".formatted(id));
|
||||
}
|
||||
if (account.isInactive()) {
|
||||
throw new BadRequestException("Account with id '%s' is inactive.".formatted(id));
|
||||
}
|
||||
return account;
|
||||
}
|
||||
|
||||
@ -90,7 +97,7 @@ public class ScoreSaberService extends TextWebSocketHandler {
|
||||
* @return The scores.
|
||||
*/
|
||||
public ScoreSaberScoresPageToken getPageScores(Account account, int page) {
|
||||
log.info("Fetching scores for account '{}' from page {}.", account.getId(), page);
|
||||
log.info("Fetching scores for account '{}' from page {}.", account.getName(), page);
|
||||
ScoreSaberScoresPageToken pageToken = WebRequest.getAsEntity(String.format(GET_PLAYER_SCORES_ENDPOINT, account.getId(), "recent", page), ScoreSaberScoresPageToken.class);
|
||||
if (pageToken == null) { // Check if the page doesn't exist.
|
||||
return null;
|
||||
@ -120,54 +127,29 @@ public class ScoreSaberService extends TextWebSocketHandler {
|
||||
return scores;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the scores until the specified score id.
|
||||
*
|
||||
* @param account The account.
|
||||
* @param scoreUntil The score to fetch until.
|
||||
* @return The scores.
|
||||
*/
|
||||
public List<ScoreSaberPlayerScoreToken> getScoreUntil(Account account, Score scoreUntil) {
|
||||
List<ScoreSaberPlayerScoreToken> scores = new ArrayList<>();
|
||||
int page = 1;
|
||||
do {
|
||||
ScoreSaberScoresPageToken pageToken = getPageScores(account, page);
|
||||
for (ScoreSaberPlayerScoreToken score : pageToken.getPlayerScores()) {
|
||||
// If the score isn't the same as the scoreUntil, add it to the list.
|
||||
if (!DateUtils.getDateFromString(score.getScore().getTimeSet()).equals(scoreUntil.getTimeSet())) {
|
||||
scores.add(score);
|
||||
}
|
||||
|
||||
if (score.getScore().getId().equals(scoreUntil.getId())) {
|
||||
// If the current score matches the specified scoreUntil, stop fetching.
|
||||
return scores;
|
||||
}
|
||||
}
|
||||
page++;
|
||||
} while (true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the scores for the account.
|
||||
*
|
||||
* @param account The account.
|
||||
*/
|
||||
public void updateScores(Account account) {
|
||||
String id = account.getId();
|
||||
String name = account.getName();
|
||||
|
||||
// Fetch the scores for the account.
|
||||
List<Score> scores = scoreRepository.getScoresForAccount(id);
|
||||
List<Score> scores = scoreRepository.getScores(account.getId());
|
||||
if (scores.isEmpty()) {
|
||||
log.warn("Account '{}' has no scores, fetching them.", id);
|
||||
log.warn("Account '{}' has no scores, fetching them.", name);
|
||||
|
||||
List<ScoreSaberScoresPageToken> scoresPageTokens = this.getScores(account);
|
||||
List<Score> newScores = new ArrayList<>();
|
||||
List<Leaderboard> leaderboardToSave = new ArrayList<>();
|
||||
|
||||
for (ScoreSaberScoresPageToken page : scoresPageTokens) {
|
||||
for (ScoreSaberPlayerScoreToken score : page.getPlayerScores()) {
|
||||
newScores.add(Score.fromToken(id, score));
|
||||
leaderboardToSave.add(Leaderboard.fromToken(score.getLeaderboard()));
|
||||
for (ScoreSaberPlayerScoreToken scoreToken : page.getPlayerScores()) {
|
||||
Score score = Score.fromToken(account.getId(), scoreToken);
|
||||
newScores.add(score);
|
||||
scores.add(score);
|
||||
leaderboardToSave.add(Leaderboard.fromToken(scoreToken.getLeaderboard()));
|
||||
}
|
||||
}
|
||||
|
||||
@ -179,39 +161,45 @@ public class ScoreSaberService extends TextWebSocketHandler {
|
||||
}
|
||||
|
||||
scoreRepository.saveAll(newScores); // Save the player's scores.
|
||||
log.info("Found {} scores for account '{}'.", newScores.size(), id);
|
||||
log.info("Found {} scores for account '{}'.", newScores.size(), name);
|
||||
return;
|
||||
}
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
log.info("Fetching new scores for account '{}'.", id);
|
||||
Score latestScore = scoreRepository.getScoresSortedByNewest(id).get(0);
|
||||
log.info("Fetching new scores for account '{}'.", name);
|
||||
|
||||
List<ScoreSaberPlayerScoreToken> newScores = this.getScoreUntil(account, latestScore);
|
||||
if (newScores.isEmpty()) {
|
||||
log.info("No new scores found for account '{}'.", id);
|
||||
return;
|
||||
}
|
||||
|
||||
int newScoreCount = 0;
|
||||
for (ScoreSaberPlayerScoreToken newScore : newScores) {
|
||||
if (saveScore(account, newScore)) {
|
||||
newScoreCount++;
|
||||
int page = 1; // The current page to search for scores.
|
||||
boolean done = false; // Whether we are done fetching scores.
|
||||
List<ScoreSaberPlayerScoreToken> newScores = new ArrayList<>();
|
||||
do {
|
||||
// This will keep fetching score pages until it finds a score that already exists.
|
||||
ScoreSaberScoresPageToken pageScores = getPageScores(account, page);
|
||||
for (ScoreSaberPlayerScoreToken score : pageScores.getPlayerScores()) {
|
||||
boolean exists = scores.stream().anyMatch(s -> s.getId().equals(score.getScore().getId()));
|
||||
if (!exists) {
|
||||
newScores.add(score);
|
||||
continue;
|
||||
}
|
||||
done = true;
|
||||
}
|
||||
page++;
|
||||
} while (!done);
|
||||
|
||||
// Save the new scores.
|
||||
for (ScoreSaberPlayerScoreToken score : newScores) {
|
||||
saveScore(account, score);
|
||||
}
|
||||
|
||||
log.info("Found {} new scores for account '{}'. (took: {}ms)", newScoreCount, id, System.currentTimeMillis() - start);
|
||||
log.info("Found {} new scores for account '{}'. (took: {}ms)", newScores.size(), name, System.currentTimeMillis() - start);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the score for the account.
|
||||
*
|
||||
* @param account The account.
|
||||
* @param score The score to save.
|
||||
* @return Whether the score was saved.
|
||||
* @param score The score to save.
|
||||
*/
|
||||
private boolean saveScore(Account account, ScoreSaberPlayerScoreToken score) {
|
||||
boolean didSave = false;
|
||||
private void saveScore(Account account, ScoreSaberPlayerScoreToken score) {
|
||||
Leaderboard newScoreLeaderboard = Leaderboard.fromToken(score.getLeaderboard());
|
||||
Score oldScore = scoreRepository.findById(score.getScore().getId()).orElse(null);
|
||||
|
||||
@ -227,19 +215,16 @@ public class ScoreSaberService extends TextWebSocketHandler {
|
||||
|
||||
scoreRepository.delete(oldScore); // Delete the old score.
|
||||
scoreRepository.save(scoreSet); // Save the new score.
|
||||
didSave = true;
|
||||
}
|
||||
} else {
|
||||
// The score is new
|
||||
scoreRepository.save(Score.fromToken(account.getId(), score)); // Save the new score.
|
||||
didSave = true;
|
||||
}
|
||||
|
||||
// Check if the leaderboard doesn't already exist.
|
||||
if (leaderboardRepository.findById(newScoreLeaderboard.getId()).isEmpty()) {
|
||||
leaderboardRepository.save(newScoreLeaderboard); // Save the leaderboard.
|
||||
}
|
||||
return didSave;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -276,9 +261,21 @@ public class ScoreSaberService extends TextWebSocketHandler {
|
||||
*/
|
||||
@SneakyThrows
|
||||
private void connectWebSocket() {
|
||||
log.info("Connecting to the ScoreSaber WSS.");
|
||||
new StandardWebSocketClient().execute(this, "wss://scoresaber.com/ws").get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterConnectionEstablished(@NonNull WebSocketSession session) {
|
||||
log.info("Connected to the ScoreSaber WSS.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterConnectionClosed(@NonNull WebSocketSession session, @NonNull CloseStatus status) {
|
||||
log.info("Disconnected from the ScoreSaber WSS.");
|
||||
connectWebSocket(); // Reconnect to the WebSocket.
|
||||
}
|
||||
|
||||
@Override @SneakyThrows
|
||||
protected void handleTextMessage(@NonNull WebSocketSession session, @NonNull TextMessage message) {
|
||||
// Ignore the connection message.
|
||||
@ -310,10 +307,4 @@ public class ScoreSaberService extends TextWebSocketHandler {
|
||||
log.error("An error occurred while handling the message.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterConnectionClosed(@NonNull WebSocketSession session, @NonNull CloseStatus status) {
|
||||
log.info("Disconnected from the ScoreSaber WSS.");
|
||||
connectWebSocket(); // Reconnect to the WebSocket.
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,11 @@ spring:
|
||||
uri: mongodb://localhost:27017
|
||||
database: ssu-prod
|
||||
|
||||
# Sentry Configuration
|
||||
sentry:
|
||||
dsn: ""
|
||||
tracesSampleRate: 1.0
|
||||
|
||||
# Set the embedded MongoDB version
|
||||
de:
|
||||
flapdoodle:
|
||||
|
Reference in New Issue
Block a user