Started to make the vote time dynamic by adding a Timer class and adjusting some of the others. Doesn't work properly yet, though
This commit is contained in:
parent
a3af5d8911
commit
39d0b76f69
@ -60,4 +60,5 @@ public class ClientVoteData {
|
||||
LOGGER.warn("Position is:" + position);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
75
src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Timer.java
Normal file
75
src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Timer.java
Normal file
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -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);
|
||||
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user