Compare commits
17 commits
v0.0.0-rc.
...
trunk
Author | SHA1 | Date | |
---|---|---|---|
e188cc5974 | |||
|
de9c0c8faf | ||
bc636df0f8 | |||
a9e4d3cbaf | |||
|
3f4b952916 | ||
6d536a3ad1 | |||
|
b6bff21a11 | ||
ec63412af2 | |||
|
0b93ef4203 | ||
033e6c1cf7 | |||
766d3c7d47 | |||
b849cd7a27 | |||
|
27d7dddd5c | ||
e317968ccd | |||
|
a78d24f711 | ||
dfcd56c7b5 | |||
e38ca38e41 |
32 changed files with 831 additions and 142 deletions
|
@ -239,7 +239,7 @@ paths:
|
||||||
503:
|
503:
|
||||||
$ref: "#/components/responses/503ServiceUnavailable"
|
$ref: "#/components/responses/503ServiceUnavailable"
|
||||||
/admin/players:
|
/admin/players:
|
||||||
get:
|
post:
|
||||||
operationId: "GetAllPlayers"
|
operationId: "GetAllPlayers"
|
||||||
tags:
|
tags:
|
||||||
- admin
|
- admin
|
||||||
|
|
13
src/main/java/de/towerdefence/server/match/Enemy.java
Normal file
13
src/main/java/de/towerdefence/server/match/Enemy.java
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
package de.towerdefence.server.match;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class Enemy {
|
||||||
|
private int poxX;
|
||||||
|
private int posY;
|
||||||
|
}
|
54
src/main/java/de/towerdefence/server/match/GameSession.java
Normal file
54
src/main/java/de/towerdefence/server/match/GameSession.java
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
package de.towerdefence.server.match;
|
||||||
|
|
||||||
|
import de.towerdefence.server.match.callbacks.PlayerHitpointsCallback;
|
||||||
|
import de.towerdefence.server.match.callbacks.PlayerMoneyCallback;
|
||||||
|
import de.towerdefence.server.match.exeptions.InvalidPlacementException;
|
||||||
|
import de.towerdefence.server.match.exeptions.InvalidPlacementReason;
|
||||||
|
import de.towerdefence.server.player.Player;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
public class GameSession {
|
||||||
|
final static int START_HITPOINTS = 100;
|
||||||
|
final static int START_MONEY = 50;
|
||||||
|
final static int MAP_SIZE_X = 10;
|
||||||
|
final static int MAP_SIZE_Y = 20;
|
||||||
|
|
||||||
|
private final Tower[][] towers = new Tower[MAP_SIZE_X][MAP_SIZE_Y];
|
||||||
|
|
||||||
|
private final Player player;
|
||||||
|
@Getter
|
||||||
|
private int money;
|
||||||
|
@Getter
|
||||||
|
private int playerHitpoints;
|
||||||
|
private final PlayerMoneyCallback moneyCallback;
|
||||||
|
//private final PlayerHitpointsCallback hitpointsCallback;
|
||||||
|
|
||||||
|
public GameSession(Player player, PlayerMoneyCallback moneyCallback) {
|
||||||
|
this.player = player;
|
||||||
|
this.moneyCallback = moneyCallback;
|
||||||
|
//this.hitpointsCallback = hitpointsCallback;
|
||||||
|
this.money = START_MONEY;
|
||||||
|
this.playerHitpoints = START_HITPOINTS;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void placeTower(Tower tower, int x, int y) throws InvalidPlacementException {
|
||||||
|
if (x < 0 || y < 0 || x + 1 > MAP_SIZE_X || y + 1 > MAP_SIZE_Y) {
|
||||||
|
throw new InvalidPlacementException(InvalidPlacementReason.OUT_OF_BOUNDS);
|
||||||
|
}
|
||||||
|
if (towers[x][y] != null) {
|
||||||
|
throw new InvalidPlacementException(InvalidPlacementReason.LOCATION_USED);
|
||||||
|
}
|
||||||
|
if (money < Tower.COST) {
|
||||||
|
throw new InvalidPlacementException(InvalidPlacementReason.NOT_ENOUGH_MONEY);
|
||||||
|
}
|
||||||
|
money -= Tower.COST;
|
||||||
|
moneyCallback.call(player, money);
|
||||||
|
towers[x][y] = tower;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addMoney(int amount) {
|
||||||
|
money += amount;
|
||||||
|
moneyCallback.call(player, money);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,13 +1,74 @@
|
||||||
package de.towerdefence.server.match;
|
package de.towerdefence.server.match;
|
||||||
|
|
||||||
|
import de.towerdefence.server.match.callbacks.PlayerHitpointsCallback;
|
||||||
|
import de.towerdefence.server.match.callbacks.PlayerMoneyCallback;
|
||||||
import de.towerdefence.server.player.Player;
|
import de.towerdefence.server.player.Player;
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
@AllArgsConstructor
|
import java.util.Optional;
|
||||||
|
|
||||||
@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;
|
||||||
|
|
||||||
|
private GameSession player1Session;
|
||||||
|
private GameSession player2Session;
|
||||||
|
|
||||||
|
public Match(String matchId, Player player1, Player player2) {
|
||||||
|
this.matchId = matchId;
|
||||||
|
this.player1 = player1;
|
||||||
|
this.player2 = player2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Player> getOpponent(Player player) {
|
||||||
|
boolean isPlayer1 = player1.equals(player);
|
||||||
|
boolean isPlayer2 = player2.equals(player);
|
||||||
|
if (!isPlayer1 && !isPlayer2) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
if (isPlayer1) {
|
||||||
|
return Optional.of(player2);
|
||||||
|
}
|
||||||
|
return Optional.of(player1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<GameSession> getPlayerGameSession(Player player) {
|
||||||
|
boolean isPlayer1 = player1.equals(player);
|
||||||
|
boolean isPlayer2 = player2.equals(player);
|
||||||
|
if (!isPlayer1 && !isPlayer2) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
if (isPlayer1) {
|
||||||
|
return Optional.of(player1Session);
|
||||||
|
}
|
||||||
|
return Optional.of(player2Session);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void connectPlayer(
|
||||||
|
Player player,
|
||||||
|
PlayerMoneyCallback moneyCallback,
|
||||||
|
PlayerHitpointsCallback hitpointsCallback
|
||||||
|
) {
|
||||||
|
boolean isPlayer1 = player1.equals(player);
|
||||||
|
boolean isPlayer2 = player2.equals(player);
|
||||||
|
if (!isPlayer1 && !isPlayer2) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (isPlayer1 && player1Session == null) {
|
||||||
|
this.player1Session = new GameSession(player, moneyCallback);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (isPlayer2 && player2Session == null) {
|
||||||
|
this.player2Session = new GameSession(player, moneyCallback);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasMatchStarted() {
|
||||||
|
return player1Session != null && player2Session != null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
89
src/main/java/de/towerdefence/server/match/MatchService.java
Normal file
89
src/main/java/de/towerdefence/server/match/MatchService.java
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
package de.towerdefence.server.match;
|
||||||
|
|
||||||
|
import de.towerdefence.server.match.callbacks.PlayerHitpointsCallback;
|
||||||
|
import de.towerdefence.server.match.callbacks.PlayerMoneyCallback;
|
||||||
|
import de.towerdefence.server.match.exeptions.InvalidPlacementException;
|
||||||
|
import de.towerdefence.server.match.exeptions.InvalidPlacementReason;
|
||||||
|
import de.towerdefence.server.match.exeptions.NotInMatchException;
|
||||||
|
import de.towerdefence.server.player.Player;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class MatchService {
|
||||||
|
private final Map<Player, Match> playerMatches = new HashMap<>();
|
||||||
|
private final Map<Match, ScheduledFuture<?>> moneyTasks = new HashMap<>();
|
||||||
|
private static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return opponent
|
||||||
|
*/
|
||||||
|
public Player placeTower(Player player, int x, int y) throws NotInMatchException, InvalidPlacementException {
|
||||||
|
Match match = playerMatches.get(player);
|
||||||
|
if (!match.hasMatchStarted()) {
|
||||||
|
throw new InvalidPlacementException(InvalidPlacementReason.MATCH_NOT_STARTED);
|
||||||
|
}
|
||||||
|
Optional<GameSession> playerSession = match.getPlayerGameSession(player);
|
||||||
|
if (playerSession.isEmpty()) {
|
||||||
|
throw new NotInMatchException();
|
||||||
|
}
|
||||||
|
Optional<Player> opponent = match.getOpponent(player);
|
||||||
|
if (opponent.isEmpty()) {
|
||||||
|
throw new NotInMatchException();
|
||||||
|
}
|
||||||
|
playerSession.get().placeTower(new Tower(), x, y);
|
||||||
|
return opponent.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void playerConnected(
|
||||||
|
Player player,
|
||||||
|
PlayerMoneyCallback moneyCallback,
|
||||||
|
PlayerHitpointsCallback hitpointsCallback
|
||||||
|
) {
|
||||||
|
Match match = playerMatches.get(player);
|
||||||
|
match.connectPlayer(player, moneyCallback, hitpointsCallback);
|
||||||
|
Optional<GameSession> optionalPlayerSession = match.getPlayerGameSession(player);
|
||||||
|
if (optionalPlayerSession.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
GameSession playerSession = optionalPlayerSession.get();
|
||||||
|
moneyCallback.call(player, playerSession.getMoney());
|
||||||
|
hitpointsCallback.call(player, playerSession.getPlayerHitpoints() );
|
||||||
|
if (!match.hasMatchStarted()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Optional<Player> optionalOpponent = match.getOpponent(player);
|
||||||
|
if (optionalOpponent.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Player opponent = optionalOpponent.get();
|
||||||
|
Optional<GameSession> optionalOpponentSession = match.getPlayerGameSession(opponent);
|
||||||
|
if (optionalOpponentSession.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
GameSession opponentSession = optionalOpponentSession.get();
|
||||||
|
ScheduledFuture<?> moneyTask = scheduler.scheduleAtFixedRate(
|
||||||
|
() -> {
|
||||||
|
playerSession.addMoney(3);
|
||||||
|
opponentSession.addMoney(3);
|
||||||
|
},
|
||||||
|
5,
|
||||||
|
5,
|
||||||
|
TimeUnit.SECONDS);
|
||||||
|
moneyTasks.put(match, moneyTask);
|
||||||
|
}
|
||||||
|
}
|
5
src/main/java/de/towerdefence/server/match/Tower.java
Normal file
5
src/main/java/de/towerdefence/server/match/Tower.java
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
package de.towerdefence.server.match;
|
||||||
|
|
||||||
|
public class Tower {
|
||||||
|
public static final int COST = 20;
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package de.towerdefence.server.match.callbacks;
|
||||||
|
|
||||||
|
import de.towerdefence.server.player.Player;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface PlayerHitpointsCallback {
|
||||||
|
void call(Player player, int playerHitpoints);
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package de.towerdefence.server.match.callbacks;
|
||||||
|
|
||||||
|
import de.towerdefence.server.player.Player;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface PlayerMoneyCallback {
|
||||||
|
void call(Player player, int playerMoney);
|
||||||
|
}
|
|
@ -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());
|
||||||
|
@ -72,7 +122,51 @@ public class MatchConfirmationService {
|
||||||
|
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case ABORTED -> {
|
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(
|
player1Callbacks.getRequeueCallback().call(
|
||||||
match.getPlayer1(),
|
match.getPlayer1(),
|
||||||
match.getMatchId(),
|
match.getMatchId(),
|
||||||
|
@ -81,7 +175,7 @@ public class MatchConfirmationService {
|
||||||
player1Callbacks.getAbortCallback(),
|
player1Callbacks.getAbortCallback(),
|
||||||
player1Callbacks.getEstablishedCallback());
|
player1Callbacks.getEstablishedCallback());
|
||||||
}
|
}
|
||||||
if (match.getPlayer2State() == PlayerMatchConfirmState.CONFIRMED) {
|
if (player2successful) {
|
||||||
player2Callbacks.getRequeueCallback().call(
|
player2Callbacks.getRequeueCallback().call(
|
||||||
match.getPlayer2(),
|
match.getPlayer2(),
|
||||||
match.getMatchId(),
|
match.getMatchId(),
|
||||||
|
@ -90,13 +184,30 @@ public class MatchConfirmationService {
|
||||||
player2Callbacks.getAbortCallback(),
|
player2Callbacks.getAbortCallback(),
|
||||||
player2Callbacks.getEstablishedCallback());
|
player2Callbacks.getEstablishedCallback());
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
case CONFIRMED -> {
|
matchService.createMatch(match.getMatchId(), match.getPlayer1(), match.getPlayer2());
|
||||||
// TODO: Create Match and Send Players the info that the Match is created
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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) {
|
||||||
UnconfirmedMatch match = unconfirmedMatch.get(player);
|
UnconfirmedMatch match = unconfirmedMatch.get(player);
|
||||||
if (match == null) {
|
if (match == null) {
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
package de.towerdefence.server.match.exeptions;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class InvalidPlacementException extends RuntimeException {
|
||||||
|
private final InvalidPlacementReason reason;
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
package de.towerdefence.server.match.exeptions;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public enum InvalidPlacementReason {
|
||||||
|
MATCH_NOT_STARTED("match-not-started"),
|
||||||
|
OUT_OF_BOUNDS("out-of-bounds"),
|
||||||
|
LOCATION_USED("location-used"),
|
||||||
|
NOT_ENOUGH_MONEY("not-enough-money");
|
||||||
|
|
||||||
|
private final String jsonName;
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
package de.towerdefence.server.match.exeptions;
|
||||||
|
|
||||||
|
public class NotInMatchException extends RuntimeException {
|
||||||
|
}
|
|
@ -39,6 +39,7 @@ public abstract class JsonWebsocketHandler extends TextWebSocketHandler {
|
||||||
protected void closeSession(WebSocketSession session, CloseStatus reason){
|
protected void closeSession(WebSocketSession session, CloseStatus reason){
|
||||||
if(session.isOpen()){
|
if(session.isOpen()){
|
||||||
try{
|
try{
|
||||||
|
sessionPlayers.remove(session);
|
||||||
session.close(reason);
|
session.close(reason);
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
logger.info("Unable to Close the Session", exception);
|
logger.info("Unable to Close the Session", exception);
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,112 @@
|
||||||
|
package de.towerdefence.server.server.channels.match;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import de.towerdefence.server.match.MatchService;
|
||||||
|
import de.towerdefence.server.match.exeptions.InvalidPlacementException;
|
||||||
|
import de.towerdefence.server.match.exeptions.NotInMatchException;
|
||||||
|
import de.towerdefence.server.player.Player;
|
||||||
|
import de.towerdefence.server.server.JsonWebsocketHandler;
|
||||||
|
import de.towerdefence.server.server.channels.match.hitpoints.PlayerHitpointsMessage;
|
||||||
|
import de.towerdefence.server.server.channels.match.money.PlayerMoneyMessage;
|
||||||
|
import de.towerdefence.server.server.channels.match.placing.GamePlayerMap;
|
||||||
|
import de.towerdefence.server.server.channels.match.placing.InvalidPlacementMessage;
|
||||||
|
import de.towerdefence.server.server.channels.match.placing.RequestTowerPlacingMessage;
|
||||||
|
import de.towerdefence.server.server.channels.match.placing.TowerPlacedMessage;
|
||||||
|
import de.towerdefence.server.session.Channel;
|
||||||
|
import de.towerdefence.server.session.SessionsService;
|
||||||
|
import org.springframework.web.socket.CloseStatus;
|
||||||
|
import org.springframework.web.socket.TextMessage;
|
||||||
|
import org.springframework.web.socket.WebSocketSession;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
public class MatchWebsocketHandler extends JsonWebsocketHandler {
|
||||||
|
|
||||||
|
protected final Map<Player, WebSocketSession> playerSessions = new ConcurrentHashMap<>();
|
||||||
|
private final MatchService matchService;
|
||||||
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
public MatchWebsocketHandler(SessionsService sessionsService, MatchService matchService) {
|
||||||
|
super(Channel.MATCH, sessionsService);
|
||||||
|
this.matchService = matchService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterConnectionEstablished(WebSocketSession session) {
|
||||||
|
super.afterConnectionEstablished(session);
|
||||||
|
playerSessions.put(sessionPlayers.get(session), session);
|
||||||
|
matchService.playerConnected(
|
||||||
|
sessionPlayers.get(session),
|
||||||
|
this::onPlayerMoneyChanged,
|
||||||
|
this::onPlayerHitpointsChanged
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onPlayerMoneyChanged(Player player, int playerMoney) {
|
||||||
|
WebSocketSession session = playerSessions.get(player);
|
||||||
|
try {
|
||||||
|
new PlayerMoneyMessage(playerMoney).send(session);
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onPlayerHitpointsChanged(Player player, int playerHitpoints) {
|
||||||
|
WebSocketSession session = playerSessions.get(player);
|
||||||
|
try {
|
||||||
|
new PlayerHitpointsMessage(playerHitpoints).send(session);
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void closeSession(WebSocketSession session, CloseStatus reason) {
|
||||||
|
Player player = sessionPlayers.get(session);
|
||||||
|
super.closeSession(session, reason);
|
||||||
|
playerSessions.remove(player);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void handleTextMessage(WebSocketSession session, TextMessage message) {
|
||||||
|
try {
|
||||||
|
String payload = message.getPayload();
|
||||||
|
if (!Objects.equals(
|
||||||
|
objectMapper.readTree(payload).get("$id").asText(),
|
||||||
|
RequestTowerPlacingMessage.MESSAGE_ID
|
||||||
|
)) {
|
||||||
|
this.closeSession(session, CloseStatus.BAD_DATA);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
handleRequestTowerPlacingMessage(session, payload);
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
this.closeSession(session, CloseStatus.BAD_DATA);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleRequestTowerPlacingMessage(
|
||||||
|
WebSocketSession session,
|
||||||
|
String payload
|
||||||
|
) throws IOException {
|
||||||
|
RequestTowerPlacingMessage msg = objectMapper.readValue(payload, RequestTowerPlacingMessage.class);
|
||||||
|
Player opponent;
|
||||||
|
try {
|
||||||
|
opponent = this.matchService.placeTower(
|
||||||
|
this.sessionPlayers.get(session),
|
||||||
|
msg.getX(),
|
||||||
|
msg.getY()
|
||||||
|
);
|
||||||
|
} catch (InvalidPlacementException exception) {
|
||||||
|
new InvalidPlacementMessage(msg.getX(), msg.getY(), exception.getReason()).send(session);
|
||||||
|
return;
|
||||||
|
} catch (NotInMatchException ignored) {
|
||||||
|
this.closeSession(session, CloseStatus.BAD_DATA);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
WebSocketSession opponentSession = playerSessions.get(opponent);
|
||||||
|
new TowerPlacedMessage(msg.getX(), msg.getY(), GamePlayerMap.PLAYER).send(session);
|
||||||
|
new TowerPlacedMessage(msg.getX(), msg.getY(), GamePlayerMap.OPPONENT).send(opponentSession);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
package de.towerdefence.server.server.channels.match.hitpoints;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||||
|
import de.towerdefence.server.server.JsonMessage;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class PlayerHitpointsMessage extends JsonMessage {
|
||||||
|
|
||||||
|
private final int playerHitpoints;
|
||||||
|
@Override
|
||||||
|
protected String getMessageId() {
|
||||||
|
return "PlayerHitpoints";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Map<String, JsonNode> getData(JsonNodeFactory factory) {
|
||||||
|
return Map.of(
|
||||||
|
"playerHitpoints", factory.numberNode(this.playerHitpoints)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package de.towerdefence.server.server.channels.time;
|
package de.towerdefence.server.server.channels.match.money;
|
||||||
|
|
||||||
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;
|
||||||
|
@ -8,16 +8,19 @@ import lombok.AllArgsConstructor;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class TimeMessage extends JsonMessage {
|
public class PlayerMoneyMessage extends JsonMessage {
|
||||||
private final long time;
|
|
||||||
|
private final int playerMoney;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getMessageId() {
|
protected String getMessageId() {
|
||||||
return "CurrentUnixTime";
|
return "PlayerMoney";
|
||||||
}
|
}
|
||||||
|
|
||||||
@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(
|
||||||
|
"playerMoney", factory.numberNode(this.playerMoney)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package de.towerdefence.server.server.channels.match.placing;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public enum GamePlayerMap {
|
||||||
|
PLAYER("player"),
|
||||||
|
OPPONENT("opponent");
|
||||||
|
|
||||||
|
private final String jsonName;
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
package de.towerdefence.server.server.channels.match.placing;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||||
|
|
||||||
|
import de.towerdefence.server.match.exeptions.InvalidPlacementReason;
|
||||||
|
import de.towerdefence.server.server.JsonMessage;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class InvalidPlacementMessage extends JsonMessage {
|
||||||
|
|
||||||
|
private final int x;
|
||||||
|
private final int y;
|
||||||
|
private final InvalidPlacementReason reason;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getMessageId() {
|
||||||
|
return "InvalidPlacement";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Map<String, JsonNode> getData(JsonNodeFactory factory) {
|
||||||
|
return Map.of(
|
||||||
|
"x", factory.numberNode(this.x),
|
||||||
|
"y", factory.numberNode(this.y),
|
||||||
|
"reason", factory.textNode(this.reason.getJsonName()));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package de.towerdefence.server.server.channels.match.placing;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NotNull
|
||||||
|
public class RequestTowerPlacingMessage {
|
||||||
|
public static final String MESSAGE_ID = "RequestTowerPlacing";
|
||||||
|
|
||||||
|
@JsonProperty("$id")
|
||||||
|
private String messageId;
|
||||||
|
private int x;
|
||||||
|
private int y;
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
package de.towerdefence.server.server.channels.match.placing;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||||
|
import de.towerdefence.server.server.JsonMessage;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class TowerPlacedMessage extends JsonMessage {
|
||||||
|
|
||||||
|
private final int x;
|
||||||
|
private final int y;
|
||||||
|
private final GamePlayerMap map;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getMessageId() {
|
||||||
|
return "TowerPlaced";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Map<String, JsonNode> getData(JsonNodeFactory factory) {
|
||||||
|
return Map.of(
|
||||||
|
"x", factory.numberNode(this.x),
|
||||||
|
"y", factory.numberNode(this.y),
|
||||||
|
"map", factory.textNode(this.map.getJsonName())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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() {
|
||||||
|
@ -21,7 +22,8 @@ public class MatchEstablishedMessage extends JsonMessage {
|
||||||
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)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,46 +0,0 @@
|
||||||
package de.towerdefence.server.server.channels.time;
|
|
||||||
|
|
||||||
import de.towerdefence.server.server.JsonWebsocketHandler;
|
|
||||||
import de.towerdefence.server.session.Channel;
|
|
||||||
import de.towerdefence.server.session.SessionsService;
|
|
||||||
import org.springframework.web.socket.WebSocketSession;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.*;
|
|
||||||
|
|
||||||
public class TimeWebsocketHandler extends JsonWebsocketHandler {
|
|
||||||
|
|
||||||
private final Map<WebSocketSession, ScheduledFuture<?>> sessionTaskMap = new ConcurrentHashMap<>();
|
|
||||||
private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
|
|
||||||
|
|
||||||
public TimeWebsocketHandler(SessionsService sessionsService) {
|
|
||||||
super(Channel.TIME, sessionsService);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterConnectionEstablished(WebSocketSession session) {
|
|
||||||
super.afterConnectionEstablished(session);
|
|
||||||
|
|
||||||
ScheduledFuture<?> scheduledTask = scheduler.scheduleAtFixedRate(
|
|
||||||
() -> sendCurrentTime(session),
|
|
||||||
0,
|
|
||||||
1,
|
|
||||||
TimeUnit.MILLISECONDS
|
|
||||||
);
|
|
||||||
sessionTaskMap.put(session, scheduledTask);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void sendCurrentTime(WebSocketSession session) {
|
|
||||||
ScheduledFuture<?> task = sessionTaskMap.get(session);
|
|
||||||
try {
|
|
||||||
if(!session.isOpen()){
|
|
||||||
throw new RuntimeException("Session is not open");
|
|
||||||
}
|
|
||||||
new TimeMessage(System.currentTimeMillis()).send(session);
|
|
||||||
} catch (RuntimeException | IOException e) {
|
|
||||||
task.cancel(true);
|
|
||||||
sessionTaskMap.remove(session);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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,15 @@
|
||||||
<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>
|
||||||
|
<Match>
|
||||||
|
<!-- This is Only a Placeholder Object, thus it's not a bad practice to instance it-->
|
||||||
|
<Class name="de.towerdefence.server.match.MatchService"/>
|
||||||
|
<Method name="placeTower"/>
|
||||||
|
<Bug code="L,B,ISC"/>
|
||||||
|
</Match>
|
||||||
</FindBugsFilter>
|
</FindBugsFilter>
|
||||||
|
|
|
@ -13,7 +13,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||||
class GetAllPlayersTest extends IntegrationTest {
|
class GetAllPlayersTest extends IntegrationTest {
|
||||||
|
|
||||||
private MockHttpServletRequestBuilder createGetAllPlayersRequest(String requestBody) {
|
private MockHttpServletRequestBuilder createGetAllPlayersRequest(String requestBody) {
|
||||||
return MockMvcRequestBuilders.get(baseUri + "/admin/players")
|
return MockMvcRequestBuilders.post(baseUri + "/admin/players")
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
.content(requestBody);
|
.content(requestBody);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
#!/bin/sh
|
|
||||||
|
|
||||||
response=$(curl -s -X 'POST' \
|
|
||||||
'http://localhost:8080/api/v1/player/login' \
|
|
||||||
-H 'accept: application/json' \
|
|
||||||
-H 'Content-Type: application/json' \
|
|
||||||
-d '{
|
|
||||||
"username": "Player1",
|
|
||||||
"password": "1234"
|
|
||||||
}')
|
|
||||||
|
|
||||||
token=$(echo "$response" | jq -r .token)
|
|
||||||
payload='{"$id": "RequestConnectionToken", "channel": "time"}'
|
|
||||||
response=$(echo "$payload" | websocat ws://localhost:8080/ws/connection -H "Authorization: $token")
|
|
||||||
|
|
||||||
time_token=$(echo "$response" | jq -r .token)
|
|
||||||
websocat ws://localhost:8080/ws/time -H "Authorization: $time_token"
|
|
140
ws/ws.yml
140
ws/ws.yml
|
@ -147,19 +147,21 @@ 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:
|
RequestTowerPlacing:
|
||||||
description: The Current time in Unix Time
|
description: Requesting a placement of a tower
|
||||||
payload:
|
payload:
|
||||||
type: object
|
type: object
|
||||||
additionalProperties: false
|
additionalProperties: false
|
||||||
|
@ -167,12 +169,92 @@ channels:
|
||||||
$id:
|
$id:
|
||||||
type: string
|
type: string
|
||||||
format: messageId
|
format: messageId
|
||||||
time:
|
x:
|
||||||
|
type: integer
|
||||||
|
y:
|
||||||
type: integer
|
type: integer
|
||||||
format: int64
|
|
||||||
required:
|
required:
|
||||||
- $id
|
- $id
|
||||||
- time
|
- x
|
||||||
|
- y
|
||||||
|
TowerPlaced:
|
||||||
|
description: A tower was placed
|
||||||
|
payload:
|
||||||
|
type: object
|
||||||
|
additionalProperties: false
|
||||||
|
properties:
|
||||||
|
$id:
|
||||||
|
type: string
|
||||||
|
format: messageId
|
||||||
|
x:
|
||||||
|
type: integer
|
||||||
|
y:
|
||||||
|
type: integer
|
||||||
|
map:
|
||||||
|
type: string
|
||||||
|
enum:
|
||||||
|
- player
|
||||||
|
- opponent
|
||||||
|
required:
|
||||||
|
- $id
|
||||||
|
- x
|
||||||
|
- y
|
||||||
|
- map
|
||||||
|
InvalidPlacement:
|
||||||
|
description: A tower was placed
|
||||||
|
payload:
|
||||||
|
type: object
|
||||||
|
additionalProperties: false
|
||||||
|
properties:
|
||||||
|
$id:
|
||||||
|
type: string
|
||||||
|
format: messageId
|
||||||
|
x:
|
||||||
|
type: integer
|
||||||
|
y:
|
||||||
|
type: integer
|
||||||
|
reason:
|
||||||
|
type: string
|
||||||
|
enum:
|
||||||
|
- "match-not-started"
|
||||||
|
- "out-of-bounds"
|
||||||
|
- "location-used"
|
||||||
|
- "not-enough-money"
|
||||||
|
required:
|
||||||
|
- $id
|
||||||
|
- x
|
||||||
|
- y
|
||||||
|
- reason
|
||||||
|
PlayerMoney:
|
||||||
|
description: Money a player currently has
|
||||||
|
payload:
|
||||||
|
type: object
|
||||||
|
additionalProperties: false
|
||||||
|
properties:
|
||||||
|
$id:
|
||||||
|
type: string
|
||||||
|
format: messageId
|
||||||
|
playerMoney:
|
||||||
|
type: integer
|
||||||
|
required:
|
||||||
|
- $id
|
||||||
|
- playerMoney
|
||||||
|
PlayerHitpoints:
|
||||||
|
description: Hitpoints a player currently has
|
||||||
|
payload:
|
||||||
|
type: object
|
||||||
|
additionalProperties: false
|
||||||
|
properties:
|
||||||
|
$id:
|
||||||
|
type: string
|
||||||
|
format: messageId
|
||||||
|
playerHitpoints:
|
||||||
|
type: integer
|
||||||
|
required:
|
||||||
|
- $id
|
||||||
|
- playerHitpoints
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
operations:
|
operations:
|
||||||
requestConnectionToken:
|
requestConnectionToken:
|
||||||
|
@ -227,13 +309,42 @@ operations:
|
||||||
$ref: "#/channels/matchmaking"
|
$ref: "#/channels/matchmaking"
|
||||||
messages:
|
messages:
|
||||||
- $ref: "#/channels/matchmaking/messages/MatchEstablished"
|
- $ref: "#/channels/matchmaking/messages/MatchEstablished"
|
||||||
updateTime:
|
towerPlacing:
|
||||||
title: Updates of the current Unix Time
|
title: TowerPlacing
|
||||||
|
action: send
|
||||||
|
channel:
|
||||||
|
$ref: "#/channels/match"
|
||||||
|
messages:
|
||||||
|
- $ref: "#/channels/match/messages/RequestTowerPlacing"
|
||||||
|
reply:
|
||||||
|
channel:
|
||||||
|
$ref: "#/channels/match"
|
||||||
|
messages:
|
||||||
|
- $ref: "#/channels/match/messages/TowerPlaced"
|
||||||
|
- $ref: "#/channels/match/messages/InvalidPlacement"
|
||||||
|
enemyPlacedTower:
|
||||||
|
title: EnemyPlacedTower
|
||||||
action: receive
|
action: receive
|
||||||
channel:
|
channel:
|
||||||
$ref: "#/channels/time"
|
$ref: "#/channels/match"
|
||||||
messages:
|
messages:
|
||||||
- $ref: "#/channels/time/messages/CurrentUnixTime"
|
- $ref: "#/channels/match/messages/TowerPlaced"
|
||||||
|
playerMoney:
|
||||||
|
title: PlayerMoney
|
||||||
|
action: receive
|
||||||
|
channel:
|
||||||
|
$ref: "#/channels/match"
|
||||||
|
messages:
|
||||||
|
- $ref: "#/channels/match/messages/PlayerMoney"
|
||||||
|
playerHitpoints:
|
||||||
|
title: PlayerHitpoints
|
||||||
|
action: receive
|
||||||
|
channel:
|
||||||
|
$ref: "#/channels/match"
|
||||||
|
messages:
|
||||||
|
- $ref: "#/channels/match/messages/PlayerHitpoints"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
components:
|
components:
|
||||||
securitySchemes:
|
securitySchemes:
|
||||||
|
@ -252,7 +363,4 @@ components:
|
||||||
Channel:
|
Channel:
|
||||||
type: string
|
type: string
|
||||||
enum:
|
enum:
|
||||||
- connection
|
|
||||||
- matchmaking
|
- matchmaking
|
||||||
- time
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue