From 951b4c198ca7d74d8ca7e4b5a41a1354dc137484 Mon Sep 17 00:00:00 2001 From: Johannes Schmelz Date: Mon, 18 Nov 2024 00:24:15 +0100 Subject: [PATCH] Server bug fix. Add player did not correspond with the params --- .../monopoly/game/server/ServerGameLogic.java | 29 +++++++++++++++++++ .../pp/monopoly/server/MonopolyServer.java | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/ServerGameLogic.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/ServerGameLogic.java index cfcd7c3..80de4b4 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/ServerGameLogic.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/ServerGameLogic.java @@ -95,6 +95,31 @@ public class ServerGameLogic implements ClientInterpreter { return player; } + /** + * Adds a new player to the game if the game is in the LOBBY state and the maximum + * player limit has not been reached. + * + * @param id the id of the player to add to the game + * @return the added Player, or null if the player could not be added + */ + public Player addPlayer(int id) { + Player player = new Player(id, playerHandler); + if (state != ServerState.LOBBY) { + LOGGER.log(Level.WARNING, "Cannot add player; game is not in LOBBY state."); + return null; + } + + if (playerHandler.getPlayerCount() >= MAX_PLAYERS) { + LOGGER.log(Level.WARNING, "Cannot add player; maximum player limit reached."); + return null; + } + + playerHandler.addPlayer(player); + LOGGER.log(Level.DEBUG, "Player added: {0}", player.getId()); + + return player; + } + /** * Handles a BuyPropertyRequest from a player, allowing the player to purchase a property * if it is unowned and they have sufficient funds. @@ -223,4 +248,8 @@ public class ServerGameLogic implements ClientInterpreter { public BoardManager getBoardManager() { return boardManager; } + + public Player getPlayerById(int id) { + return playerHandler.getPlayerById(id); + } } diff --git a/Projekte/monopoly/server/src/main/java/pp/monopoly/server/MonopolyServer.java b/Projekte/monopoly/server/src/main/java/pp/monopoly/server/MonopolyServer.java index ff77c8a..73aaa17 100644 --- a/Projekte/monopoly/server/src/main/java/pp/monopoly/server/MonopolyServer.java +++ b/Projekte/monopoly/server/src/main/java/pp/monopoly/server/MonopolyServer.java @@ -120,7 +120,7 @@ public class MonopolyServer implements MessageListener, Connec @Override public void connectionAdded(Server server, HostedConnection hostedConnection) { LOGGER.log(Level.INFO, "new connection {0}", hostedConnection); //NON-NLS - logic.addPlayer(new Player(hostedConnection.getId())); + logic.addPlayer(hostedConnection.getId()); } @Override