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:
Seraina 2022-04-16 12:06:58 +02:00
parent d389a8fbb0
commit 22715e038d
10 changed files with 88 additions and 24 deletions

View File

@ -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){
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) {
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
* @param passenger the new passenger being put into the train
@ -151,19 +151,19 @@ public class GameState {
String[] print = new String[6];
for (int i = 0; i < array.length; i++) {
if (array[i].getKickedOff()) {
print[i] = "| kicked off: " + array[i].getPosition() + " |";
print[i] = "| " + array[i].getName() + ", kicked off: " + array[i].getPosition() + " |";
} else {
if (array[i].getIsPlayer()) {
if (array[i].getIsGhost()) {
print[i] = "| ghostPlayer: " + array[i].getPosition() + " |";
print[i] = "| " + array[i].getName() + "(ghostPlayer): " + array[i].getPosition() + " |";
} else {
print[i] = "-| humanPlayer: " + array[i].getPosition() + " |";
print[i] = "| " + array[i].getName() + "(humanPlayer): " + array[i].getPosition() + " |";
}
} else {
if (array[i].getIsGhost()) {
print[i] = "| ghostNPC: " + array[i].getPosition() + " |";
print[i] = "| " + array[i].getName() + "(ghostNPC): " + array[i].getPosition() + " |";
} else {
print[i] = "| humanNPC: " + array[i].getPosition() + " |";
print[i] = "| " + array[i].getName() + "(humanNPC): " + array[i].getPosition() + " |";
}
}
}

View File

@ -19,7 +19,7 @@ public class GhostifyHandler {
* @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());
Ghost g;
if (p.getIsPlayer()) {
@ -36,7 +36,9 @@ public class GhostifyHandler {
ghostNPC.setGhost();
ghostNPC.setPosition(p.getPosition());
g = ghostNPC;
}
if (p.getKickedOff()) {
return p;
}
game.gameState.addNewPassenger(g, g.getPosition());
LOGGER.info("Passenger at position " + p.getPosition() + " has been ghostified");

View File

@ -22,7 +22,7 @@ public class NoiseHandler {
* @param victim human player who has been turned into a ghost this night
* @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()
> 0) { // if predator is to the right of victim
for (int i = predator.getPosition() - 1; i > victim.getPosition(); i--) {

View File

@ -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.
* @param msg the message to be formatted
* @param passenger the passenger getting the message
@ -51,6 +51,17 @@ public class ServerGameInfoHandler {
* @return a message in a protocol format
*/
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;
}

View File

@ -72,7 +72,7 @@ public class VoteHandler {
}
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].send(
ClientGameInfoHandler.youGotGhostyfied, game);
@ -106,6 +106,7 @@ public class VoteHandler {
// set hasVoted to false for all passengers for future votings
for (Passenger passenger : passengers) {
passenger.setHasVoted(false);
passenger.setVote(Integer.MAX_VALUE);
}
return "";
}
@ -188,6 +189,7 @@ public class VoteHandler {
// set hasVoted to false for all passengers for future voting
for (Passenger passenger : passengers) {
passenger.setHasVoted(false);
passenger.setVote(Integer.MAX_VALUE);
}
return "";
}

View File

@ -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
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;
Passenger[] train = game.getGameState().getPassengerTrain();
for (Passenger passenger : train) {
if (passenger.isGhost) {
if (passenger.isGhost && !passenger.getKickedOff()) {
ghostCounter++;
}
}
@ -55,10 +62,9 @@ public class GhostNPC extends Ghost {
if (humanPositions.length == 0) {
vote = Integer.MAX_VALUE;
} else {
int j = 0;
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();
j++;
}

View File

@ -35,7 +35,6 @@ public class GhostPlayer extends Ghost {
/**
* 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 game the game the GhostPlayer lives on (in game.gameState.passengerTrain)
*/

View File

@ -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 game the game the HumanNPC lives on (in game.gameState.passengerTrain)
*/
@Override
public void send(String msg, Game game) {
ServerGameInfoHandler.humanNpcParser(this, msg, game);
if (!getKickedOff()) {
ServerGameInfoHandler.humanNpcParser(this, msg, game);
}
}
/**

View File

@ -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 isOG = false; //true if the player is the original ghost, false by default.
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 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
@ -50,27 +51,50 @@ public class Passenger {
this.kickedOff = kickedOff;
}
/**
* Sets boolean isGhost to true
*/
public void setGhost() {
// changes this passenger's status from human to ghost
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) {
// used to signal that this passenger voted during a voting
hasVoted = voted;
}
/**
* Sets the vote of this Passenger to:
* @param vote vote for a position, an integer
*/
public void setVote(int vote) {
this.vote = vote;
}
/**
* Sets the isOg boolean to true
*/
public void setIsOg() {
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) {
isPlayer = player;
}
/**
* Sets the clientHandler of this passenger to the specified clientHandler
* @param clientHandler the specified clientHamdler
*/
public void setClientHandler(ClientHandler clientHandler) {
this.clientHandler = clientHandler;
}
@ -84,16 +108,32 @@ public class Passenger {
return name;
}
/**
* true if passenger is a ghost, false if passenger is a human
* @return the boolean
*/
public boolean getIsGhost() {
return isGhost;
}
/**
* true if passenger is the OG ghost
* @return the boolean
*/
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() {
return kickedOff;
}
/**
* true if passenger is a Player, false if passenger is an NPC
* @return the boolean
*/
public boolean getIsPlayer() {
return isPlayer;
}

View File

@ -20,10 +20,12 @@ public class Spectator extends Passenger{
isGhost = false;
isPlayer = true;
kickedOff = true;
isSpectator = true;
}
@Override
public void send(String msg, Game game) {
clientHandler.sendMsgToClient(ServerGameInfoHandler.format(msg, this, game));
clientHandler.sendMsgToClient(ServerGameInfoHandler.spectatorFormat(msg, this, game));
}
}