From 460f4fb9ea79ec82a110511b46490fe38911e007 Mon Sep 17 00:00:00 2001 From: Seraina Date: Sat, 30 Apr 2022 14:36:47 +0200 Subject: [PATCH] Added two methods to ClientHandler that send messages to multiple lobbies, this is needed for the gui interaction with multiple clients --- .../multiplayer/helpers/GuiParameters.java | 4 +-- .../multiplayer/server/ClientHandler.java | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/GuiParameters.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/GuiParameters.java index 0b50caf..3d8ff18 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/GuiParameters.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/GuiParameters.java @@ -1,7 +1,7 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.helpers; /** - * This class contains all parameters for the PTGUI protocol message + * This class contains parameters for the PTGUI protocol message */ public class GuiParameters { @@ -15,7 +15,7 @@ public class GuiParameters { public static final String listOfLobbies = "LOBBIES"; /** - * Tells Gui, that what follows is a list of players (per Lobby?) + * Tells Gui, that what follows is a list of players per Lobby */ public static final String listOfPLayers = "PLAYERS"; 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 352650e..27a47ed 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 @@ -305,6 +305,35 @@ public class ClientHandler implements Runnable { } } + /** + * Sends a given message to all connected client. The message has to already be protocol-formatted. + * + * @param msg the given message. Should already be protocol-formatted. + */ + public void sendMsgToAllClients(String msg) { + for (ClientHandler client : connectedClients) { + client.sendMsgToClient(msg); + } + } + + /** + * Sends a Message to all clients in the same lobby. The message has to already be protocol-formatted. + * @param msg the given message. Should already be protocol-formatted. + */ + public void sendMsgToClientsInLobby(String msg) { + Lobby l = getLobby(); + if (l != null) { + //System.out.println(msg); we can-comment this if you want lobby-announcements to print on the server console as well. + for (ClientHandler client : l.getLobbyClients()) { + client.sendMsgToClient(msg); + } + } else { + LOGGER.debug("Could not send announcements; probably client isn't in a lobby." + + "Will send across all lobbies now."); + sendMsgToAllClients(msg); + } + } + /** * Decode a whisper message *