Implemented a run method in game, that goes through a game until it ends

This commit is contained in:
Seraina 2022-04-08 21:00:57 +02:00
parent f878bee40d
commit 5b4a06dd75
8 changed files with 100 additions and 53 deletions

View File

@ -2,6 +2,7 @@ package ch.unibas.dmi.dbis.cs108.gamelogic;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
import org.apache.logging.log4j.*;
public class Game {
@ -15,6 +16,8 @@ public class Game {
protected int nrOfGhosts; // sets how many Ghosts we start witch
protected int nrOfUsers; // safes how many clients are active in this Game
protected GameFunctions gameFunctions;
protected boolean isDay; //false means it is night, it is night by default
protected VoteHandler voteHandler;
//TODO: Figure out where Day/Night game state is saved maybe think about a game state class or smt.
/**
* Constructs a Game instance where:
@ -47,12 +50,35 @@ public class Game {
return nrOfUsers;
}
public void setDay(boolean day) {
isDay = day;
}
public void run(ClientHandler clientHandler) {
int i = 0;
String gameOverCheck = "";
while (true) { //ToDo: was ist die Abbruchbedingung? VoteHandler muss das schicken.
if (!isDay) {
voteHandler.ghostVote(gameFunctions.getPassengerTrain(), this);
setDay(true);
} else {
gameOverCheck = voteHandler.humanVote(gameFunctions.getPassengerTrain(), this);
}
if (gameOverCheck.equals("Game over: ghosts win!") || gameOverCheck.equals("Game over: humans win!")){
clientHandler.broadcastAnnouncement(gameOverCheck);
return;
}
}
}
public static void main(String[] args) {
try {
Game game1 = new Game(6, 1, 1);
} catch (TrainOverflow e) {
System.out.println(e.getMessage());
}

View File

@ -1,6 +1,7 @@
package ch.unibas.dmi.dbis.cs108.gamelogic;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Ghost;
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Human;
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger;
import org.apache.logging.log4j.LogManager;
@ -13,11 +14,11 @@ public class GameFunctions {
/**
* Can be extended for optional Game-settings
**/
int nrOfPlayers; //sets the length of the train
int nrOfGhosts; // sets how many Ghosts we start witch
int nrOfUsers; // safes how many clients are active in this Game
Train train; // safes who sits where
public Passenger[] passengerTrain;
protected int nrOfPlayers; //sets the length of the train
protected int nrOfGhosts; // sets how many Ghosts we start witch
protected int nrOfUsers; // safes how many clients are active in this Game
protected Train train; // safes who sits where
protected Passenger[] passengerTrain;
protected Game game;
/**
@ -37,10 +38,18 @@ public class GameFunctions {
this.train = new Train(nrOfPlayers, nrOfUsers);
Passenger[] passengerTrain = new Passenger[nrOfPlayers]; //Creates an array with Passengers with correlation positions (Train)
for (int i = 0; i < nrOfPlayers; i++) {
Human h = new Human(game);
h.setPosition(train.orderOfTrain[i]);
passengerTrain[train.orderOfTrain[i]] = h;
if (i == 3) {
Ghost g = new Ghost(game);
g.setPosition(train.orderOfTrain[i]);
g.setIsOG(true);
passengerTrain[train.orderOfTrain[i]] = g;
} else {
Human h = new Human(game);
h.setPosition(train.orderOfTrain[i]);
passengerTrain[train.orderOfTrain[i]] = h;
}
}
this.passengerTrain = passengerTrain;
}
@ -56,6 +65,7 @@ public class GameFunctions {
return nrOfUsers;
}
public Passenger[] getPassengerTrain() {
return passengerTrain;
}
}

View File

@ -1,6 +1,9 @@
package ch.unibas.dmi.dbis.cs108.gamelogic;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Ghost;
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.Protocol;
import org.apache.logging.log4j.LogManager;
@ -37,26 +40,31 @@ public class ServerGameInfoHandler {
return msg;
}
/**
* TODO(Seraina): Handle NPC's Maybe do that in Passenger send methode!
* Send a message "GVOTR" to a passenger urging them to vote for a human to infect
* Currently only handles only Players, so send a message to corresponding client
* @param passenger the passenger the message is meant to, should be a Ghost
*/
public void sendVoteRequestGhosts(Passenger passenger){
passenger.getClientHandler().sendMsgToClient("GVOTR");
public static void ghostNpcParser(GhostNPC npc, String msg, Game game) {
switch (msg) {
case "noise":
npc.noise();
break;
case "Vote on who to ghostify!":
npc.vote(game);
}
}
public static void humanNpcParser(HumanNPC npc, String msg, Game game) {
switch (msg) {
case "noise":
npc.noise();
break;
case "Vote for a ghost to kick off!":
npc.vote();
}
}
/**
* TODO(Seraina): Handle NPC's
* Send a message "HVOTR" to a passenger urging them to vote for sm to kick off the train.
* Currently only handles only Players, so send a message to corresponding client
* @param passenger the passenger the message is meant to, can be either human or ghost
*/
public void sendVoteRequestHumans(Passenger passenger){
passenger.getClientHandler().sendMsgToClient("HVOTR");
}
public static void main(String[] args) {
ServerGameInfoHandler s = new ServerGameInfoHandler();

View File

@ -2,6 +2,7 @@ package ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.gamelogic.Game;
import ch.unibas.dmi.dbis.cs108.gamelogic.ServerGameInfoHandler;
import ch.unibas.dmi.dbis.cs108.gamelogic.TrainOverflow;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -32,13 +33,18 @@ public class GhostNPC extends Ghost {
}
}
@Override
public void send(String msg) {
ServerGameInfoHandler.ghostNpcParser(this, msg, game);
}
/**
* Sets vote of this Ghost position on a number between 0 and 5,
* but only for positions where there aren't any ghosts and sets hasVoted to true
*/
public void vote(Game game){
int ghostCounter = 0;
Passenger[] train = game.getGameFunctions().passengerTrain;
Passenger[] train = game.getGameFunctions().getPassengerTrain();
for(Passenger passenger : train) {
if(passenger.isGhost) {
ghostCounter++;

View File

@ -2,6 +2,7 @@ package ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.gamelogic.Game;
import ch.unibas.dmi.dbis.cs108.gamelogic.ServerGameInfoHandler;
import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -34,7 +35,10 @@ public class GhostPlayer extends Ghost {
}
@Override
public void send(String msg) {
//todo(Jonas): pass message along to client.
String formattedMsg = ServerGameInfoHandler.format(msg);
clientHandler.sendMsgToClient(formattedMsg);
}
}

View File

@ -2,6 +2,7 @@ package ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.gamelogic.Game;
import ch.unibas.dmi.dbis.cs108.gamelogic.ServerGameInfoHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -29,6 +30,11 @@ public class HumanNPC extends Human {
}
}
@Override
public void send(String msg) {
ServerGameInfoHandler.humanNpcParser(this, msg, game);
}
/**
* Currently returns a random integer for voting
* @return integer between 0 and 5
@ -36,4 +42,8 @@ public class HumanNPC extends Human {
public int vote(){
return (int) (Math.random()*6);
}
public void noise() {
clientHandler.broadcastChatMessage("I heard some noise tonight");
}
}

View File

@ -2,6 +2,7 @@ package ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.gamelogic.Game;
import ch.unibas.dmi.dbis.cs108.gamelogic.ServerGameInfoHandler;
import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -31,5 +32,9 @@ public class HumanPlayer extends Human {
}
}
@Override
public void send(String msg) {
String formattedMsg = ServerGameInfoHandler.format(msg);
clientHandler.sendMsgToClient(formattedMsg);
}
}

View File

@ -32,33 +32,11 @@ public class Passenger {
* @param msg the message that is sent to this player.
**/
public void send(String msg) {
/*sendcounter++;
if (msg.equals("Vote on who to ghostify!")) {
vote = 1*sendcounter;
hasVoted = true; // for testing, when is it set to false again?
LOGGER.info("Voted for Position " + vote);
} else if(msg.equals("Vote for a ghost to kick off!")) {
vote = (int) (0.5*sendcounter);
hasVoted = true; // for testing, when is it set to false again?
LOGGER.info("Voted for Position " + vote);
} else {
LOGGER.debug(msg);
}*/
if (isPlayer) {
String formattedMsg = ServerGameInfoHandler.format(msg);
clientHandler.sendMsgToClient(formattedMsg);
} else { //is a NPC
if(isGhost) {
} else { //is a human
}
//TODO: call a method that identifies message for NPC and calls respective methode NPCParser
}
LOGGER.warn("This object should not just be a passenger");
}
/**