TD-18: Match Creation
All checks were successful
Quality Check / Validate OAS (push) Successful in 42s
Quality Check / Linting (push) Successful in 1m22s
Quality Check / Validate OAS (pull_request) Successful in 41s
Quality Check / Static Analysis (push) Successful in 1m29s
Quality Check / Testing (push) Successful in 1m21s
Quality Check / Linting (pull_request) Successful in 1m13s
Quality Check / Testing (pull_request) Successful in 1m5s
Quality Check / Static Analysis (pull_request) Successful in 1m9s

This commit is contained in:
Kevin Schmidt 2025-03-05 11:39:47 +01:00
parent dfcd56c7b5
commit a78d24f711
14 changed files with 256 additions and 79 deletions

View file

@ -1,5 +1,6 @@
package de.towerdefence.server.match;
import de.towerdefence.server.match.confirmation.ConfirmationCallbacks;
import de.towerdefence.server.player.Player;
import lombok.AllArgsConstructor;
import lombok.Getter;
@ -7,7 +8,8 @@ import lombok.Getter;
@AllArgsConstructor
@Getter
public class Match {
private final String matchId;
private final Player playerOne;
private final Player playerTwo;
private final Player player1;
private final Player player2;
}

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

View file

@ -1,28 +1,76 @@
package de.towerdefence.server.match.confirmation;
import de.towerdefence.server.match.MatchService;
import de.towerdefence.server.player.Player;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.*;
@AllArgsConstructor
@Service
public class MatchConfirmationService {
private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
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,
Player player2,
ConfirmationCallbacks player1Callbacks,
ConfirmationCallbacks player2callbacks) {
ConfirmationCallbacks player2Callbacks) {
UnconfirmedMatch match = new UnconfirmedMatch(
player1,
player2,
player1Callbacks,
player2callbacks);
player2Callbacks);
unconfirmedMatch.put(player1, 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;
}
@ -63,6 +111,8 @@ public class MatchConfirmationService {
if (state == UnconfirmedMatchState.WAITING) {
return;
}
matchAbortTasks.get(match).cancel(true);
matchAbortTasks.remove(match);
unconfirmedMatch.remove(match.getPlayer1());
unconfirmedMatch.remove(match.getPlayer2());
@ -72,7 +122,51 @@ public class MatchConfirmationService {
switch (state) {
case ABORTED -> {
if (match.getPlayer1State() == PlayerMatchConfirmState.CONFIRMED) {
if (match.getPlayer1State() != PlayerMatchConfirmState.ABORTED ) {
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.ABORTED) {
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(),
@ -81,7 +175,7 @@ public class MatchConfirmationService {
player1Callbacks.getAbortCallback(),
player1Callbacks.getEstablishedCallback());
}
if (match.getPlayer2State() == PlayerMatchConfirmState.CONFIRMED) {
if (player2successful) {
player2Callbacks.getRequeueCallback().call(
match.getPlayer2(),
match.getMatchId(),
@ -90,13 +184,30 @@ public class MatchConfirmationService {
player2Callbacks.getAbortCallback(),
player2Callbacks.getEstablishedCallback());
}
return;
}
case CONFIRMED -> {
// TODO: Create Match and Send Players the info that the Match is created
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) {
UnconfirmedMatch match = unconfirmedMatch.get(player);
if (match == null) {

View file

@ -44,7 +44,12 @@ public class UnconfirmedMatch {
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);
}
if (player1State == PlayerMatchConfirmState.UNKNOWN || player2State == PlayerMatchConfirmState.UNKNOWN) {

View file

@ -1,11 +1,12 @@
package de.towerdefence.server.server.channels;
import de.towerdefence.server.match.MatchService;
import de.towerdefence.server.match.confirmation.MatchConfirmationService;
import de.towerdefence.server.match.queue.MatchQueueService;
import de.towerdefence.server.server.JsonWebsocketHandler;
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.time.TimeWebsocketHandler;
import de.towerdefence.server.session.SessionsService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
@ -25,6 +26,8 @@ public class WebsocketConfig implements WebSocketConfigurer {
private final MatchQueueService matchQueueService;
@Autowired
private final MatchConfirmationService matchConfirmationService;
@Autowired
private final MatchService matchService;
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
@ -34,7 +37,7 @@ public class WebsocketConfig implements WebSocketConfigurer {
this.matchQueueService,
this.matchConfirmationService
));
registerJsonChannel(registry, new TimeWebsocketHandler(this.sessionsService));
registerJsonChannel(registry, new MatchWebsocketHandler(this.sessionsService, this.matchService));
}
private void registerJsonChannel(WebSocketHandlerRegistry registry, JsonWebsocketHandler handler){

View file

@ -34,6 +34,9 @@ public class ConnectionWebsocketHandler extends JsonWebsocketHandler {
private void handleRequestConnectionToken(WebSocketSession session, String payload) throws Exception {
RequestConnectionTokenMessage msg = objectMapper.readValue(payload, RequestConnectionTokenMessage.class);
Player player = this.sessionPlayers.get(session);
if (msg.getChannel() != Channel.MATCHMAKING) {
return;
}
String jwt = this.sessionsService.createSession(player, msg.getChannel());
new ProvidedConnectionTokenMessage(msg.getChannel(), jwt).send(session);
}

View file

@ -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.session.Channel;
import de.towerdefence.server.session.SessionsService;
@ -9,24 +10,25 @@ import java.io.IOException;
import java.util.Map;
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 final MatchService matchService;
public TimeWebsocketHandler(SessionsService sessionsService) {
super(Channel.TIME, sessionsService);
public MatchWebsocketHandler(SessionsService sessionsService, MatchService matchService) {
super(Channel.MATCH, sessionsService);
this.matchService = matchService;
}
@Override
public void afterConnectionEstablished(WebSocketSession session) {
super.afterConnectionEstablished(session);
ScheduledFuture<?> scheduledTask = scheduler.scheduleAtFixedRate(
() -> sendCurrentTime(session),
0,
1,
TimeUnit.MILLISECONDS
TimeUnit.SECONDS
);
sessionTaskMap.put(session, scheduledTask);
}
@ -37,7 +39,10 @@ public class TimeWebsocketHandler extends JsonWebsocketHandler {
if (!session.isOpen()) {
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) {
task.cancel(true);
sessionTaskMap.remove(session);

View file

@ -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.node.JsonNodeFactory;
@ -10,6 +10,7 @@ import java.util.Map;
@AllArgsConstructor
public class TimeMessage extends JsonMessage {
private final long time;
private final String matchId;
@Override
protected String getMessageId() {
@ -18,6 +19,9 @@ public class TimeMessage extends JsonMessage {
@Override
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)
);
}
}

View file

@ -46,6 +46,7 @@ public class MatchmakingWebsocketHandler extends JsonWebsocketHandler {
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) {
try {
String payload = message.getPayload();
switch (objectMapper.readTree(payload).get("$id").asText()) {
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 {
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);
}

View file

@ -1,5 +1,6 @@
package de.towerdefence.server.server.channels.matchmaking.bi;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
@ -12,7 +13,6 @@ import java.util.Map;
@Getter
@NotNull
@AllArgsConstructor
public class MatchSetSearchStateMessage extends JsonMessage {
public static final String MESSAGE_ID = "MatchSetSearchState";
@ -26,6 +26,15 @@ public class MatchSetSearchStateMessage extends JsonMessage {
this(MESSAGE_ID, searching);
}
@JsonCreator
public MatchSetSearchStateMessage(
@JsonProperty("$id") String messageId,
@JsonProperty("searching") boolean searching
) {
this.messageId = messageId;
this.searching = searching;
}
@Override
protected Map<String, JsonNode> getData(JsonNodeFactory factory) {
return Map.of(

View file

@ -11,6 +11,7 @@ import java.util.Map;
public class MatchEstablishedMessage extends JsonMessage {
private String matchId;
private String opponentName;
private String token;
@Override
protected String getMessageId() {
@ -21,7 +22,8 @@ public class MatchEstablishedMessage extends JsonMessage {
protected Map<String, JsonNode> getData(JsonNodeFactory factory) {
return Map.of(
"matchId", factory.textNode(this.matchId),
"opponentName", factory.textNode(this.opponentName)
"opponentName", factory.textNode(this.opponentName),
"token", factory.textNode(this.token)
);
}
}

View file

@ -9,7 +9,7 @@ import lombok.Getter;
public enum Channel {
CONNECTION("connection"),
MATCHMAKING("matchmaking"),
TIME("time");
MATCH("match");
private final String jsonName;

View file

@ -14,4 +14,9 @@
<Class name="de.towerdefence.server.session.JwtService"/>
<Bug code="M,B,CT"/>
</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>

View file

@ -147,16 +147,18 @@ channels:
type: string
opponentName:
type: string
token:
$ref: "#/components/schemas/JWT"
required:
- $id
- matchId
- opponentName
- token
time:
title: Time
match:
title: Match
description: |
A Simple example channel for receiving
the current Unix time
Channel for managing an active match
messages:
CurrentUnixTime:
description: The Current time in Unix Time
@ -170,9 +172,14 @@ channels:
time:
type: integer
format: int64
matchId:
type: string
required:
- $id
- time
- matchId
operations:
requestConnectionToken:
@ -227,13 +234,13 @@ operations:
$ref: "#/channels/matchmaking"
messages:
- $ref: "#/channels/matchmaking/messages/MatchEstablished"
updateTime:
title: Updates of the current Unix Time
matchUpdate:
title: MatchUpdate
action: receive
channel:
$ref: "#/channels/time"
$ref: "#/channels/match"
messages:
- $ref: "#/channels/time/messages/CurrentUnixTime"
- $ref: "#/channels/match/messages/CurrentUnixTime"
components:
securitySchemes:
@ -252,7 +259,4 @@ components:
Channel:
type: string
enum:
- connection
- matchmaking
- time