Merge pull request '[Feature]: Tower Placement' (!10) from story/td-11-tower-platzieren into trunk
Reviewed-on: #10 Reviewed-by: SZUT-Rajbir <rajbir2@schule.bremen.de>
This commit is contained in:
commit
b849cd7a27
11 changed files with 301 additions and 24 deletions
|
@ -0,0 +1,11 @@
|
|||
package de.towerdefence.server.match;
|
||||
|
||||
import de.towerdefence.server.server.channels.match.placing.InvalidPlacementReason;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class InvalidPlacementException extends RuntimeException {
|
||||
private final InvalidPlacementReason reason;
|
||||
}
|
|
@ -1,7 +1,8 @@
|
|||
package de.towerdefence.server.match;
|
||||
|
||||
import de.towerdefence.server.match.confirmation.ConfirmationCallbacks;
|
||||
import de.towerdefence.server.player.Player;
|
||||
import de.towerdefence.server.server.channels.match.placing.InvalidPlacementReason;
|
||||
import de.towerdefence.server.server.channels.match.placing.Tower;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
|
@ -9,7 +10,47 @@ import lombok.Getter;
|
|||
@Getter
|
||||
public class Match {
|
||||
|
||||
public final static int MAP_SIZE_X = 10;
|
||||
public final static int MAP_SIZE_Y = 20;
|
||||
private final String matchId;
|
||||
private final Player player1;
|
||||
private final Player player2;
|
||||
private final Tower[][] player1Map = new Tower[MAP_SIZE_X][MAP_SIZE_Y];
|
||||
private final Tower[][] player2Map = new Tower[MAP_SIZE_X][MAP_SIZE_Y];
|
||||
|
||||
public Player getOpponent(Player player) {
|
||||
boolean isPlayer1 = player1.equals(player);
|
||||
boolean isPlayer2 = player2.equals(player);
|
||||
if (!isPlayer1 && !isPlayer2) {
|
||||
return null;
|
||||
}
|
||||
if (isPlayer1) {
|
||||
return player2;
|
||||
}
|
||||
return player1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return opponent
|
||||
*/
|
||||
public Player addTower(Player player, Tower tower, int x, int y) throws InvalidPlacementException {
|
||||
if (player != player1 && player != player2) {
|
||||
return null;
|
||||
}
|
||||
if (x < 0 || y < 0 || x + 1 > Match.MAP_SIZE_X || y + 1 > Match.MAP_SIZE_Y) {
|
||||
throw new InvalidPlacementException(InvalidPlacementReason.OUT_OF_BOUNDS);
|
||||
}
|
||||
if (player == player1) {
|
||||
if (player1Map[x][y] != null) {
|
||||
throw new InvalidPlacementException(InvalidPlacementReason.LOCATION_USED);
|
||||
}
|
||||
player1Map[x][y] = tower;
|
||||
} else {
|
||||
if (player2Map[x][y] != null) {
|
||||
throw new InvalidPlacementException(InvalidPlacementReason.LOCATION_USED);
|
||||
}
|
||||
player2Map[x][y] = tower;
|
||||
}
|
||||
return getOpponent(player);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package de.towerdefence.server.match;
|
||||
|
||||
import de.towerdefence.server.player.Player;
|
||||
import de.towerdefence.server.server.channels.match.placing.Tower;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
@ -19,4 +20,12 @@ public class MatchService {
|
|||
public Match get(Player player) {
|
||||
return playerMatches.get(player);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return opponent
|
||||
*/
|
||||
public Player placeTower(Player player, int x, int y) throws InvalidPlacementException {
|
||||
return playerMatches.get(player).addTower(player, new Tower(), x, y);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,20 +1,30 @@
|
|||
package de.towerdefence.server.server.channels.match;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import de.towerdefence.server.match.InvalidPlacementException;
|
||||
import de.towerdefence.server.match.MatchService;
|
||||
import de.towerdefence.server.player.Player;
|
||||
import de.towerdefence.server.server.JsonWebsocketHandler;
|
||||
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.concurrent.*;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class MatchWebsocketHandler extends JsonWebsocketHandler {
|
||||
|
||||
final Map<WebSocketSession, ScheduledFuture<?>> sessionTaskMap = new ConcurrentHashMap<>();
|
||||
private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
|
||||
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);
|
||||
|
@ -24,28 +34,45 @@ public class MatchWebsocketHandler extends JsonWebsocketHandler {
|
|||
@Override
|
||||
public void afterConnectionEstablished(WebSocketSession session) {
|
||||
super.afterConnectionEstablished(session);
|
||||
ScheduledFuture<?> scheduledTask = scheduler.scheduleAtFixedRate(
|
||||
() -> sendCurrentTime(session),
|
||||
0,
|
||||
1,
|
||||
TimeUnit.SECONDS
|
||||
);
|
||||
sessionTaskMap.put(session, scheduledTask);
|
||||
playerSessions.put(sessionPlayers.get(session), session);
|
||||
}
|
||||
|
||||
private void sendCurrentTime(WebSocketSession session) {
|
||||
ScheduledFuture<?> task = sessionTaskMap.get(session);
|
||||
@Override
|
||||
protected void handleTextMessage(WebSocketSession session, TextMessage message) {
|
||||
try {
|
||||
if (!session.isOpen()) {
|
||||
throw new RuntimeException("Session is not open");
|
||||
String payload = message.getPayload();
|
||||
if (!Objects.equals(
|
||||
objectMapper.readTree(payload).get("$id").asText(),
|
||||
RequestTowerPlacingMessage.MESSAGE_ID
|
||||
)) {
|
||||
this.closeSession(session, CloseStatus.BAD_DATA);
|
||||
return;
|
||||
}
|
||||
new TimeMessage(
|
||||
System.currentTimeMillis(),
|
||||
matchService.get(this.sessionPlayers.get(session)).getMatchId()
|
||||
).send(session);
|
||||
} catch (RuntimeException | IOException e) {
|
||||
task.cancel(true);
|
||||
sessionTaskMap.remove(session);
|
||||
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;
|
||||
}
|
||||
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,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,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 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,13 @@
|
|||
package de.towerdefence.server.server.channels.match.placing;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum InvalidPlacementReason {
|
||||
OUT_OF_BOUNDS("out-of-bounds"),
|
||||
LOCATION_USED("location-used");
|
||||
|
||||
private final String jsonName;
|
||||
}
|
|
@ -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,4 @@
|
|||
package de.towerdefence.server.server.channels.match.placing;
|
||||
|
||||
public class Tower {
|
||||
}
|
|
@ -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())
|
||||
);
|
||||
}
|
||||
}
|
87
ws/ws.yml
87
ws/ws.yml
|
@ -178,8 +178,69 @@ channels:
|
|||
- $id
|
||||
- time
|
||||
- matchId
|
||||
|
||||
|
||||
RequestTowerPlacing:
|
||||
description: Requesting a placement of a tower
|
||||
payload:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
properties:
|
||||
$id:
|
||||
type: string
|
||||
format: messageId
|
||||
x:
|
||||
type: integer
|
||||
y:
|
||||
type: integer
|
||||
required:
|
||||
- $id
|
||||
- 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:
|
||||
- "out-of-bounds"
|
||||
- "location-used"
|
||||
required:
|
||||
- $id
|
||||
- x
|
||||
- y
|
||||
- reason
|
||||
|
||||
operations:
|
||||
requestConnectionToken:
|
||||
|
@ -241,6 +302,28 @@ operations:
|
|||
$ref: "#/channels/match"
|
||||
messages:
|
||||
- $ref: "#/channels/match/messages/CurrentUnixTime"
|
||||
towerPlacing:
|
||||
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
|
||||
channel:
|
||||
$ref: "#/channels/match"
|
||||
messages:
|
||||
- $ref: "#/channels/match/messages/TowerPlaced"
|
||||
|
||||
|
||||
|
||||
components:
|
||||
securitySchemes:
|
||||
|
|
Loading…
Add table
Reference in a new issue