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; return nrOfUsers;
} }
public ClientHandler getClientHandler() {
return clientHandler;
}
public boolean getIsDay() {return isDay;} public boolean getIsDay() {return isDay;}
public void setDay(boolean day) { public void setDay(boolean day) {

View File

@ -48,7 +48,7 @@ public class GameState {
clientVoteData = new ClientVoteData(); clientVoteData = new ClientVoteData();
Passenger[] passengerTrain = new Passenger[nrOfPlayers]; //Creates an array with Passengers with correlation positions (Train) Passenger[] passengerTrain = new Passenger[nrOfPlayers]; //Creates an array with Passengers with correlation positions (Train)
for (int i = 0; i < nrOfPlayers; i++) { for (int i = 0; i < nrOfPlayers; i++) {
if (i == 3) { if (i == 3) { //TODO: randomize via Train.ghostposition
Ghost g = new Ghost(); Ghost g = new Ghost();
g.setPosition(train.orderOfTrain[i]); g.setPosition(train.orderOfTrain[i]);
g.setGhost(); 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.HumanNPC;
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger; 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.helpers.Protocol;
import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -50,7 +51,7 @@ public class ServerGameInfoHandler {
switch (msg) { switch (msg) {
case ClientGameInfoHandler.noiseNotification: case ClientGameInfoHandler.noiseNotification:
//TODO(Seraina & Alex): noise handling //TODO(Seraina & Alex): noise handling
npc.noise(); game.getClientHandler().broadcastChatMessage(ClientGameInfoHandler.noiseNotification);
break; break;
case ClientGameInfoHandler.ghostVoteRequest: case ClientGameInfoHandler.ghostVoteRequest:
npc.vote(game); npc.vote(game);
@ -68,7 +69,7 @@ public class ServerGameInfoHandler {
public static void humanNpcParser(HumanNPC npc, String msg, Game game) { public static void humanNpcParser(HumanNPC npc, String msg, Game game) {
switch (msg) { switch (msg) {
case ClientGameInfoHandler.noiseNotification: case ClientGameInfoHandler.noiseNotification:
npc.noise(); game.getClientHandler().broadcastChatMessage(ClientGameInfoHandler.noiseNotification);
break; break;
case ClientGameInfoHandler.humanVoteRequest: case ClientGameInfoHandler.humanVoteRequest:
npc.vote(); npc.vote();

View File

@ -113,6 +113,11 @@ public class VoteHandler {
passengers[ghostPosition] = g; passengers[ghostPosition] = g;
passengers[ghostPosition].send( passengers[ghostPosition].send(
ClientGameInfoHandler.youGotGhostyfied, game); // TODO: ServerGameInfoHandler might deal with this one 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 /* 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 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 * Decides what to do when a noise ist heard, currently just always broadcasts it
* TODO: Make NPC smarter * 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; import org.apache.logging.log4j.Logger;
public class HumanNPC extends Human { public class HumanNPC extends Human {
public static final Logger LOGGER = LogManager.getLogger(); public static final Logger LOGGER = LogManager.getLogger();
public static final BudaLogConfig l = new BudaLogConfig(LOGGER); public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
@ -31,7 +32,8 @@ public class HumanNPC extends Human {
/** /**
* Sends a msg to the ServerGameInfoHandler.humanNpcParser to decide what has to happen now * 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 msg the message that is sent to this player.
* @param game the game the HumanNPC lives on (in game.gameState.passengerTrain) * @param game the game the HumanNPC lives on (in game.gameState.passengerTrain)
*/ */
@Override @Override
@ -41,16 +43,13 @@ public class HumanNPC extends Human {
/** /**
* Currently returns a random integer for voting * Currently returns a random integer for voting
*
* @return integer between 0 and 5 * @return integer between 0 and 5
*/ */
public void vote(){ public void vote() {
int randomNr = (int) (Math.random()*6); int randomNr = (int) (Math.random() * 6);
vote = randomNr; vote = randomNr;
hasVoted = true; hasVoted = true;
LOGGER.info("HumanNPC at Position: " + this.getPosition() + " has voted for: " + vote); 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 if(vote != Integer.MAX_VALUE) { //gets MAX_VALUE when the vote wasn't valid
VoteHandler.getClientVoteData().setVote(position,vote); VoteHandler.getClientVoteData().setVote(position,vote);
LOGGER.debug("Player vote: " + vote); LOGGER.debug("Player vote: " + vote);
VoteHandler.getClientVoteData().setHasVoted(position,true); VoteHandler.getClientVoteData().setHasVoted(position,true); //TODO: move clientVoteData to gamestate
} }
} }