[Feature]: Matchmaking part 2 #9
14 changed files with 256 additions and 79 deletions
|
@ -1,5 +1,6 @@
|
||||||
package de.towerdefence.server.match;
|
package de.towerdefence.server.match;
|
||||||
|
|
||||||
|
import de.towerdefence.server.match.confirmation.ConfirmationCallbacks;
|
||||||
import de.towerdefence.server.player.Player;
|
import de.towerdefence.server.player.Player;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
@ -7,7 +8,8 @@ import lombok.Getter;
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@Getter
|
@Getter
|
||||||
public class Match {
|
public class Match {
|
||||||
|
|
||||||
private final String matchId;
|
private final String matchId;
|
||||||
private final Player playerOne;
|
private final Player player1;
|
||||||
private final Player playerTwo;
|
private final Player player2;
|
||||||
}
|
}
|
||||||
|
|
22
src/main/java/de/towerdefence/server/match/MatchService.java
Normal file
22
src/main/java/de/towerdefence/server/match/MatchService.java
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
package de.towerdefence.server.match;
|
||||||
|
|
||||||
|
import de.towerdefence.server.player.Player;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class MatchService {
|
||||||
|
private final Map<Player, Match> playerMatches = new HashMap<>();
|
||||||
|
|
||||||
|
public void createMatch(String matchId, Player player1, Player player2) {
|
||||||
|
Match match = new Match(matchId, player1, player2);
|
||||||
|
playerMatches.put(player1, match);
|
||||||
|
playerMatches.put(player2, match);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Match get(Player player) {
|
||||||
|
return playerMatches.get(player);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,28 +1,76 @@
|
||||||
package de.towerdefence.server.match.confirmation;
|
package de.towerdefence.server.match.confirmation;
|
||||||
|
|
||||||
|
import de.towerdefence.server.match.MatchService;
|
||||||
import de.towerdefence.server.player.Player;
|
import de.towerdefence.server.player.Player;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
|
@AllArgsConstructor
|
||||||
@Service
|
@Service
|
||||||
public class MatchConfirmationService {
|
public class MatchConfirmationService {
|
||||||
|
private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
|
||||||
private final Map<Player, UnconfirmedMatch> unconfirmedMatch = new HashMap<>();
|
private final Map<Player, UnconfirmedMatch> unconfirmedMatch = new HashMap<>();
|
||||||
|
final Map<UnconfirmedMatch, ScheduledFuture<?>> matchAbortTasks = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private final MatchService matchService;
|
||||||
|
|
||||||
public UnconfirmedMatch createMatch(Player player1,
|
public UnconfirmedMatch createMatch(Player player1,
|
||||||
Player player2,
|
Player player2,
|
||||||
ConfirmationCallbacks player1Callbacks,
|
ConfirmationCallbacks player1Callbacks,
|
||||||
ConfirmationCallbacks player2callbacks) {
|
ConfirmationCallbacks player2Callbacks) {
|
||||||
UnconfirmedMatch match = new UnconfirmedMatch(
|
UnconfirmedMatch match = new UnconfirmedMatch(
|
||||||
player1,
|
player1,
|
||||||
player2,
|
player2,
|
||||||
player1Callbacks,
|
player1Callbacks,
|
||||||
player2callbacks);
|
player2Callbacks);
|
||||||
unconfirmedMatch.put(player1, match);
|
unconfirmedMatch.put(player1, match);
|
||||||
unconfirmedMatch.put(player2, match);
|
unconfirmedMatch.put(player2, match);
|
||||||
|
ScheduledFuture<?> scheduledTask = scheduler.schedule(
|
||||||
|
() -> {
|
||||||
|
matchAbortTasks.remove(match);
|
||||||
|
unconfirmedMatch.remove(match.getPlayer1());
|
||||||
|
unconfirmedMatch.remove(match.getPlayer2());
|
||||||
|
if (match.getPlayer1State() == PlayerMatchConfirmState.CONFIRMED ) {
|
||||||
|
player1Callbacks.getRequeueCallback().call(
|
||||||
|
match.getPlayer1(),
|
||||||
|
match.getMatchId(),
|
||||||
|
player1Callbacks.getFoundCallback(),
|
||||||
|
player1Callbacks.getQueuedCallback(),
|
||||||
|
player1Callbacks.getAbortCallback(),
|
||||||
|
player1Callbacks.getEstablishedCallback());
|
||||||
|
} else {
|
||||||
|
player1Callbacks.getAbortCallback().call(
|
||||||
|
match.getPlayer1(),
|
||||||
|
match.getMatchId()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (match.getPlayer2State() == PlayerMatchConfirmState.CONFIRMED) {
|
||||||
|
player2Callbacks.getRequeueCallback().call(
|
||||||
|
match.getPlayer2(),
|
||||||
|
match.getMatchId(),
|
||||||
|
player2Callbacks.getFoundCallback(),
|
||||||
|
player2Callbacks.getQueuedCallback(),
|
||||||
|
player2Callbacks.getAbortCallback(),
|
||||||
|
player2Callbacks.getEstablishedCallback());
|
||||||
|
} else {
|
||||||
|
player2Callbacks.getAbortCallback().call(
|
||||||
|
match.getPlayer2(),
|
||||||
|
match.getMatchId()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
UnconfirmedMatch.TTL,
|
||||||
|
TimeUnit.MILLISECONDS
|
||||||
|
);
|
||||||
|
matchAbortTasks.put(match, scheduledTask);
|
||||||
return match;
|
return match;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,6 +111,8 @@ public class MatchConfirmationService {
|
||||||
if (state == UnconfirmedMatchState.WAITING) {
|
if (state == UnconfirmedMatchState.WAITING) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
matchAbortTasks.get(match).cancel(true);
|
||||||
|
matchAbortTasks.remove(match);
|
||||||
|
|
||||||
unconfirmedMatch.remove(match.getPlayer1());
|
unconfirmedMatch.remove(match.getPlayer1());
|
||||||
unconfirmedMatch.remove(match.getPlayer2());
|
unconfirmedMatch.remove(match.getPlayer2());
|
||||||
|
@ -71,30 +121,91 @@ public class MatchConfirmationService {
|
||||||
ConfirmationCallbacks player2Callbacks = match.getPlayer2Callbacks();
|
ConfirmationCallbacks player2Callbacks = match.getPlayer2Callbacks();
|
||||||
|
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case ABORTED -> {
|
case ABORTED -> {
|
||||||
if (match.getPlayer1State() == PlayerMatchConfirmState.CONFIRMED) {
|
if (match.getPlayer1State() != PlayerMatchConfirmState.ABORTED ) {
|
||||||
player1Callbacks.getRequeueCallback().call(
|
player1Callbacks.getRequeueCallback().call(
|
||||||
match.getPlayer1(),
|
match.getPlayer1(),
|
||||||
match.getMatchId(),
|
match.getMatchId(),
|
||||||
player1Callbacks.getFoundCallback(),
|
player1Callbacks.getFoundCallback(),
|
||||||
player1Callbacks.getQueuedCallback(),
|
player1Callbacks.getQueuedCallback(),
|
||||||
player1Callbacks.getAbortCallback(),
|
player1Callbacks.getAbortCallback(),
|
||||||
player1Callbacks.getEstablishedCallback());
|
player1Callbacks.getEstablishedCallback());
|
||||||
}
|
} else {
|
||||||
if (match.getPlayer2State() == PlayerMatchConfirmState.CONFIRMED) {
|
player1Callbacks.getAbortCallback().call(
|
||||||
player2Callbacks.getRequeueCallback().call(
|
match.getPlayer1(),
|
||||||
match.getPlayer2(),
|
match.getMatchId()
|
||||||
match.getMatchId(),
|
);
|
||||||
player2Callbacks.getFoundCallback(),
|
|
||||||
player2Callbacks.getQueuedCallback(),
|
|
||||||
player2Callbacks.getAbortCallback(),
|
|
||||||
player2Callbacks.getEstablishedCallback());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
case CONFIRMED -> {
|
if (match.getPlayer2State() != PlayerMatchConfirmState.ABORTED) {
|
||||||
// TODO: Create Match and Send Players the info that the Match is created
|
player2Callbacks.getRequeueCallback().call(
|
||||||
|
match.getPlayer2(),
|
||||||
|
match.getMatchId(),
|
||||||
|
player2Callbacks.getFoundCallback(),
|
||||||
|
player2Callbacks.getQueuedCallback(),
|
||||||
|
player2Callbacks.getAbortCallback(),
|
||||||
|
player2Callbacks.getEstablishedCallback());
|
||||||
|
} else {
|
||||||
|
player2Callbacks.getAbortCallback().call(
|
||||||
|
match.getPlayer2(),
|
||||||
|
match.getMatchId()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case CONFIRMED -> {
|
||||||
|
|
||||||
|
boolean player1successful = sendPlayerEstablished(
|
||||||
|
match.getMatchId(),
|
||||||
|
player1Callbacks.getEstablishedCallback(),
|
||||||
|
match.getPlayer1(),
|
||||||
|
match.getPlayer2()
|
||||||
|
);
|
||||||
|
boolean player2successful = sendPlayerEstablished(
|
||||||
|
match.getMatchId(),
|
||||||
|
player2Callbacks.getEstablishedCallback(),
|
||||||
|
match.getPlayer2(),
|
||||||
|
match.getPlayer1()
|
||||||
|
);
|
||||||
|
if (!player1successful || !player2successful) {
|
||||||
|
if (player1successful) {
|
||||||
|
player1Callbacks.getRequeueCallback().call(
|
||||||
|
match.getPlayer1(),
|
||||||
|
match.getMatchId(),
|
||||||
|
player1Callbacks.getFoundCallback(),
|
||||||
|
player1Callbacks.getQueuedCallback(),
|
||||||
|
player1Callbacks.getAbortCallback(),
|
||||||
|
player1Callbacks.getEstablishedCallback());
|
||||||
|
}
|
||||||
|
if (player2successful) {
|
||||||
|
player2Callbacks.getRequeueCallback().call(
|
||||||
|
match.getPlayer2(),
|
||||||
|
match.getMatchId(),
|
||||||
|
player2Callbacks.getFoundCallback(),
|
||||||
|
player2Callbacks.getQueuedCallback(),
|
||||||
|
player2Callbacks.getAbortCallback(),
|
||||||
|
player2Callbacks.getEstablishedCallback());
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
matchService.createMatch(match.getMatchId(), match.getPlayer1(), match.getPlayer2());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return if successful
|
||||||
|
*/
|
||||||
|
private boolean sendPlayerEstablished(
|
||||||
|
String matchId,
|
||||||
|
EstablishedCallback callback,
|
||||||
|
Player player,
|
||||||
|
Player opponent
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
callback.call(player, matchId, opponent);
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<UnconfirmedMatch> getPlayerMatch(Player player, String matchId) {
|
private Optional<UnconfirmedMatch> getPlayerMatch(Player player, String matchId) {
|
||||||
|
|
|
@ -18,10 +18,10 @@ public class UnconfirmedMatch {
|
||||||
private final ConfirmationCallbacks player2Callbacks;
|
private final ConfirmationCallbacks player2Callbacks;
|
||||||
|
|
||||||
public UnconfirmedMatch(
|
public UnconfirmedMatch(
|
||||||
Player player1,
|
Player player1,
|
||||||
Player player2,
|
Player player2,
|
||||||
ConfirmationCallbacks player1Callbacks,
|
ConfirmationCallbacks player1Callbacks,
|
||||||
ConfirmationCallbacks player2Callbacks) {
|
ConfirmationCallbacks player2Callbacks) {
|
||||||
this.player1 = player1;
|
this.player1 = player1;
|
||||||
this.player2 = player2;
|
this.player2 = player2;
|
||||||
this.player1Callbacks = player1Callbacks;
|
this.player1Callbacks = player1Callbacks;
|
||||||
|
@ -35,16 +35,21 @@ public class UnconfirmedMatch {
|
||||||
|
|
||||||
public Optional<UnconfirmedMatchState> setPlayerConfirmState(Player player, PlayerMatchConfirmState state) {
|
public Optional<UnconfirmedMatchState> setPlayerConfirmState(Player player, PlayerMatchConfirmState state) {
|
||||||
Optional<MatchPlayer> matchPlayer = getMatchPlayer(player);
|
Optional<MatchPlayer> matchPlayer = getMatchPlayer(player);
|
||||||
if(matchPlayer.isEmpty()){
|
if (matchPlayer.isEmpty()) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (matchPlayer.get()){
|
switch (matchPlayer.get()) {
|
||||||
case ONE -> player1State = state;
|
case ONE -> player1State = state;
|
||||||
case TWO -> player2State = state;
|
case TWO -> player2State = state;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (player1State == PlayerMatchConfirmState.ABORTED || player2State == PlayerMatchConfirmState.ABORTED) {
|
boolean timedOut = Instant.now().toEpochMilli() > created + TTL;
|
||||||
|
if (
|
||||||
|
timedOut
|
||||||
|
|| player1State == PlayerMatchConfirmState.ABORTED
|
||||||
|
|| player2State == PlayerMatchConfirmState.ABORTED
|
||||||
|
) {
|
||||||
return Optional.of(UnconfirmedMatchState.ABORTED);
|
return Optional.of(UnconfirmedMatchState.ABORTED);
|
||||||
}
|
}
|
||||||
if (player1State == PlayerMatchConfirmState.UNKNOWN || player2State == PlayerMatchConfirmState.UNKNOWN) {
|
if (player1State == PlayerMatchConfirmState.UNKNOWN || player2State == PlayerMatchConfirmState.UNKNOWN) {
|
||||||
|
@ -53,31 +58,31 @@ public class UnconfirmedMatch {
|
||||||
return Optional.of(UnconfirmedMatchState.CONFIRMED);
|
return Optional.of(UnconfirmedMatchState.CONFIRMED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<PlayerMatchConfirmState> getPlayerState(Player player){
|
public Optional<PlayerMatchConfirmState> getPlayerState(Player player) {
|
||||||
Optional<MatchPlayer> matchPlayer = getMatchPlayer(player);
|
Optional<MatchPlayer> matchPlayer = getMatchPlayer(player);
|
||||||
if(matchPlayer.isEmpty()){
|
if (matchPlayer.isEmpty()) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
return switch (matchPlayer.get()){
|
return switch (matchPlayer.get()) {
|
||||||
case ONE -> Optional.of(player1State);
|
case ONE -> Optional.of(player1State);
|
||||||
case TWO -> Optional.of(player2State);
|
case TWO -> Optional.of(player2State);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private enum MatchPlayer{
|
private enum MatchPlayer {
|
||||||
ONE,
|
ONE,
|
||||||
TWO
|
TWO
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<MatchPlayer> getMatchPlayer(Player player){
|
private Optional<MatchPlayer> getMatchPlayer(Player player) {
|
||||||
boolean isPlayerOne = player.equals(player1);
|
boolean isPlayerOne = player.equals(player1);
|
||||||
boolean isPlayerTwo = player.equals(player2);
|
boolean isPlayerTwo = player.equals(player2);
|
||||||
if (!isPlayerOne && !isPlayerTwo) {
|
if (!isPlayerOne && !isPlayerTwo) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isPlayerOne){
|
if (isPlayerOne) {
|
||||||
return Optional.of(MatchPlayer.ONE);
|
return Optional.of(MatchPlayer.ONE);
|
||||||
}
|
}
|
||||||
return Optional.of(MatchPlayer.TWO);
|
return Optional.of(MatchPlayer.TWO);
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
package de.towerdefence.server.server.channels;
|
package de.towerdefence.server.server.channels;
|
||||||
|
|
||||||
|
import de.towerdefence.server.match.MatchService;
|
||||||
import de.towerdefence.server.match.confirmation.MatchConfirmationService;
|
import de.towerdefence.server.match.confirmation.MatchConfirmationService;
|
||||||
import de.towerdefence.server.match.queue.MatchQueueService;
|
import de.towerdefence.server.match.queue.MatchQueueService;
|
||||||
import de.towerdefence.server.server.JsonWebsocketHandler;
|
import de.towerdefence.server.server.JsonWebsocketHandler;
|
||||||
import de.towerdefence.server.server.channels.connection.ConnectionWebsocketHandler;
|
import de.towerdefence.server.server.channels.connection.ConnectionWebsocketHandler;
|
||||||
|
import de.towerdefence.server.server.channels.match.MatchWebsocketHandler;
|
||||||
import de.towerdefence.server.server.channels.matchmaking.MatchmakingWebsocketHandler;
|
import de.towerdefence.server.server.channels.matchmaking.MatchmakingWebsocketHandler;
|
||||||
import de.towerdefence.server.server.channels.time.TimeWebsocketHandler;
|
|
||||||
import de.towerdefence.server.session.SessionsService;
|
import de.towerdefence.server.session.SessionsService;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -25,6 +26,8 @@ public class WebsocketConfig implements WebSocketConfigurer {
|
||||||
private final MatchQueueService matchQueueService;
|
private final MatchQueueService matchQueueService;
|
||||||
@Autowired
|
@Autowired
|
||||||
private final MatchConfirmationService matchConfirmationService;
|
private final MatchConfirmationService matchConfirmationService;
|
||||||
|
@Autowired
|
||||||
|
private final MatchService matchService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
||||||
|
@ -34,7 +37,7 @@ public class WebsocketConfig implements WebSocketConfigurer {
|
||||||
this.matchQueueService,
|
this.matchQueueService,
|
||||||
this.matchConfirmationService
|
this.matchConfirmationService
|
||||||
));
|
));
|
||||||
registerJsonChannel(registry, new TimeWebsocketHandler(this.sessionsService));
|
registerJsonChannel(registry, new MatchWebsocketHandler(this.sessionsService, this.matchService));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void registerJsonChannel(WebSocketHandlerRegistry registry, JsonWebsocketHandler handler){
|
private void registerJsonChannel(WebSocketHandlerRegistry registry, JsonWebsocketHandler handler){
|
||||||
|
|
|
@ -34,6 +34,9 @@ public class ConnectionWebsocketHandler extends JsonWebsocketHandler {
|
||||||
private void handleRequestConnectionToken(WebSocketSession session, String payload) throws Exception {
|
private void handleRequestConnectionToken(WebSocketSession session, String payload) throws Exception {
|
||||||
RequestConnectionTokenMessage msg = objectMapper.readValue(payload, RequestConnectionTokenMessage.class);
|
RequestConnectionTokenMessage msg = objectMapper.readValue(payload, RequestConnectionTokenMessage.class);
|
||||||
Player player = this.sessionPlayers.get(session);
|
Player player = this.sessionPlayers.get(session);
|
||||||
|
if (msg.getChannel() != Channel.MATCHMAKING) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
String jwt = this.sessionsService.createSession(player, msg.getChannel());
|
String jwt = this.sessionsService.createSession(player, msg.getChannel());
|
||||||
new ProvidedConnectionTokenMessage(msg.getChannel(), jwt).send(session);
|
new ProvidedConnectionTokenMessage(msg.getChannel(), jwt).send(session);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package de.towerdefence.server.server.channels.time;
|
package de.towerdefence.server.server.channels.match;
|
||||||
|
|
||||||
|
import de.towerdefence.server.match.MatchService;
|
||||||
import de.towerdefence.server.server.JsonWebsocketHandler;
|
import de.towerdefence.server.server.JsonWebsocketHandler;
|
||||||
import de.towerdefence.server.session.Channel;
|
import de.towerdefence.server.session.Channel;
|
||||||
import de.towerdefence.server.session.SessionsService;
|
import de.towerdefence.server.session.SessionsService;
|
||||||
|
@ -9,24 +10,25 @@ import java.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
public class TimeWebsocketHandler extends JsonWebsocketHandler {
|
public class MatchWebsocketHandler extends JsonWebsocketHandler {
|
||||||
|
|
||||||
private final Map<WebSocketSession, ScheduledFuture<?>> sessionTaskMap = new ConcurrentHashMap<>();
|
final Map<WebSocketSession, ScheduledFuture<?>> sessionTaskMap = new ConcurrentHashMap<>();
|
||||||
private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
|
private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
|
||||||
|
private final MatchService matchService;
|
||||||
|
|
||||||
public TimeWebsocketHandler(SessionsService sessionsService) {
|
public MatchWebsocketHandler(SessionsService sessionsService, MatchService matchService) {
|
||||||
super(Channel.TIME, sessionsService);
|
super(Channel.MATCH, sessionsService);
|
||||||
|
this.matchService = matchService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterConnectionEstablished(WebSocketSession session) {
|
public void afterConnectionEstablished(WebSocketSession session) {
|
||||||
super.afterConnectionEstablished(session);
|
super.afterConnectionEstablished(session);
|
||||||
|
|
||||||
ScheduledFuture<?> scheduledTask = scheduler.scheduleAtFixedRate(
|
ScheduledFuture<?> scheduledTask = scheduler.scheduleAtFixedRate(
|
||||||
() -> sendCurrentTime(session),
|
() -> sendCurrentTime(session),
|
||||||
0,
|
0,
|
||||||
1,
|
1,
|
||||||
TimeUnit.MILLISECONDS
|
TimeUnit.SECONDS
|
||||||
);
|
);
|
||||||
sessionTaskMap.put(session, scheduledTask);
|
sessionTaskMap.put(session, scheduledTask);
|
||||||
}
|
}
|
||||||
|
@ -34,10 +36,13 @@ public class TimeWebsocketHandler extends JsonWebsocketHandler {
|
||||||
private void sendCurrentTime(WebSocketSession session) {
|
private void sendCurrentTime(WebSocketSession session) {
|
||||||
ScheduledFuture<?> task = sessionTaskMap.get(session);
|
ScheduledFuture<?> task = sessionTaskMap.get(session);
|
||||||
try {
|
try {
|
||||||
if(!session.isOpen()){
|
if (!session.isOpen()) {
|
||||||
throw new RuntimeException("Session is not open");
|
throw new RuntimeException("Session is not open");
|
||||||
}
|
}
|
||||||
new TimeMessage(System.currentTimeMillis()).send(session);
|
new TimeMessage(
|
||||||
|
System.currentTimeMillis(),
|
||||||
|
matchService.get(this.sessionPlayers.get(session)).getMatchId()
|
||||||
|
).send(session);
|
||||||
} catch (RuntimeException | IOException e) {
|
} catch (RuntimeException | IOException e) {
|
||||||
task.cancel(true);
|
task.cancel(true);
|
||||||
sessionTaskMap.remove(session);
|
sessionTaskMap.remove(session);
|
|
@ -1,4 +1,4 @@
|
||||||
package de.towerdefence.server.server.channels.time;
|
package de.towerdefence.server.server.channels.match;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||||
|
@ -10,6 +10,7 @@ import java.util.Map;
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class TimeMessage extends JsonMessage {
|
public class TimeMessage extends JsonMessage {
|
||||||
private final long time;
|
private final long time;
|
||||||
|
private final String matchId;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getMessageId() {
|
protected String getMessageId() {
|
||||||
|
@ -18,6 +19,9 @@ public class TimeMessage extends JsonMessage {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Map<String, JsonNode> getData(JsonNodeFactory factory) {
|
protected Map<String, JsonNode> getData(JsonNodeFactory factory) {
|
||||||
return Map.of("time", factory.numberNode(this.time));
|
return Map.of(
|
||||||
|
"time", factory.numberNode(this.time),
|
||||||
|
"matchId", factory.textNode(this.matchId)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -46,6 +46,7 @@ public class MatchmakingWebsocketHandler extends JsonWebsocketHandler {
|
||||||
@Override
|
@Override
|
||||||
protected void handleTextMessage(WebSocketSession session, TextMessage message) {
|
protected void handleTextMessage(WebSocketSession session, TextMessage message) {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
String payload = message.getPayload();
|
String payload = message.getPayload();
|
||||||
switch (objectMapper.readTree(payload).get("$id").asText()) {
|
switch (objectMapper.readTree(payload).get("$id").asText()) {
|
||||||
case MatchSetSearchStateMessage.MESSAGE_ID -> handleMatchSetSearchStateMessage(session, payload);
|
case MatchSetSearchStateMessage.MESSAGE_ID -> handleMatchSetSearchStateMessage(session, payload);
|
||||||
|
@ -96,7 +97,8 @@ public class MatchmakingWebsocketHandler extends JsonWebsocketHandler {
|
||||||
|
|
||||||
private void onEstablished(Player player, String matchId, Player opponent) throws IOException {
|
private void onEstablished(Player player, String matchId, Player opponent) throws IOException {
|
||||||
WebSocketSession session = playerSessions.get(player);
|
WebSocketSession session = playerSessions.get(player);
|
||||||
MatchEstablishedMessage msg = new MatchEstablishedMessage(matchId, opponent.getUsername());
|
String token = this.sessionsService.createSession(player, Channel.MATCH);
|
||||||
|
MatchEstablishedMessage msg = new MatchEstablishedMessage(matchId, opponent.getUsername(), token);
|
||||||
msg.send(session);
|
msg.send(session);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package de.towerdefence.server.server.channels.matchmaking.bi;
|
package de.towerdefence.server.server.channels.matchmaking.bi;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||||
|
@ -12,7 +13,6 @@ import java.util.Map;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@NotNull
|
@NotNull
|
||||||
@AllArgsConstructor
|
|
||||||
public class MatchSetSearchStateMessage extends JsonMessage {
|
public class MatchSetSearchStateMessage extends JsonMessage {
|
||||||
public static final String MESSAGE_ID = "MatchSetSearchState";
|
public static final String MESSAGE_ID = "MatchSetSearchState";
|
||||||
|
|
||||||
|
@ -26,6 +26,15 @@ public class MatchSetSearchStateMessage extends JsonMessage {
|
||||||
this(MESSAGE_ID, searching);
|
this(MESSAGE_ID, searching);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JsonCreator
|
||||||
|
public MatchSetSearchStateMessage(
|
||||||
|
@JsonProperty("$id") String messageId,
|
||||||
|
@JsonProperty("searching") boolean searching
|
||||||
|
) {
|
||||||
|
this.messageId = messageId;
|
||||||
|
this.searching = searching;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Map<String, JsonNode> getData(JsonNodeFactory factory) {
|
protected Map<String, JsonNode> getData(JsonNodeFactory factory) {
|
||||||
return Map.of(
|
return Map.of(
|
||||||
|
|
|
@ -11,6 +11,7 @@ import java.util.Map;
|
||||||
public class MatchEstablishedMessage extends JsonMessage {
|
public class MatchEstablishedMessage extends JsonMessage {
|
||||||
private String matchId;
|
private String matchId;
|
||||||
private String opponentName;
|
private String opponentName;
|
||||||
|
private String token;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getMessageId() {
|
protected String getMessageId() {
|
||||||
|
@ -20,8 +21,9 @@ public class MatchEstablishedMessage extends JsonMessage {
|
||||||
@Override
|
@Override
|
||||||
protected Map<String, JsonNode> getData(JsonNodeFactory factory) {
|
protected Map<String, JsonNode> getData(JsonNodeFactory factory) {
|
||||||
return Map.of(
|
return Map.of(
|
||||||
"matchId", factory.textNode(this.matchId),
|
"matchId", factory.textNode(this.matchId),
|
||||||
"opponentName", factory.textNode(this.opponentName)
|
"opponentName", factory.textNode(this.opponentName),
|
||||||
|
"token", factory.textNode(this.token)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ import lombok.Getter;
|
||||||
public enum Channel {
|
public enum Channel {
|
||||||
CONNECTION("connection"),
|
CONNECTION("connection"),
|
||||||
MATCHMAKING("matchmaking"),
|
MATCHMAKING("matchmaking"),
|
||||||
TIME("time");
|
MATCH("match");
|
||||||
|
|
||||||
private final String jsonName;
|
private final String jsonName;
|
||||||
|
|
||||||
|
|
|
@ -14,4 +14,9 @@
|
||||||
<Class name="de.towerdefence.server.session.JwtService"/>
|
<Class name="de.towerdefence.server.session.JwtService"/>
|
||||||
<Bug code="M,B,CT"/>
|
<Bug code="M,B,CT"/>
|
||||||
</Match>
|
</Match>
|
||||||
|
<Match>
|
||||||
|
<!-- Spotbugs does not detect that the other cases are handled above in an if-->
|
||||||
|
<Class name="de.towerdefence.server.match.confirmation.MatchConfirmationService"/>
|
||||||
|
<Bug code="M,D,SF"/>
|
||||||
|
</Match>
|
||||||
</FindBugsFilter>
|
</FindBugsFilter>
|
||||||
|
|
28
ws/ws.yml
28
ws/ws.yml
|
@ -66,7 +66,7 @@ channels:
|
||||||
|
|
||||||
matchmaking:
|
matchmaking:
|
||||||
title: Matchmaking
|
title: Matchmaking
|
||||||
description: |
|
description: |
|
||||||
A Channel used to search for a match and
|
A Channel used to search for a match and
|
||||||
to receive one
|
to receive one
|
||||||
messages:
|
messages:
|
||||||
|
@ -147,16 +147,18 @@ channels:
|
||||||
type: string
|
type: string
|
||||||
opponentName:
|
opponentName:
|
||||||
type: string
|
type: string
|
||||||
|
token:
|
||||||
|
$ref: "#/components/schemas/JWT"
|
||||||
required:
|
required:
|
||||||
- $id
|
- $id
|
||||||
- matchId
|
- matchId
|
||||||
- opponentName
|
- opponentName
|
||||||
|
- token
|
||||||
|
|
||||||
time:
|
match:
|
||||||
title: Time
|
title: Match
|
||||||
description: |
|
description: |
|
||||||
A Simple example channel for receiving
|
Channel for managing an active match
|
||||||
the current Unix time
|
|
||||||
messages:
|
messages:
|
||||||
CurrentUnixTime:
|
CurrentUnixTime:
|
||||||
description: The Current time in Unix Time
|
description: The Current time in Unix Time
|
||||||
|
@ -170,9 +172,14 @@ channels:
|
||||||
time:
|
time:
|
||||||
type: integer
|
type: integer
|
||||||
format: int64
|
format: int64
|
||||||
|
matchId:
|
||||||
|
type: string
|
||||||
required:
|
required:
|
||||||
- $id
|
- $id
|
||||||
- time
|
- time
|
||||||
|
- matchId
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
operations:
|
operations:
|
||||||
requestConnectionToken:
|
requestConnectionToken:
|
||||||
|
@ -227,13 +234,13 @@ operations:
|
||||||
$ref: "#/channels/matchmaking"
|
$ref: "#/channels/matchmaking"
|
||||||
messages:
|
messages:
|
||||||
- $ref: "#/channels/matchmaking/messages/MatchEstablished"
|
- $ref: "#/channels/matchmaking/messages/MatchEstablished"
|
||||||
updateTime:
|
matchUpdate:
|
||||||
title: Updates of the current Unix Time
|
title: MatchUpdate
|
||||||
action: receive
|
action: receive
|
||||||
channel:
|
channel:
|
||||||
$ref: "#/channels/time"
|
$ref: "#/channels/match"
|
||||||
messages:
|
messages:
|
||||||
- $ref: "#/channels/time/messages/CurrentUnixTime"
|
- $ref: "#/channels/match/messages/CurrentUnixTime"
|
||||||
|
|
||||||
components:
|
components:
|
||||||
securitySchemes:
|
securitySchemes:
|
||||||
|
@ -252,7 +259,4 @@ components:
|
||||||
Channel:
|
Channel:
|
||||||
type: string
|
type: string
|
||||||
enum:
|
enum:
|
||||||
- connection
|
|
||||||
- matchmaking
|
- matchmaking
|
||||||
- time
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue