diff --git a/OgGhostWinners.txt b/OgGhostWinners.txt new file mode 100644 index 0000000..223b783 --- /dev/null +++ b/OgGhostWinners.txt @@ -0,0 +1 @@ +B diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/BudaLogConfig.java b/src/main/java/ch/unibas/dmi/dbis/cs108/BudaLogConfig.java index 2da3348..0ee0b70 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/BudaLogConfig.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/BudaLogConfig.java @@ -19,7 +19,7 @@ public class BudaLogConfig { LoggerContext ctx = (LoggerContext) LogManager.getContext(false); Configuration config = ctx.getConfiguration(); LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME); - loggerConfig.setLevel(Level.ERROR); // change level here + loggerConfig.setLevel(Level.INFO); // change level here ctx.updateLoggers(); // This causes all Loggers to refetch information from their LoggerConfig. } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Game.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Game.java index 27f3f04..670ec53 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Game.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Game.java @@ -8,6 +8,8 @@ import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.HumanNPC; import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.HumanPlayer; import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger; import ch.unibas.dmi.dbis.cs108.highscore.OgGhostHighScore; +import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.GuiParameters; +import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol; import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler; import ch.unibas.dmi.dbis.cs108.multiplayer.server.Lobby; import java.util.HashSet; @@ -76,6 +78,29 @@ public class Game implements Runnable { return null; } + public Game getGame() { + return this; + } + + /** + * Initializes new thread that constantly sends a gameState update to all clients in this game + */ + public void gameStateModelUpdater(){ + new Thread(() -> { + while (getGame().isOngoing) { + for (Passenger passenger : getGameState().getPassengerTrain()) { + passenger.send(GuiParameters.updateGameState, getGame()); + } + try { + Thread.sleep(4000); //TODO: Is this a good intervall? + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }).start(); + + } + /** * Starts a new game, creates a passenger array and saves it in gameState, sets the OG * currently at gameState.train[3] fills the passengerTrain moving from left to rigth in the @@ -119,23 +144,27 @@ public class Game implements Runnable { i++; } LOGGER.info(gameState.toString()); + gameStateModelUpdater(); //TODO: does that work? i = 0; - while (isOngoing) {//game cycle + while (isOngoing) {//game cycle TODO: maybe check that more often inside game loop?! if (!isDay) { LOGGER.info("NIGHT"); gameOverCheck = voteHandler.ghostVote(gameState.getPassengerTrain(), this); setDay(true); + lobby.getAdmin().sendMsgToClientsInLobby(Protocol.printToGUI + "$" + ClientGameInfoHandler.itsDayTime + "$"); } else { LOGGER.info("DAY"); gameOverCheck = voteHandler.humanVote(gameState.getPassengerTrain(), this); setDay(false); + lobby.getAdmin().sendMsgToClientsInLobby(Protocol.printToGUI + "$" + ClientGameInfoHandler.itsNightTime + "$"); } if (gameOverCheck.equals(ClientGameInfoHandler.gameOverGhostsWin) || gameOverCheck.equals( ClientGameInfoHandler.gameOverHumansWin)) { if (gameOverCheck.equals(ClientGameInfoHandler.gameOverGhostsWin) && getOgGhost().getIsPlayer()) { OgGhostHighScore.addOgGhostWinner(getOgGhost().getName()); } + lobby.getAdmin().sendMsgToClientsInLobby(Protocol.printToGUI + "$" + GuiParameters.viewChangeToLobby + "$"); lobby.getAdmin().broadcastAnnouncementToLobby(gameOverCheck); lobby.removeGameFromRunningGames(this); lobby.addGameToFinishedGames(this); diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/ServerGameInfoHandler.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/ServerGameInfoHandler.java index 773672b..54eb4de 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/ServerGameInfoHandler.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/ServerGameInfoHandler.java @@ -4,6 +4,7 @@ import ch.unibas.dmi.dbis.cs108.BudaLogConfig; import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.GhostNPC; import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.HumanNPC; import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger; +import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.GuiParameters; import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -48,11 +49,10 @@ public class ServerGameInfoHandler { * won't get bored Formartiert Nachrichten die für einen Spectator gedacht sind. * * @param msg the message to be formatted - * @param passenger the passenger getting the message * @param game the game in wich the passenger lives * @return a message in a protocol format */ - public static String spectatorFormat(String msg, Passenger passenger, Game game) { + public static String spectatorFormat(String msg, Game game) { switch (msg) { case ClientGameInfoHandler.ghostVoteRequest: case ClientGameInfoHandler.itsNightTime: @@ -62,6 +62,9 @@ public class ServerGameInfoHandler { case ClientGameInfoHandler.itsDayTime: msg = Protocol.printToClientConsole + "$Humans are voting:" + game.gameState.toString(); break; + case GuiParameters.updateGameState: + msg = Protocol.printToGUI + "$" + GuiParameters.updateGameState + game.getGameState().toString(); + break; default: msg = Protocol.printToClientConsole + "$" + msg; } @@ -110,7 +113,10 @@ public class ServerGameInfoHandler { case ClientGameInfoHandler.noiseNotification + 4 + " time(s)": case ClientGameInfoHandler.noiseNotification + 5 + " time(s)": String outMsg = npc.getName() + ": " + noiseRandomizer(); + //TODO: add likelyhood game.getLobby().getAdmin().broadcastNpcChatMessageToLobby(outMsg); + game.getLobby().getAdmin().sendMsgToClientsInLobby(Protocol.printToGUI + GuiParameters.noiseHeardAtPosition + + "$" + npc.getPosition() + "$"); break; case ClientGameInfoHandler.ghostVoteRequest: npc.vote(game); @@ -133,6 +139,8 @@ public class ServerGameInfoHandler { case ClientGameInfoHandler.noiseNotification + 5 + " time(s)": String outMsg = npc.getName() + ": " + noiseRandomizer(); game.getLobby().getAdmin().broadcastNpcChatMessageToLobby(outMsg); + game.getLobby().getAdmin().sendMsgToClientsInLobby(Protocol.printToGUI + GuiParameters.noiseHeardAtPosition + + "$" + npc.getPosition() + "$"); break; case ClientGameInfoHandler.humanVoteRequest: npc.vote(game); diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/GhostPlayer.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/GhostPlayer.java index a9aaa32..ff7e783 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/GhostPlayer.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/GhostPlayer.java @@ -4,6 +4,8 @@ import ch.unibas.dmi.dbis.cs108.BudaLogConfig; import ch.unibas.dmi.dbis.cs108.gamelogic.ClientVoteData; import ch.unibas.dmi.dbis.cs108.gamelogic.Game; import ch.unibas.dmi.dbis.cs108.gamelogic.ServerGameInfoHandler; +import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.GuiParameters; +import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol; import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -41,7 +43,12 @@ public class GhostPlayer extends Ghost { */ @Override public void send(String msg, Game game) { - String formattedMsg = ServerGameInfoHandler.format(msg, this, game); + String formattedMsg; + if (msg.equals(GuiParameters.updateGameState)) { + formattedMsg = Protocol.printToGUI + "$" + GuiParameters.updateGameState + game.getGameState().toString(); + } else { + formattedMsg = ServerGameInfoHandler.format(msg, this, game); + } clientHandler.sendMsgToClient(formattedMsg); } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/HumanPlayer.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/HumanPlayer.java index f306d8d..5695d0b 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/HumanPlayer.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/HumanPlayer.java @@ -4,6 +4,8 @@ import ch.unibas.dmi.dbis.cs108.BudaLogConfig; import ch.unibas.dmi.dbis.cs108.gamelogic.ClientVoteData; import ch.unibas.dmi.dbis.cs108.gamelogic.Game; import ch.unibas.dmi.dbis.cs108.gamelogic.ServerGameInfoHandler; +import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.GuiParameters; +import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol; import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler; import java.util.Arrays; import org.apache.logging.log4j.LogManager; @@ -42,7 +44,12 @@ public class HumanPlayer extends Human { */ @Override public void send(String msg, Game game) { - String formattedMsg = ServerGameInfoHandler.format(msg,this, game); + String formattedMsg; + if (msg.equals(GuiParameters.updateGameState)) { + formattedMsg = Protocol.printToGUI + "$" + GuiParameters.updateGameState + game.getGameState().humanToString(); + } else { + formattedMsg = ServerGameInfoHandler.format(msg, this, game); + } clientHandler.sendMsgToClient(formattedMsg); } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Spectator.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Spectator.java index 9569714..338b8db 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Spectator.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Spectator.java @@ -29,6 +29,6 @@ public class Spectator extends Passenger{ */ @Override public void send(String msg, Game game) { - clientHandler.sendMsgToClient(ServerGameInfoHandler.spectatorFormat(msg, this, game)); + clientHandler.sendMsgToClient(ServerGameInfoHandler.spectatorFormat(msg, game)); } } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/Client.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/Client.java index 03274f1..e8b919a 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/Client.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/Client.java @@ -11,6 +11,7 @@ import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.game.GameController; import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ClientPinger; +import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.GuiParameters; import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol; import java.net.InetAddress; @@ -309,35 +310,92 @@ public class Client { } /** - * funnels a message to the gui, where depending on the message different functions/controls/methods - * of the gui are targeted. The message contains information on what to do, which are extracted - * @param msg a message of the form {@code parameter$msg} - * + * funnels a message to the gui, where depending on the parameter different functions/controls/methods + * of the gui are targeted. The data contains more information the gui needs + * @param parameter a string according to {@link GuiParameters} and {@link ClientGameInfoHandler} can be empty + * @param data some information in a string, separators can be $ or : + *TODO(Seraina&Sebi): evtl. auslagern? */ - public void sendToGUI(String msg) { - int indexFirstDollar = msg.indexOf('$'); - String header = ""; + public void sendToGUI(String parameter, String data) { try { - header = msg.substring(0,indexFirstDollar); - } catch (IndexOutOfBoundsException e) { - LOGGER.info(e.getMessage()); - } - - switch (header) { - case ClientGameInfoHandler.itsNightTime: - gameStateModel.setDayClone(false); - break; - case ClientGameInfoHandler.itsDayTime: - gameStateModel.setDayClone(true); - break; - - default: - gameController.addMessageToNotificationText(msg); //TODO(Sebi,Seraina): should the gameController be in the Application just like the ChatController? - + switch (parameter) { + case ClientGameInfoHandler.itsNightTime: //ClientGameInfoHandler + gameStateModel.setDayClone(false); + break; + case ClientGameInfoHandler.itsDayTime: //ClientGameInfoHandler + gameStateModel.setDayClone(true); + break; + case GuiParameters.updateGameState: + gameStateModel.setGSFromString(data); + gameController.updateRoomLabels(); + break; + case GuiParameters.noiseHeardAtPosition: + try { + int position = Integer.parseInt(data); + determineNoiseDisplay(position); + } catch (Exception e) { + LOGGER.warn("Not a position given for noise"); + } + break; + case GuiParameters.listOfLobbies: + //TODO + break; + case GuiParameters.listOfPLayers: + //TODO + break; + case GuiParameters.viewChangeToGame: + //TODO + break; + case GuiParameters.viewChangeToStart: + //TODO + break; + case GuiParameters.viewChangeToLobby: + //TODO + break; + default: + notificationTextDisplay(data); + //TODO(Sebi,Seraina): should the gameController be in the Application just like the ChatController? + } + } catch (Exception e) { + LOGGER.warn("Communication with GUI currently not possible: " + e.getMessage()); } + } + /** + * Starts a new thread, thad adds a message to notificationText in the gameController, + * waits 3 seconds and deletes it again. + * @param data the message to be added + */ + public void notificationTextDisplay(String data) { + new Thread(() -> { + try { + gameController.addMessageToNotificationText(data); + Thread.sleep(3000); + gameController.clearNotificationText(); + } catch (InterruptedException e) { + LOGGER.warn(e.getMessage()); + } + }).start(); + + } + + public void determineNoiseDisplay(int position) { + switch (position) { + case 0: + gameController.noiseDisplay0(); + case 1: + gameController.noiseDisplay1(); + case 2: + gameController.noiseDisplay2(); + case 3: + gameController.noiseDisplay3(); + case 4: + gameController.noiseDisplay4(); + case 5: + gameController.noiseDisplay5(); + } } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/JClientProtocolParser.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/JClientProtocolParser.java index 58db92d..2ad6979 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/JClientProtocolParser.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/JClientProtocolParser.java @@ -49,7 +49,6 @@ public class JClientProtocolParser { break; case Protocol.serverRequestsGhostVote: LOGGER.debug("Ghost received Vote request"); - //c.sendToGUI(ClientGameInfoHandler.ghostVoteRequest); c.positionSetter(msg.substring(6)); break; case Protocol.serverRequestsHumanVote: @@ -61,7 +60,17 @@ public class JClientProtocolParser { c.changeUsername(msg.substring(6)); break; case Protocol.printToGUI: - c.sendToGUI(msg.substring(6)); + String substring = msg.substring(6); + int index = msg.indexOf("$"); + String parameter = ""; + String data = substring; + try { + parameter = msg.substring(0,index); + data = msg.substring(index+1); + } catch (Exception e) { + LOGGER.warn("No parameter in PTGUI"); + } + c.sendToGUI(parameter,data); break; default: System.out.println("Received unknown command"); diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/GameController.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/GameController.java index 73d5cfd..2cfde99 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/GameController.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/GameController.java @@ -1,5 +1,8 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.game; +import ch.unibas.dmi.dbis.cs108.multiplayer.client.Client; +import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.GameStateModel; +import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.GuiParameters; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.GameStateModel; import javafx.event.EventHandler; import ch.unibas.dmi.dbis.cs108.BudaLogConfig; @@ -23,6 +26,8 @@ import javafx.scene.control.Label; import javafx.scene.control.SplitPane; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.Background; import javafx.scene.layout.HBox; @@ -31,13 +36,19 @@ import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.text.Text; import javafx.scene.text.TextFlow; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; public class GameController { + public static final Logger LOGGER = LogManager.getLogger(GameController.class); + public static final BudaLogConfig l = new BudaLogConfig(LOGGER); private static ClientModel client; private static GameStateModel gameStateModel; + + //TODO(Seraina, Sebi): Same issue as ChatController? do with setters? public GameController(ClientModel c, GameStateModel g) { client = c; @@ -76,6 +87,20 @@ public class GameController { @FXML private TextFlow lableRoom5; @FXML + private HBox notificationHBox; + @FXML + private ImageView noiseImage0; + @FXML + private ImageView noiseImage1; + @FXML + private ImageView noiseImage2; + @FXML + private ImageView noiseImage3; + @FXML + private ImageView noiseImage4; + @FXML + private ImageView noiseImage5; + @FXML private Button noiseButton; @FXML private TextFlow notificationText; @@ -132,22 +157,29 @@ public class GameController { * Sends a noise message, to the server, should be a gui message? */ public void noise() { - client.getClient().sendMsgToServer("noise"); //TODO: Add message + client.getClient().sendMsgToServer( + Protocol.sendMessageToAllClients + "$" + Protocol.printToGUI + GuiParameters.noiseHeardAtPosition + "$" + + client.getClient().getPosition()); //TODO: Test!! } /** * Takes a given message and displays it in the notificationText Flow in the game Scene * @param msg the message to be displayed */ - public void addMessageToNotificationText(String msg) { + public void addMessageToNotificationText(String msg){ Text notification = new Text(msg); - notificationText.getChildren().clear(); - notificationText.getChildren().add(notification); + try { + notificationText.getChildren().clear(); + notificationText.getChildren().add(notification); + } catch (Exception e) { + LOGGER.trace("Not yet initialized"); + } //TODO: Wait for a certain time, then clear all again } /** - * Updates the labels of the rooms accordingly to the datastructures in GameStateModel + * Adds a msg to the room Lable at the specified position + * @param names a String array containing all the names */ public void updateRoomLabels() { String[] names = gameStateModel.getPassengerTrainClone()[0]; @@ -165,26 +197,103 @@ public class GameController { Text role4 = new Text(roles[4]); Text role5 = new Text(roles[5]); - lableRoom0.getChildren().clear(); - lableRoom0.getChildren().add(name0); - lableRoom0.getChildren().add(role0); - lableRoom1.getChildren().clear(); - lableRoom1.getChildren().add(name1); - lableRoom1.getChildren().add(role1); - lableRoom2.getChildren().clear(); - lableRoom2.getChildren().add(name2); - lableRoom2.getChildren().add(role2); - lableRoom3.getChildren().clear(); - lableRoom3.getChildren().add(name3); - lableRoom3.getChildren().add(role3); - lableRoom4.getChildren().clear(); - lableRoom4.getChildren().add(name4); - lableRoom4.getChildren().add(role4); - lableRoom5.getChildren().clear(); - lableRoom5.getChildren().add(name5); - lableRoom5.getChildren().add(role5); + try { + lableRoom0.getChildren().clear(); + lableRoom0.getChildren().add(name0); + lableRoom0.getChildren().add(role0); + lableRoom1.getChildren().clear(); + lableRoom1.getChildren().add(name1); + lableRoom1.getChildren().add(role1); + lableRoom2.getChildren().clear(); + lableRoom2.getChildren().add(name2); + lableRoom2.getChildren().add(role2); + lableRoom3.getChildren().clear(); + lableRoom3.getChildren().add(name3); + lableRoom3.getChildren().add(role3); + lableRoom4.getChildren().clear(); + lableRoom4.getChildren().add(name4); + lableRoom4.getChildren().add(role4); + lableRoom5.getChildren().clear(); + lableRoom5.getChildren().add(name5); + lableRoom5.getChildren().add(role5); + } catch (Exception e) { + LOGGER.trace("Not yet initialized"); + } } + /** + * Adds an image of a bell on top of button0 + */ + public void noiseDisplay0(){ + Image bell = new Image("ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.game.DayOpen.bell.png"); + try { + noiseImage0.setImage(bell); + } catch (Exception e) { + LOGGER.trace("Not yet initialized"); + } + } + + /** + * Adds an image of a bell on top of button1 + */ + public void noiseDisplay1(){ + Image bell = new Image("ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.game.DayOpen.bell.png"); + try { + noiseImage0.setImage(bell); + } catch (Exception e) { + LOGGER.trace("Not yet initialized"); + } + } + + /** + * Adds an image of a bell on top of button2 + */ + public void noiseDisplay2(){ + Image bell = new Image("ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.game.DayOpen.bell.png"); + try { + noiseImage0.setImage(bell); + } catch (Exception e) { + LOGGER.trace("Not yet initialized"); + } + } + + /** + * Adds an image of a bell on top of button3 + */ + public void noiseDisplay3(){ + Image bell = new Image("ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.game.DayOpen.bell.png"); + try { + noiseImage0.setImage(bell); + } catch (Exception e) { + LOGGER.trace("Not yet initialized"); + } + } + + /** + * Adds an image of a bell on top of button4 + */ + public void noiseDisplay4(){ + Image bell = new Image("ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.game.DayOpen.bell.png"); + try { + noiseImage0.setImage(bell); + } catch (Exception e) { + LOGGER.trace("Not yet initialized"); + } + } + + /** + * Adds an image of a bell on top of button5 + */ + public void noiseDisplay5(){ + Image bell = new Image("ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.game.DayOpen.bell.png"); + try { + noiseImage0.setImage(bell); + } catch (Exception e) { + LOGGER.trace("Not yet initialized"); + } + } + + public void setGameStateModel( GameStateModel gameStateModel) { 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 new file mode 100644 index 0000000..2b5e0c2 --- /dev/null +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/GuiParameters.java @@ -0,0 +1,45 @@ +package ch.unibas.dmi.dbis.cs108.multiplayer.helpers; + +/** + * This class contains parameters for the PTGUI protocol message + */ +public class GuiParameters { + + /** + * Tells GUI to update the gameStateModel, in the form {@code UPDATE$name:role:kickedOff$name:role:kickedOff} ... usw. + */ + public static final String updateGameState = "UPDATE"; + /** + * Tells Gui, that the following statement after $, is a String containing the listOfLobbies + */ + public static final String listOfLobbies = "LOBBIES"; + + /** + * Tells Gui, that what follows is a list of players per Lobby + */ + public static final String listOfPLayers = "PLAYERS"; + + /** + * Tells Gui, that the passenger at position {@code position} has heard some noise + * Form: {@code NOISE$position$} + */ + public static final String noiseHeardAtPosition = "NOISE"; + + /** + * Tells Gui, that the start view should be displayed + */ + public static final String viewChangeToStart = "VCSTART"; + + /** + * Tells Gui, that the lobby view should be displayed + */ + public static final String viewChangeToLobby = "VCLOBBY"; + + /** + * Tells Gui, that the game view should be displayed + */ + public static final String viewChangeToGame = "VCGAME"; + + + +} diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/Protocol.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/Protocol.java index a4f5931..b9e3ae1 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/Protocol.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/Protocol.java @@ -143,6 +143,12 @@ public class Protocol { */ public static final String highScoreList = "HSCOR"; + /** + * The client requests that a message in {@code STACL$msg} is sent to all clients but only the message + * without a specific Server message to be added. + */ + public static final String sendMessageToAllClients = "STACL"; + @@ -195,10 +201,9 @@ public class Protocol { public static final String changedUserName = "CHNAM"; /** - * Sends a message to a client containing information for the gui. The command is structured {@code PTGUI$parameter$msg} - * where the parameter specifies what exactly to do in de gui (i.e. change scene) and the optional - * message contains information the gui needs from the server to execute the command specified in the parameter - * + * Handles all information that the gui of the client needs. The Form is {@code PTGUI$parameters$msg} + * where the parameter tells the gui to do different things according to {@link GuiParameters} and the message + * contains a certain information i.e. who is where in the train */ public static final String printToGUI = "PTGUI"; 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 e5928c4..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 @@ -238,7 +238,7 @@ public class ClientHandler implements Runnable { * @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) { + public static void broadcastAnnouncementToAll(String msg) { //TODO: Adjust to GUI command? System.out.println(msg); for (ClientHandler client : connectedClients) { client.sendMsgToClient(Protocol.printToClientConsole + "$" + msg); @@ -254,7 +254,7 @@ public class ClientHandler implements Runnable { * @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) { + public void broadcastAnnouncementToLobby(String msg) { //TODO: Adjust to GUI command? 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. @@ -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 * 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 c9029ba..a9f031e 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 @@ -2,6 +2,7 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.server; import ch.unibas.dmi.dbis.cs108.BudaLogConfig; +import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.GuiParameters; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol; @@ -81,6 +82,7 @@ public class JServerProtocolParser { try { int i = Integer.parseInt(msg.substring(6, 7)); h.joinLobby(i); + h.sendMsgToClient(Protocol.printToGUI + "$" + GuiParameters.viewChangeToLobby + "$"); } catch (Exception e) { h.sendMsgToClient(Protocol.printToClientConsole + "$Invalid input. Please use JOINL$1 to join Lobby 1, for example."); @@ -88,6 +90,7 @@ public class JServerProtocolParser { break; case Protocol.createNewLobby: h.createNewLobby(); + h.sendMsgToClient(Protocol.printToGUI + "$" + GuiParameters.viewChangeToLobby+ "$"); break; case Protocol.listLobbies: h.listLobbies(); @@ -97,6 +100,7 @@ public class JServerProtocolParser { break; case Protocol.leaveLobby: h.leaveLobby(); + h.sendMsgToClient(Protocol.printToGUI + "$" + GuiParameters.viewChangeToStart + "$"); break; case Protocol.votedFor: LOGGER.debug("Made it here"); @@ -105,6 +109,7 @@ public class JServerProtocolParser { break; case Protocol.startANewGame: h.startNewGame(); + h.sendMsgToClientsInLobby(Protocol.printToGUI + "$" + GuiParameters.viewChangeToGame + "$"); break; case Protocol.listGames: h.listGames(); @@ -112,6 +117,9 @@ public class JServerProtocolParser { case Protocol.highScoreList: h.sendHighScoreList(); break; + case Protocol.sendMessageToAllClients: + msg = msg.substring(6); + h.sendMsgToClient(msg); default: System.out.println("Received unknown command"); } diff --git a/src/main/resources/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/DayOpen/bell.png b/src/main/resources/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/DayOpen/bell.png new file mode 100644 index 0000000..3fa873e Binary files /dev/null and b/src/main/resources/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/DayOpen/bell.png differ diff --git a/src/main/resources/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/GameDayAll.fxml b/src/main/resources/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/GameDayAll.fxml index f36cb4a..18eaabb 100644 --- a/src/main/resources/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/GameDayAll.fxml +++ b/src/main/resources/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/GameDayAll.fxml @@ -13,6 +13,16 @@ + + + + + + + + + +