maybe fix disconnects from websockets?
All checks were successful
Deploy to Dokku / docker (ubuntu-latest) (push) Successful in 48s

This commit is contained in:
Lee 2024-07-31 22:49:49 +01:00
parent d3268769e1
commit e7ea152bf9
4 changed files with 50 additions and 14 deletions

@ -1,7 +1,6 @@
package cc.fascinated.controller; package cc.fascinated.controller;
import cc.fascinated.exception.impl.BadRequestException; import cc.fascinated.exception.impl.BadRequestException;
import cc.fascinated.model.score.TrackedScore;
import cc.fascinated.platform.Platform; import cc.fascinated.platform.Platform;
import cc.fascinated.services.TrackedScoreService; import cc.fascinated.services.TrackedScoreService;
import lombok.NonNull; import lombok.NonNull;

@ -21,7 +21,6 @@ import org.springframework.stereotype.Component;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
@ -38,11 +37,6 @@ public class ScoreSaberPlatform extends Platform {
*/ */
private final double starMultiplier = 42.11; private final double starMultiplier = 42.11;
/**
* The weight coefficient for the platform.
*/
private final double weightCoefficient = 0.965;
/** /**
* The ScoreSaber service to use * The ScoreSaber service to use
*/ */

@ -5,16 +5,21 @@ import lombok.NonNull;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.client.standard.StandardWebSocketClient; import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.handler.TextWebSocketHandler; import org.springframework.web.socket.handler.TextWebSocketHandler;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
@Log4j2 @Log4j2
@Getter @Getter
public class Websocket extends TextWebSocketHandler { public abstract class Websocket extends TextWebSocketHandler {
/** /**
* The name of the WebSocket. * The name of the WebSocket.
*/ */
@ -25,19 +30,58 @@ public class Websocket extends TextWebSocketHandler {
*/ */
private final String url; private final String url;
/**
* The WebSocket session.
*/
private WebSocketSession webSocketSession;
/**
* The last message received
*/
private Date lastMessage;
public Websocket(@NonNull String name, @NonNull String url) { public Websocket(@NonNull String name, @NonNull String url) {
this.name = name; this.name = name;
this.url = url; this.url = url;
connectWebSocket(); // Connect to the WebSocket. connectWebSocket(); // Connect to the WebSocket.
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override @SneakyThrows
public void run() {
if (lastMessage == null) {
return;
}
long difference = new Date().getTime() - lastMessage.getTime();
if (difference > 10000) {
log.error("The {} WebSocket has not received a message in over 10 seconds, reconnecting...", getName());
webSocketSession.close(); // Close the WebSocket session.
connectWebSocket(); // Reconnect to the WebSocket.
}
}
}, 0, 5000);
}
/**
* Handles a message received from the WebSocket.
*
* @param message the message received
*/
public abstract void handleMessage(@NonNull TextMessage message);
@Override
protected final void handleTextMessage(@NonNull WebSocketSession session, @NonNull TextMessage message) {
this.lastMessage = new Date();
this.handleMessage(message);
} }
/** /**
* Connects to the ScoreSaber WebSocket. * Connects to the ScoreSaber WebSocket.
*/ */
@SneakyThrows @SneakyThrows
private void connectWebSocket() { private final void connectWebSocket() {
log.info("Connecting to the {}", this.getName()); log.info("Connecting to the {}", this.getName());
new StandardWebSocketClient().execute(this, this.getUrl()).get(); this.webSocketSession = new StandardWebSocketClient().execute(this, this.getUrl()).get();
} }
@Override @Override

@ -14,11 +14,9 @@ import io.questdb.client.Sender;
import lombok.NonNull; import lombok.NonNull;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired; 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 org.springframework.web.socket.WebSocketSession;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
@ -50,7 +48,7 @@ public class ScoreSaberWebsocket extends Websocket {
} }
@Override @SneakyThrows @Override @SneakyThrows
protected void handleTextMessage(@NotNull WebSocketSession session, @NotNull TextMessage message) { public void handleMessage(@NonNull TextMessage message) {
String payload = message.getPayload(); String payload = message.getPayload();
if (payload.equals("Connected to the ScoreSaber WSS")) { // Ignore the connection message if (payload.equals("Connected to the ScoreSaber WSS")) { // Ignore the connection message
return; return;
@ -96,6 +94,7 @@ public class ScoreSaberWebsocket extends Websocket {
.stringColumn("difficulty", difficulty) .stringColumn("difficulty", difficulty)
.atNow(); .atNow();
} }
log.info("Tracked score for '%s' on '%s'".formatted(player.getName(), leaderboard.getSongName())); log.info("Tracked score for {} with a score of {} and {}pp on {} with a rank of {}",
player.getId(), score.getBaseScore(), score.getPp(), leaderboard.getId(), score.getRank());
} }
} }