From 19c31356c243b5468ec273bcbcba41ad00a6bc8c Mon Sep 17 00:00:00 2001 From: Seraina Date: Mon, 4 Apr 2022 13:48:45 +0200 Subject: [PATCH] Some changes to Structure of VoteHandler and related classes --- .../dbis/cs108/gamelogic/GameFunctions.java | 2 +- .../dbis/cs108/gamelogic/GhostifyHandler.java | 11 +++--- .../dmi/dbis/cs108/gamelogic/VoteHandler.java | 38 ++++++++++++------- .../gamelogic/klassenstruktur/Ghost.java | 2 +- 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GameFunctions.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GameFunctions.java index c2723c3..556635c 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GameFunctions.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GameFunctions.java @@ -17,7 +17,7 @@ public class GameFunctions { int nrOfGhosts; // sets how many Ghosts we start witch int nrOfUsers; // safes how many clients are active in this Game Train train; // safes who sits where - Passenger[] passengerTrain; + public Passenger[] passengerTrain; /** * Constructs a GameFunctions instance where nrOfPlayers >= nrOfUsers. Fills passengerTrain with diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GhostifyHandler.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GhostifyHandler.java index 25395b0..553ad19 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GhostifyHandler.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GhostifyHandler.java @@ -12,15 +12,16 @@ public class GhostifyHandler { */ private static int ghostifyCallCounter = -1; - public GhostPlayer ghost(Passenger p) { + public GhostPlayer ghost(Passenger p, Game game) { p.setGhost(); + GhostPlayer g; ghostifyCallCounter++; if (ghostifyCallCounter == 0) { - GhostPlayer g = new GhostPlayer(p.getPosition(), p.getName(), p.getClientHandler(), true); - return g; + g = new GhostPlayer(p.getPosition(), p.getName(), p.getClientHandler(), true); } else { - GhostPlayer g = new GhostPlayer(p.getPosition(), p.getName(), p.getClientHandler(), false); - return g; + g = new GhostPlayer(p.getPosition(), p.getName(), p.getClientHandler(), false); } + game.gameFunctions.passengerTrain[g.getPosition()] = g; + return g; } } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java index 6f55447..1efddc7 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java @@ -1,5 +1,6 @@ package ch.unibas.dmi.dbis.cs108.gamelogic; +import ch.unibas.dmi.dbis.cs108.BudaLogConfig; import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Ghost; import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.GhostPlayer; import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger; @@ -22,11 +23,13 @@ public class VoteHandler { public static final Logger LOGGER = LogManager.getLogger(); public static final BudaLogConfig l = new BudaLogConfig(LOGGER); + /** * TODO(Alex): Documentation * @param passengers */ - public void ghostVote(Passenger[] passengers) { + public void ghostVote(Passenger[] passengers, Game game) { + // array to collect votes for all players during voting, i.e. votes for player 1 (passengers[0]) are saved in // votesForPlayers[0] @@ -69,15 +72,19 @@ public class VoteHandler { LOGGER.info("Most votes" + currentMax); // ghostify the player with most votes + int ghostPosition = 0; for (int i = 0; i < votesForPlayers.length; i++) { if (votesForPlayers[i] == currentMax) { // if player has most votes + ghostPosition = i; LOGGER.info("Most votes for Passenger" + i); - GhostifyHandler gh = new GhostifyHandler(); - Ghost g = gh.ghost(passengers[i]); - passengers[i].send( - "You are now a ghost!"); // TODO: ServerGameInfoHandler might deal with this one + } } + GhostifyHandler gh = new GhostifyHandler(); + Ghost g = gh.ghost(passengers[ghostPosition],game); + passengers[ghostPosition] = g; + passengers[ghostPosition].send( + "You are now a ghost!"); // TODO: ServerGameInfoHandler might deal with this one } /** @@ -122,18 +129,21 @@ public class VoteHandler { } } // deal with voting results + int voteIndex = 0; for (int i = 0; i < votesForPlayers.length; i++) { if (votesForPlayers[i] == currentMax) { // if player has most votes - if (!passengers[i].getIsGhost()) { // if player with most votes is human, notify everyone about it - for (Passenger passenger : passengers) { - passenger.send( - "You voted for a human!"); // TODO: ServerGameInfoHandler might be better to use here - } - } - if (passengers[i].getIsGhost()) { // if player is a ghost - if ( - } + voteIndex = i; + } } + if (!passengers[voteIndex].getIsGhost()) { // if player with most votes is human, notify everyone about it + for (Passenger passenger : passengers) { + passenger.send( + "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].i + } } } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Ghost.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Ghost.java index edd6967..4456946 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Ghost.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Ghost.java @@ -8,7 +8,7 @@ public class Ghost extends Passenger { public static final Logger LOGGER = LogManager.getLogger(); public static final BudaLogConfig l = new BudaLogConfig(LOGGER); - protected boolean isOG; //true if the Ghost is the original ghost. + protected boolean isOG = false; //true if the Ghost is the original ghost false by default. public boolean getIsOG() { return isOG;