Implemented the gameState Update parameter of the the printToGUI message in client, with its needed methods

This commit is contained in:
Seraina 2022-04-29 18:56:15 +02:00
parent f058eabb75
commit c5293bc8ee
3 changed files with 68 additions and 21 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,30 +310,31 @@ 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 = "";
try {
header = msg.substring(0,indexFirstDollar);
} catch (IndexOutOfBoundsException e) {
LOGGER.info(e.getMessage());
}
switch (header) {
case ClientGameInfoHandler.itsNightTime:
public void sendToGUI(String parameter, String data) {
switch (parameter) {
case ClientGameInfoHandler.itsNightTime: //ClientGameInfoHandler
gameStateModel.setDayClone(false);
break;
case ClientGameInfoHandler.itsDayTime:
case ClientGameInfoHandler.itsDayTime: //ClientGameInfoHandler
gameStateModel.setDayClone(true);
break;
case GuiParameters.updateGameState:
gameStateModel.setGSFromString(data);
break;
case GuiParameters.noiseHeardAtPosition:
break;
case GuiParameters.listOfLobbies:
break;
case GuiParameters.listOfPLayers:
break;
default:
gameController.addMessageToNotificationText(msg); //TODO(Sebi,Seraina): should the gameController be in the Application just like the ChatController?
gameController.addMessageToNotificationText(data); //TODO(Sebi,Seraina): should the gameController be in the Application just like the ChatController?
}

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:
@ -63,8 +62,8 @@ public class JClientProtocolParser {
case Protocol.printToGUI:
String substring = msg.substring(6);
int index = msg.indexOf("$");
String parameter;
String data;
String parameter = "";
String data = substring;
try {
parameter = msg.substring(0,index);
data = msg.substring(index+1);

View File

@ -1,10 +1,18 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.gamelogic.ServerGameInfoHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* A Class that saves the current game State of the Game the client is in. Should get updated regularly
* The gui gets its game information from here
*/
public class GameStateModel {
public static final Logger LOGGER = LogManager.getLogger(GameStateModel.class);
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
private int nrOfPlayers; //sets the length of the train
/**
* true if it is the day
@ -79,4 +87,42 @@ public class GameStateModel {
public boolean getDayClone() {
return isDayClone;
}
public void setKickedOff(boolean[] kickedOff) {
this.kickedOff = kickedOff;
}
public boolean[] getKickedOff() {
return kickedOff;
}
/**
* Extracts information of a String and fills the information into the passengerTrainClone and
* kickedOff fields of this gameState
* @param data the data in the form: {@code name:role:kickedOff$name:role:kickedOff$}..usw.
*/
public void setGSFromString(String data) {
try {
String[] names = new String[6];
String[] roles = new String[6];
boolean[] kickedOff = new boolean[6];
int i = 0;
while (!data.isEmpty()) {
int index = data.indexOf('$');
String left = data.substring(0, index);
data = data.substring(index + 1);
int j = left.indexOf(':');
names[i] = left.substring(0, j);
String right = left.substring(j + 1);
j = right.indexOf(':');
roles[i] = right.substring(0, j);
kickedOff[i] = Boolean.parseBoolean(right.substring(j + 1));
i++;
}
setPassengerTrainClone(names, roles);
setKickedOff(kickedOff);
} catch (Exception e) {
LOGGER.warn("data has wrong format");
}
}
}