From 74fdca0efddb5e88dbf33cd831c2a7b7ff46a0b8 Mon Sep 17 00:00:00 2001 From: Sebastian Lenzlinger Date: Sun, 17 Apr 2022 15:06:24 +0200 Subject: [PATCH] added whisper functionality for whisper in ClientHandler and Gui and deleted old recources that aren't needed. --- .../client/gui/chat/ChatController.java | 9 +- .../multiplayer/server/ClientHandler.java | 139 +++++++++++------- .../server/JServerProtocolParser.java | 5 +- .../cs108/resources/splitPaneChatView.fxml | 66 --------- 4 files changed, 97 insertions(+), 122 deletions(-) delete mode 100644 src/main/java/ch/unibas/dmi/dbis/cs108/resources/splitPaneChatView.fxml diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/chat/ChatController.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/chat/ChatController.java index eacfbc3..316dd91 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/chat/ChatController.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/chat/ChatController.java @@ -54,13 +54,14 @@ public class ChatController implements Initializable { public ChatController() { //TODO: why does this get called super(); whisperTargetChosen = new SimpleBooleanProperty(); - cmd = "CHATA$"; + cmd = ""; + LOGGER.info("ChatController empty constructor used"); } public ChatController(ClientModel client) { this.client = client; whisperTargetChosen = new SimpleBooleanProperty(); - cmd = "CHATA"; - + cmd = ""; + LOGGER.info("ChatController single parameter constructor used"); } @@ -138,7 +139,7 @@ public class ChatController implements Initializable { Boolean newValue) { //is true if {@code whisperTargetSelectedField} has content if (!newValue) { - cmd = whisper + "$"; + cmd = whisper + "$" + whisperTargetSelectField.getText() + "$"; } else { cmd = chatToLobby + "$"; } 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 5ba0654..3c11376 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 @@ -117,10 +117,10 @@ public class ClientHandler implements Runnable { } - /** * Lets the client change their username, if the username is already taken, a similar option is * chosen. + * * @param newName The desired new name to replace the old one with. */ public void changeUsername(String newName) { @@ -128,7 +128,7 @@ public class ClientHandler implements Runnable { this.clientUserName = nameDuplicateChecker.checkName(newName); broadcastAnnouncementToAll(helper + " has changed their nickname to " + clientUserName); try { - getLobby().getGame().getGameState().changeUsername(helper,newName); + getLobby().getGame().getGameState().changeUsername(helper, newName); } catch (NullPointerException e) { } } @@ -152,8 +152,8 @@ public class ClientHandler implements Runnable { } /** - * Returns the Lobby this ClientHandler is in. If this ClientHandler is not in a Lobby, - * it returns null. + * Returns the Lobby this ClientHandler is in. If this ClientHandler is not in a Lobby, it returns + * null. */ public Lobby getLobby() { try { @@ -164,8 +164,9 @@ public class ClientHandler implements Runnable { } /** - * Broadcasts a chat Message to all clients in the same lobby in the form "Username: @msg" - * If this client isn't in a lobby, it instead sends the message to everyone not in a lobby + * Broadcasts a chat Message to all clients in the same lobby in the form "Username: @msg" If this + * client isn't in a lobby, it instead sends the message to everyone not in a lobby + * * @param msg the Message to be broadcast */ public void broadcastChatMessageToLobby(String msg) { @@ -176,7 +177,7 @@ public class ClientHandler implements Runnable { } } else { //send msg to all clients who are not in a lobby. - for (ClientHandler client: connectedClients) { + for (ClientHandler client : connectedClients) { if (Lobby.clientIsInLobby(client) == -1) { client.sendMsgToClient(Protocol.printToClientChat + "$" + clientUserName + ": " + msg); } @@ -199,8 +200,9 @@ public class ClientHandler implements Runnable { } /** - * Broadcasts a chat Message to all clients across all lobbies & clients who are not in a lobby - * in the form "Username: @msg" + * Broadcasts a chat Message to all clients across all lobbies & clients who are not in a lobby in + * the form "Username: @msg" + * * @param msg the Message to be broadcast */ public void broadcastChatMessageToAll(String msg) { @@ -212,9 +214,11 @@ public class ClientHandler implements Runnable { /** * Broadcasts a non-chat Message to all active clients. This can be used for server messages / * announcements rather than chat messages. The message will be printed to the user exactly as it - * is given to this method. Unlike eg. broadcastChatMessageToLobby, it will also be printed onto the server - * console. - * @param msg the Message to be broadcast. Does not have to be protocol-formatted, this method will take care of that. + * is given to this method. Unlike eg. broadcastChatMessageToLobby, it will also be printed onto + * the server console. + * + * @param msg the Message to be broadcast. Does not have to be protocol-formatted, this method + * will take care of that. */ public static void broadcastAnnouncementToAll(String msg) { System.out.println(msg); @@ -224,12 +228,13 @@ public class ClientHandler implements Runnable { } /** - * Broadcasts a non-chat Message to all clients in the same lobby. This can be used for server messages / - * announcements rather than chat messages. The message will be printed to the user exactly as it - * is given to this method. The announcement will not be printed on the server console. - * If this clienthandler is not in a lobby, it will instead broadcast to all clients. + * Broadcasts a non-chat Message to all clients in the same lobby. This can be used for server + * messages / announcements rather than chat messages. The message will be printed to the user + * exactly as it is given to this method. The announcement will not be printed on the server + * console. If this clienthandler is not in a lobby, it will instead broadcast to all clients. * - * @param msg the Message to be broadcast. Does not have to be protocol-formatted, this method will take care of that. + * @param msg the Message to be broadcast. Does not have to be protocol-formatted, this method + * will take care of that. */ public void broadcastAnnouncementToLobby(String msg) { Lobby l = getLobby(); @@ -246,13 +251,17 @@ public class ClientHandler implements Runnable { } /** - * Sends a message only to the specified user, as well as sending a confirmation to the user who sent the message - * that it has been sent. Syntax: + * Sends a message only to the specified user, as well as sending a confirmation to the user who + * sent the message that it has been sent. Syntax: + * * @param target MUST NOT BE NULL! */ public void whisper(String msg, ClientHandler target) { - target.sendMsgToClient(Protocol.printToClientChat + "$" + this.getClientUserName() + " whispers: " + msg); - sendMsgToClient(Protocol.printToClientChat + "$You whispered to " + target.getClientUserName() + ": " + msg); + target.sendMsgToClient( + Protocol.printToClientChat + "$" + this.getClientUserName() + " whispers: " + msg); + sendMsgToClient( + Protocol.printToClientChat + "$You whispered to " + target.getClientUserName() + ": " + + msg); } /** @@ -275,34 +284,56 @@ public class ClientHandler implements Runnable { } /** - * Takes a msg of the form position$vote and extracts vote and position from it and saves it - * in VoteHandler.getClientVoteData - * @param msg the messaged to decode + * Decode a whisper mesage + * + * @param msg to decode. the command has been removed from the front + * @return a String[] containing the target at index 0 and the message at index 1. */ - public void decodeVote(String msg){ + public String[] decodeWhisper(String msg) { + int msgIndex = msg.indexOf('$'); + String target = ""; + String chatMsg = ""; + LOGGER.debug("incoming whisper to decode is:" + msg); + try { + target = msg.substring(0, msgIndex); + chatMsg = msg.substring(msgIndex + 1); + LOGGER.debug("whisper target is: " + target); + LOGGER.debug("whisper chatMsg is: " + chatMsg); + } catch (Exception e) { + LOGGER.warn("decode whisper issue: " + e.getMessage()); + } + return new String[]{target, chatMsg}; + } + + /** + * Takes a msg of the form position$vote and extracts vote and position from it and saves it in + * VoteHandler.getClientVoteData + * + * @param msg the messaged to decode + */ + public void decodeVote(String msg) { int msgIndex = msg.indexOf('$'); int vote = Integer.MAX_VALUE; int position = 0; LOGGER.debug("Message is " + msg); try { - position = Integer.parseInt(msg.substring(0,msgIndex)); + position = Integer.parseInt(msg.substring(0, msgIndex)); vote = Integer.parseInt(msg.substring(msgIndex + 1)); LOGGER.debug("Vote is:" + vote); } catch (Exception e) { LOGGER.warn("Invalid vote " + e.getMessage()); } LOGGER.debug("Vote is:" + vote); - if(vote != Integer.MAX_VALUE) { //gets MAX_VALUE when the vote wasn't valid - getLobby().getGame().getGameState().getClientVoteData().setVote(position,vote); + if (vote != Integer.MAX_VALUE) { //gets MAX_VALUE when the vote wasn't valid + getLobby().getGame().getGameState().getClientVoteData().setVote(position, vote); LOGGER.debug("Player vote: " + vote); - getLobby().getGame().getGameState().getClientVoteData().setHasVoted(position,true); + getLobby().getGame().getGameState().getClientVoteData().setHasVoted(position, true); } } /** - * Initializes a new Game instance and starts its run method in a new thread. - * Puts the game in the corresponding lobby. Only the admin of this lobby can start a new - * game. + * Initializes a new Game instance and starts its run method in a new thread. Puts the game in the + * corresponding lobby. Only the admin of this lobby can start a new game. */ public void startNewGame() { try { @@ -328,9 +359,9 @@ public class ClientHandler implements Runnable { } /** - * Sends an announcement to just this client. Essentially the same as broadcastAnnouncementToAll except - * it only sends an announcement to just this client instead of everyone. - * Can be used for private non-chat messages (e.g. "You are now a ghost"). + * Sends an announcement to just this client. Essentially the same as broadcastAnnouncementToAll + * except it only sends an announcement to just this client instead of everyone. Can be used for + * private non-chat messages (e.g. "You are now a ghost"). */ public void sendAnnouncementToClient(String msg) { sendMsgToClient(Protocol.printToClientConsole + "$" + msg); @@ -347,7 +378,8 @@ public class ClientHandler implements Runnable { connectedClients.remove(this); disconnectClient(); leaveLobby(); - broadcastAnnouncementToAll(getClientUserName() + " has left the server due to a connection loss."); + broadcastAnnouncementToAll( + getClientUserName() + " has left the server due to a connection loss."); disconnectedClients.add(this); } @@ -377,7 +409,9 @@ public class ClientHandler implements Runnable { } /** - * The client wants to join the lobby with the index i. If the lobby is closed, the client will be notified. + * The client wants to join the lobby with the index i. If the lobby is closed, the client will be + * notified. + * * @param i the number of the lobby to join */ public void joinLobby(int i) { @@ -386,7 +420,8 @@ public class ClientHandler implements Runnable { if (l.getLobbyIsOpen()) { l.addPlayer(this); } else { - sendAnnouncementToClient("The game in Lobby " + l.getLobbyID() + " has already started, or the lobby is already full."); + sendAnnouncementToClient("The game in Lobby " + l.getLobbyID() + + " has already started, or the lobby is already full."); } } else { sendAnnouncementToClient("Invalid Lobby nr."); @@ -403,7 +438,7 @@ public class ClientHandler implements Runnable { if (l != null) { l.removePlayer(this); Game game = l.getGame(); - if(game != null) { + if (game != null) { l.getGame().getGameState().handleClientDisconnect(this); } } @@ -411,8 +446,8 @@ public class ClientHandler implements Runnable { /** - * Lists all lobbies and their members, along with players outside lobbies - * to this clientHandler's client as an announcement. + * Lists all lobbies and their members, along with players outside lobbies to this clientHandler's + * client as an announcement. */ public void listLobbies() { if (Lobby.lobbies.isEmpty()) { @@ -420,7 +455,7 @@ public class ClientHandler implements Runnable { } else { for (Lobby l : Lobby.lobbies) { String lobbyStatus = "closed"; - if(l.getLobbyIsOpen()) { + if (l.getLobbyIsOpen()) { lobbyStatus = "open"; } sendAnnouncementToClient("Lobby nr. " + l.getLobbyID() + ": (" + lobbyStatus + ")"); @@ -434,7 +469,7 @@ public class ClientHandler implements Runnable { } } boolean helper = false; //used to print "Clients not in lobbies" only once, if needed. - for (ClientHandler c: connectedClients) { + for (ClientHandler c : connectedClients) { if (Lobby.clientIsInLobby(c) == -1) { if (!helper) { helper = true; @@ -449,8 +484,8 @@ public class ClientHandler implements Runnable { } /** - * Lists all players in the client's lobby. If the client is not in a Lobby, it will say - * "You are not in a lobby." + * Lists all players in the client's lobby. If the client is not in a Lobby, it will say "You are + * not in a lobby." */ public void listPlayersInLobby() { Lobby l = getLobby(); @@ -469,7 +504,8 @@ public class ClientHandler implements Runnable { } /** - * Lists all Games currenty running and already finished and displays it to the client handled by this + * Lists all Games currenty running and already finished and displays it to the client handled by + * this */ public void listGames() { if (Lobby.runningGames.isEmpty() && Lobby.finishedGames.isEmpty()) { @@ -478,7 +514,8 @@ public class ClientHandler implements Runnable { sendAnnouncementToClient("Running Games:"); try { for (Game runningGame : Lobby.runningGames) { - sendAnnouncementToClient(" - " + runningGame.getName() + ", Lobby" + runningGame.getLobby().getLobbyID()); + sendAnnouncementToClient( + " - " + runningGame.getName() + ", Lobby" + runningGame.getLobby().getLobbyID()); } } catch (Exception e) { sendAnnouncementToClient(" - No running Games"); @@ -486,7 +523,8 @@ public class ClientHandler implements Runnable { sendAnnouncementToClient("Finished Games:"); try { for (Game finishedGame : Lobby.finishedGames) { - sendAnnouncementToClient(" - " + finishedGame.getName() + ", Lobby" + finishedGame.getLobby().getLobbyID()); + sendAnnouncementToClient( + " - " + finishedGame.getName() + ", Lobby" + finishedGame.getLobby().getLobbyID()); } } catch (Exception e) { sendAnnouncementToClient(" - No finished Games"); @@ -501,8 +539,9 @@ public class ClientHandler implements Runnable { Lobby l = getLobby(); if (l != null) { Game g = l.getGame(); - if(g != null) - l.getGame().getGameState().handleClientDisconnect(this); + if (g != null) { + l.getGame().getGameState().handleClientDisconnect(this); + } } socket = this.getSocket(); in = this.getIn(); diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/JServerProtocolParser.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/JServerProtocolParser.java index 3664a89..eb5006a 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/JServerProtocolParser.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/JServerProtocolParser.java @@ -47,8 +47,9 @@ public class JServerProtocolParser { //find ClientHandler try { ClientHandler target = null; - String targetName = msg.substring(6, msg.indexOf("$", 6)); - String chatMsg = msg.substring(msg.indexOf("$", 6)+1); + String[] targetAndMsg = h.decodeWhisper(msg.substring(6)); + String targetName = targetAndMsg[0]; + String chatMsg = targetAndMsg[1]; System.out.println(targetName); System.out.println(chatMsg); for (ClientHandler c : ClientHandler.getConnectedClients()) { diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/resources/splitPaneChatView.fxml b/src/main/java/ch/unibas/dmi/dbis/cs108/resources/splitPaneChatView.fxml deleted file mode 100644 index 1ae8b3e..0000000 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/resources/splitPaneChatView.fxml +++ /dev/null @@ -1,66 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -