Further development of VoteHandler: extended humanVote method, added Logger statements and comments

This commit is contained in:
Alexander Sazonov 2022-04-07 11:59:51 +02:00
parent de69ea27bb
commit b470fc78a2
3 changed files with 61 additions and 31 deletions

View File

@ -23,15 +23,17 @@ public class VoteHandler {
public static final Logger LOGGER = LogManager.getLogger(); public static final Logger LOGGER = LogManager.getLogger();
public static final BudaLogConfig l = new BudaLogConfig(LOGGER); public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
/** /**
* TODO(Alex): Documentation * Handles the ghost vote during nighttime: passengers who are ghosts are being asked on who to
* @param passengers * ghostify, others are waiting. Results are being collected and the player with most votes is
* being ghostified.
*
* @param passengers: passengers on the train
*/ */
public void ghostVote(Passenger[] passengers, Game game) { public void ghostVote(Passenger[] passengers, Game game) {
// array to collect votes for all players during voting, i.e. votes for player 1 (passengers[0])
// array to collect votes for all players during voting, i.e. votes for player 1 (passengers[0]) are saved in // are saved in
// votesForPlayers[0] // votesForPlayers[0]
int[] votesForPlayers = new int[6]; int[] votesForPlayers = new int[6];
@ -42,7 +44,9 @@ public class VoteHandler {
LOGGER.info("Send msg to Ghost in Position: " + passenger); LOGGER.info("Send msg to Ghost in Position: " + passenger);
passenger.send("Vote on who to ghostify!"); passenger.send("Vote on who to ghostify!");
} else { } else {
passenger.send("Please wait, ghosts are active"); //TODO(Seraina): make sure whatever clients send in this time, except chat is ignored passenger.send(
"Please wait, ghosts are active"); // TODO(Seraina): make sure whatever clients send in
// this time, except chat is ignored
LOGGER.info("Send msg to Human in Position: " + passenger); LOGGER.info("Send msg to Human in Position: " + passenger);
} }
} }
@ -56,6 +60,7 @@ public class VoteHandler {
for (int i = 0; i < votesForPlayers.length; i++) { for (int i = 0; i < votesForPlayers.length; i++) {
if (passenger.getVote() == i) { if (passenger.getVote() == i) {
votesForPlayers[i]++; votesForPlayers[i]++;
LOGGER.info(passengers[i] + " has received the most votes");
} }
} }
} }
@ -64,9 +69,9 @@ public class VoteHandler {
// count the votes - determine which player has the most votes by going through the // count the votes - determine which player has the most votes by going through the
// votesForPlayers array // votesForPlayers array
int currentMax = 0; int currentMax = 0;
for (int i = 0; i < votesForPlayers.length; i++) { for (int votesForPlayer : votesForPlayers) {
if (votesForPlayers[i] > currentMax) { if (votesForPlayer > currentMax) {
currentMax = votesForPlayers[i]; currentMax = votesForPlayer;
} }
} }
LOGGER.info("Most votes" + currentMax); LOGGER.info("Most votes" + currentMax);
@ -74,25 +79,27 @@ public class VoteHandler {
// ghostify the player with most votes // ghostify the player with most votes
int ghostPosition = 0; int ghostPosition = 0;
for (int i = 0; i < votesForPlayers.length; i++) { for (int i = 0; i < votesForPlayers.length; i++) {
if (votesForPlayers[i] == currentMax) { // if player has most votes if (votesForPlayers[i] == currentMax) { // if player at position i has most votes
ghostPosition = i; ghostPosition = i;
LOGGER.info("Most votes for Passenger" + i); LOGGER.info("Most votes for Passenger" + i);
} }
} }
GhostifyHandler gh = new GhostifyHandler(); GhostifyHandler gh = new GhostifyHandler();
Ghost g = gh.ghost(passengers[ghostPosition],game); Ghost g = gh.ghost(passengers[ghostPosition], game);
passengers[ghostPosition] = g; passengers[ghostPosition] = g;
passengers[ghostPosition].send( passengers[ghostPosition].send(
"You are now a ghost!"); // TODO: ServerGameInfoHandler might deal with this one "You are now a ghost!"); // TODO: ServerGameInfoHandler might deal with this one
} }
/** /**
* TODO(Alex): Documentation * Handles the human vote during daytime. Asks human players to vote for a ghost to kick out while
* @param passengers * ghosts are waiting. Votes are being collected, vote results are being handled in three possible
* ways: if passenger who was voted for is a human, continue with next ghost vote; if it's a
* normal ghost, kick him off; if it's the OG ghost, end game, humans win.
*
* @param passengers: train passengers
*/ */
public void humanVote(Passenger[] passengers) { public void humanVote(Passenger[] passengers) {
// very similar to ghostVote, differs mostly in the way votes are handled
// array to collect votes for all players during voting, i.e. votes for player 1 are saved in // array to collect votes for all players during voting, i.e. votes for player 1 are saved in
// votesForPlayers[0] // votesForPlayers[0]
@ -126,6 +133,7 @@ public class VoteHandler {
for (int votesForPlayer : votesForPlayers) { for (int votesForPlayer : votesForPlayers) {
if (votesForPlayer > currentMax) { if (votesForPlayer > currentMax) {
currentMax = votesForPlayer; currentMax = votesForPlayer;
LOGGER.info("Max amount of votes: " + currentMax);
} }
} }
// deal with voting results // deal with voting results
@ -133,17 +141,39 @@ public class VoteHandler {
for (int i = 0; i < votesForPlayers.length; i++) { for (int i = 0; i < votesForPlayers.length; i++) {
if (votesForPlayers[i] == currentMax) { // if player has most votes if (votesForPlayers[i] == currentMax) { // if player has most votes
voteIndex = i; voteIndex = i;
LOGGER.info("Player " + voteIndex + "has the most votes");
} }
} }
if (!passengers[voteIndex].getIsGhost()) { // if player with most votes is human, notify everyone about it if (!passengers[voteIndex]
.getIsGhost()) { // if player with most votes is human, notify everyone about it
for (Passenger passenger : passengers) { for (Passenger passenger : passengers) {
passenger.send( passenger.send(
"You voted for a human!"); // TODO: ServerGameInfoHandler might be better to use here "You voted for a human!"); // TODO: ServerGameInfoHandler might be better to use here
} }
} }
if (passengers[voteIndex].getIsGhost()) { // if player is a ghost if (passengers[voteIndex].getIsGhost()) { // if player is a ghost
if (passengers[voteIndex].i if (passengers[voteIndex].getIsOG()) { // if ghost is OG --> end game, humans win
System.out.println("Game over: humans win!"); // TODO: correctly handle end of game
} else {
/* Special case: if ghost is not OG and if only one human is left (--> last human didn't vote for OG ghost),
ghosts win.
*/
int humans = 0; // variable to count the amount of humans left in the game
for (Passenger passenger : passengers) {
if (!passenger.getIsGhost()) {
humans++;
}
if (humans == 1) {
System.out.println("Game over: ghosts win!");
}
}
// Usual case: there is more than one human left and a normal ghost has been voted for -->
// kick this ghost off
passengers[voteIndex].setKickedOff(true);
for (Passenger passenger : passengers) {
passenger.send("Player " + voteIndex + "has been kicked off!");
}
}
} }
} }
} }

View File

@ -8,11 +8,9 @@ public class Ghost extends Passenger {
public static final Logger LOGGER = LogManager.getLogger(); public static final Logger LOGGER = LogManager.getLogger();
public static final BudaLogConfig l = new BudaLogConfig(LOGGER); public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
protected boolean isOG = false; //true if the Ghost is the original ghost false by default.
public boolean getIsOG() { public boolean getIsOG() {
return isOG; return isOG;
} }
public void setIsOG(boolean og) { isOG = og; }; public void setIsOG(boolean og) { isOG = og; }
} }

View File

@ -11,9 +11,10 @@ public class Passenger {
protected int position; //the player's Cabin number (0 to 5) protected int position; //the player's Cabin number (0 to 5)
protected String name; //the player's Name protected String name; //the player's Name
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 isPlayer; //same here protected boolean isOG = false; //true if the player is the original ghost, false by default.
protected Boolean kickedOff; //true if the player has been voted off. protected boolean isPlayer; //same here
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
protected int vote; //saves the number of the player this passenger voted for during voting (0-5) protected int vote; //saves the number of the player this passenger voted for during voting (0-5)
@ -25,8 +26,7 @@ public class Passenger {
**/ **/
public void send(String msg) { public void send(String msg) {
//todo(Seraina): send protocol message to the respective client OR process messages for NPCS //todo(Seraina): send protocol message to the respective client OR process messages for NPCS
int voteRandmom = (int) (Math.random() * 6); this.vote = (int) (Math.random() * 6);
this.vote = voteRandmom;
} }
/** /**
@ -74,21 +74,23 @@ public class Passenger {
return name; return name;
} }
public Boolean getIsGhost() { public boolean getIsGhost() {
return isGhost; return isGhost;
} }
public Boolean getKickedOff() { public boolean getIsOG() { return isOG; }
public boolean getKickedOff() {
return kickedOff; return kickedOff;
} }
public Boolean getIsPlayer() { public boolean getIsPlayer() {
return isPlayer; return isPlayer;
} }
public boolean getHasVoted() { return hasVoted; }; // returns true if player already voted during a voting public boolean getHasVoted() { return hasVoted; } // returns true if player already voted during a voting
public int getVote() { return vote; } public int getVote() { return vote; } // returns who this passenger voted for during a voting
public ClientHandler getClientHandler() { public ClientHandler getClientHandler() {
return clientHandler; return clientHandler;