A very basic command now starts a game in the JServerProtocolParser (needs to bee relocated and but does the job for now)
The game runs, now the Clients need to be able to give inputs (so it ends at some point)
This commit is contained in:
parent
3b0bc40ad0
commit
ce3fa4dde1
@ -11,19 +11,20 @@ import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
|
||||
import java.util.HashSet;
|
||||
import org.apache.logging.log4j.*;
|
||||
|
||||
public class Game {
|
||||
public class Game implements Runnable {
|
||||
public static final Logger LOGGER = LogManager.getLogger();
|
||||
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
|
||||
|
||||
/**
|
||||
* Can be extended for optional Game-settings
|
||||
**/
|
||||
protected int nrOfPlayers; //sets the length of the train
|
||||
protected int nrOfGhosts; // sets how many Ghosts we start witch
|
||||
protected final int nrOfPlayers; //sets the length of the train
|
||||
protected final int nrOfGhosts; // sets how many Ghosts we start witch
|
||||
protected int nrOfUsers; // safes how many clients are active in this Game
|
||||
protected GameState gameState;
|
||||
protected boolean isDay; //false means it is night, it is night by default
|
||||
protected VoteHandler voteHandler;
|
||||
protected boolean isDay = false; //false means it is night, it is night by default
|
||||
protected VoteHandler voteHandler = new VoteHandler();
|
||||
private ClientHandler clientHandler;
|
||||
//TODO: Figure out where Day/Night game state is saved maybe think about a game state class or smt.
|
||||
/**
|
||||
* Constructs a Game instance where:
|
||||
@ -32,12 +33,13 @@ public class Game {
|
||||
* @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(int nrOfPlayers, int nrOfGhosts, int nrOfUsers)
|
||||
public Game(ClientHandler clientHandler, int nrOfPlayers, int nrOfGhosts, int nrOfUsers)
|
||||
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;
|
||||
}
|
||||
|
||||
public GameState getGameState() {
|
||||
@ -60,7 +62,9 @@ public class Game {
|
||||
isDay = day;
|
||||
}
|
||||
|
||||
public void run(ClientHandler clientHandler) {
|
||||
@Override
|
||||
public void run() {
|
||||
LOGGER.info("the run-method has been called");
|
||||
int i = 0;
|
||||
HashSet<ClientHandler> clients = ClientHandler.getConnectedClients();
|
||||
String gameOverCheck = "";
|
||||
@ -68,6 +72,7 @@ public class Game {
|
||||
Passenger[] passengerTrain = gameState.getPassengerTrain();
|
||||
|
||||
|
||||
LOGGER.info(gameState.toString());
|
||||
for (ClientHandler client : clients) {
|
||||
int index = order[i];
|
||||
if (passengerTrain[index].getIsGhost()) { //if there is a ghost
|
||||
@ -93,6 +98,7 @@ public class Game {
|
||||
}
|
||||
i++;
|
||||
}
|
||||
LOGGER.info(gameState.toString());
|
||||
|
||||
i = 0;
|
||||
while (true) { //ToDo: was ist die Abbruchbedingung? VoteHandler muss das schicken.
|
||||
@ -111,18 +117,7 @@ public class Game {
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
try {
|
||||
|
||||
Game game1 = new Game(6, 1, 1);
|
||||
|
||||
|
||||
} catch (TrainOverflow e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ -44,6 +44,7 @@ public class GameState {
|
||||
if (i == 3) {
|
||||
Ghost g = new Ghost();
|
||||
g.setPosition(train.orderOfTrain[i]);
|
||||
g.setGhost();
|
||||
g.setIsOG(true);
|
||||
passengerTrain[train.orderOfTrain[i]] = g;
|
||||
} else {
|
||||
@ -74,4 +75,34 @@ public class GameState {
|
||||
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
Passenger[] array = passengerTrain;
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
String[] print = new String[6];
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
if (array[i].getKickedOff()) {
|
||||
print[i] = "| kicked off " + array[i].getPosition() + "|";
|
||||
} else {
|
||||
if (array[i].getIsPlayer()) {
|
||||
if (array[i].getIsGhost()) {
|
||||
print[i] = "| ghostPlayer " + array[i].getPosition() + "|";
|
||||
} else {
|
||||
print[i] = "| humanPlayer " + array[i].getPosition() + "|";
|
||||
}
|
||||
} else {
|
||||
if (array[i].getIsGhost()) {
|
||||
print[i] = "| ghostNPC " + array[i].getPosition() + "|";
|
||||
} else {
|
||||
print[i] = "| humanNPC " + array[i].getPosition() + "|";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
stringBuilder.append(print[i]);
|
||||
}
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -25,10 +25,10 @@ public class ServerGameInfoHandler {
|
||||
* @param msg the message to be formatted
|
||||
* @return a message in a protocol format
|
||||
*/
|
||||
public static String format(String msg) {
|
||||
public static String format(String msg, Game game) {
|
||||
switch (msg) {
|
||||
case "Vote on who to ghostify!":
|
||||
msg = Protocol.serverRequestsGhostVote;
|
||||
msg = Protocol.serverRequestsGhostVote + "$" + game.gameState.toString();
|
||||
break;
|
||||
case "Vote for a ghost to kick off!":
|
||||
msg = Protocol.serverRequestsHumanVote;
|
||||
@ -65,10 +65,4 @@ public class ServerGameInfoHandler {
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
ServerGameInfoHandler s = new ServerGameInfoHandler();
|
||||
s.format("jhbvdwfzu");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -32,6 +32,7 @@ public class VoteHandler {
|
||||
* @param passengers: passengers on the train
|
||||
*/
|
||||
public void ghostVote(Passenger[] passengers, Game game) {
|
||||
LOGGER.debug("ghostVote has been called");
|
||||
|
||||
// array to collect votes for all players during voting, i.e. votes for player 1 (passengers[0])
|
||||
// are saved in
|
||||
@ -220,7 +221,7 @@ public class VoteHandler {
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/*public static void main(String[] args) {
|
||||
try {
|
||||
Game game = new Game(6,1, 6);
|
||||
VoteHandler voteHandler = new VoteHandler();
|
||||
@ -253,5 +254,5 @@ public class VoteHandler {
|
||||
|
||||
|
||||
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
@ -68,7 +68,7 @@ public class GhostNPC extends Ghost {
|
||||
clientHandler.broadcastChatMessage("I heard some noise tonight");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/*public static void main(String[] args) {
|
||||
|
||||
try {
|
||||
Game game = new Game(6,1,1);
|
||||
@ -80,5 +80,5 @@ public class GhostNPC extends Ghost {
|
||||
LOGGER.warn(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
@ -35,7 +35,7 @@ public class GhostPlayer extends Ghost {
|
||||
|
||||
@Override
|
||||
public void send(String msg, Game game) {
|
||||
String formattedMsg = ServerGameInfoHandler.format(msg);
|
||||
String formattedMsg = ServerGameInfoHandler.format(msg, game);
|
||||
clientHandler.sendMsgToClient(formattedMsg);
|
||||
}
|
||||
}
|
||||
|
||||
@ -32,7 +32,7 @@ public class HumanPlayer extends Human {
|
||||
|
||||
@Override
|
||||
public void send(String msg, Game game) {
|
||||
String formattedMsg = ServerGameInfoHandler.format(msg);
|
||||
String formattedMsg = ServerGameInfoHandler.format(msg, game);
|
||||
clientHandler.sendMsgToClient(formattedMsg);
|
||||
}
|
||||
}
|
||||
|
||||
@ -31,7 +31,7 @@ public class Passenger {
|
||||
String formattedMsg = ServerGameInfoHandler.format(msg);
|
||||
clientHandler.sendMsgToClient(formattedMsg);
|
||||
}
|
||||
LOGGER.warn("This object should not just be a passenger");
|
||||
LOGGER.warn("This object should not just be a passenger. Position:" + position);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -72,6 +72,15 @@ public class Client {
|
||||
}).start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells user to enter a position to vote for passenger at that position
|
||||
*/
|
||||
|
||||
public void voteGetter() {
|
||||
//TODO(Seraina): implement
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Starts a thread which listens for incoming chat messages / other messages that the user
|
||||
|
||||
@ -71,8 +71,10 @@ public class JServerProtocolParser {
|
||||
break;
|
||||
case Protocol.startANewGame:
|
||||
try {
|
||||
Game game = new Game(6,1, h.getConnectedClients().size());
|
||||
game.run(h);
|
||||
|
||||
Game game = new Game(h,6,1, ClientHandler.getConnectedClients().size());
|
||||
Thread t = new Thread(game);
|
||||
game.run();
|
||||
} catch (TrainOverflow e) {
|
||||
LOGGER.warn(e.getMessage());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user