Commits maybe need to be added to merge with GUI_MilestoneIV is possible

This commit is contained in:
Seraina
2022-04-30 16:52:04 +02:00
parent 81ee9a8841
commit 7109ed113f
16 changed files with 386 additions and 61 deletions

View File

@@ -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();
}
}

View File

@@ -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");

View File

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

View File

@@ -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";
}

View File

@@ -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";

View File

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

View File

@@ -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");
}