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(
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) {

View File

@ -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.
*

View File

@ -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<ClientHandler> 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 +