Timers fully implemented.

This commit is contained in:
Jonas 2022-04-29 21:25:03 +02:00
parent 81ee9a8841
commit fc737012c7
4 changed files with 81 additions and 36 deletions

View File

@ -65,6 +65,9 @@ public class Game implements Runnable {
isOngoing = ongoing; isOngoing = ongoing;
} }
/**
* Returns this game's OG ghost as a Passenger object
*/
Passenger getOgGhost(){ Passenger getOgGhost(){
int[] order = gameState.getTrain().getOrderOfTrain(); int[] order = gameState.getTrain().getOrderOfTrain();
Passenger[] passengerTrain = gameState.getPassengerTrain(); Passenger[] passengerTrain = gameState.getPassengerTrain();

View File

@ -1,11 +1,13 @@
package ch.unibas.dmi.dbis.cs108.gamelogic; package ch.unibas.dmi.dbis.cs108.gamelogic;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig; import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
/** /**
* A class that handles all timed events in the game, such as vote times * A class that handles all timed events in the game, such as vote times. This class essentially
* is used to pause the main game thread for a given time / until a given event.
*/ */
public class Timer { public class Timer {
public static final Logger LOGGER = LogManager.getLogger(Timer.class); public static final Logger LOGGER = LogManager.getLogger(Timer.class);
@ -14,36 +16,68 @@ public class Timer {
/** /**
* The maximum length of the ghost vote in the night, in seconds * The maximum length of the ghost vote in the night, in seconds
*/ */
public static final int ghostVote = 30; public static final int ghostVoteTime = 30;
/**
* The length of time in seconds after the ghost vote during which the ghosts visually walk to /
* from their victim and the timespan within which humans will hear a noise. After this, the day starts.
*/
public static final int ghostAfterVoteTime = 7;
/** /**
* The maximum length of the human vote in the day, in seconds * The maximum length of the human vote in the day, in seconds
*/ */
public static final int humanVote = 60; public static final int humanVoteTime = 60;
/** /**
* The checking intervall in seconds * The length of time in seconds after the human vote, as the 'winner' of the vote is announced,
* before the night begins
*/ */
public static final int intervall = 1; public static final int humanAfterVoteTime = 5;
/** /**
* The timer for the ghost vote. Checks every {@code intervall} seconds if every ghost has already voted. * The checking interval in seconds
* If all have voted or if the {@code ghostVote} value is reached, the timer ends */
public static final int interval = 1;
/**
* The timer for the ghost vote. Checks every {@code interval} seconds if every ghost has already voted.
* If all have voted or if the {@code ghostVoteTime} value is reached, the timer ends
* @param game the game this Timer has been called in * @param game the game this Timer has been called in
*/ */
public static void ghostVoteTimer(Game game) { public static void ghostVoteTimer(Game game) {
int counter = 0; int counter = 0;
while(counter < ghostVote) { while(counter < ghostVoteTime) {
if(haveAllGhostsVoted(game)) { //if all ghost have voted if(haveAllGhostsVoted(game)) { //if all ghost have voted
return; return;
} }
try { try {
Thread.sleep(intervall*1000); Thread.sleep(interval*1000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
LOGGER.warn("Thread " + Thread.currentThread() + " was interrupted"); LOGGER.warn("Thread " + Thread.currentThread() + " was interrupted");
} }
counter = counter + (intervall*1000); counter += (interval);
} }
}
public static void humanVoteTimer(Game game) {
int counter = 0;
while (counter < humanVoteTime) {
if (haveAllHumansVoted(game)) return;
try {
Thread.sleep(interval*1000);
} catch (InterruptedException e) {
LOGGER.warn("Thread " + Thread.currentThread() + " was interrupted");
}
counter += interval;
}
}
public static void ghostAfterVoteTimer() {
try {
Thread.sleep(ghostAfterVoteTime *1000);
} catch (InterruptedException e) {
LOGGER.warn("Thread " + Thread.currentThread() + " was interrupted");
}
} }
/** /**
@ -71,5 +105,18 @@ public class Timer {
return nrOfGhosts == j; return nrOfGhosts == j;
} }
/**
* Checks if all humans have already voted, returns true if so, else returns false
*/
public static boolean haveAllHumansVoted(Game game) {
boolean[] whoHasVoted = game.getGameState().getClientVoteData().getHasVoted();
Passenger[] passengerArray = game.getGameState().getPassengerTrain();
for(int i = 0; i < whoHasVoted.length; i++) {
if(!passengerArray[i].getIsGhost() && !whoHasVoted[i]) {
return false;
}
}
return true;
}
} }

View File

@ -50,42 +50,34 @@ public class VoteHandler {
} }
} }
//Timer.ghostVoteTimer(game); Timer.ghostVoteTimer(game);
try {
Thread.sleep(20*1000);
} catch (InterruptedException e) {
LOGGER.warn("Thread " + Thread.currentThread() + " was interrupted");
}
int currentMax = ghostVoteEvaluation(passengers, votesForPlayers, game.getGameState().getClientVoteData(), game); int currentMax = ghostVoteEvaluation(passengers, votesForPlayers, game.getGameState().getClientVoteData(), game);
LOGGER.debug("Most votes: " + currentMax + " vote(s)"); LOGGER.debug("Most votes: " + currentMax + " vote(s)");
// ghostify the player with most votes // ghostify the player with most votes
int ghostPosition = 0; int newGhostPosition = 0;
for (int i = 0; i < votesForPlayers.length; i++) { for (int i = 0; i < votesForPlayers.length; i++) {
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; newGhostPosition = 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: " + newGhostPosition);
for(Passenger passenger : passengers) { for(Passenger passenger : passengers) {
if(passenger.getIsGhost() || passenger.getIsSpectator()) { if(passenger.getIsGhost() || passenger.getIsSpectator()) {
passenger.send(passengers[ghostPosition].getName() + ClientGameInfoHandler.gotGhostyfied, game); passenger.send(passengers[newGhostPosition].getName() + ClientGameInfoHandler.gotGhostyfied, game);
} }
} }
Passenger g = GhostifyHandler.ghost(passengers[ghostPosition], game); Passenger g = GhostifyHandler.ghost(passengers[newGhostPosition], game);
passengers[ghostPosition] = g; passengers[newGhostPosition] = g;
if (!passengers[ghostPosition].getIsSpectator()) { if (!passengers[newGhostPosition].getIsSpectator()) {
passengers[ghostPosition].send( passengers[newGhostPosition].send(
ClientGameInfoHandler.youGotGhostyfied, game); ClientGameInfoHandler.youGotGhostyfied, game);
} passengers[newGhostPosition].send(game.gameState.toString(), game);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
LOGGER.warn("Thread " + Thread.currentThread() + " was interrupted");
} }
/* notify passengers the ghosts passed by - for each ghost that ghostified a player, an instance of NoiseHandler /* notify passengers the ghosts passed by - for each ghost that ghostified a player, an instance of NoiseHandler
@ -96,7 +88,7 @@ public class VoteHandler {
int[] noiseAmount = new int[6]; int[] noiseAmount = new int[6];
for (int i = 0; i < passengers.length; i++) { for (int i = 0; i < passengers.length; i++) {
if (passengers[i].getIsGhost() && i != ghostPosition) { if (passengers[i].getIsGhost() && i != newGhostPosition) {
NoiseHandler n = new NoiseHandler(); NoiseHandler n = new NoiseHandler();
noiseAmount = n.noiseNotifier(passengers[i], g, noiseAmount); noiseAmount = n.noiseNotifier(passengers[i], g, noiseAmount);
} }
@ -107,6 +99,11 @@ public class VoteHandler {
} }
} }
/* used to wait for some time so the humans have time to hear noises and so the ghosts (& victim)
can see the infection happen. */
Timer.ghostAfterVoteTimer();
// no humans left in the game --> everyone has been ghostified, ghosts win // no humans left in the game --> everyone has been ghostified, ghosts win
int humanCounter = 0; int humanCounter = 0;
for(Passenger passenger : passengers) { for(Passenger passenger : passengers) {
@ -154,11 +151,7 @@ public class VoteHandler {
} }
} }
try { // waits 60 seconds before votes get collected Timer.humanVoteTimer(game);
Thread.sleep(20*1000);
} catch (InterruptedException e) {
LOGGER.warn("Thread " + Thread.currentThread() + " was interrupted");
}
int currentMax = humanVoteEvaluation(passengers, votesForPlayers, game.getGameState().getClientVoteData(), game); int currentMax = humanVoteEvaluation(passengers, votesForPlayers, game.getGameState().getClientVoteData(), game);

View File

@ -35,7 +35,9 @@ public class GhostPlayer extends Ghost {
} }
/** /**
* Sends a message to the client handled bye this client handler * Sends a message to the client handled by this client handler. By default, it adds the
* protocol signature to print the given msg as is to the client console. For more detail on
* how the message gets formatted, look at the ServerGameInfoHandler.format() method.
* @param msg the message that is sent to this player. * @param msg the message that is sent to this player.
* @param game the game the GhostPlayer lives on (in game.gameState.passengerTrain) * @param game the game the GhostPlayer lives on (in game.gameState.passengerTrain)
*/ */