diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/ClientVoteData.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/ClientVoteData.java index 1471e20..ba93e85 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/ClientVoteData.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/ClientVoteData.java @@ -60,4 +60,5 @@ public class ClientVoteData { LOGGER.warn("Position is:" + position); } } + } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GameState.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GameState.java index 3e3c9da..9dd478d 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GameState.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/GameState.java @@ -92,13 +92,13 @@ public class GameState { } /** - * Collects the current position of all ghosts and saves them in an array + * Collects the current position of all not kicked off ghosts and saves them in an array * @return Boolean array, true if there is a ghost at that position */ public boolean[] getPositionOfGhosts(){ boolean[] ghosts = new boolean[passengerTrain.length]; for(int i = 0; i < passengerTrain.length; i++) { - if(passengerTrain[i].getIsGhost()) { + if(passengerTrain[i].getIsGhost() && !passengerTrain[i].getKickedOff()) { ghosts[i] = true; } } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Timer.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Timer.java new file mode 100644 index 0000000..74072f4 --- /dev/null +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Timer.java @@ -0,0 +1,75 @@ +package ch.unibas.dmi.dbis.cs108.gamelogic; + +import ch.unibas.dmi.dbis.cs108.BudaLogConfig; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +/** + * A class that handles all timed events in the game, such as vote times + */ +public class Timer { + public static final Logger LOGGER = LogManager.getLogger(Timer.class); + public static final BudaLogConfig l = new BudaLogConfig(LOGGER); + + /** + * The maximum length of the ghost vote in the night, in seconds + */ + public static final int ghostVote = 30; + /** + * The maximum length of the human vote in the day, in seconds + */ + public static final int humanVote = 60; + + /** + * The checking intervall in seconds + */ + public static final int intervall = 1; + + /** + * The timer for the ghost vote. Checks every {@code intervall} seconds if every ghost has already voted. + * If all have voted or if the {@code ghostVote} value is reached, the timer ends + * @param game the game this Timer has been called in + */ + public static void ghostVoteTimer(Game game) { + int counter = 0; + while(counter < ghostVote) { + if(haveAllGhostsVoted(game)) { //if all ghost have voted + return; + } + try { + Thread.sleep(intervall*1000); + } catch (InterruptedException e) { + LOGGER.warn("Thread " + Thread.currentThread() + " was interrupted"); + } + counter = counter + (intervall*1000); + } + + } + + /** + * Checks if all ghosts in the game have already voted, returns true if so + * @param game the Game the ghosts live in + * @return true if all Ghosts have voted and false if at least 1 didn't + */ + public static boolean haveAllGhostsVoted(Game game) { + int nrOfGhosts = 0; + int j = 0; //counter + boolean[] positionOfGhosts = game.gameState.getPositionOfGhosts(); + boolean[] whoHasVoted = game.getGameState().getClientVoteData().getHasVoted(); + for (boolean positionOfGhost : positionOfGhosts) { //determines how many ghosts are in the game + if (positionOfGhost) { + nrOfGhosts++; + } + } + for(int i = 0; i < positionOfGhosts.length; i++) { + if (positionOfGhosts[i]) { + if(whoHasVoted[i]) { + j++; + } + } + } + return nrOfGhosts == j; + } + + +} 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 19092be..27d85d5 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 @@ -50,11 +50,7 @@ public class VoteHandler { } } - try { // waits 30 seconds before votes get collected - Thread.sleep(10*1000); - } catch (InterruptedException e) { - LOGGER.warn("Thread " + Thread.currentThread() + " was interrupted"); - } + Timer.ghostVoteTimer(game); int currentMax = ghostVoteEvaluation(passengers, votesForPlayers, game.getGameState().getClientVoteData(), game); diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/GhostNPC.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/GhostNPC.java index c218814..f0faaab 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/GhostNPC.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/GhostNPC.java @@ -71,6 +71,7 @@ public class GhostNPC extends Ghost { int randomPosition = (int) (Math.random() * humanPositions.length); vote = humanPositions[randomPosition]; hasVoted = true; + game.getGameState().getClientVoteData().setHasVoted(position,hasVoted); LOGGER.info("GhostNPC at Position: " + this.getPosition() + " has voted for: " + vote); } } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/HumanNPC.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/HumanNPC.java index d43590c..32b6ee5 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/HumanNPC.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/HumanNPC.java @@ -67,6 +67,7 @@ public class HumanNPC extends Human { int randomNr = (int) (Math.random() * inGamePositions.length); vote = inGamePositions[randomNr]; hasVoted = true; + game.getGameState().getClientVoteData().setHasVoted(position,hasVoted); LOGGER.info("HumanNPC at Position: " + this.getPosition() + " has voted for: " + vote); } } diff --git a/src/test/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/GameStateTests.java b/src/test/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/GameStateTests.java index e6fb5d9..3c042ed 100644 --- a/src/test/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/GameStateTests.java +++ b/src/test/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/GameStateTests.java @@ -1,5 +1,17 @@ package ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur; + +import org.junit.jupiter.api.Test; + public class GameStateTests { + @Test + public void testGetGhostPositions() { + Passenger[] passengers = {new GhostNPC(0, "hansli", true), new HumanPlayer(1,"berta",null,false), + new GhostPlayer(2, "uhu", null, false), new HumanNPC(3, "vreneli"), new GhostNPC(4, "...", false), + new GhostPlayer(5, "last one", null, false)}; + boolean[] check = {true,false,true,false,true,true}; + boolean[] test = passengers + } + }