Implemented a thread in Game, that constantly sends out gameState updates to all clients while the game is ongoing
This commit is contained in:
parent
460f4fb9ea
commit
421074a59b
@ -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,32 @@ 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(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
while (getGame().isOngoing) {
|
||||
for (Passenger passenger : getGameState().getPassengerTrain()) {
|
||||
passenger.send(GuiParameters.updateGameState, getGame());
|
||||
}
|
||||
try {
|
||||
Thread.sleep(1000); //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,17 +147,20 @@ 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)) {
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user