From 7aa3de38271d4ebd4455302751aa743ca7cc6928 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 1 Aug 2024 16:25:21 +0100 Subject: [PATCH] move where the accounts get updated --- .../cc/fascinated/services/UserService.java | 41 +++++++++++++++++-- .../websocket/impl/ScoreSaberWebsocket.java | 39 +++--------------- 2 files changed, 43 insertions(+), 37 deletions(-) diff --git a/API/src/main/java/cc/fascinated/services/UserService.java b/API/src/main/java/cc/fascinated/services/UserService.java index 0172121..a14b3e4 100644 --- a/API/src/main/java/cc/fascinated/services/UserService.java +++ b/API/src/main/java/cc/fascinated/services/UserService.java @@ -1,29 +1,49 @@ package cc.fascinated.services; +import cc.fascinated.common.TimeUtils; import cc.fascinated.exception.impl.BadRequestException; +import cc.fascinated.model.token.ScoreSaberAccountToken; +import cc.fascinated.model.user.ScoreSaberAccount; import cc.fascinated.model.user.User; import cc.fascinated.repository.mongo.UserRepository; import lombok.NonNull; +import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.Date; import java.util.List; import java.util.Optional; import java.util.UUID; +import java.util.concurrent.TimeUnit; /** * @author Fascinated (fascinated7) */ @Service +@Log4j2(topic = "User Service") public class UserService { + /** + * The interval to force update the user's account. + */ + private static long FORCE_UPDATE_INTERVAL = TimeUnit.HOURS.toMillis(4); + /** * The user repository to use */ - @NonNull private final UserRepository userRepository; + @NonNull + private final UserRepository userRepository; + + /** + * The ScoreSaber service to use + */ + @NonNull + private final ScoreSaberService scoreSaberService; @Autowired - public UserService(@NonNull UserRepository userRepository) { + public UserService(@NonNull UserRepository userRepository, @NonNull ScoreSaberService scoreSaberService) { this.userRepository = userRepository; + this.scoreSaberService = scoreSaberService; } /** @@ -37,7 +57,6 @@ public class UserService { if (!this.isValidSteamId(steamId)) { throw new BadRequestException("Invalid steam id"); } - Optional userOptional = this.userRepository.findBySteamId(steamId); if (userOptional.isEmpty()) { // todo: check the steam API to see if the user exists @@ -45,7 +64,21 @@ public class UserService { user.setSteamId(steamId); return userRepository.save(user); } - return userOptional.get(); + User user = userOptional.get(); + + // Ensure the users ScoreSaber account is up-to-date + ScoreSaberAccount scoresaberAccount = user.getScoresaberAccount(); + if (scoresaberAccount == null || scoresaberAccount.getLastUpdated().before(new Date(System.currentTimeMillis() - FORCE_UPDATE_INTERVAL))) { + log.info("Updating account for '{}', last update: {}", + steamId, + scoresaberAccount == null ? "now" : TimeUtils.format(System.currentTimeMillis() - scoresaberAccount.getLastUpdated().getTime()) + ); + ScoreSaberAccountToken accountToken = scoreSaberService.getAccount(user); + user.setScoresaberAccount(ScoreSaberAccount.getFromToken(accountToken)); + this.saveUser(user); // Save the user + } + + return user; } /** diff --git a/API/src/main/java/cc/fascinated/websocket/impl/ScoreSaberWebsocket.java b/API/src/main/java/cc/fascinated/websocket/impl/ScoreSaberWebsocket.java index b8d774b..e4a3a72 100644 --- a/API/src/main/java/cc/fascinated/websocket/impl/ScoreSaberWebsocket.java +++ b/API/src/main/java/cc/fascinated/websocket/impl/ScoreSaberWebsocket.java @@ -1,14 +1,13 @@ package cc.fascinated.websocket.impl; import cc.fascinated.common.ScoreSaberUtils; -import cc.fascinated.common.TimeUtils; -import cc.fascinated.model.token.*; -import cc.fascinated.model.user.ScoreSaberAccount; -import cc.fascinated.model.user.User; +import cc.fascinated.model.token.ScoreSaberLeaderboardToken; +import cc.fascinated.model.token.ScoreSaberPlayerScoreToken; +import cc.fascinated.model.token.ScoreSaberScoreToken; +import cc.fascinated.model.token.ScoreSaberWebsocketDataToken; import cc.fascinated.platform.Platform; import cc.fascinated.services.PlatformService; import cc.fascinated.services.QuestDBService; -import cc.fascinated.services.ScoreSaberService; import cc.fascinated.services.UserService; import cc.fascinated.websocket.Websocket; import com.fasterxml.jackson.databind.ObjectMapper; @@ -20,20 +19,12 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.socket.TextMessage; -import java.util.Date; -import java.util.concurrent.TimeUnit; - /** * @author Fascinated (fascinated7) */ @Component @Log4j2(topic = "ScoreSaber Websocket") public class ScoreSaberWebsocket extends Websocket { - /** - * The interval to force update the user's account. - */ - private static long FORCE_UPDATE_INTERVAL = TimeUnit.HOURS.toMillis(12); - /** * The Jackson deserializer to use. */ @@ -54,20 +45,14 @@ public class ScoreSaberWebsocket extends Websocket { */ private final PlatformService platformService; - /** - * The ScoreSaber service to use - */ - private final ScoreSaberService scoreSaberService; - @Autowired public ScoreSaberWebsocket(@NonNull ObjectMapper objectMapper, @NonNull UserService userService, @NonNull QuestDBService questDBService, - @NonNull PlatformService platformService, @NonNull ScoreSaberService scoreSaberService) { + @NonNull PlatformService platformService) { super("ScoreSaber", "wss://scoresaber.com/ws"); this.objectMapper = objectMapper; this.userService = userService; this.questDBService = questDBService; this.platformService = platformService; - this.scoreSaberService = scoreSaberService; } @Override @@ -94,19 +79,7 @@ public class ScoreSaberWebsocket extends Websocket { return; } - User user = userService.getUser(player.getId()); - ScoreSaberAccount scoresaberAccount = user.getScoresaberAccount(); - // Ensure the users account is up-to-date - if (scoresaberAccount == null || scoresaberAccount.getLastUpdated().before(new Date(System.currentTimeMillis() - FORCE_UPDATE_INTERVAL))) { - log.info("Updating account for '{}', last update: {}", - player.getName(), - scoresaberAccount == null ? "now" : TimeUtils.format(System.currentTimeMillis() - scoresaberAccount.getLastUpdated().getTime()) - ); - ScoreSaberAccountToken accountToken = scoreSaberService.getAccount(user); - user.setScoresaberAccount(ScoreSaberAccount.getFromToken(accountToken)); - userService.saveUser(user); // Save the user - } - + userService.getUser(player.getId()); double accuracy = ((double) score.getBaseScore() / leaderboard.getMaxScore()) * 100; String difficulty = ScoreSaberUtils.parseDifficulty(leaderboard.getDifficulty().getDifficulty()); double pp = platformService.getScoreSaberPlatform().getPp(leaderboard.getStars(), accuracy); // Recalculate the PP