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