Skip to content

Commit

Permalink
add simple sounds handling
Browse files Browse the repository at this point in the history
  • Loading branch information
salvoilmiosi committed Aug 27, 2024
1 parent 2459e17 commit 7c35525
Show file tree
Hide file tree
Showing 20 changed files with 39 additions and 9 deletions.
Binary file added public/sounds/bandidos.wav
Binary file not shown.
Binary file added public/sounds/bang.wav
Binary file not shown.
Binary file added public/sounds/death.wav
Binary file not shown.
Binary file added public/sounds/draw.wav
Binary file not shown.
Binary file added public/sounds/duel.wav
Binary file not shown.
Binary file added public/sounds/dynamite.wav
Binary file not shown.
Binary file added public/sounds/gamestart.wav
Binary file not shown.
Binary file added public/sounds/gatling.wav
Binary file not shown.
Binary file added public/sounds/generalstore.wav
Binary file not shown.
Binary file added public/sounds/indians.wav
Binary file not shown.
Binary file added public/sounds/invalid.wav
Binary file not shown.
Binary file added public/sounds/shuffle.wav
Binary file not shown.
Binary file added public/sounds/snake.wav
Binary file not shown.
1 change: 1 addition & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default function App() {
lobbyState={scene.lobbyState}
gameChannel={gameChannel}
overlayRef={overlayRef}
muteSounds={settings.muteSounds}
/>
}
};
Expand Down
10 changes: 8 additions & 2 deletions src/Components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ function Header({ scene, settings, connection }: HeaderProps) {
}
};

const handleToggleSounds = () => {
settings.setMuteSounds(value => !value);
};

const closeMenuAnd = (fn: () => void) => {
return () => {
setIsMenuOpen(false);
Expand Down Expand Up @@ -77,8 +81,10 @@ function Header({ scene, settings, connection }: HeaderProps) {
</button>
{ isMenuOpen &&
<UserMenu username={settings.username} setUsername={username => handleEditUser(username, settings.propic)}>
{ scene.type === 'game' && isLobbyOwner(scene.lobbyState) &&
<UserMenuItem onClick={closeMenuAnd(handleReturnLobby)}>{getLabel('ui', 'BUTTON_RETURN_LOBBY')}</UserMenuItem>}
{ scene.type === 'game' && <>
<UserMenuItem onClick={handleToggleSounds}>{getLabel('ui', settings.muteSounds ? 'BUTTON_ENABLE_SOUNDS' : 'BUTTON_DISABLE_SOUNDS')}</UserMenuItem>
{ isLobbyOwner(scene.lobbyState) && <UserMenuItem onClick={closeMenuAnd(handleReturnLobby)}>{getLabel('ui', 'BUTTON_RETURN_LOBBY')}</UserMenuItem>}
</>}
{ 'lobbyInfo' in scene
? <UserMenuItem onClick={closeMenuAnd(handleLeaveLobby)}>{getLabel('ui', 'BUTTON_LEAVE_LOBBY')}</UserMenuItem>
: <UserMenuItem onClick={closeMenuAnd(handleDisconnect)}>{getLabel('ui', 'BUTTON_DISCONNECT')}</UserMenuItem> }
Expand Down
2 changes: 2 additions & 0 deletions src/Locale/English/Labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export const LABELS_ENGLISH: LabelRegistry = {
GAME_OPTIONS: "Game Options",
BUTTON_CHAT_SEND: "Send",
BUTTON_START_GAME: "Start Game",
BUTTON_ENABLE_SOUNDS: "Enable Sounds",
BUTTON_DISABLE_SOUNDS: "Disable Sounds",
BUTTON_RETURN_LOBBY: "Return to Lobby",
BUTTON_OK: "OK",
BUTTON_UNDO: "Undo",
Expand Down
2 changes: 2 additions & 0 deletions src/Locale/Italian/Labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export const LABELS_ITALIAN: LabelRegistry = {
GAME_OPTIONS: "Opzioni di Partita",
BUTTON_CHAT_SEND: "Invia",
BUTTON_START_GAME: "Avvia Partita",
BUTTON_ENABLE_SOUNDS: "Abilita Souni",
BUTTON_DISABLE_SOUNDS: "Disabilita Suoni",
BUTTON_RETURN_LOBBY: "Ritorna alla Lobby",
BUTTON_OK: "OK",
BUTTON_UNDO: "Annulla",
Expand Down
7 changes: 5 additions & 2 deletions src/Model/AppSettings.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { GameOptions } from "../Scenes/Game/Model/GameUpdate";
import { intConverter, jsonConverter, stringConverter, useLocalStorage, useSessionStorage } from "../Utils/UseLocalStorage";
import { boolConverter, intConverter, jsonConverter, stringConverter, useLocalStorage, useSessionStorage } from "../Utils/UseLocalStorage";

export function useSettings() {
const [sessionId, setSessionId] = useSessionStorage('session_id', intConverter);

const [username, setUsername] = useLocalStorage('username', stringConverter);
const [propic, setPropic] = useLocalStorage('propic', stringConverter);
const [lobbyName, setLobbyName] = useLocalStorage('lobby_name', stringConverter);
const [gameOptions, setGameOptions] = useLocalStorage<GameOptions>('game_options', jsonConverter);
const [muteSounds, setMuteSounds] = useLocalStorage('mute_sounds', boolConverter);

return {
sessionId, setSessionId,
username, setUsername,
propic, setPropic,
lobbyName, setLobbyName,
gameOptions, setGameOptions
gameOptions, setGameOptions,
muteSounds, setMuteSounds
} as const;
}

Expand Down
5 changes: 3 additions & 2 deletions src/Scenes/Game/GameScene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ export interface GameProps {
lobbyState: LobbyState;
gameChannel: GameChannel;
overlayRef: RefObject<HTMLDivElement>;
muteSounds?: boolean;
}

const EMPTY_GAME_STATE = newGameState(0);
export const GameStateContext = createContext(EMPTY_GAME_STATE);

export default function GameScene({ connection, lobbyState, gameChannel, overlayRef }: GameProps) {
const { state, selectorDispatch, gameLogs, gameError, clearGameError } = useGameState(gameChannel, lobbyState.myUserId);
export default function GameScene({ connection, lobbyState, gameChannel, overlayRef, muteSounds }: GameProps) {
const { state, selectorDispatch, gameLogs, gameError, clearGameError } = useGameState(gameChannel, lobbyState.myUserId, muteSounds ?? false);
const { table, selector } = state;

const pocketRefs = useMapRef<PocketType, PocketRef>();
Expand Down
21 changes: 18 additions & 3 deletions src/Scenes/Game/Model/UseGameState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import gameTableReducer from "./GameTableReducer";
import { GameString, GameUpdate, TableUpdate } from "./GameUpdate";
import targetSelectorReducer, { SelectorUpdate } from "./TargetSelectorReducer";
import { newTargetSelector, TargetSelector } from "./TargetSelector";
import { useMapRef } from "../../../Utils/UseMapRef";
import useEvent from "react-use-event-hook";

export interface GameState {
table: GameTable;
Expand All @@ -33,13 +35,26 @@ export function newGameState(myUserId: UserId): GameState {
};
}

export default function useGameState(gameChannel: GameChannel, myUserId: UserId) {
export default function useGameState(gameChannel: GameChannel, myUserId: UserId, muteSounds: boolean) {
const [state, stateDispatch] = useReducer(gameStateReducer, myUserId, newGameState);

const [gameLogs, setGameLogs] = useState<GameString[]>([]);
const [gameError, setGameError] = useState<GameString>();
const gameUpdates = useRef<GameUpdate[]>([]);

const soundsMap = useMapRef<string, HTMLAudioElement>();

const playSound = useEvent((name: string) => {
let sound = soundsMap.get(name);
if (sound === null) {
sound = new Audio(`/sounds/${name}.wav`);
soundsMap.set(name, sound);
}
if (!muteSounds) {
sound.play();
}
});

const clearGameError = useCallback(() => {
if (gameError) setGameError(undefined);
}, [gameError]);
Expand Down Expand Up @@ -88,7 +103,7 @@ export default function useGameState(gameChannel: GameChannel, myUserId: UserId)
},

play_sound(sound) {
// TODO
playSound(sound);
},

add_cards(update) {
Expand Down Expand Up @@ -221,7 +236,7 @@ export default function useGameState(gameChannel: GameChannel, myUserId: UserId)
clearTimeout(timeout);
gameChannel.unsubscribe();
}
}, [gameChannel]);
}, [gameChannel, playSound]);

return { state, selectorDispatch, gameLogs, gameError, clearGameError } as const;
}

0 comments on commit 7c35525

Please sign in to comment.