Implemented the GhostNPC vote and noise functions
had to add Game game as a parameter in a couple of constructors, so everybody can always access the current game and its gamestate
This commit is contained in:
parent
9583cd56e1
commit
b297c9edda
@ -23,14 +23,29 @@ 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)
|
||||
*/
|
||||
Game(int nrOfPlayers, int nrOfGhosts, int nrOfUsers)
|
||||
public Game(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.gameFunctions = new GameFunctions(nrOfPlayers, nrOfGhosts, nrOfUsers);
|
||||
this.gameFunctions = new GameFunctions(nrOfPlayers, nrOfGhosts, nrOfUsers, this);
|
||||
}
|
||||
|
||||
public GameFunctions getGameFunctions() {
|
||||
return gameFunctions;
|
||||
}
|
||||
|
||||
public int getNrOfGhosts() {
|
||||
return nrOfGhosts;
|
||||
}
|
||||
|
||||
public int getNrOfPlayers() {
|
||||
return nrOfPlayers;
|
||||
}
|
||||
|
||||
public int getNrOfUsers() {
|
||||
return nrOfUsers;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
|
||||
@ -18,6 +18,7 @@ public class GameFunctions {
|
||||
int nrOfUsers; // safes how many clients are active in this Game
|
||||
Train train; // safes who sits where
|
||||
public Passenger[] passengerTrain;
|
||||
protected Game game;
|
||||
|
||||
/**
|
||||
* Constructs a GameFunctions instance where nrOfPlayers >= nrOfUsers. Fills passengerTrain with
|
||||
@ -28,7 +29,7 @@ public class GameFunctions {
|
||||
* @param nrOfUsers is the number of active users at the time (non NPCs)
|
||||
* @throws TrainOverflow if nrOfPlayers < nrOfUsers
|
||||
*/
|
||||
GameFunctions(int nrOfPlayers, int nrOfGhosts, int nrOfUsers)
|
||||
GameFunctions(int nrOfPlayers, int nrOfGhosts, int nrOfUsers, Game game)
|
||||
throws TrainOverflow { //ToDo: where will Exception be handled?
|
||||
this.nrOfPlayers = nrOfPlayers;
|
||||
this.nrOfGhosts = nrOfGhosts;
|
||||
@ -36,7 +37,7 @@ 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();
|
||||
Human h = new Human(game);
|
||||
h.setPosition(train.orderOfTrain[i]);
|
||||
passengerTrain[train.orderOfTrain[i]] = h;
|
||||
}
|
||||
|
||||
@ -21,7 +21,7 @@ public class GhostifyHandler {
|
||||
LOGGER.debug("Passenger Position " + p.getPosition());
|
||||
p.setGhost();
|
||||
Ghost g;
|
||||
g = new Ghost();
|
||||
g = new Ghost(game);
|
||||
g.setGhost();
|
||||
g.setPosition(p.getPosition());
|
||||
game.gameFunctions.passengerTrain[g.getPosition()] = g;
|
||||
|
||||
@ -1,6 +1,10 @@
|
||||
package ch.unibas.dmi.dbis.cs108.gamelogic;
|
||||
|
||||
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
|
||||
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger;
|
||||
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
/**
|
||||
* Handles all communications Server to Client concerning game state or game state related requests
|
||||
@ -10,6 +14,28 @@ import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger;
|
||||
*/
|
||||
|
||||
public class ServerGameInfoHandler {
|
||||
public static final Logger LOGGER = LogManager.getLogger();
|
||||
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
|
||||
|
||||
/**
|
||||
* Gets a string msg from somewhere and formats it into protocol messages
|
||||
* @param msg the message to be formatted
|
||||
* @return a message in a protocol format
|
||||
*/
|
||||
public static String format(String msg) {
|
||||
switch (msg) {
|
||||
case "Vote on who to ghostify!":
|
||||
msg = Protocol.serverRequestsGhostVote;
|
||||
break;
|
||||
case "Vote for a ghost to kick off!":
|
||||
msg = Protocol.serverRequestsHumanVote;
|
||||
break;
|
||||
default:
|
||||
msg = Protocol.printToClientConsole + "$" + msg;
|
||||
}
|
||||
LOGGER.info(msg);
|
||||
return msg;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO(Seraina): Handle NPC's Maybe do that in Passenger send methode!
|
||||
@ -32,4 +58,9 @@ public class ServerGameInfoHandler {
|
||||
passenger.getClientHandler().sendMsgToClient("HVOTR");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ServerGameInfoHandler s = new ServerGameInfoHandler();
|
||||
s.format("jhbvdwfzu");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -222,7 +222,7 @@ public class VoteHandler {
|
||||
VoteHandler voteHandler = new VoteHandler();
|
||||
|
||||
Passenger[] testArray = game.gameFunctions.passengerTrain;
|
||||
Passenger ghost = new Ghost();
|
||||
Passenger ghost = new Ghost(game);
|
||||
testArray[3] = ghost;
|
||||
testArray[3].setGhost();
|
||||
testArray[3].setIsOg();
|
||||
|
||||
@ -1,6 +1,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 org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@ -8,6 +9,10 @@ public class Ghost extends Passenger {
|
||||
public static final Logger LOGGER = LogManager.getLogger();
|
||||
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
|
||||
|
||||
public Ghost(Game game) {
|
||||
super(game);
|
||||
}
|
||||
|
||||
public boolean getIsOG() {
|
||||
return isOG;
|
||||
}
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
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.TrainOverflow;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@ -15,7 +17,8 @@ public class GhostNPC extends Ghost {
|
||||
* @param name player name. if null, then a default name is used.
|
||||
* @param isOG true if the ghost is the original ghost.
|
||||
*/
|
||||
public GhostNPC(int position, String name, boolean isOG) {
|
||||
public GhostNPC(int position, String name, boolean isOG, Game game) {
|
||||
super(game);
|
||||
this.isOG = isOG;
|
||||
this.position = position;
|
||||
this.clientHandler = null;
|
||||
@ -28,4 +31,48 @@ public class GhostNPC extends Ghost {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
for(Passenger passenger : train) {
|
||||
if(passenger.isGhost) {
|
||||
ghostCounter++;
|
||||
}
|
||||
}
|
||||
int[] humanPositions = new int[game.getNrOfPlayers() - ghostCounter ];
|
||||
int j = 0;
|
||||
for (int i = 0; i < train.length; i++) {
|
||||
if (!train[i].isGhost) { //is human
|
||||
humanPositions[j] = train[i].getPosition();
|
||||
j++;
|
||||
}
|
||||
}
|
||||
int randomPosition = (int) (Math.random()*humanPositions.length);
|
||||
vote = humanPositions[randomPosition];
|
||||
hasVoted = true;
|
||||
LOGGER.info("GhostNPC at Position: " + this.getPosition() + " has voted for: " + vote);
|
||||
}
|
||||
|
||||
public void noise() {
|
||||
clientHandler.broadcastChatMessage("I heard some noise tonight");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
try {
|
||||
Game game = new Game(6,1,1);
|
||||
Passenger p = new Passenger(game);
|
||||
GhostNPC ghostNPC = new GhostNPC(2,"peter", false, game);
|
||||
p = ghostNPC;
|
||||
ghostNPC.vote(game);
|
||||
} catch (TrainOverflow e) {
|
||||
LOGGER.warn(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,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.multiplayer.server.ClientHandler;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@ -17,7 +18,8 @@ public class GhostPlayer extends Ghost {
|
||||
* @param name name. if null, then a default name is used.
|
||||
* @param isOG true if the ghost is the original ghost.
|
||||
*/
|
||||
public GhostPlayer(int position, String name, ClientHandler clientHandler, boolean isOG) {
|
||||
public GhostPlayer(int position, String name, ClientHandler clientHandler, boolean isOG, Game game) {
|
||||
super(game);
|
||||
this.position = position;
|
||||
this.clientHandler = clientHandler;
|
||||
this.isOG = isOG;
|
||||
|
||||
@ -1,6 +1,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 org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@ -8,4 +9,8 @@ public class Human extends Passenger {
|
||||
public static final Logger LOGGER = LogManager.getLogger();
|
||||
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
|
||||
|
||||
public Human(Game game) {
|
||||
super(game);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,6 +1,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 org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@ -14,7 +15,8 @@ public class HumanNPC extends Human {
|
||||
* @param position position on the train
|
||||
* @param name player name. if null, then a default name is used.
|
||||
*/
|
||||
public HumanNPC(int position, String name) {
|
||||
public HumanNPC(int position, String name, Game game) {
|
||||
super(game);
|
||||
this.position = position;
|
||||
this.clientHandler = null;
|
||||
isGhost = false;
|
||||
@ -26,4 +28,12 @@ public class HumanNPC extends Human {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Currently returns a random integer for voting
|
||||
* @return integer between 0 and 5
|
||||
*/
|
||||
public int vote(){
|
||||
return (int) (Math.random()*6);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,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.multiplayer.server.ClientHandler;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@ -16,7 +17,8 @@ public class HumanPlayer extends Human {
|
||||
* @param position position on the train
|
||||
* @param name name. if null, then a default name is used.
|
||||
*/
|
||||
public HumanPlayer(int position, String name, ClientHandler clientHandler, boolean isOG) {
|
||||
public HumanPlayer(int position, String name, ClientHandler clientHandler, boolean isOG, Game game) {
|
||||
super(game);
|
||||
this.position = position;
|
||||
this.clientHandler = clientHandler;
|
||||
isGhost = false;
|
||||
|
||||
@ -1,7 +1,9 @@
|
||||
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.helpers.Protocol;
|
||||
import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@ -19,13 +21,18 @@ public class Passenger {
|
||||
protected ClientHandler clientHandler;//the socket for the client associated with this Passenger, for NPCs, this can be null.
|
||||
protected boolean hasVoted; //true if the player gave his vote during voting time
|
||||
protected int vote; //saves the number of the player this passenger voted for during voting (0-5)
|
||||
int sendcounter = 0;
|
||||
protected Game game;
|
||||
|
||||
Passenger(Game game) {
|
||||
this.game = game;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a protocol message to the respective player or NPC.
|
||||
* @param msg the message that is sent to this player.
|
||||
**/
|
||||
public void send(String msg) {
|
||||
sendcounter++;
|
||||
/*sendcounter++;
|
||||
if (msg.equals("Vote on who to ghostify!")) {
|
||||
vote = 1*sendcounter;
|
||||
hasVoted = true; // for testing, when is it set to false again?
|
||||
@ -37,13 +44,21 @@ public class Passenger {
|
||||
} else {
|
||||
|
||||
LOGGER.debug(msg);
|
||||
}
|
||||
/*if (isPlayer) {
|
||||
//TODO: maybe put a formatter here, so protocol msg are only send between sever-client
|
||||
clientHandler.sendMsgToClient(msg); //ToDO(Seraina): Make sure only the right kind of messages are sent
|
||||
} else { //is a NPC
|
||||
//TODO: call a method that identifies message for NPC and calls respective methode NPCParser
|
||||
}*/
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -116,4 +131,5 @@ public class Passenger {
|
||||
public ClientHandler getClientHandler() {
|
||||
return clientHandler;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -128,4 +128,6 @@ public class Protocol {
|
||||
public static final String serverRequestsHumanVote = "HVOTR";
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user