Worked on game logic:

- Spectators now get all information
- votes for kicked off people don't count
This commit is contained in:
Seraina 2022-04-16 13:13:20 +02:00
parent 22715e038d
commit b76d57a407
8 changed files with 48 additions and 16 deletions

View File

@ -24,10 +24,12 @@ public class ClientGameInfoHandler {
//just messages //just messages
public static final String itsNightTime = "Please wait, ghosts are active"; public static final String itsNightTime = "Please wait, ghosts are active";
public static final String youGotGhostyfied = "You are now a ghost!"; public static final String youGotGhostyfied = "You are now a ghost!";
public static final String youGotKickedOff = "Bye bye - you've been kicked off";
public static final String itsDayTime = "Please wait, humans are active"; public static final String itsDayTime = "Please wait, humans are active";
public static final String humansVotedFor = "Humans voted for: "; public static final String humansVotedFor = "Humans voted for: ";
public static final String isAHuman = " but they're a human!"; public static final String isAHuman = " but they're a human!";
public static final String gotKickedOff = " is a Ghost and got kicked off"; public static final String gotKickedOff = " is a Ghost and got kicked off";
public static final String gotGhostyfied = " is now also a ghost!";
} }

View File

@ -105,7 +105,7 @@ public class Game implements Runnable {
while (true) { while (true) {
if (!isDay) { if (!isDay) {
LOGGER.info("NIGHT"); LOGGER.info("NIGHT");
voteHandler.ghostVote(gameState.getPassengerTrain(), this); gameOverCheck = voteHandler.ghostVote(gameState.getPassengerTrain(), this);
setDay(true); setDay(true);
} else { } else {
LOGGER.info("DAY"); LOGGER.info("DAY");

View File

@ -53,9 +53,11 @@ public class ServerGameInfoHandler {
public static String spectatorFormat(String msg, Passenger passenger, Game game) { public static String spectatorFormat(String msg, Passenger passenger, Game game) {
switch (msg) { switch (msg) {
case ClientGameInfoHandler.ghostVoteRequest: case ClientGameInfoHandler.ghostVoteRequest:
case ClientGameInfoHandler.itsNightTime:
msg = Protocol.printToClientConsole + "$Ghosts are voting: " + game.gameState.toString(); msg = Protocol.printToClientConsole + "$Ghosts are voting: " + game.gameState.toString();
break; break;
case ClientGameInfoHandler.humanVoteRequest: case ClientGameInfoHandler.humanVoteRequest:
case ClientGameInfoHandler.itsDayTime:
msg = Protocol.printToClientConsole + "$Humans are voting:" + game.gameState.toString(); msg = Protocol.printToClientConsole + "$Humans are voting:" + game.gameState.toString();
break; break;
default: default:
@ -120,11 +122,8 @@ public class ServerGameInfoHandler {
game.getLobby().getAdmin().broadcastNpcChatMessageToLobby(outMsg); game.getLobby().getAdmin().broadcastNpcChatMessageToLobby(outMsg);
break; break;
case ClientGameInfoHandler.humanVoteRequest: case ClientGameInfoHandler.humanVoteRequest:
npc.vote(); npc.vote(game);
} }
} }
} }

View File

@ -41,7 +41,6 @@ public class VoteHandler {
// Walk through entire train, ask ghosts to ghostify and humans to wait // Walk through entire train, ask ghosts to ghostify and humans to wait
for (Passenger passenger : passengers) { for (Passenger passenger : passengers) {
if (passenger.getIsGhost()) { if (passenger.getIsGhost()) {
passenger.send(ClientGameInfoHandler.ghostVoteRequest, game); passenger.send(ClientGameInfoHandler.ghostVoteRequest, game);
} else { } else {
passenger.send( passenger.send(
@ -67,15 +66,21 @@ public class VoteHandler {
if (votesForPlayers[i] == currentMax) { // if player at position i has most votes if (votesForPlayers[i] == currentMax) { // if player at position i has most votes
ghostPosition = i; ghostPosition = i;
LOGGER.debug("Most votes for Passenger " + i); LOGGER.debug("Most votes for Passenger " + i);
} }
} }
LOGGER.info("Most votes for: " + ghostPosition); LOGGER.info("Most votes for: " + ghostPosition);
for(Passenger passenger : passengers) {
if(passenger.getIsGhost() || passenger.getIsSpectator()) {
passenger.send(passengers[ghostPosition].getName() + ClientGameInfoHandler.gotGhostyfied, game);
}
}
Passenger g = GhostifyHandler.ghost(passengers[ghostPosition], game); Passenger g = GhostifyHandler.ghost(passengers[ghostPosition], game);
passengers[ghostPosition] = g; passengers[ghostPosition] = g;
passengers[ghostPosition].send( if (!passengers[ghostPosition].getIsSpectator()) {
ClientGameInfoHandler.youGotGhostyfied, game); passengers[ghostPosition].send(
ClientGameInfoHandler.youGotGhostyfied, game);
}
try { // waits 20 seconds before votes get collected try { // waits 20 seconds before votes get collected
Thread.sleep(10); Thread.sleep(10);
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -180,8 +185,9 @@ public class VoteHandler {
// Usual case: there is more than one human left and a normal ghost has been voted for --> // Usual case: there is more than one human left and a normal ghost has been voted for -->
// kick this ghost off // kick this ghost off
passengers[voteIndex] = GhostifyHandler.kickOff(passengers[voteIndex], game); passengers[voteIndex] = GhostifyHandler.kickOff(passengers[voteIndex], game);
passengers[voteIndex].send(ClientGameInfoHandler.youGotKickedOff, game);
for (Passenger passenger : passengers) { for (Passenger passenger : passengers) {
passenger.send("Player " + voteIndex + ClientGameInfoHandler.gotKickedOff, game); passenger.send(passengers[voteIndex].getName() + ClientGameInfoHandler.gotKickedOff, game);
} }
} }
} }

View File

@ -58,7 +58,8 @@ public class GhostPlayer extends Ghost {
/* /*
* if vote wasn't valid, make sure, the passenger field hasVoted == false, probably redundant but better be safe than sorry * if vote wasn't valid, make sure, the passenger field hasVoted == false, probably redundant but better be safe than sorry
*/ */
if(vote == Integer.MAX_VALUE) { if(vote == Integer.MAX_VALUE|| game.getGameState().getPassengerTrain()[vote].getKickedOff()) {
send("Your vote was invalid", game);
hasVoted = false; hasVoted = false;
} }
} }

View File

@ -44,13 +44,29 @@ public class HumanNPC extends Human {
} }
/** /**
* Currently returns a random integer for voting * Currently returns a random integer for voting, but only for passengers that haven't been
* kicked off yet
* *
* @return integer between 0 and 5 * @return integer between 0 and 5
*/ */
public void vote() { public void vote(Game game) {
int randomNr = (int) (Math.random() * 6); Passenger[] passengers = game.getGameState().getPassengerTrain();
vote = randomNr; int kickedOffCounter = 0;
for(Passenger passenger : passengers) {
if(passenger.getKickedOff()) {
kickedOffCounter++;
}
}
int[] inGamePositions = new int[passengers.length - kickedOffCounter];
int i = 0;
for(Passenger passenger : passengers) {
if(!passenger.getKickedOff()) {
inGamePositions[i] = passenger.getPosition();
i++;
}
}
int randomNr = (int) (Math.random() * inGamePositions.length);
vote = inGamePositions[randomNr];
hasVoted = true; hasVoted = true;
LOGGER.info("HumanNPC at Position: " + this.getPosition() + " has voted for: " + vote); LOGGER.info("HumanNPC at Position: " + this.getPosition() + " has voted for: " + vote);
} }

View File

@ -62,7 +62,8 @@ public class HumanPlayer extends Human {
/* /*
* if vote wasn't valid, make sure, the passenger field hasVoted == false, probably redundant but better be safe than sorry * if vote wasn't valid, make sure, the passenger field hasVoted == false, probably redundant but better be safe than sorry
*/ */
if (vote == Integer.MAX_VALUE) { if (vote == Integer.MAX_VALUE || game.getGameState().getPassengerTrain()[vote].getKickedOff()) {
send("Your vote was invalid", game);
hasVoted = false; hasVoted = false;
} }
} }

View File

@ -122,6 +122,13 @@ public class Passenger {
*/ */
public boolean getIsOG() { return isOG; } public boolean getIsOG() { return isOG; }
/**
* true if passenger is a spectator
*/
public boolean getIsSpectator() {
return isSpectator;
}
/** /**
* true if passenger has been voted off, false if passenger is still in the game * true if passenger has been voted off, false if passenger is still in the game
* @return the boolean * @return the boolean