Worked on game logic:
-Ensured that votes of kicked off people don't count - Ensured that kicked off people don't get reentered in the game via gostification - fleshed out the Spectator class a litte
This commit is contained in:
parent
d389a8fbb0
commit
22715e038d
@ -94,8 +94,9 @@ public class GameState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Changes the name of the passenger in the Array that has the oldName
|
||||||
*
|
* @param oldName the old Name of the Passenger to be name-changed
|
||||||
|
* @param newName the new name for the Passenger
|
||||||
*/
|
*/
|
||||||
public void changeUsername(String oldName, String newName){
|
public void changeUsername(String oldName, String newName){
|
||||||
for (Passenger passenger : passengerTrain) {
|
for (Passenger passenger : passengerTrain) {
|
||||||
@ -107,7 +108,8 @@ public class GameState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Replaces a disconnected Clients with an NPC
|
||||||
|
* @param disconnectedClient the ClientHandler handling the disconnected client
|
||||||
*/
|
*/
|
||||||
public void handleClientDisconnect(ClientHandler disconnectedClient) {
|
public void handleClientDisconnect(ClientHandler disconnectedClient) {
|
||||||
for(Passenger passenger : passengerTrain) {
|
for(Passenger passenger : passengerTrain) {
|
||||||
@ -128,8 +130,6 @@ public class GameState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Takes a given Passenger and puts it into the passengerTrain at a certain position
|
* Takes a given Passenger and puts it into the passengerTrain at a certain position
|
||||||
* @param passenger the new passenger being put into the train
|
* @param passenger the new passenger being put into the train
|
||||||
@ -151,19 +151,19 @@ public class GameState {
|
|||||||
String[] print = new String[6];
|
String[] print = new String[6];
|
||||||
for (int i = 0; i < array.length; i++) {
|
for (int i = 0; i < array.length; i++) {
|
||||||
if (array[i].getKickedOff()) {
|
if (array[i].getKickedOff()) {
|
||||||
print[i] = "| kicked off: " + array[i].getPosition() + " |";
|
print[i] = "| " + array[i].getName() + ", kicked off: " + array[i].getPosition() + " |";
|
||||||
} else {
|
} else {
|
||||||
if (array[i].getIsPlayer()) {
|
if (array[i].getIsPlayer()) {
|
||||||
if (array[i].getIsGhost()) {
|
if (array[i].getIsGhost()) {
|
||||||
print[i] = "| ghostPlayer: " + array[i].getPosition() + " |";
|
print[i] = "| " + array[i].getName() + "(ghostPlayer): " + array[i].getPosition() + " |";
|
||||||
} else {
|
} else {
|
||||||
print[i] = "-| humanPlayer: " + array[i].getPosition() + " |";
|
print[i] = "| " + array[i].getName() + "(humanPlayer): " + array[i].getPosition() + " |";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (array[i].getIsGhost()) {
|
if (array[i].getIsGhost()) {
|
||||||
print[i] = "| ghostNPC: " + array[i].getPosition() + " |";
|
print[i] = "| " + array[i].getName() + "(ghostNPC): " + array[i].getPosition() + " |";
|
||||||
} else {
|
} else {
|
||||||
print[i] = "| humanNPC: " + array[i].getPosition() + " |";
|
print[i] = "| " + array[i].getName() + "(humanNPC): " + array[i].getPosition() + " |";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,7 +19,7 @@ public class GhostifyHandler {
|
|||||||
* @param p Passenger to be ghostified
|
* @param p Passenger to be ghostified
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public static Ghost ghost(Passenger p, Game game) {
|
public static Passenger ghost(Passenger p, Game game) {
|
||||||
LOGGER.debug("Passenger Position " + p.getPosition());
|
LOGGER.debug("Passenger Position " + p.getPosition());
|
||||||
Ghost g;
|
Ghost g;
|
||||||
if (p.getIsPlayer()) {
|
if (p.getIsPlayer()) {
|
||||||
@ -36,7 +36,9 @@ public class GhostifyHandler {
|
|||||||
ghostNPC.setGhost();
|
ghostNPC.setGhost();
|
||||||
ghostNPC.setPosition(p.getPosition());
|
ghostNPC.setPosition(p.getPosition());
|
||||||
g = ghostNPC;
|
g = ghostNPC;
|
||||||
|
}
|
||||||
|
if (p.getKickedOff()) {
|
||||||
|
return p;
|
||||||
}
|
}
|
||||||
game.gameState.addNewPassenger(g, g.getPosition());
|
game.gameState.addNewPassenger(g, g.getPosition());
|
||||||
LOGGER.info("Passenger at position " + p.getPosition() + " has been ghostified");
|
LOGGER.info("Passenger at position " + p.getPosition() + " has been ghostified");
|
||||||
|
|||||||
@ -22,7 +22,7 @@ public class NoiseHandler {
|
|||||||
* @param victim human player who has been turned into a ghost this night
|
* @param victim human player who has been turned into a ghost this night
|
||||||
* @param game current game instance
|
* @param game current game instance
|
||||||
*/
|
*/
|
||||||
public void noiseNotifier(Passenger[] passengers, Passenger predator, Ghost victim, Game game) {
|
public void noiseNotifier(Passenger[] passengers, Passenger predator, Passenger victim, Game game) {
|
||||||
if (predator.getPosition() - victim.getPosition()
|
if (predator.getPosition() - victim.getPosition()
|
||||||
> 0) { // if predator is to the right of victim
|
> 0) { // if predator is to the right of victim
|
||||||
for (int i = predator.getPosition() - 1; i > victim.getPosition(); i--) {
|
for (int i = predator.getPosition() - 1; i > victim.getPosition(); i--) {
|
||||||
|
|||||||
@ -43,7 +43,7 @@ public class ServerGameInfoHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* //TODO(Seraina): implementation
|
* //TODO(Seraina): Smart implementation that sends all relevant things to spectator, so they won't get bored
|
||||||
* Formartiert Nachrichten die für einen Spectator gedacht sind.
|
* Formartiert Nachrichten die für einen Spectator gedacht sind.
|
||||||
* @param msg the message to be formatted
|
* @param msg the message to be formatted
|
||||||
* @param passenger the passenger getting the message
|
* @param passenger the passenger getting the message
|
||||||
@ -51,6 +51,17 @@ public class ServerGameInfoHandler {
|
|||||||
* @return a message in a protocol format
|
* @return a message in a protocol format
|
||||||
*/
|
*/
|
||||||
public static String spectatorFormat(String msg, Passenger passenger, Game game) {
|
public static String spectatorFormat(String msg, Passenger passenger, Game game) {
|
||||||
|
switch (msg) {
|
||||||
|
case ClientGameInfoHandler.ghostVoteRequest:
|
||||||
|
msg = Protocol.printToClientConsole + "$Ghosts are voting: " + game.gameState.toString();
|
||||||
|
break;
|
||||||
|
case ClientGameInfoHandler.humanVoteRequest:
|
||||||
|
msg = Protocol.printToClientConsole + "$Humans are voting:" + game.gameState.toString();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
msg = Protocol.printToClientConsole + "$"+ msg;
|
||||||
|
}
|
||||||
|
LOGGER.debug(msg);
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -72,7 +72,7 @@ public class VoteHandler {
|
|||||||
}
|
}
|
||||||
LOGGER.info("Most votes for: " + ghostPosition);
|
LOGGER.info("Most votes for: " + ghostPosition);
|
||||||
|
|
||||||
Ghost g = GhostifyHandler.ghost(passengers[ghostPosition], game);
|
Passenger g = GhostifyHandler.ghost(passengers[ghostPosition], game);
|
||||||
passengers[ghostPosition] = g;
|
passengers[ghostPosition] = g;
|
||||||
passengers[ghostPosition].send(
|
passengers[ghostPosition].send(
|
||||||
ClientGameInfoHandler.youGotGhostyfied, game);
|
ClientGameInfoHandler.youGotGhostyfied, game);
|
||||||
@ -106,6 +106,7 @@ public class VoteHandler {
|
|||||||
// set hasVoted to false for all passengers for future votings
|
// set hasVoted to false for all passengers for future votings
|
||||||
for (Passenger passenger : passengers) {
|
for (Passenger passenger : passengers) {
|
||||||
passenger.setHasVoted(false);
|
passenger.setHasVoted(false);
|
||||||
|
passenger.setVote(Integer.MAX_VALUE);
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
@ -188,6 +189,7 @@ public class VoteHandler {
|
|||||||
// set hasVoted to false for all passengers for future voting
|
// set hasVoted to false for all passengers for future voting
|
||||||
for (Passenger passenger : passengers) {
|
for (Passenger passenger : passengers) {
|
||||||
passenger.setHasVoted(false);
|
passenger.setHasVoted(false);
|
||||||
|
passenger.setVote(Integer.MAX_VALUE);
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|||||||
@ -33,9 +33,16 @@ public class GhostNPC extends Ghost {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends the message to the ghostNpcParser, if ghost has not been kicked off
|
||||||
|
* @param msg the message that is sent to this npc.
|
||||||
|
* @param game the game this npc is in
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void send(String msg, Game game) {
|
public void send(String msg, Game game) {
|
||||||
ServerGameInfoHandler.ghostNpcParser(this, msg, game);
|
if (!getKickedOff()) {
|
||||||
|
ServerGameInfoHandler.ghostNpcParser(this, msg, game);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -47,7 +54,7 @@ public class GhostNPC extends Ghost {
|
|||||||
int ghostCounter = 0;
|
int ghostCounter = 0;
|
||||||
Passenger[] train = game.getGameState().getPassengerTrain();
|
Passenger[] train = game.getGameState().getPassengerTrain();
|
||||||
for (Passenger passenger : train) {
|
for (Passenger passenger : train) {
|
||||||
if (passenger.isGhost) {
|
if (passenger.isGhost && !passenger.getKickedOff()) {
|
||||||
ghostCounter++;
|
ghostCounter++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -55,10 +62,9 @@ public class GhostNPC extends Ghost {
|
|||||||
if (humanPositions.length == 0) {
|
if (humanPositions.length == 0) {
|
||||||
vote = Integer.MAX_VALUE;
|
vote = Integer.MAX_VALUE;
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
int j = 0;
|
int j = 0;
|
||||||
for (int i = 0; i < train.length; i++) {
|
for (int i = 0; i < train.length; i++) {
|
||||||
if (!train[i].isGhost) { //is human
|
if (!train[i].isGhost && !train[i].getKickedOff()) { //is human
|
||||||
humanPositions[j] = train[i].getPosition();
|
humanPositions[j] = train[i].getPosition();
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -35,7 +35,6 @@ public class GhostPlayer extends Ghost {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a message to the client handled bye this client handler
|
* Sends a message to the client handled bye this client handler
|
||||||
* TODO: does this also work with 2 clients?
|
|
||||||
* @param msg the message that is sent to this player.
|
* @param msg the message that is sent to this player.
|
||||||
* @param game the game the GhostPlayer lives on (in game.gameState.passengerTrain)
|
* @param game the game the GhostPlayer lives on (in game.gameState.passengerTrain)
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -31,14 +31,16 @@ public class HumanNPC extends Human {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a msg to the ServerGameInfoHandler.humanNpcParser to decide what has to happen now
|
* Sends a msg to the ServerGameInfoHandler.humanNpcParser to decide what has to happen now, if the
|
||||||
*
|
* npc hasn't been kicked off 8(should never happen to a human though)
|
||||||
* @param msg the message that is sent to this player.
|
* @param msg the message that is sent to this player.
|
||||||
* @param game the game the HumanNPC lives on (in game.gameState.passengerTrain)
|
* @param game the game the HumanNPC lives on (in game.gameState.passengerTrain)
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void send(String msg, Game game) {
|
public void send(String msg, Game game) {
|
||||||
ServerGameInfoHandler.humanNpcParser(this, msg, game);
|
if (!getKickedOff()) {
|
||||||
|
ServerGameInfoHandler.humanNpcParser(this, msg, game);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -17,6 +17,7 @@ public class Passenger {
|
|||||||
protected boolean isGhost; //boolean regarding if the player is a ghost. Could probably be removed since ghost is a subclass but I'm keeping it in.
|
protected boolean isGhost; //boolean regarding if the player is a ghost. Could probably be removed since ghost is a subclass but I'm keeping it in.
|
||||||
protected boolean isOG = false; //true if the player is the original ghost, false by default.
|
protected boolean isOG = false; //true if the player is the original ghost, false by default.
|
||||||
protected boolean isPlayer; //same here
|
protected boolean isPlayer; //same here
|
||||||
|
protected boolean isSpectator = false; //true if a player is a spectator
|
||||||
protected boolean kickedOff; //true if the player has been voted off
|
protected boolean kickedOff; //true if the player has been voted off
|
||||||
protected ClientHandler clientHandler;//the socket for the client associated with this Passenger, for NPCs, this can be null.
|
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 boolean hasVoted; //true if the player gave his vote during voting time
|
||||||
@ -50,27 +51,50 @@ public class Passenger {
|
|||||||
this.kickedOff = kickedOff;
|
this.kickedOff = kickedOff;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets boolean isGhost to true
|
||||||
|
*/
|
||||||
public void setGhost() {
|
public void setGhost() {
|
||||||
// changes this passenger's status from human to ghost
|
// changes this passenger's status from human to ghost
|
||||||
isGhost = true;
|
isGhost = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets hasVoted of this Passenger, should be true if the passenger has voted
|
||||||
|
* @param voted the boolean either true or false
|
||||||
|
*/
|
||||||
public void setHasVoted(boolean voted) {
|
public void setHasVoted(boolean voted) {
|
||||||
// used to signal that this passenger voted during a voting
|
// used to signal that this passenger voted during a voting
|
||||||
hasVoted = voted;
|
hasVoted = voted;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the vote of this Passenger to:
|
||||||
|
* @param vote vote for a position, an integer
|
||||||
|
*/
|
||||||
public void setVote(int vote) {
|
public void setVote(int vote) {
|
||||||
this.vote = vote;
|
this.vote = vote;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the isOg boolean to true
|
||||||
|
*/
|
||||||
public void setIsOg() {
|
public void setIsOg() {
|
||||||
isOG = true;
|
isOG = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the isPlayer to either true or false, should be set to true if Passneger is a Player (not an NPC)
|
||||||
|
* @param player the boolean
|
||||||
|
*/
|
||||||
public void setPlayer(boolean player) {
|
public void setPlayer(boolean player) {
|
||||||
isPlayer = player;
|
isPlayer = player;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the clientHandler of this passenger to the specified clientHandler
|
||||||
|
* @param clientHandler the specified clientHamdler
|
||||||
|
*/
|
||||||
public void setClientHandler(ClientHandler clientHandler) {
|
public void setClientHandler(ClientHandler clientHandler) {
|
||||||
this.clientHandler = clientHandler;
|
this.clientHandler = clientHandler;
|
||||||
}
|
}
|
||||||
@ -84,16 +108,32 @@ public class Passenger {
|
|||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* true if passenger is a ghost, false if passenger is a human
|
||||||
|
* @return the boolean
|
||||||
|
*/
|
||||||
public boolean getIsGhost() {
|
public boolean getIsGhost() {
|
||||||
return isGhost;
|
return isGhost;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* true if passenger is the OG ghost
|
||||||
|
* @return the boolean
|
||||||
|
*/
|
||||||
public boolean getIsOG() { return isOG; }
|
public boolean getIsOG() { return isOG; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* true if passenger has been voted off, false if passenger is still in the game
|
||||||
|
* @return the boolean
|
||||||
|
*/
|
||||||
public boolean getKickedOff() {
|
public boolean getKickedOff() {
|
||||||
return kickedOff;
|
return kickedOff;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* true if passenger is a Player, false if passenger is an NPC
|
||||||
|
* @return the boolean
|
||||||
|
*/
|
||||||
public boolean getIsPlayer() {
|
public boolean getIsPlayer() {
|
||||||
return isPlayer;
|
return isPlayer;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,10 +20,12 @@ public class Spectator extends Passenger{
|
|||||||
isGhost = false;
|
isGhost = false;
|
||||||
isPlayer = true;
|
isPlayer = true;
|
||||||
kickedOff = true;
|
kickedOff = true;
|
||||||
|
isSpectator = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void send(String msg, Game game) {
|
public void send(String msg, Game game) {
|
||||||
clientHandler.sendMsgToClient(ServerGameInfoHandler.format(msg, this, game));
|
clientHandler.sendMsgToClient(ServerGameInfoHandler.spectatorFormat(msg, this, game));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user