Compare commits
2 commits
TD-4_Admin
...
trunk
Author | SHA1 | Date | |
---|---|---|---|
e188cc5974 | |||
|
de9c0c8faf |
13 changed files with 77 additions and 94 deletions
40
api/api.yml
40
api/api.yml
|
@ -95,14 +95,8 @@ components:
|
||||||
properties:
|
properties:
|
||||||
username:
|
username:
|
||||||
type: string
|
type: string
|
||||||
id:
|
|
||||||
type: integer
|
|
||||||
format: int64
|
|
||||||
banned:
|
|
||||||
type: boolean
|
|
||||||
required:
|
required:
|
||||||
- username
|
- username
|
||||||
- banned
|
|
||||||
#############################################
|
#############################################
|
||||||
# PlayerFilter #
|
# PlayerFilter #
|
||||||
#############################################
|
#############################################
|
||||||
|
@ -265,40 +259,6 @@ paths:
|
||||||
type: array
|
type: array
|
||||||
items:
|
items:
|
||||||
$ref: "#/components/schemas/AdministratablePlayer"
|
$ref: "#/components/schemas/AdministratablePlayer"
|
||||||
/admin/player/{id}/ban:
|
|
||||||
parameters:
|
|
||||||
- in: path
|
|
||||||
name: id
|
|
||||||
schema:
|
|
||||||
type: integer
|
|
||||||
format: int64
|
|
||||||
required: true
|
|
||||||
post:
|
|
||||||
operationId: "BanPlayer"
|
|
||||||
tags:
|
|
||||||
- admin
|
|
||||||
summary: "Ban player by id"
|
|
||||||
description: "Bans a player by id, making them unable to play the game."
|
|
||||||
responses:
|
|
||||||
200:
|
|
||||||
description: "Successfully banned player!"
|
|
||||||
/admin/player/{id}/unban:
|
|
||||||
parameters:
|
|
||||||
- in: path
|
|
||||||
name: id
|
|
||||||
schema:
|
|
||||||
type: integer
|
|
||||||
format: int64
|
|
||||||
required: true
|
|
||||||
post:
|
|
||||||
operationId: "UnbanPlayer"
|
|
||||||
tags:
|
|
||||||
- admin
|
|
||||||
summary: "Unban player by id"
|
|
||||||
description: "Unbans a player by id, allowing them to play the game."
|
|
||||||
responses:
|
|
||||||
200:
|
|
||||||
description: "Successfully unbanned player!"
|
|
||||||
401:
|
401:
|
||||||
$ref: "#/components/responses/401Unauthorized"
|
$ref: "#/components/responses/401Unauthorized"
|
||||||
500:
|
500:
|
||||||
|
|
|
@ -8,7 +8,6 @@ import de.towerdefence.server.oas.models.AdministratablePlayer;
|
||||||
import de.towerdefence.server.oas.models.PlayerFilter;
|
import de.towerdefence.server.oas.models.PlayerFilter;
|
||||||
import de.towerdefence.server.player.Player;
|
import de.towerdefence.server.player.Player;
|
||||||
import de.towerdefence.server.player.PlayerRepository;
|
import de.towerdefence.server.player.PlayerRepository;
|
||||||
import de.towerdefence.server.player.PlayerService;
|
|
||||||
import de.towerdefence.server.utils.OrderToDirectionMapperService;
|
import de.towerdefence.server.utils.OrderToDirectionMapperService;
|
||||||
import de.towerdefence.server.utils.PlayerMapperService;
|
import de.towerdefence.server.utils.PlayerMapperService;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
@ -37,9 +36,6 @@ public class AdminApiController implements AdminApi {
|
||||||
@Autowired
|
@Autowired
|
||||||
PlayerMapperService playerMapperService;
|
PlayerMapperService playerMapperService;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
PlayerService playerService;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
OrderToDirectionMapperService orderToDirectionMapperService;
|
OrderToDirectionMapperService orderToDirectionMapperService;
|
||||||
|
|
||||||
|
@ -61,12 +57,6 @@ public class AdminApiController implements AdminApi {
|
||||||
return ResponseEntity.ok(authInfo);
|
return ResponseEntity.ok(authInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public ResponseEntity<Void> banPlayer(Long id) {
|
|
||||||
this.playerService.banPlayer(id);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ResponseEntity<List<AdministratablePlayer>> getAllPlayers(PlayerFilter body) {
|
public ResponseEntity<List<AdministratablePlayer>> getAllPlayers(PlayerFilter body) {
|
||||||
|
|
||||||
|
@ -93,10 +83,4 @@ public class AdminApiController implements AdminApi {
|
||||||
|
|
||||||
return ResponseEntity.ok(playersMapped);
|
return ResponseEntity.ok(playersMapped);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public ResponseEntity<Void> unbanPlayer(Long id) {
|
|
||||||
this.playerService.unbanPlayer(id);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
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.match.callbacks.PlayerMoneyCallback;
|
||||||
import de.towerdefence.server.match.exeptions.InvalidPlacementException;
|
import de.towerdefence.server.match.exeptions.InvalidPlacementException;
|
||||||
import de.towerdefence.server.match.exeptions.InvalidPlacementReason;
|
import de.towerdefence.server.match.exeptions.InvalidPlacementReason;
|
||||||
|
@ -7,6 +8,7 @@ import de.towerdefence.server.player.Player;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
public class GameSession {
|
public class GameSession {
|
||||||
|
final static int START_HITPOINTS = 100;
|
||||||
final static int START_MONEY = 50;
|
final static int START_MONEY = 50;
|
||||||
final static int MAP_SIZE_X = 10;
|
final static int MAP_SIZE_X = 10;
|
||||||
final static int MAP_SIZE_Y = 20;
|
final static int MAP_SIZE_Y = 20;
|
||||||
|
@ -16,12 +18,18 @@ public class GameSession {
|
||||||
private final Player player;
|
private final Player player;
|
||||||
@Getter
|
@Getter
|
||||||
private int money;
|
private int money;
|
||||||
|
@Getter
|
||||||
|
private int playerHitpoints;
|
||||||
private final PlayerMoneyCallback moneyCallback;
|
private final PlayerMoneyCallback moneyCallback;
|
||||||
|
//private final PlayerHitpointsCallback hitpointsCallback;
|
||||||
|
|
||||||
public GameSession(Player player, PlayerMoneyCallback moneyCallback) {
|
public GameSession(Player player, PlayerMoneyCallback moneyCallback) {
|
||||||
this.player = player;
|
this.player = player;
|
||||||
this.moneyCallback = moneyCallback;
|
this.moneyCallback = moneyCallback;
|
||||||
|
//this.hitpointsCallback = hitpointsCallback;
|
||||||
this.money = START_MONEY;
|
this.money = START_MONEY;
|
||||||
|
this.playerHitpoints = START_HITPOINTS;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void placeTower(Tower tower, int x, int y) throws InvalidPlacementException {
|
public void placeTower(Tower tower, int x, int y) throws InvalidPlacementException {
|
||||||
|
@ -41,5 +49,6 @@ public class GameSession {
|
||||||
|
|
||||||
public void addMoney(int amount) {
|
public void addMoney(int amount) {
|
||||||
money += amount;
|
money += amount;
|
||||||
|
moneyCallback.call(player, money);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
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.match.callbacks.PlayerMoneyCallback;
|
||||||
import de.towerdefence.server.player.Player;
|
import de.towerdefence.server.player.Player;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
@ -47,7 +48,11 @@ public class Match {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void connectPlayer(Player player, PlayerMoneyCallback moneyCallback) {
|
public void connectPlayer(
|
||||||
|
Player player,
|
||||||
|
PlayerMoneyCallback moneyCallback,
|
||||||
|
PlayerHitpointsCallback hitpointsCallback
|
||||||
|
) {
|
||||||
boolean isPlayer1 = player1.equals(player);
|
boolean isPlayer1 = player1.equals(player);
|
||||||
boolean isPlayer2 = player2.equals(player);
|
boolean isPlayer2 = player2.equals(player);
|
||||||
if (!isPlayer1 && !isPlayer2) {
|
if (!isPlayer1 && !isPlayer2) {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
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.match.callbacks.PlayerMoneyCallback;
|
||||||
import de.towerdefence.server.match.exeptions.InvalidPlacementException;
|
import de.towerdefence.server.match.exeptions.InvalidPlacementException;
|
||||||
import de.towerdefence.server.match.exeptions.InvalidPlacementReason;
|
import de.towerdefence.server.match.exeptions.InvalidPlacementReason;
|
||||||
|
@ -48,15 +49,20 @@ public class MatchService {
|
||||||
return opponent.get();
|
return opponent.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void playerConnected(Player player, PlayerMoneyCallback moneyCallback) {
|
public void playerConnected(
|
||||||
|
Player player,
|
||||||
|
PlayerMoneyCallback moneyCallback,
|
||||||
|
PlayerHitpointsCallback hitpointsCallback
|
||||||
|
) {
|
||||||
Match match = playerMatches.get(player);
|
Match match = playerMatches.get(player);
|
||||||
match.connectPlayer(player, moneyCallback);
|
match.connectPlayer(player, moneyCallback, hitpointsCallback);
|
||||||
Optional<GameSession> optionalPlayerSession = match.getPlayerGameSession(player);
|
Optional<GameSession> optionalPlayerSession = match.getPlayerGameSession(player);
|
||||||
if (optionalPlayerSession.isEmpty()) {
|
if (optionalPlayerSession.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
GameSession playerSession = optionalPlayerSession.get();
|
GameSession playerSession = optionalPlayerSession.get();
|
||||||
moneyCallback.call(player, playerSession.getMoney());
|
moneyCallback.call(player, playerSession.getMoney());
|
||||||
|
hitpointsCallback.call(player, playerSession.getPlayerHitpoints() );
|
||||||
if (!match.hasMatchStarted()) {
|
if (!match.hasMatchStarted()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
|
@ -2,9 +2,11 @@ package de.towerdefence.server.player;
|
||||||
|
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import jakarta.validation.constraints.Size;
|
import jakarta.validation.constraints.Size;
|
||||||
import lombok.*;
|
import lombok.Setter;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import org.hibernate.annotations.ColumnDefault;
|
|
||||||
|
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
@ -31,8 +33,4 @@ public class Player {
|
||||||
@NotNull
|
@NotNull
|
||||||
@Size(min = PASSWORD_SALT_BYTE_LENGTH, max = PASSWORD_SALT_BYTE_LENGTH)
|
@Size(min = PASSWORD_SALT_BYTE_LENGTH, max = PASSWORD_SALT_BYTE_LENGTH)
|
||||||
private byte[] passwordSalt;
|
private byte[] passwordSalt;
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@ColumnDefault("false")
|
|
||||||
private boolean banned;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package de.towerdefence.server.player;
|
package de.towerdefence.server.player;
|
||||||
|
|
||||||
import de.towerdefence.server.oas.models.AdministratablePlayer;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@ -50,18 +49,4 @@ public class PlayerService {
|
||||||
md.update(salt);
|
md.update(salt);
|
||||||
return md.digest(password);
|
return md.digest(password);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void banPlayer(Long id) {
|
|
||||||
this.playerRepository.findById(id).ifPresent(player -> {
|
|
||||||
player.setBanned(true);
|
|
||||||
this.playerRepository.saveAndFlush(player);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public void unbanPlayer(Long id) {
|
|
||||||
this.playerRepository.findById(id).ifPresent(player -> {
|
|
||||||
player.setBanned(false);
|
|
||||||
this.playerRepository.saveAndFlush(player);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,7 +45,7 @@ public class ServerApiController implements ServerApi {
|
||||||
@Override
|
@Override
|
||||||
public ResponseEntity<Void> playerRegister(PlayerRegistrationData body) {
|
public ResponseEntity<Void> playerRegister(PlayerRegistrationData body) {
|
||||||
if(playerRepository.existsByUsername(body.getUsername())){
|
if(playerRepository.existsByUsername(body.getUsername())){
|
||||||
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
|
return new ResponseEntity<>(HttpStatus.CONFLICT);
|
||||||
}
|
}
|
||||||
Player newPlayer = new Player();
|
Player newPlayer = new Player();
|
||||||
newPlayer.setUsername(body.getUsername());
|
newPlayer.setUsername(body.getUsername());
|
||||||
|
@ -61,10 +61,6 @@ public class ServerApiController implements ServerApi {
|
||||||
@Override
|
@Override
|
||||||
public ResponseEntity<PlayerLoginSession> playerLogin(PlayerLoginData body) {
|
public ResponseEntity<PlayerLoginSession> playerLogin(PlayerLoginData body) {
|
||||||
Player player = playerRepository.findByUsername(body.getUsername());
|
Player player = playerRepository.findByUsername(body.getUsername());
|
||||||
if(player.isBanned())
|
|
||||||
{
|
|
||||||
return new ResponseEntity<>(HttpStatus.CONFLICT);
|
|
||||||
}
|
|
||||||
if(player == null){
|
if(player == null){
|
||||||
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
|
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import de.towerdefence.server.match.exeptions.InvalidPlacementException;
|
||||||
import de.towerdefence.server.match.exeptions.NotInMatchException;
|
import de.towerdefence.server.match.exeptions.NotInMatchException;
|
||||||
import de.towerdefence.server.player.Player;
|
import de.towerdefence.server.player.Player;
|
||||||
import de.towerdefence.server.server.JsonWebsocketHandler;
|
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.money.PlayerMoneyMessage;
|
||||||
import de.towerdefence.server.server.channels.match.placing.GamePlayerMap;
|
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.InvalidPlacementMessage;
|
||||||
|
@ -37,7 +38,11 @@ public class MatchWebsocketHandler extends JsonWebsocketHandler {
|
||||||
public void afterConnectionEstablished(WebSocketSession session) {
|
public void afterConnectionEstablished(WebSocketSession session) {
|
||||||
super.afterConnectionEstablished(session);
|
super.afterConnectionEstablished(session);
|
||||||
playerSessions.put(sessionPlayers.get(session), session);
|
playerSessions.put(sessionPlayers.get(session), session);
|
||||||
matchService.playerConnected(sessionPlayers.get(session), this::onPlayerMoneyChanged);
|
matchService.playerConnected(
|
||||||
|
sessionPlayers.get(session),
|
||||||
|
this::onPlayerMoneyChanged,
|
||||||
|
this::onPlayerHitpointsChanged
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onPlayerMoneyChanged(Player player, int playerMoney) {
|
private void onPlayerMoneyChanged(Player player, int playerMoney) {
|
||||||
|
@ -48,6 +53,14 @@ public class MatchWebsocketHandler extends JsonWebsocketHandler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void onPlayerHitpointsChanged(Player player, int playerHitpoints) {
|
||||||
|
WebSocketSession session = playerSessions.get(player);
|
||||||
|
try {
|
||||||
|
new PlayerHitpointsMessage(playerHitpoints).send(session);
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void closeSession(WebSocketSession session, CloseStatus reason) {
|
protected void closeSession(WebSocketSession session, CloseStatus reason) {
|
||||||
Player player = sessionPlayers.get(session);
|
Player player = sessionPlayers.get(session);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package de.towerdefence.server.server.channels.match;
|
package de.towerdefence.server.server.channels.match.hitpoints;
|
||||||
|
|
||||||
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,19 +8,18 @@ import lombok.AllArgsConstructor;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class PlayerMoneyMessage extends JsonMessage {
|
public class PlayerHitpointsMessage extends JsonMessage {
|
||||||
|
|
||||||
private final int playerMoney;
|
|
||||||
|
|
||||||
|
private final int playerHitpoints;
|
||||||
@Override
|
@Override
|
||||||
protected String getMessageId() {
|
protected String getMessageId() {
|
||||||
return "PlayerMoney";
|
return "PlayerHitpoints";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Map<String, JsonNode> getData(JsonNodeFactory factory) {
|
protected Map<String, JsonNode> getData(JsonNodeFactory factory) {
|
||||||
return Map.of(
|
return Map.of(
|
||||||
"playerMoney", factory.numberNode(this.playerMoney)
|
"playerHitpoints", factory.numberNode(this.playerHitpoints)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -15,8 +15,6 @@ public class PlayerMapperService {
|
||||||
for (Player player : players) {
|
for (Player player : players) {
|
||||||
AdministratablePlayer apiPlayer = new AdministratablePlayer();
|
AdministratablePlayer apiPlayer = new AdministratablePlayer();
|
||||||
apiPlayer.setUsername(player.getUsername());
|
apiPlayer.setUsername(player.getUsername());
|
||||||
apiPlayer.setId(player.getId());
|
|
||||||
apiPlayer.setBanned(player.isBanned());
|
|
||||||
administratablePlayers.add(apiPlayer);
|
administratablePlayers.add(apiPlayer);
|
||||||
}
|
}
|
||||||
return administratablePlayers;
|
return administratablePlayers;
|
||||||
|
|
22
ws/ws.yml
22
ws/ws.yml
|
@ -239,6 +239,21 @@ channels:
|
||||||
required:
|
required:
|
||||||
- $id
|
- $id
|
||||||
- playerMoney
|
- 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:
|
||||||
|
@ -321,6 +336,13 @@ operations:
|
||||||
$ref: "#/channels/match"
|
$ref: "#/channels/match"
|
||||||
messages:
|
messages:
|
||||||
- $ref: "#/channels/match/messages/PlayerMoney"
|
- $ref: "#/channels/match/messages/PlayerMoney"
|
||||||
|
playerHitpoints:
|
||||||
|
title: PlayerHitpoints
|
||||||
|
action: receive
|
||||||
|
channel:
|
||||||
|
$ref: "#/channels/match"
|
||||||
|
messages:
|
||||||
|
- $ref: "#/channels/match/messages/PlayerHitpoints"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue