20 Commits

Author SHA1 Message Date
adb670324a Update dependency org.apache.commons:commons-lang3 to v3.15.0 2024-07-17 22:01:28 +00:00
Lee
9771b04589 Merge pull request 'Update dependency com.google.code.gson:gson to v2.10.1' (#1) from renovate/com.google.code.gson-gson-2.x into master
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 1m22s
Reviewed-on: #1
2024-04-28 02:11:49 +00:00
3a60a8050b Update dependency com.google.code.gson:gson to v2.10.1 2024-04-28 01:00:54 +00:00
8d1ef26183 add Sentry
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 1m31s
2024-04-28 01:07:26 +01:00
d96a38a996 don't save banned or inactive accounts
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 1m21s
2024-04-25 20:23:51 +01:00
6c63534988 Don't fetch new scores if the account is inactive or banned.
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 1m19s
2024-04-25 20:22:07 +01:00
f2f45ffa87 move it to another repo
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 1m22s
2024-04-25 20:12:58 +01:00
0b5f083366 don't load again if only page params changed
Some checks failed
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Has been cancelled
2024-04-25 20:12:16 +01:00
ddaa1e7c97 fix double data loading
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 1m20s
2024-04-25 20:08:41 +01:00
c0d2781fd0 use account name not id
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 1m19s
2024-04-25 20:06:06 +01:00
e7ee0ef4af fix changing pages on the script
Some checks failed
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Has been cancelled
2024-04-25 20:05:51 +01:00
80331ba972 maybe fix page switching?
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 1m19s
2024-04-25 19:56:06 +01:00
f5b8aa82c7 improve score fetching and add a curve version to scores
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 1m24s
2024-04-25 19:43:48 +01:00
e5fbb3d44c bump script version
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 1m19s
2024-04-25 08:32:15 +01:00
54b20cf016 make the script work when changing pages
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 1m20s
2024-04-25 08:30:50 +01:00
bc8a9f6fdc fix
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 1m21s
2024-04-25 08:15:19 +01:00
ae19233ddf return null bio if the raw bio is null
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 1m20s
2024-04-25 08:09:59 +01:00
2b8b135cf8 fix script failing to load on page load
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 1m19s
2024-04-25 07:34:22 +01:00
4cd6c27b2b joe
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 1m19s
2024-04-25 07:11:06 +01:00
b08961adf8 start work on the tampermonkey script 2024-04-25 07:11:05 +01:00
12 changed files with 140 additions and 73 deletions

12
pom.xml
View File

@ -69,7 +69,7 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
<version>3.15.0</version>
<scope>compile</scope>
</dependency>
<dependency>
@ -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 -->

View File

@ -1,5 +1,8 @@
package cc.fascinated.backend.common;
import lombok.experimental.UtilityClass;
@UtilityClass
public class MathUtils {
/**

View File

@ -1,5 +1,8 @@
package cc.fascinated.backend.common;
import lombok.experimental.UtilityClass;
@UtilityClass
public class Timer {
/**

View File

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

View File

@ -10,6 +10,11 @@ public class Leaderboard {
*/
private final String name;
/**
* The version of the leaderboard.
*/
private int curveVersion;
/**
* The curve of the leaderboard.
*/

View File

@ -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),

View File

@ -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")

View File

@ -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())
);
}

View File

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

View File

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

View File

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

View File

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