Skip to content

Commit

Permalink
Add volume slider and rework audio handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gereon77 committed Feb 8, 2024
1 parent 46c73bf commit 902ce58
Show file tree
Hide file tree
Showing 14 changed files with 621 additions and 361 deletions.
26 changes: 2 additions & 24 deletions agot-bg-game-server/src/client/EntireGameComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import CancelledComponent from "./CancelledComponent";
import CancelledGameState from "../common/cancelled-game-state/CancelledGameState";
import Col from "react-bootstrap/Col";
import Badge from "react-bootstrap/Badge";
import notificationSound from "../../public/sounds/notification.ogg";
import faviconNormal from "../../public/images/favicon.ico";
import faviconAlert from "../../public/images/favicon-alert.ico";
import rollingDicesImage from "../../public/images/icons/rolling-dices.svg";
Expand All @@ -25,7 +24,6 @@ import HouseIconComponent from "./game-state-panel/utils/HouseIconComponent";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faLock, faTriangleExclamation } from "@fortawesome/free-solid-svg-icons";
import GameEndedGameState from "../common/ingame-game-state/game-ended-game-state/GameEndedGameState";
import introSound from "../../public/sounds/game-of-thrones-intro.ogg";
import CombatGameState from "../common/ingame-game-state/action-game-state/resolve-march-order-game-state/combat-game-state/CombatGameState";
import { toast, ToastContainer } from "react-toastify";
import { cssTransition } from "react-toastify";
Expand All @@ -48,7 +46,6 @@ interface EntireGameComponentProps {
@observer
export default class EntireGameComponent extends Component<EntireGameComponentProps> {
@observable showMapWhenDrafting = false;
@observable playWelcomeSound = false;
setIntervalId = -1;

get entireGame(): EntireGame {
Expand Down Expand Up @@ -121,9 +118,6 @@ export default class EntireGameComponent extends Component<EntireGameComponentPr
? <IngameComponent gameClient={this.props.gameClient} gameState={this.entireGame.childGameState} />
: this.entireGame.childGameState instanceof CancelledGameState && <CancelledComponent gameClient={this.props.gameClient} gameState={this.entireGame.childGameState} />
}
{this.playWelcomeSound && !this.props.gameClient.musicMuted &&
<audio id="welcome-sound" src={introSound} autoPlay onEnded={() => this.playWelcomeSound = false} />
}
<ToastContainer
autoClose={7500}
position="top-center"
Expand Down Expand Up @@ -350,24 +344,12 @@ export default class EntireGameComponent extends Component<EntireGameComponentPr
}

onGameStarted(): void {
const audio = document.getElementById("welcome-sound") as HTMLAudioElement;
// Make sure it's not playing right now
if (audio && !audio.paused) {
return;
}

if (!this.props.gameClient.musicMuted) {
const intro = new Audio(introSound);
intro.play();
}
this.props.gameClient.sfxManager.playGotTheme();
}

onClientGameStateChange(): void {
if (this.props.gameClient.isOwnTurn()) {
if (!this.props.gameClient.muted) {
const audio = new Audio(notificationSound);
audio.play();
}
this.props.gameClient.sfxManager.playNotificationSound();

const player = this.props.gameClient.authenticatedPlayer;
if (player) {
Expand Down Expand Up @@ -411,10 +393,6 @@ export default class EntireGameComponent extends Component<EntireGameComponentPr
}

this.setIntervalId = window.setInterval(() => this.setNow(), 1000);

/* if (!this.isInCombat) {
this.playWelcomeSound = true;
} */
}

componentWillUnmount(): void {
Expand Down
49 changes: 44 additions & 5 deletions agot-bg-game-server/src/client/GameClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import House from "../common/ingame-game-state/game-data-structure/House";
import ChatClient from "./chat-client/ChatClient";
import BetterMap from "../utils/BetterMap";
import { compress, decompress } from "./utils/compression";
import { playSoundWhenClickingMarchOrder, playSoundForLogEvent, stopRunningSoundEffect } from "./utils/sound-effects";
import SfxManager from "./utils/SfxManager";

export interface AuthData {
userId: string;
Expand Down Expand Up @@ -40,6 +40,7 @@ export default class GameClient {
@observable isReconnecting = false;

chatClient: ChatClient = new ChatClient(this);
sfxManager: SfxManager = new SfxManager(this);

get muted(): boolean {
if (!this.authenticatedUser) {
Expand All @@ -55,6 +56,9 @@ export default class GameClient {
}

this.authenticatedUser.settings.muted = value;
if (value == true) {
this.authenticatedUser.settings.notificationsVolume = 0;
}
this.authenticatedUser.syncSettings();
}

Expand All @@ -72,6 +76,45 @@ export default class GameClient {
}

this.authenticatedUser.settings.musicMuted = value;
if (value == true) {
this.authenticatedUser.settings.musicVolume = 0;
}
this.authenticatedUser.syncSettings();
}

get notificationsVolume(): number {
if (!this.authenticatedUser) {
throw new Error("Game client must have an authenticated user");
}

return this.authenticatedUser.settings.notificationsVolume;
}

set notificationsVolume(value: number) {
if (!this.authenticatedUser) {
throw new Error("Game client must have an authenticated user");
}

this.authenticatedUser.settings.notificationsVolume = value;
this.authenticatedUser.settings.muted = value == 0;
this.authenticatedUser.syncSettings();
}

get musicVolume(): number {
if (!this.authenticatedUser) {
throw new Error("Game client must have an authenticated user");
}

return this.authenticatedUser.settings.musicVolume;
}

set musicVolume(value: number) {
if (!this.authenticatedUser) {
throw new Error("Game client must have an authenticated user");
}

this.authenticatedUser.settings.musicVolume = value;
this.authenticatedUser.settings.musicMuted = value == 0;
this.authenticatedUser.syncSettings();
}

Expand Down Expand Up @@ -300,8 +343,4 @@ export default class GameClient {
this.authenticatedUser = null;
this.isReconnecting = false;
}

public playSoundWhenClickingMarchOrder = playSoundWhenClickingMarchOrder;
public playSoundForLogEvent = playSoundForLogEvent;
public stopRunningSoundEffect = stopRunningSoundEffect;
}
Loading

0 comments on commit 902ce58

Please sign in to comment.