Adjusted game logic to lobbies, now a game is started inside a lobby and only sends msg to clients in the respective lobby
TODO: when a client leaves a lobby or the server, the corresponding passenger needs to be transformed into an npc
This commit is contained in:
parent
682b2634fc
commit
33d9ea899e
@ -8,6 +8,7 @@ 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.multiplayer.server.ClientHandler;
|
||||
import ch.unibas.dmi.dbis.cs108.multiplayer.server.Lobby;
|
||||
import java.util.HashSet;
|
||||
import org.apache.logging.log4j.*;
|
||||
|
||||
@ -24,7 +25,7 @@ public class Game implements Runnable {
|
||||
protected GameState gameState;
|
||||
protected boolean isDay = false; //false means it is night, it is night by default
|
||||
protected VoteHandler voteHandler = new VoteHandler();
|
||||
private ClientHandler clientHandler;
|
||||
private Lobby lobby;
|
||||
//TODO: Figure out where Day/Night game state is saved maybe think about a game state class or smt.
|
||||
/**
|
||||
* Constructs a Game instance where:
|
||||
@ -33,13 +34,13 @@ public class Game implements Runnable {
|
||||
* @param nrOfGhosts is the number of OG Ghosts you want to start with and
|
||||
* @param nrOfUsers is the number of active users at the time (non NPCs)
|
||||
*/
|
||||
public Game(ClientHandler clientHandler, int nrOfPlayers, int nrOfGhosts, int nrOfUsers)
|
||||
public Game(int nrOfPlayers, int nrOfGhosts, int nrOfUsers, Lobby lobby)
|
||||
throws TrainOverflow { //ToDo: Who handles Exception how and where
|
||||
this.nrOfPlayers = nrOfPlayers;
|
||||
this.nrOfGhosts = nrOfGhosts;
|
||||
this.nrOfUsers = nrOfUsers;
|
||||
this.gameState = new GameState(nrOfPlayers, nrOfGhosts, nrOfUsers);
|
||||
this.clientHandler = clientHandler;
|
||||
this.lobby = lobby;
|
||||
}
|
||||
|
||||
public GameState getGameState() {
|
||||
@ -58,8 +59,8 @@ public class Game implements Runnable {
|
||||
return nrOfUsers;
|
||||
}
|
||||
|
||||
public ClientHandler getClientHandler() {
|
||||
return clientHandler;
|
||||
public Lobby getLobby() {
|
||||
return lobby;
|
||||
}
|
||||
|
||||
public boolean getIsDay() {return isDay;}
|
||||
@ -79,14 +80,14 @@ public class Game implements Runnable {
|
||||
public void run() {
|
||||
LOGGER.info("the run-method has been called");
|
||||
int i = 0;
|
||||
HashSet<ClientHandler> clients = ClientHandler.getConnectedClients();
|
||||
HashSet<ClientHandler> lobbyClients = lobby.getLobbyClients();
|
||||
String gameOverCheck = "";
|
||||
int[] order = gameState.getTrain().orderOfTrain;
|
||||
Passenger[] passengerTrain = gameState.getPassengerTrain();
|
||||
|
||||
|
||||
LOGGER.info(gameState.toString());
|
||||
for (ClientHandler client : clients) {
|
||||
for (ClientHandler client : lobbyClients) { //TODO(Seraina): Adjust for lobbies
|
||||
int index = order[i];
|
||||
if (passengerTrain[index].getIsGhost()) { //if there is a ghost
|
||||
GhostPlayer ghostPlayer = new GhostPlayer(passengerTrain[index].getPosition(),
|
||||
@ -126,7 +127,7 @@ public class Game implements Runnable {
|
||||
}
|
||||
if (gameOverCheck.equals(ClientGameInfoHandler.gameOverGhostsWin) || gameOverCheck.equals(
|
||||
ClientGameInfoHandler.gameOverHumansWin)) {
|
||||
clientHandler.broadcastAnnouncementToAll(gameOverCheck); //ToDo(Seraina): adjust for lobby
|
||||
lobby.getAdmin().broadcastAnnouncementToLobby(gameOverCheck); //ToDo(Seraina): adjust for lobby
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@ -76,7 +76,7 @@ public class ServerGameInfoHandler {
|
||||
switch (msg) {
|
||||
case ClientGameInfoHandler.noiseNotification:
|
||||
String outMsg = npc.getName() + ": " + noiseRandomizer();
|
||||
game.getClientHandler().broadcastNpcChatMessage(outMsg);
|
||||
game.getLobby().getAdmin().broadcastNpcChatMessageToLobby(outMsg);
|
||||
break;
|
||||
case ClientGameInfoHandler.ghostVoteRequest:
|
||||
npc.vote(game);
|
||||
@ -93,7 +93,7 @@ public class ServerGameInfoHandler {
|
||||
switch (msg) {
|
||||
case ClientGameInfoHandler.noiseNotification:
|
||||
String outMsg = npc.getName() + ": " + noiseRandomizer();;
|
||||
game.getClientHandler().broadcastNpcChatMessage(outMsg);
|
||||
game.getLobby().getAdmin().broadcastNpcChatMessageToLobby(outMsg);
|
||||
break;
|
||||
case ClientGameInfoHandler.humanVoteRequest:
|
||||
npc.vote();
|
||||
|
||||
@ -190,9 +190,12 @@ public class ClientHandler implements Runnable {
|
||||
*
|
||||
* @param msg the Message to be broadcast
|
||||
*/
|
||||
public void broadcastNpcChatMessage(String msg) {
|
||||
for (ClientHandler client : connectedClients) {
|
||||
client.sendMsgToClient(Protocol.printToClientConsole + "$" + msg);
|
||||
public void broadcastNpcChatMessageToLobby(String msg) {
|
||||
Lobby l = getLobby();
|
||||
if (l != null) {
|
||||
for (ClientHandler client : l.getLobbyClients()) {
|
||||
client.sendMsgToClient(Protocol.printToClientChat + "$" + msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -267,7 +270,7 @@ public class ClientHandler implements Runnable {
|
||||
out.flush();
|
||||
} catch (IOException e) {
|
||||
//e.printStackTrace();
|
||||
LOGGER.debug("unable to send msg: " + msg);
|
||||
LOGGER.warn("unable to send msg: " + msg);
|
||||
removeClientOnConnectionLoss();
|
||||
}
|
||||
}
|
||||
@ -302,7 +305,8 @@ public class ClientHandler implements Runnable {
|
||||
*/
|
||||
public void startNewGame() {
|
||||
try {
|
||||
Game game = new Game(this,6,1, ClientHandler.getConnectedClients().size());
|
||||
Lobby l = getLobby();
|
||||
Game game = new Game(6,1, l.getLobbyClients().size(), l);
|
||||
Thread t = new Thread(game);
|
||||
t.start();
|
||||
} catch (TrainOverflow e) {
|
||||
|
||||
Reference in New Issue
Block a user