Integrated noise handling so it would run, some bugs still to work out

This commit is contained in:
Seraina 2022-04-13 11:43:35 +02:00
parent 062304719f
commit 850e09e0af
7 changed files with 20 additions and 14 deletions

View File

@ -58,6 +58,10 @@ public class Game implements Runnable {
return nrOfUsers;
}
public ClientHandler getClientHandler() {
return clientHandler;
}
public boolean getIsDay() {return isDay;}
public void setDay(boolean day) {

View File

@ -48,7 +48,7 @@ public class GameState {
clientVoteData = new ClientVoteData();
Passenger[] passengerTrain = new Passenger[nrOfPlayers]; //Creates an array with Passengers with correlation positions (Train)
for (int i = 0; i < nrOfPlayers; i++) {
if (i == 3) {
if (i == 3) { //TODO: randomize via Train.ghostposition
Ghost g = new Ghost();
g.setPosition(train.orderOfTrain[i]);
g.setGhost();

View File

@ -6,6 +6,7 @@ import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.GhostNPC;
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.HumanNPC;
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol;
import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -50,7 +51,7 @@ public class ServerGameInfoHandler {
switch (msg) {
case ClientGameInfoHandler.noiseNotification:
//TODO(Seraina & Alex): noise handling
npc.noise();
game.getClientHandler().broadcastChatMessage(ClientGameInfoHandler.noiseNotification);
break;
case ClientGameInfoHandler.ghostVoteRequest:
npc.vote(game);
@ -68,7 +69,7 @@ public class ServerGameInfoHandler {
public static void humanNpcParser(HumanNPC npc, String msg, Game game) {
switch (msg) {
case ClientGameInfoHandler.noiseNotification:
npc.noise();
game.getClientHandler().broadcastChatMessage(ClientGameInfoHandler.noiseNotification);
break;
case ClientGameInfoHandler.humanVoteRequest:
npc.vote();

View File

@ -113,6 +113,11 @@ public class VoteHandler {
passengers[ghostPosition] = g;
passengers[ghostPosition].send(
ClientGameInfoHandler.youGotGhostyfied, game); // TODO: ServerGameInfoHandler might deal with this one
try { // waits 20 seconds before votes get collected
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
is being created and the passengers this ghost passed by are being notified. The player who's just been ghostified

View File

@ -69,9 +69,6 @@ public class GhostNPC extends Ghost {
* Decides what to do when a noise ist heard, currently just always broadcasts it
* TODO: Make NPC smarter
*/
public void noise() {
clientHandler.broadcastChatMessage("I heard some noise tonight");
}
}

View File

@ -7,6 +7,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class HumanNPC extends Human {
public static final Logger LOGGER = LogManager.getLogger();
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
@ -31,6 +32,7 @@ public class HumanNPC extends Human {
/**
* Sends a msg to the ServerGameInfoHandler.humanNpcParser to decide what has to happen now
*
* @param msg the message that is sent to this player.
* @param game the game the HumanNPC lives on (in game.gameState.passengerTrain)
*/
@ -41,6 +43,7 @@ public class HumanNPC extends Human {
/**
* Currently returns a random integer for voting
*
* @return integer between 0 and 5
*/
public void vote() {
@ -49,8 +52,4 @@ public class HumanNPC extends Human {
hasVoted = true;
LOGGER.info("HumanNPC at Position: " + this.getPosition() + " has voted for: " + vote);
}
public void noise() {
clientHandler.broadcastChatMessage("I heard some noise tonight");
}
}

View File

@ -210,7 +210,7 @@ public class ClientHandler implements Runnable {
if(vote != Integer.MAX_VALUE) { //gets MAX_VALUE when the vote wasn't valid
VoteHandler.getClientVoteData().setVote(position,vote);
LOGGER.debug("Player vote: " + vote);
VoteHandler.getClientVoteData().setHasVoted(position,true);
VoteHandler.getClientVoteData().setHasVoted(position,true); //TODO: move clientVoteData to gamestate
}
}