From 2e901490b2844fdd167e333a88dcd85124a01b8a Mon Sep 17 00:00:00 2001 From: Jonas Date: Mon, 16 May 2022 20:20:49 +0200 Subject: [PATCH 1/2] Updated Bell timing for performance optimization, changed some values in Timer class to make gameplay less hectic --- .../dmi/dbis/cs108/gamelogic/Timer.java | 6 +- .../client/gui/DayNightChangeListener.java | 4 +- .../client/gui/game/GameController.java | 99 +++++++------------ 3 files changed, 40 insertions(+), 69 deletions(-) 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 index bef9f07..b121302 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Timer.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Timer.java @@ -16,13 +16,13 @@ public class Timer { /** * The maximum length of the ghost vote in the night, in seconds */ - public static final int ghostVoteTime = 30; + public static final int ghostVoteTime = 40; /** * 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 = 6; + public static final int ghostAfterVoteTime = 8; /** * The maximum length of the human vote in the day, in seconds */ @@ -32,7 +32,7 @@ public class Timer { * 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 humanAfterVoteTime = 5; + public static final int humanAfterVoteTime = 8; /** * The checking interval in seconds diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/DayNightChangeListener.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/DayNightChangeListener.java index d5b0622..21e9776 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/DayNightChangeListener.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/DayNightChangeListener.java @@ -5,7 +5,7 @@ import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.game.GameController; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.lounge.LoungeSceneViewController; /** - * This class adds methods to listen if there is a change in the day&night state and calls methods accordingly + * This class adds methods to listen if there is a change in the day and night state and calls methods accordingly */ public class DayNightChangeListener implements Runnable { @@ -63,7 +63,7 @@ public class DayNightChangeListener implements Runnable { chatApp.getGameController().updateRoomLabels(); gameStateModel.setRoleFromPosition(position); try { - Thread.sleep(100); + Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/GameController.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/GameController.java index 4dcfc21..797c718 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/GameController.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/GameController.java @@ -2,6 +2,7 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.game; import static javafx.scene.AccessibleRole.PARENT; +import ch.unibas.dmi.dbis.cs108.gamelogic.Timer; import ch.unibas.dmi.dbis.cs108.multiplayer.client.Sound; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ChatApp; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.GameStateModel; @@ -534,17 +535,7 @@ public class GameController implements Initializable { try { if(!gameStateModel.getKickedOff()[0]) { Animation bell = new BellAnimation(noiseImage5, bells); - //wait until it's day: - while (!getGameStateModel().getDayClone()) { - Thread.sleep(100); - } - Thread.sleep(500); - //just so the alarm isn't rung exactly when the day starts, add random delay - Random random = new Random(); - Thread.sleep(random.nextInt(1000)); - - bell.play(); - ringBellSound(); + waitForDayThenRingBell(bell); } } catch (Exception e) { e.printStackTrace(); @@ -565,16 +556,7 @@ public class GameController implements Initializable { try { if(!gameStateModel.getKickedOff()[1]) { Animation bell = new BellAnimation(noiseImage4, bells); - //wait until it's day: - while (!getGameStateModel().getDayClone()) { - Thread.sleep(100); - } - Thread.sleep(500); - //just so the alarm isn't rung exactly when the day starts, add random delay - Random random = new Random(); - Thread.sleep(random.nextInt(1000)); - bell.play(); - ringBellSound(); + waitForDayThenRingBell(bell); } } catch (Exception e) { e.printStackTrace(); @@ -594,17 +576,7 @@ public class GameController implements Initializable { try { if(!gameStateModel.getKickedOff()[2]) { Animation bell = new BellAnimation(noiseImage3, bells); - //wait until it's day: - while (!getGameStateModel().getDayClone()) { - Thread.sleep(100); - } - Thread.sleep(500); - //just so the alarm isn't rung exactly when the day starts, add random delay - Random random = new Random(); - Thread.sleep(random.nextInt(1000)); - - bell.play(); - ringBellSound(); + waitForDayThenRingBell(bell); } } catch (Exception e) { e.printStackTrace(); @@ -624,17 +596,7 @@ public class GameController implements Initializable { try { if(!gameStateModel.getKickedOff()[3]) { Animation bell = new BellAnimation(noiseImage2, bells); - //wait until it's day: - while (!getGameStateModel().getDayClone()) { - Thread.sleep(100); - } - Thread.sleep(500); - //just so the alarm isn't rung exactly when the day starts, add random delay - Random random = new Random(); - Thread.sleep(random.nextInt(1000)); - - bell.play(); - ringBellSound(); + waitForDayThenRingBell(bell); } } catch (Exception e) { e.printStackTrace(); @@ -654,17 +616,7 @@ public class GameController implements Initializable { try { if(!gameStateModel.getKickedOff()[4]) { Animation bell = new BellAnimation(noiseImage1, bells); - //wait until it's day: - while (!getGameStateModel().getDayClone()) { - Thread.sleep(100); - } - Thread.sleep(500); - //just so the alarm isn't rung exactly when the day starts, add random delay - Random random = new Random(); - Thread.sleep(random.nextInt(1000)); - - bell.play(); - ringBellSound(); + waitForDayThenRingBell(bell); } } catch (Exception e) { e.printStackTrace(); @@ -684,16 +636,7 @@ public class GameController implements Initializable { try { if(!gameStateModel.getKickedOff()[5]) { Animation bell = new BellAnimation(noiseImage0, bells); - //wait until it's day: - while (!getGameStateModel().getDayClone()) { - Thread.sleep(100); - } - Thread.sleep(500); - //just so the alarm isn't rung exactly when the day starts, add random delay - Random random = new Random(); - Thread.sleep(random.nextInt(1000)); - bell.play(); - ringBellSound(); + waitForDayThenRingBell(bell); } } catch (Exception e) { e.printStackTrace(); @@ -734,6 +677,34 @@ public class GameController implements Initializable { ChatApp.setGameController(this); } + public static void waitForDayThenRingBell(Animation bell) { + new Thread(new Runnable() { + @Override + public void run() { + try { + //wait until it's day: + int timeoutCounter = 0; //otherwise this thread can get stuck in a loop if player leaves server + if (!getGameStateModel().getDayClone()) { //used to ring bell immediately if it's already day + while (!getGameStateModel().getDayClone() + && timeoutCounter < Timer.ghostAfterVoteTime * 15) { + Thread.sleep(100); + timeoutCounter++; + } + //just so the alarm isn't rung exactly when the day starts, also add random delay + Thread.sleep(1000); + Random random = new Random(); + Thread.sleep(random.nextInt(1000)); + } + + } catch (Exception e) { + e.printStackTrace(); + } + bell.play(); + ringBellSound(); + } + }).start(); + } + /** * plays bell sound, but only if it hasn't been played recently, to avoid artefacts due to * overlapping sounds From 2d6e15a39f099eba1d6ae657ba58d35300f4f387 Mon Sep 17 00:00:00 2001 From: Jonas Date: Mon, 16 May 2022 21:21:40 +0200 Subject: [PATCH 2/2] game over prompts game over music, groundwork laid for human vote result music, ghost volume adjusted. --- .../unibas/dmi/dbis/cs108/gamelogic/Game.java | 8 ++++++++ .../client/JClientProtocolParser.java | 17 +++++++++++++++++ .../dbis/cs108/multiplayer/client/Sound.java | 11 +++++++++-- .../client/gui/game/GameController.java | 2 +- .../cs108/multiplayer/helpers/Protocol.java | 7 +++++++ 5 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Game.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Game.java index ac7156a..3d3c6f0 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Game.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/Game.java @@ -172,6 +172,14 @@ public class Game implements Runnable { if (gameOverCheck.equals(ClientGameInfoHandler.gameOverGhostsWin) && getOgGhost().getIsPlayer()) { OgGhostHighScore.addOgGhostWinner(getOgGhost().getName()); } + + //send command to play game over sound: + if (gameOverCheck.equals(ClientGameInfoHandler.gameOverGhostsWin)) { + lobby.getAdmin().sendMsgToClientsInLobby(Protocol.playSound + "$" + "GW"); + } else { + lobby.getAdmin().sendMsgToClientsInLobby(Protocol.playSound + "$" + "HW"); + } + lobby.getAdmin().broadcastAnnouncementToLobby(gameOverCheck); isOngoing = false; Timer.ghostAfterVoteTimer(); diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/JClientProtocolParser.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/JClientProtocolParser.java index ba867ee..ca96835 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/JClientProtocolParser.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/JClientProtocolParser.java @@ -91,6 +91,23 @@ public class JClientProtocolParser { case Protocol.noiseNotificationProtocol: Sound.ghost(); break; + case Protocol.playSound: + switch (msg.substring(6)) { + case "GW": + Sound.gameoverghosts(); + break; + case "HW": + Sound.gameoverhumans(); + break; + case "GV": + Sound.voteforghost(); + break; + case "HV": + Sound.voteforhuman(); + break; + default: + LOGGER.warn("Invalid sound request"); + } default: System.out.println("Received unknown command: " + msg); } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/Sound.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/Sound.java index 6067c6a..45948d5 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/Sound.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/Sound.java @@ -97,7 +97,14 @@ public class Sound { public static void gameoverghosts() { gameoverghosts.play(defaultvolume); } - public static void gameoverhumans() { gameoverhumans.play(defaultvolume); } + public static void gameoverhumans() { + stopmusicday(); + gameoverhumans.play(defaultvolume); + } + + public static void voteforghost() { voteforghost.play(defaultvolume); } + + public static void voteforhuman() { voteforhuman.play(defaultvolume); } public static void musicday() { musicday.play(defaultvolume); @@ -127,7 +134,7 @@ public class Sound { ghost = ghost04; break; } - ghost.play(defaultvolume - 0.3, 0.0, playbackspeed, 0.0, 5); + ghost.play(0.1, 0.0, playbackspeed, 0.0, 5); } } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/GameController.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/GameController.java index 797c718..833db14 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/GameController.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/GameController.java @@ -40,7 +40,7 @@ public class GameController implements Initializable { public static final Logger LOGGER = LogManager.getLogger(GameController.class); public static final BudaLogConfig l = new BudaLogConfig(LOGGER); static boolean justRangBell = false; //used to track if the bell has been rung recently - static final int minimumBellTime = 1000; //minimal time that has to pass between bells, in ms + static final int minimumBellTime = 100; //minimal time that has to pass between bells, in ms static boolean playingDayNoises = true; //true if playing day noises, false if playing night noises private static ClientModel client; diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/Protocol.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/Protocol.java index 44c3061..673cada 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/Protocol.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/Protocol.java @@ -212,6 +212,13 @@ public class Protocol { */ public static final String noiseNotificationProtocol = "NOISE"; + /** + * Used to tell the client to play a sound, namely the sounds for when humans have voted for a human (PLSND$HV), + * when humans have voted for a ghost (PLSND$GV), when humans have won (i.e. have voted for the OG - PLSND$HW) + * or when ghosts have won (PLSND$GW) + */ + public static final String playSound = "PLSND"; + /** * Sends an information to client at which position in the train from the game (0 to 5) they sit, as soon as the game starts * {@code POSOF$position}