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.

This commit is contained in:
Sebastian Lenzlinger 2022-04-08 18:38:59 +02:00
parent b55494c106
commit d8692152b4
3 changed files with 29 additions and 7 deletions

View File

@ -225,7 +225,8 @@ public class ClientHandler implements Runnable {
LOGGER.debug( LOGGER.debug(
this.getClientUserName() + " created a new lobby with ID: " + newGame.getLobbyID()); this.getClientUserName() + " created a new lobby with ID: " + newGame.getLobbyID());
//TODO add server response. Here a possibility: //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(); in = this.getIn();
out = this.getOut(); out = this.getOut();
serverData.removeClientFromSetOfAllClients(this); serverData.removeClientFromSetOfAllClients(this);
serverData.removeClientFromLobby(this);
try { try {
Thread.sleep(100); Thread.sleep(100);
if (in != null) { if (in != null) {

View File

@ -64,6 +64,14 @@ public class CentralServerData {
LOGGER.debug(client.getClientUserName() + " removed from CentralServerData list of clients."); 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. * Getter for List of all lobbies.
* *

View File

@ -72,14 +72,25 @@ public class Lobby {
/** /**
* getter for the lobby ID * getter for the lobby ID
*
* @return lobbyID as set in constructor based on number of lobbies. * @return lobbyID as set in constructor based on number of lobbies.
*/ */
public int getLobbyID() { public int getLobbyID() {
return this.lobbyID; return this.lobbyID;
} }
/**
* Returns the list containing players currently in the lobby
*
* @return list of players
*/
public List<ClientHandler> getPlayers() {
return this.players;
}
/** /**
* Builds a message for the LISTL command. * Builds a message for the LISTL command.
*
* @return a string formatted for the clients convenients. * @return a string formatted for the clients convenients.
*/ */
public String getIdAndAdminForList() { public String getIdAndAdminForList() {
@ -95,8 +106,9 @@ public class Lobby {
/** /**
* Adds a player to the lobby. * Adds a player to the lobby.
* TODO: ad an appropriate response. Currently hardcoded. * TODO: add an appropriate response. Currently hardcoded.
* TODO: Does this method need to implemented somewhere else, e.g. in the ClientHandler? * TODO: Does this method need to be implemented somewhere else, e.g. in the ClientHandler?
*
* @param player who wants to join the lobby. * @param player who wants to join the lobby.
*/ */
public synchronized void addPlayer(ClientHandler player) { public synchronized void addPlayer(ClientHandler player) {
@ -107,7 +119,7 @@ public class Lobby {
+ ". Current number of players in this lobby: " + players.size()); + ". Current number of players in this lobby: " + players.size());
} else { } else {
LOGGER.debug( 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); + numberOfPlayersInLobby);
//TODO: does this have to be formatted in any way to conform to protocol? //TODO: does this have to be formatted in any way to conform to protocol?
player.sendMsgToClient(Protocol.printToClientConsole + player.sendMsgToClient(Protocol.printToClientConsole +