Server bug fix. Add player did not correspond with the params

This commit is contained in:
Johannes Schmelz 2024-11-18 00:24:15 +01:00
parent 11faeeaf45
commit 951b4c198c
2 changed files with 30 additions and 1 deletions

View File

@ -95,6 +95,31 @@ public class ServerGameLogic implements ClientInterpreter {
return player; 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 * Handles a BuyPropertyRequest from a player, allowing the player to purchase a property
* if it is unowned and they have sufficient funds. * if it is unowned and they have sufficient funds.
@ -223,4 +248,8 @@ public class ServerGameLogic implements ClientInterpreter {
public BoardManager getBoardManager() { public BoardManager getBoardManager() {
return boardManager; return boardManager;
} }
public Player getPlayerById(int id) {
return playerHandler.getPlayerById(id);
}
} }

View File

@ -120,7 +120,7 @@ public class MonopolyServer implements MessageListener<HostedConnection>, Connec
@Override @Override
public void connectionAdded(Server server, HostedConnection hostedConnection) { public void connectionAdded(Server server, HostedConnection hostedConnection) {
LOGGER.log(Level.INFO, "new connection {0}", hostedConnection); //NON-NLS LOGGER.log(Level.INFO, "new connection {0}", hostedConnection); //NON-NLS
logic.addPlayer(new Player(hostedConnection.getId())); logic.addPlayer(hostedConnection.getId());
} }
@Override @Override