Client Login into Lobby as well as creating a lobby now works and client can only be in one lobby at the same time.

This commit is contained in:
Jonas 2022-04-11 18:08:39 +02:00
parent 42e2720016
commit 33cbb4e57d
6 changed files with 78 additions and 53 deletions

View File

@ -39,6 +39,10 @@ public class JClientProtocolParser {
case Protocol.printToClientConsole: case Protocol.printToClientConsole:
System.out.println(msg.substring(6)); System.out.println(msg.substring(6));
break; break;
case Protocol.printToClientChat:
//todo: handle chat separately from console.
System.out.println(msg.substring(6));
break;
case Protocol.serverConfirmQuit: case Protocol.serverConfirmQuit:
c.disconnectFromServer(); c.disconnectFromServer();
break; break;

View File

@ -50,7 +50,7 @@ public class MessageFormatter {
break; break;
case "/g": case "/g":
//CRTGM command //CRTGM command
stringBuilder.append(Protocol.createNewGame + "$"); stringBuilder.append(Protocol.createNewLobby + "$");
s = ""; //command has no parameters s = ""; //command has no parameters
//TODO add LOGGER msg. Find out if .info or .debug. //TODO add LOGGER msg. Find out if .info or .debug.
break; break;

View File

@ -75,14 +75,12 @@ public class Protocol {
public static final String clientQuitRequest = "QUITR"; public static final String clientQuitRequest = "QUITR";
/** /**
* TODO: enable for client * Client sends this message when they want to create a new game.
* TODO: add sever response
* Client sends this message when he wants to create a new game.
* Client issues this command in {@link ch.unibas.dmi.dbis.cs108.multiplayer.client.MessageFormatter} * Client issues this command in {@link ch.unibas.dmi.dbis.cs108.multiplayer.client.MessageFormatter}
* using "/g". * using "/g".
* First a lobby {@link ch.unibas.dmi.dbis.cs108.sebaschi.Lobby} is created of which the requesting client is the admin of. * First a lobby {@link ch.unibas.dmi.dbis.cs108.sebaschi.Lobby} is created of which the requesting client is the admin of.
*/ */
public static final String createNewGame = "CRTGM"; public static final String createNewLobby = "CRTLB";
/** /**
* TODO: implement in {@link ch.unibas.dmi.dbis.cs108.multiplayer.client.MessageFormatter} * TODO: implement in {@link ch.unibas.dmi.dbis.cs108.multiplayer.client.MessageFormatter}
@ -111,11 +109,19 @@ public class Protocol {
public static final String pingFromServer = "SPING"; public static final String pingFromServer = "SPING";
/** /**
* prints out incoming chat messages / announcements into the user's console. any string that * prints out incoming announcements into the user's console. any string that
* follows CONSM$ is printed as is, so the message that follows already has to be formatted the
* way it should be shown to the client.
*/
public static final String printToClientConsole = "CONSM";
/**
* prints out incoming chat messages into the user's chat. any string that
* follows CHATM$ is printed as is, so the message that follows already has to be formatted the * follows CHATM$ is printed as is, so the message that follows already has to be formatted the
* way it should be shown to the client. * way it should be shown to the client.
*/ */
public static final String printToClientConsole = "CHATM"; public static final String printToClientChat = "CHATM";
/** /**
* Server confirms the client's quit request, meaning that the client can now close its * Server confirms the client's quit request, meaning that the client can now close its
@ -137,7 +143,7 @@ public class Protocol {
/** /**
* todo: doc * todo: doc
*/ */
public static final String serverDeliversLobbyList = "LLIST"; public static final String serverDeliversLobbyList = "LLIST"; //todo: do we need this?

View File

@ -152,7 +152,7 @@ public class ClientHandler implements Runnable {
*/ */
public void broadcastChatMessage(String msg) { public void broadcastChatMessage(String msg) {
for (ClientHandler client : connectedClients) { for (ClientHandler client : connectedClients) {
client.sendMsgToClient(Protocol.printToClientConsole + "$" + clientUserName + ": " + msg); client.sendMsgToClient(Protocol.printToClientChat + "$" + clientUserName + ": " + msg);
} }
} }
@ -217,17 +217,35 @@ public class ClientHandler implements Runnable {
} }
/** /**
* Invoked by CRTGM. Creates a new lobby with the ClientHandler as admin and adds the lobby to the * Invoked by Protocol.createNewLobby. Creates a new lobby with the ClientHandler as admin and
* server data. * adds the lobby to the server data.
*/ */
public void createNewLobby() { public void createNewLobby() {
if (Lobby.clientIsInLobby(this) == -1) {
Lobby newGame = new Lobby(this); Lobby newGame = new Lobby(this);
serverData.addLobbyToListOfAllLobbies(newGame); serverData.addLobbyToListOfAllLobbies(newGame);
LOGGER.debug( broadcastAnnouncement("New lobby with ID: " + newGame.getLobbyID()
this.getClientUserName() + " created a new lobby with ID: " + newGame.getLobbyID()); + " created by " + this.getClientUserName());
//TODO add server response. Here a possibility: } else {
sendMsgToClient(Protocol.printToClientConsole + "$New lobby with ID: " + newGame.getLobbyID() sendMsgToClient(Protocol.printToClientConsole +
+ " created."); "$You are already in lobby nr. " + Lobby.clientIsInLobby(this));
}
}
/**
* The client wants to join the lobby with the index i.
* //todo: needs more doc.
* @param i
*/
public void joinLobby(int i) {
Lobby l = Lobby.getLobbyFromID(i);
if (l != null) {
l.addPlayer(this);
} else {
LOGGER.debug(getClientUserName() + " tried to join Lobby nr. "
+ i + " but that doesn't exist.");
}
} }
/** /**
@ -260,6 +278,7 @@ public class ClientHandler implements Runnable {
} }
/** /**
* Closes the client's socket, in, and out. and removes from global list of clients. * Closes the client's socket, in, and out. and removes from global list of clients.
*/ */

View File

@ -59,22 +59,16 @@ public class JServerProtocolParser {
h.removeClientOnLogout(); h.removeClientOnLogout();
break; break;
case Protocol.joinLobby: case Protocol.joinLobby:
//todo: have this be a method of clientHandler. try {
int i = Integer.parseInt(msg.substring(6, 7)); int i = Integer.parseInt(msg.substring(6, 7));
Lobby l = Lobby.getLobbyFromID(i); h.joinLobby(i);
if (l != null) { } catch (Exception e) {
l.addPlayer(h); h.sendMsgToClient(Protocol.printToClientConsole
h.broadcastAnnouncement(h.getClientUserName() + " joined Lobby nr. " + l.getLobbyID()); + "$Invalid input. Please use JOINL$1 to join Lobby 1, for example.");
} else {
LOGGER.debug(h.getClientUserName() + " tried to join Lobby nr. "
+ i + " but that doesn't exist.");
} }
break; break;
case Protocol.createNewGame: case Protocol.createNewLobby:
h.createNewLobby(); h.createNewLobby();
LOGGER.debug(Protocol.createNewGame
+ " command reached in JServerProtocolParser. Command issued by: "
+ h.getClientUserName());
break; break;
case Protocol.listLobbies: case Protocol.listLobbies:
h.listAllLobbiesToClient(); h.listAllLobbiesToClient();

View File

@ -35,9 +35,6 @@ public class Lobby {
*/ */
private HashSet<ClientHandler> lobbyClients = new HashSet<>(6); private HashSet<ClientHandler> lobbyClients = new HashSet<>(6);
private int numberOfPlayersInLobby;
private final int lobbyID; private final int lobbyID;
@ -50,7 +47,6 @@ public class Lobby {
public Lobby(ClientHandler admin) { public Lobby(ClientHandler admin) {
this.admin = admin; this.admin = admin;
this.lobbyClients.add(admin); this.lobbyClients.add(admin);
this.numberOfPlayersInLobby = 1;
lobbies.add(this); lobbies.add(this);
this.lobbyID = lobbies.size(); this.lobbyID = lobbies.size();
admin.broadcastAnnouncement("New Lobby created by " + admin.getClientUserName() + admin.broadcastAnnouncement("New Lobby created by " + admin.getClientUserName() +
@ -114,38 +110,43 @@ public class Lobby {
return null; return null;
} }
public static boolean clientIsInALobby(ClientHandler h) { /**
* Returns the ID of the lobby that the client is in. If the client is not in any
* lobby, it returns -1.
*/
public static int clientIsInLobby(ClientHandler h) {
for (Lobby l: lobbies) { for (Lobby l: lobbies) {
for (ClientHandler clientHandler: l.getLobbyClients()) { for (ClientHandler clientHandler: l.getLobbyClients()) {
if (h.equals(clientHandler)) { if (h.equals(clientHandler)) {
return true; return l.getLobbyID();
} }
} }
} }
return false; return -1;
} }
/** /**
* Adds a player to the lobby. * Adds a player to the lobby. Returns true if successful.
* TODO: add an appropriate response. Currently hardcoded. * TODO: add an appropriate response. Currently hardcoded.
* TODO: Does this method need to be implemented somewhere else, e.g. in the ClientHandler? * @param client who wants to join the lobby.
* //todo: Client can only join one Lobby.
* @param player who wants to join the lobby.
*/ */
public synchronized void addPlayer(ClientHandler player) { public synchronized boolean addPlayer(ClientHandler client) {
if (lobbyClients.size() < MAX_NO_OF_CLIENTS) { if (lobbyClients.size() < MAX_NO_OF_CLIENTS) {
lobbyClients.add(player); if (clientIsInLobby(client) == -1) {
numberOfPlayersInLobby++; lobbyClients.add(client);
LOGGER.debug(player.getClientUserName() + " has been added to Lobby with ID: " + lobbyID client.broadcastAnnouncement(client.getClientUserName() + " has joined lobby " + this.getLobbyID());
LOGGER.debug(client.getClientUserName() + " has been added to Lobby with ID: " + lobbyID
+ ". Current number of lobbyClients in this lobby: " + lobbyClients.size()); + ". Current number of lobbyClients in this lobby: " + lobbyClients.size());
return true;
} else { } else {
LOGGER.debug( client.sendMsgToClient(Protocol.printToClientConsole + "$You are already in lobby nr. "
player.getClientUserName() + " could not be added to lobby. Number of lobbyClients in lobby: " + clientIsInLobby(client));
+ numberOfPlayersInLobby);
//TODO: does this have to be formatted in any way to conform to protocol?
player.sendMsgToClient(Protocol.printToClientConsole +
"$The lobby is full. Please try joining a different lobby or create a new game");
} }
} else {
client.sendMsgToClient(Protocol.printToClientConsole +
"$This lobby is full. Please try joining a different lobby or create a new lobby");
}
return false;
} }
/** /**
@ -160,6 +161,7 @@ public class Lobby {
return this.getLobbyClients().remove(player); return this.getLobbyClients().remove(player);
} }
//todo: this here?
public void broadcastToLounge(String msg) { public void broadcastToLounge(String msg) {
for (ClientHandler lounger : this.getLobbyClients()) { for (ClientHandler lounger : this.getLobbyClients()) {