move where the accounts get updated
Some checks failed
Deploy API / docker (17, 3.8.5) (push) Failing after 45s
Some checks failed
Deploy API / docker (17, 3.8.5) (push) Failing after 45s
This commit is contained in:
parent
4135af4743
commit
7aa3de3827
@ -1,29 +1,49 @@
|
|||||||
package cc.fascinated.services;
|
package cc.fascinated.services;
|
||||||
|
|
||||||
|
import cc.fascinated.common.TimeUtils;
|
||||||
import cc.fascinated.exception.impl.BadRequestException;
|
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.model.user.User;
|
||||||
import cc.fascinated.repository.mongo.UserRepository;
|
import cc.fascinated.repository.mongo.UserRepository;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Fascinated (fascinated7)
|
* @author Fascinated (fascinated7)
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
|
@Log4j2(topic = "User Service")
|
||||||
public class UserService {
|
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
|
* 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
|
@Autowired
|
||||||
public UserService(@NonNull UserRepository userRepository) {
|
public UserService(@NonNull UserRepository userRepository, @NonNull ScoreSaberService scoreSaberService) {
|
||||||
this.userRepository = userRepository;
|
this.userRepository = userRepository;
|
||||||
|
this.scoreSaberService = scoreSaberService;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -37,7 +57,6 @@ public class UserService {
|
|||||||
if (!this.isValidSteamId(steamId)) {
|
if (!this.isValidSteamId(steamId)) {
|
||||||
throw new BadRequestException("Invalid steam id");
|
throw new BadRequestException("Invalid steam id");
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<User> userOptional = this.userRepository.findBySteamId(steamId);
|
Optional<User> userOptional = this.userRepository.findBySteamId(steamId);
|
||||||
if (userOptional.isEmpty()) {
|
if (userOptional.isEmpty()) {
|
||||||
// todo: check the steam API to see if the user exists
|
// todo: check the steam API to see if the user exists
|
||||||
@ -45,7 +64,21 @@ public class UserService {
|
|||||||
user.setSteamId(steamId);
|
user.setSteamId(steamId);
|
||||||
return userRepository.save(user);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,14 +1,13 @@
|
|||||||
package cc.fascinated.websocket.impl;
|
package cc.fascinated.websocket.impl;
|
||||||
|
|
||||||
import cc.fascinated.common.ScoreSaberUtils;
|
import cc.fascinated.common.ScoreSaberUtils;
|
||||||
import cc.fascinated.common.TimeUtils;
|
import cc.fascinated.model.token.ScoreSaberLeaderboardToken;
|
||||||
import cc.fascinated.model.token.*;
|
import cc.fascinated.model.token.ScoreSaberPlayerScoreToken;
|
||||||
import cc.fascinated.model.user.ScoreSaberAccount;
|
import cc.fascinated.model.token.ScoreSaberScoreToken;
|
||||||
import cc.fascinated.model.user.User;
|
import cc.fascinated.model.token.ScoreSaberWebsocketDataToken;
|
||||||
import cc.fascinated.platform.Platform;
|
import cc.fascinated.platform.Platform;
|
||||||
import cc.fascinated.services.PlatformService;
|
import cc.fascinated.services.PlatformService;
|
||||||
import cc.fascinated.services.QuestDBService;
|
import cc.fascinated.services.QuestDBService;
|
||||||
import cc.fascinated.services.ScoreSaberService;
|
|
||||||
import cc.fascinated.services.UserService;
|
import cc.fascinated.services.UserService;
|
||||||
import cc.fascinated.websocket.Websocket;
|
import cc.fascinated.websocket.Websocket;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
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.stereotype.Component;
|
||||||
import org.springframework.web.socket.TextMessage;
|
import org.springframework.web.socket.TextMessage;
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Fascinated (fascinated7)
|
* @author Fascinated (fascinated7)
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
@Log4j2(topic = "ScoreSaber Websocket")
|
@Log4j2(topic = "ScoreSaber Websocket")
|
||||||
public class ScoreSaberWebsocket extends 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.
|
* The Jackson deserializer to use.
|
||||||
*/
|
*/
|
||||||
@ -54,20 +45,14 @@ public class ScoreSaberWebsocket extends Websocket {
|
|||||||
*/
|
*/
|
||||||
private final PlatformService platformService;
|
private final PlatformService platformService;
|
||||||
|
|
||||||
/**
|
|
||||||
* The ScoreSaber service to use
|
|
||||||
*/
|
|
||||||
private final ScoreSaberService scoreSaberService;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public ScoreSaberWebsocket(@NonNull ObjectMapper objectMapper, @NonNull UserService userService, @NonNull QuestDBService questDBService,
|
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");
|
super("ScoreSaber", "wss://scoresaber.com/ws");
|
||||||
this.objectMapper = objectMapper;
|
this.objectMapper = objectMapper;
|
||||||
this.userService = userService;
|
this.userService = userService;
|
||||||
this.questDBService = questDBService;
|
this.questDBService = questDBService;
|
||||||
this.platformService = platformService;
|
this.platformService = platformService;
|
||||||
this.scoreSaberService = scoreSaberService;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -94,19 +79,7 @@ public class ScoreSaberWebsocket extends Websocket {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
User user = userService.getUser(player.getId());
|
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
|
|
||||||
}
|
|
||||||
|
|
||||||
double accuracy = ((double) score.getBaseScore() / leaderboard.getMaxScore()) * 100;
|
double accuracy = ((double) score.getBaseScore() / leaderboard.getMaxScore()) * 100;
|
||||||
String difficulty = ScoreSaberUtils.parseDifficulty(leaderboard.getDifficulty().getDifficulty());
|
String difficulty = ScoreSaberUtils.parseDifficulty(leaderboard.getDifficulty().getDifficulty());
|
||||||
double pp = platformService.getScoreSaberPlatform().getPp(leaderboard.getStars(), accuracy); // Recalculate the PP
|
double pp = platformService.getScoreSaberPlatform().getPp(leaderboard.getStars(), accuracy); // Recalculate the PP
|
||||||
|
Reference in New Issue
Block a user