From d8692152b483786228f318bdc814980a69ac887c Mon Sep 17 00:00:00 2001 From: Sebastian Lenzlinger Date: Fri, 8 Apr 2022 18:38:59 +0200 Subject: [PATCH] If a client connection is closed the ClientHandleris now removed from the set of all clients on the server and from any lobby he was in. --- .../multiplayer/server/ClientHandler.java | 4 +++- .../cs108/sebaschi/CentralServerData.java | 8 +++++++ .../unibas/dmi/dbis/cs108/sebaschi/Lobby.java | 24 ++++++++++++++----- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/ClientHandler.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/ClientHandler.java index 5fc6669..20aa7bc 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/ClientHandler.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/ClientHandler.java @@ -225,7 +225,8 @@ public class ClientHandler implements Runnable { LOGGER.debug( this.getClientUserName() + " created a new lobby with ID: " + newGame.getLobbyID()); //TODO add server response. Here a possibility: - sendMsgToClient(Protocol.printToClientConsole + "$New lobby with ID: " + newGame.getLobbyID() + " created."); + sendMsgToClient(Protocol.printToClientConsole + "$New lobby with ID: " + newGame.getLobbyID() + + " created."); } /** @@ -258,6 +259,7 @@ public class ClientHandler implements Runnable { in = this.getIn(); out = this.getOut(); serverData.removeClientFromSetOfAllClients(this); + serverData.removeClientFromLobby(this); try { Thread.sleep(100); if (in != null) { diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/CentralServerData.java b/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/CentralServerData.java index c6d4bbe..2897713 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/CentralServerData.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/CentralServerData.java @@ -64,6 +64,14 @@ public class CentralServerData { LOGGER.debug(client.getClientUserName() + " removed from CentralServerData list of clients."); } + public synchronized void removeClientFromLobby(ClientHandler client) { + boolean foundAndRemoved = false; + for (Lobby l : allLobbies) { + foundAndRemoved = l.getPlayers().remove(client); + } + LOGGER.debug("foundAndRemoved value: " + foundAndRemoved); + } + /** * Getter for List of all lobbies. * diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/Lobby.java b/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/Lobby.java index 4f83481..4bcc61e 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/Lobby.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/Lobby.java @@ -72,17 +72,28 @@ public class Lobby { /** * getter for the lobby ID + * * @return lobbyID as set in constructor based on number of lobbies. */ - public int getLobbyID(){ + public int getLobbyID() { return this.lobbyID; } + /** + * Returns the list containing players currently in the lobby + * + * @return list of players + */ + public List getPlayers() { + return this.players; + } + /** * Builds a message for the LISTL command. + * * @return a string formatted for the clients convenients. */ - public String getIdAndAdminForList(){ + public String getIdAndAdminForList() { StringBuilder response = new StringBuilder(); response.append("Lobby ID: "); response.append(this.lobbyID); @@ -95,19 +106,20 @@ public class Lobby { /** * Adds a player to the lobby. - * TODO: ad an appropriate response. Currently hardcoded. - * TODO: Does this method need to implemented somewhere else, e.g. in the ClientHandler? + * TODO: add an appropriate response. Currently hardcoded. + * TODO: Does this method need to be implemented somewhere else, e.g. in the ClientHandler? + * * @param player who wants to join the lobby. */ public synchronized void addPlayer(ClientHandler player) { - if (players.size() <= MAX_NO_OF_CLIENTS) { + if (players.size() <= MAX_NO_OF_CLIENTS) { players.add(player); numberOfPlayersInLobby++; LOGGER.debug(player.getClientUserName() + " has been added to Lobby with ID: " + lobbyID + ". Current number of players in this lobby: " + players.size()); } else { LOGGER.debug( - player.getClientUserName() + " could not be added to lobby. No. of players in lobby: " + player.getClientUserName() + " could not be added to lobby. Number of players in lobby: " + numberOfPlayersInLobby); //TODO: does this have to be formatted in any way to conform to protocol? player.sendMsgToClient(Protocol.printToClientConsole +