diff --git a/.gitignore b/.gitignore index 87904c12..324fbee4 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,5 @@ tsconfig.tsbuildinfo .cdk.staging cdk.out cdk.context.json + +._* diff --git a/public/sound/futuristic/Capture.mp3 b/public/sound/futuristic/Capture.mp3 new file mode 100644 index 00000000..8a297e59 Binary files /dev/null and b/public/sound/futuristic/Capture.mp3 differ diff --git a/public/sound/futuristic/Check.mp3 b/public/sound/futuristic/Check.mp3 new file mode 100644 index 00000000..66f1f0ab Binary files /dev/null and b/public/sound/futuristic/Check.mp3 differ diff --git a/public/sound/futuristic/Checkmate.mp3 b/public/sound/futuristic/Checkmate.mp3 new file mode 120000 index 00000000..5d03cc53 --- /dev/null +++ b/public/sound/futuristic/Checkmate.mp3 @@ -0,0 +1 @@ +Check.mp3 \ No newline at end of file diff --git a/public/sound/futuristic/Confirmation.mp3 b/public/sound/futuristic/Confirmation.mp3 new file mode 120000 index 00000000..13853d97 --- /dev/null +++ b/public/sound/futuristic/Confirmation.mp3 @@ -0,0 +1 @@ +../sfx/GenericNotify.mp3 \ No newline at end of file diff --git a/public/sound/futuristic/Draw.mp3 b/public/sound/futuristic/Draw.mp3 new file mode 100644 index 00000000..bb6aeae8 Binary files /dev/null and b/public/sound/futuristic/Draw.mp3 differ diff --git a/public/sound/futuristic/Error.mp3 b/public/sound/futuristic/Error.mp3 new file mode 120000 index 00000000..261b68e0 --- /dev/null +++ b/public/sound/futuristic/Error.mp3 @@ -0,0 +1 @@ +../standard/Error.mp3 \ No newline at end of file diff --git a/public/sound/futuristic/Move.mp3 b/public/sound/futuristic/Move.mp3 new file mode 100644 index 00000000..fab41acd Binary files /dev/null and b/public/sound/futuristic/Move.mp3 differ diff --git a/public/sound/futuristic/OutOfBound.mp3 b/public/sound/futuristic/OutOfBound.mp3 new file mode 120000 index 00000000..76800caa --- /dev/null +++ b/public/sound/futuristic/OutOfBound.mp3 @@ -0,0 +1 @@ +../standard/OutOfBound.mp3 \ No newline at end of file diff --git a/public/sound/lisp/Capture.mp3 b/public/sound/lisp/Capture.mp3 new file mode 100644 index 00000000..5a318697 Binary files /dev/null and b/public/sound/lisp/Capture.mp3 differ diff --git a/public/sound/lisp/Check.mp3 b/public/sound/lisp/Check.mp3 new file mode 100644 index 00000000..961dccee Binary files /dev/null and b/public/sound/lisp/Check.mp3 differ diff --git a/public/sound/lisp/Checkmate.mp3 b/public/sound/lisp/Checkmate.mp3 new file mode 120000 index 00000000..5d03cc53 --- /dev/null +++ b/public/sound/lisp/Checkmate.mp3 @@ -0,0 +1 @@ +Check.mp3 \ No newline at end of file diff --git a/public/sound/lisp/Draw.mp3 b/public/sound/lisp/Draw.mp3 new file mode 100644 index 00000000..daef1bed Binary files /dev/null and b/public/sound/lisp/Draw.mp3 differ diff --git a/public/sound/lisp/Move.mp3 b/public/sound/lisp/Move.mp3 new file mode 100644 index 00000000..479e98f3 Binary files /dev/null and b/public/sound/lisp/Move.mp3 differ diff --git a/public/sound/lisp/OutOfBound.mp3 b/public/sound/lisp/OutOfBound.mp3 new file mode 120000 index 00000000..76800caa --- /dev/null +++ b/public/sound/lisp/OutOfBound.mp3 @@ -0,0 +1 @@ +../standard/OutOfBound.mp3 \ No newline at end of file diff --git a/public/sound/nes/Capture.mp3 b/public/sound/nes/Capture.mp3 new file mode 100644 index 00000000..839ece4c Binary files /dev/null and b/public/sound/nes/Capture.mp3 differ diff --git a/public/sound/nes/Check.mp3 b/public/sound/nes/Check.mp3 new file mode 100644 index 00000000..e030664e Binary files /dev/null and b/public/sound/nes/Check.mp3 differ diff --git a/public/sound/nes/Checkmate.mp3 b/public/sound/nes/Checkmate.mp3 new file mode 120000 index 00000000..5d03cc53 --- /dev/null +++ b/public/sound/nes/Checkmate.mp3 @@ -0,0 +1 @@ +Check.mp3 \ No newline at end of file diff --git a/public/sound/nes/Confirmation.mp3 b/public/sound/nes/Confirmation.mp3 new file mode 120000 index 00000000..13853d97 --- /dev/null +++ b/public/sound/nes/Confirmation.mp3 @@ -0,0 +1 @@ +../sfx/GenericNotify.mp3 \ No newline at end of file diff --git a/public/sound/nes/Draw.mp3 b/public/sound/nes/Draw.mp3 new file mode 100644 index 00000000..5e26c191 Binary files /dev/null and b/public/sound/nes/Draw.mp3 differ diff --git a/public/sound/nes/Error.mp3 b/public/sound/nes/Error.mp3 new file mode 120000 index 00000000..261b68e0 --- /dev/null +++ b/public/sound/nes/Error.mp3 @@ -0,0 +1 @@ +../standard/Error.mp3 \ No newline at end of file diff --git a/public/sound/nes/Move.mp3 b/public/sound/nes/Move.mp3 new file mode 100644 index 00000000..82907793 Binary files /dev/null and b/public/sound/nes/Move.mp3 differ diff --git a/public/sound/nes/OutOfBound.mp3 b/public/sound/nes/OutOfBound.mp3 new file mode 120000 index 00000000..76800caa --- /dev/null +++ b/public/sound/nes/OutOfBound.mp3 @@ -0,0 +1 @@ +../standard/OutOfBound.mp3 \ No newline at end of file diff --git a/public/sound/piano/Capture.mp3 b/public/sound/piano/Capture.mp3 new file mode 100644 index 00000000..837d37d2 Binary files /dev/null and b/public/sound/piano/Capture.mp3 differ diff --git a/public/sound/piano/Check.mp3 b/public/sound/piano/Check.mp3 new file mode 100644 index 00000000..585e0f1f Binary files /dev/null and b/public/sound/piano/Check.mp3 differ diff --git a/public/sound/piano/Checkmate.mp3 b/public/sound/piano/Checkmate.mp3 new file mode 120000 index 00000000..5d03cc53 --- /dev/null +++ b/public/sound/piano/Checkmate.mp3 @@ -0,0 +1 @@ +Check.mp3 \ No newline at end of file diff --git a/public/sound/piano/Confirmation.mp3 b/public/sound/piano/Confirmation.mp3 new file mode 120000 index 00000000..13853d97 --- /dev/null +++ b/public/sound/piano/Confirmation.mp3 @@ -0,0 +1 @@ +../sfx/GenericNotify.mp3 \ No newline at end of file diff --git a/public/sound/piano/Draw.mp3 b/public/sound/piano/Draw.mp3 new file mode 100644 index 00000000..6529a8ce Binary files /dev/null and b/public/sound/piano/Draw.mp3 differ diff --git a/public/sound/piano/Error.mp3 b/public/sound/piano/Error.mp3 new file mode 120000 index 00000000..261b68e0 --- /dev/null +++ b/public/sound/piano/Error.mp3 @@ -0,0 +1 @@ +../standard/Error.mp3 \ No newline at end of file diff --git a/public/sound/piano/Move.mp3 b/public/sound/piano/Move.mp3 new file mode 100644 index 00000000..2e8623df Binary files /dev/null and b/public/sound/piano/Move.mp3 differ diff --git a/public/sound/piano/OutOfBound.mp3 b/public/sound/piano/OutOfBound.mp3 new file mode 120000 index 00000000..76800caa --- /dev/null +++ b/public/sound/piano/OutOfBound.mp3 @@ -0,0 +1 @@ +../standard/OutOfBound.mp3 \ No newline at end of file diff --git a/public/sound/robot/Capture.mp3 b/public/sound/robot/Capture.mp3 new file mode 100644 index 00000000..c512697e Binary files /dev/null and b/public/sound/robot/Capture.mp3 differ diff --git a/public/sound/robot/Check.mp3 b/public/sound/robot/Check.mp3 new file mode 100644 index 00000000..203016f4 Binary files /dev/null and b/public/sound/robot/Check.mp3 differ diff --git a/public/sound/robot/Checkmate.mp3 b/public/sound/robot/Checkmate.mp3 new file mode 100644 index 00000000..2f866f9e Binary files /dev/null and b/public/sound/robot/Checkmate.mp3 differ diff --git a/public/sound/robot/Confirmation.mp3 b/public/sound/robot/Confirmation.mp3 new file mode 120000 index 00000000..13853d97 --- /dev/null +++ b/public/sound/robot/Confirmation.mp3 @@ -0,0 +1 @@ +../sfx/GenericNotify.mp3 \ No newline at end of file diff --git a/public/sound/robot/Draw.mp3 b/public/sound/robot/Draw.mp3 new file mode 100644 index 00000000..acde5445 Binary files /dev/null and b/public/sound/robot/Draw.mp3 differ diff --git a/public/sound/robot/Error.mp3 b/public/sound/robot/Error.mp3 new file mode 120000 index 00000000..261b68e0 --- /dev/null +++ b/public/sound/robot/Error.mp3 @@ -0,0 +1 @@ +../standard/Error.mp3 \ No newline at end of file diff --git a/public/sound/robot/Move.mp3 b/public/sound/robot/Move.mp3 new file mode 120000 index 00000000..77c3d2b4 --- /dev/null +++ b/public/sound/robot/Move.mp3 @@ -0,0 +1 @@ +../standard/Move.mp3 \ No newline at end of file diff --git a/public/sound/robot/OutOfBound.mp3 b/public/sound/robot/OutOfBound.mp3 new file mode 120000 index 00000000..76800caa --- /dev/null +++ b/public/sound/robot/OutOfBound.mp3 @@ -0,0 +1 @@ +../standard/OutOfBound.mp3 \ No newline at end of file diff --git a/public/sound/robot/TournamentOther.mp3 b/public/sound/robot/TournamentOther.mp3 new file mode 120000 index 00000000..08be50e1 --- /dev/null +++ b/public/sound/robot/TournamentOther.mp3 @@ -0,0 +1 @@ +../piano/TournamentOther.mp3 \ No newline at end of file diff --git a/public/sound/sfx/Capture.mp3 b/public/sound/sfx/Capture.mp3 new file mode 100644 index 00000000..3eef9260 Binary files /dev/null and b/public/sound/sfx/Capture.mp3 differ diff --git a/public/sound/sfx/Check.mp3 b/public/sound/sfx/Check.mp3 new file mode 100644 index 00000000..1c901efa Binary files /dev/null and b/public/sound/sfx/Check.mp3 differ diff --git a/public/sound/sfx/Checkmate.mp3 b/public/sound/sfx/Checkmate.mp3 new file mode 120000 index 00000000..5d03cc53 --- /dev/null +++ b/public/sound/sfx/Checkmate.mp3 @@ -0,0 +1 @@ +Check.mp3 \ No newline at end of file diff --git a/public/sound/sfx/Confirmation.mp3 b/public/sound/sfx/Confirmation.mp3 new file mode 120000 index 00000000..3f117c33 --- /dev/null +++ b/public/sound/sfx/Confirmation.mp3 @@ -0,0 +1 @@ +GenericNotify.mp3 \ No newline at end of file diff --git a/public/sound/sfx/Draw.mp3 b/public/sound/sfx/Draw.mp3 new file mode 100644 index 00000000..ba95dd67 Binary files /dev/null and b/public/sound/sfx/Draw.mp3 differ diff --git a/public/sound/sfx/Error.mp3 b/public/sound/sfx/Error.mp3 new file mode 120000 index 00000000..261b68e0 --- /dev/null +++ b/public/sound/sfx/Error.mp3 @@ -0,0 +1 @@ +../standard/Error.mp3 \ No newline at end of file diff --git a/public/sound/sfx/Move.mp3 b/public/sound/sfx/Move.mp3 new file mode 100644 index 00000000..7e846495 Binary files /dev/null and b/public/sound/sfx/Move.mp3 differ diff --git a/public/sound/sfx/OutOfBound.mp3 b/public/sound/sfx/OutOfBound.mp3 new file mode 120000 index 00000000..76800caa --- /dev/null +++ b/public/sound/sfx/OutOfBound.mp3 @@ -0,0 +1 @@ +../standard/OutOfBound.mp3 \ No newline at end of file diff --git a/public/sounds/capture.mp3 b/public/sound/standard/Capture.mp3 similarity index 100% rename from public/sounds/capture.mp3 rename to public/sound/standard/Capture.mp3 diff --git a/public/sound/standard/Check.mp3 b/public/sound/standard/Check.mp3 new file mode 120000 index 00000000..973048d4 --- /dev/null +++ b/public/sound/standard/Check.mp3 @@ -0,0 +1 @@ +../Silence.mp3 \ No newline at end of file diff --git a/public/sound/standard/Checkmate.mp3 b/public/sound/standard/Checkmate.mp3 new file mode 120000 index 00000000..5d03cc53 --- /dev/null +++ b/public/sound/standard/Checkmate.mp3 @@ -0,0 +1 @@ +Check.mp3 \ No newline at end of file diff --git a/public/sound/standard/Defeat.mp3 b/public/sound/standard/Defeat.mp3 new file mode 120000 index 00000000..3f117c33 --- /dev/null +++ b/public/sound/standard/Defeat.mp3 @@ -0,0 +1 @@ +GenericNotify.mp3 \ No newline at end of file diff --git a/public/sound/standard/Draw.mp3 b/public/sound/standard/Draw.mp3 new file mode 120000 index 00000000..3f117c33 --- /dev/null +++ b/public/sound/standard/Draw.mp3 @@ -0,0 +1 @@ +GenericNotify.mp3 \ No newline at end of file diff --git a/public/sounds/move.mp3 b/public/sound/standard/Move.mp3 similarity index 100% rename from public/sounds/move.mp3 rename to public/sound/standard/Move.mp3 diff --git a/public/sound/standard/NewChallenge.mp3 b/public/sound/standard/NewChallenge.mp3 new file mode 120000 index 00000000..c07d01ce --- /dev/null +++ b/public/sound/standard/NewChallenge.mp3 @@ -0,0 +1 @@ +SocialNotify.mp3 \ No newline at end of file diff --git a/public/sound/standard/NewPM.mp3 b/public/sound/standard/NewPM.mp3 new file mode 120000 index 00000000..c07d01ce --- /dev/null +++ b/public/sound/standard/NewPM.mp3 @@ -0,0 +1 @@ +SocialNotify.mp3 \ No newline at end of file diff --git a/public/sound/standard/TournamentOther.mp3 b/public/sound/standard/TournamentOther.mp3 new file mode 120000 index 00000000..08be50e1 --- /dev/null +++ b/public/sound/standard/TournamentOther.mp3 @@ -0,0 +1 @@ +../piano/TournamentOther.mp3 \ No newline at end of file diff --git a/public/sound/standard/Victory.mp3 b/public/sound/standard/Victory.mp3 new file mode 120000 index 00000000..3f117c33 --- /dev/null +++ b/public/sound/standard/Victory.mp3 @@ -0,0 +1 @@ +GenericNotify.mp3 \ No newline at end of file diff --git a/public/sound/woodland/Capture.mp3 b/public/sound/woodland/Capture.mp3 new file mode 100644 index 00000000..c5acb328 Binary files /dev/null and b/public/sound/woodland/Capture.mp3 differ diff --git a/public/sound/woodland/Check.mp3 b/public/sound/woodland/Check.mp3 new file mode 100644 index 00000000..9f188c02 Binary files /dev/null and b/public/sound/woodland/Check.mp3 differ diff --git a/public/sound/woodland/Checkmate.mp3 b/public/sound/woodland/Checkmate.mp3 new file mode 120000 index 00000000..5d03cc53 --- /dev/null +++ b/public/sound/woodland/Checkmate.mp3 @@ -0,0 +1 @@ +Check.mp3 \ No newline at end of file diff --git a/public/sound/woodland/Draw.mp3 b/public/sound/woodland/Draw.mp3 new file mode 100644 index 00000000..262be92d Binary files /dev/null and b/public/sound/woodland/Draw.mp3 differ diff --git a/public/sound/woodland/Error.mp3 b/public/sound/woodland/Error.mp3 new file mode 120000 index 00000000..261b68e0 --- /dev/null +++ b/public/sound/woodland/Error.mp3 @@ -0,0 +1 @@ +../standard/Error.mp3 \ No newline at end of file diff --git a/public/sound/woodland/Move.mp3 b/public/sound/woodland/Move.mp3 new file mode 100644 index 00000000..8cffb866 Binary files /dev/null and b/public/sound/woodland/Move.mp3 differ diff --git a/public/sounds/error.mp3 b/public/sounds/error.mp3 deleted file mode 100644 index af769c0e..00000000 Binary files a/public/sounds/error.mp3 and /dev/null differ diff --git a/src/hooks/useGameSound.ts b/src/hooks/useGameSound.ts new file mode 100644 index 00000000..8311d45c --- /dev/null +++ b/src/hooks/useGameSound.ts @@ -0,0 +1,43 @@ +import { Chess } from "chess.js"; +import { PrimitiveAtom, useAtomValue } from "jotai"; +import { useEffect, useRef } from "react"; +import { soundThemeAtom } from "@/sections/play/states"; + +export const useGameSound = (gameAtom: PrimitiveAtom) => { + const game = useAtomValue(gameAtom); + const soundTheme = useAtomValue(soundThemeAtom); + const previousMoveCount = useRef(game.history().length); + + useEffect(() => { + const history = game.history({ verbose: true }); + const currentMoveCount = history.length; + + if (currentMoveCount === previousMoveCount.current) { + return; + } + + previousMoveCount.current = currentMoveCount; + + const lastMove = history.at(-1); + if (!lastMove) return; + + let soundFile = "Move.mp3"; + + if (game.isGameOver()) { + if (game.isCheckmate()) { + soundFile = "Checkmate.mp3"; + } else if (game.isDraw()) { + soundFile = "Draw.mp3"; + } else { + soundFile = "Error.mp3"; + } + } else if (game.inCheck()) { + soundFile = "Check.mp3"; + } else if (lastMove.captured || lastMove.promotion) { + soundFile = "Capture.mp3"; + } + + const audio = new Audio(`/sound/${soundTheme}/${soundFile}`); + audio.play().catch((e) => console.error("Error playing sound:", e)); + }, [game, soundTheme]); +}; diff --git a/src/sections/analysis/board/index.tsx b/src/sections/analysis/board/index.tsx index 7f8d3cec..6c492a2e 100644 --- a/src/sections/analysis/board/index.tsx +++ b/src/sections/analysis/board/index.tsx @@ -12,6 +12,7 @@ import { useScreenSize } from "@/hooks/useScreenSize"; import { Color } from "@/types/enums"; import Board from "@/components/board"; import { usePlayersData } from "@/hooks/usePlayersData"; +import { useGameSound } from "@/hooks/useGameSound"; export default function BoardContainer() { const screenSize = useScreenSize(); @@ -31,6 +32,8 @@ export default function BoardContainer() { return Math.min(width - 700, height * 0.92); }, [screenSize]); + useGameSound(boardAtom); + return ( { if (!isEngineSupported(engineName)) { if (Stockfish16_1.isSupported()) { @@ -202,6 +208,25 @@ export default function EngineSettingsDialog({ open, onClose }: Props) { ))} + + + Sound Theme + + )} + + + + Sound Theme + + + diff --git a/src/sections/play/states.ts b/src/sections/play/states.ts index d12ec934..64994b71 100644 --- a/src/sections/play/states.ts +++ b/src/sections/play/states.ts @@ -1,5 +1,5 @@ import { DEFAULT_ENGINE } from "@/constants"; -import { Color, EngineName } from "@/types/enums"; +import { Color, EngineName, SoundTheme } from "@/types/enums"; import { CurrentPosition } from "@/types/eval"; import { Chess } from "chess.js"; import { atom } from "jotai"; @@ -10,3 +10,4 @@ export const playerColorAtom = atom(Color.White); export const enginePlayNameAtom = atom(DEFAULT_ENGINE); export const engineEloAtom = atom(1320); export const isGameInProgressAtom = atom(false); +export const soundThemeAtom = atom(SoundTheme.Standard); diff --git a/src/types/enums.ts b/src/types/enums.ts index 4faae0c5..b1280660 100644 --- a/src/types/enums.ts +++ b/src/types/enums.ts @@ -31,3 +31,14 @@ export enum Color { White = "w", Black = "b", } + +export enum SoundTheme { + Standard = "standard", + Piano = "piano", + NES = "nes", + SFX = "sfx", + Futuristic = "futuristic", + Lisp = "lisp", + Woodland = "woodland", + Robot = "robot", +}