Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added apps/client/public/sounds/standard/Capture.mp3
Binary file not shown.
Binary file added apps/client/public/sounds/standard/Castle.mp3
Binary file not shown.
Binary file added apps/client/public/sounds/standard/Check.mp3
Binary file not shown.
Binary file added apps/client/public/sounds/standard/Game-End.mp3
Binary file not shown.
Binary file not shown.
Binary file added apps/client/public/sounds/standard/Move.mp3
Binary file not shown.
Binary file added apps/client/public/sounds/standard/Promote.mp3
Binary file not shown.
6 changes: 4 additions & 2 deletions apps/client/src/hooks/game/useChessGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import type {
} from "@chesslab/shared/types";
import { Chess } from "chess.js";
import { useCallback, useEffect, useState } from "react";
import { useSocket } from "../useSocket";
import { useSocket } from "@hooks/useSocket";
import type { ChessboardOptions, PieceDropHandlerArgs } from "react-chessboard";
import useTimer from "./useTimer";
import useTimer from "@hooks/game/useTimer";
import { playMoveSound } from "@services/sfx/index";

export function useChessGame(gameId?: string) {
const { socket } = useSocket();
Expand All @@ -34,6 +35,7 @@ export function useChessGame(gameId?: string) {

const handleMove = useCallback(
({ fen, turn, timeInfo }: MoveMadeEvent) => {
playMoveSound(chessGame);
chessGame.load(fen);
setChessPosition(fen);
setTurn(turn);
Expand Down
7 changes: 6 additions & 1 deletion apps/client/src/hooks/game/useComputerGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { Chess } from "chess.js";
import { useState } from "react";
import { getGameOverInfo } from "@chesslab/shared/utils";
import { useCompTimer } from "./useCompTimer";
import { playMoveSound } from "@services/sfx/index";

export function useComputerGame() {
const [chessGame] = useState(() => new Chess());
const [chessPosition, setChessPosition] = useState(() => new Chess().fen());
const [turn, setTurn] = useState<"w" | "b">("w");
const [gameHistory, setGameHistory] = useState<string[]>([]);
const [side] = useState<"w" | "b">(() => (Math.random() < 0.5 ? "w" : "b"));
// const [side] = useState<"w" | "b">(() => (Math.random() < 0.5 ? "w" : "b"));
const [side] = useState<"w" | "b">(() => "w");
Comment thread
MohamedSayed0573 marked this conversation as resolved.
Comment thread
MohamedSayed0573 marked this conversation as resolved.
Comment thread
MohamedSayed0573 marked this conversation as resolved.

let gameOverInfo = getGameOverInfo(chessGame);

Expand All @@ -37,6 +39,7 @@ export function useComputerGame() {
setChessPosition(chessGame.fen());
setTurn(chessGame.turn());
setGameHistory(chessGame.history());
playMoveSound(chessGame);
},
});

Expand All @@ -53,6 +56,8 @@ export function useComputerGame() {
if (!move) return false;

setChessPosition(chessGame.fen());

playMoveSound(chessGame);
setTurn(chessGame.turn());
setGameHistory(chessGame.history());

Expand Down
44 changes: 44 additions & 0 deletions apps/client/src/services/sfx/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { Chess } from "chess.js";

const sounds = {
move: new Audio("/sounds/standard/Move.mp3"),
check: new Audio("/sounds/standard/Check.mp3"),
capture: new Audio("/sounds/standard/Capture.mp3"),
checkmate: new Audio("/sounds/standard/Game-End.mp3"),
castle: new Audio("/sounds/standard/Castle.mp3"),
promotion: new Audio("/sounds/standard/Promote.mp3"),
defeat: new Audio("/sounds/standard/GenericNotify.mp3"),
draw: new Audio("/sounds/standard/GenericNotify.mp3"),
};

function playSound(sound: keyof typeof sounds) {
const audio = sounds[sound];

audio.currentTime = 0;
audio.play();
Comment thread
MohamedSayed0573 marked this conversation as resolved.
}
Comment thread
MohamedSayed0573 marked this conversation as resolved.

export function playMoveSound(chess: Chess) {
if (chess.isGameOver()) {
if (chess.isCheckmate()) playSound("checkmate");
else if (chess.isDraw()) playSound("draw");
else playSound("defeat");
return;
Comment thread
MohamedSayed0573 marked this conversation as resolved.
}

const moves = chess.history({ verbose: true });
if (moves.length > 0) {
const lastMove = moves[moves.length - 1];
if (lastMove?.san.endsWith("+")) {
playSound("check");
} else if (lastMove?.isCapture()) {
playSound("capture");
} else if (lastMove?.isKingsideCastle() || lastMove?.isQueensideCastle()) {
playSound("castle");
} else if (lastMove?.isPromotion()) {
playSound("promotion");
} else {
playSound("move");
}
}
}
3 changes: 2 additions & 1 deletion apps/client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"@pages/*": ["./src/pages/*"],
"@stockfish/*": ["./src/stockfish/*"],
"@app-types/*": ["./src/types/*"],
"@utils/*": ["./src/utils/*"]
"@utils/*": ["./src/utils/*"],
"@services/*": ["./src/services/*"]
}
},
"include": ["vite.config.ts", "src"]
Expand Down
1 change: 1 addition & 0 deletions apps/client/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default defineConfig({
"@stockfish": path.resolve(__dirname, "./src/stockfish"),
"@app-types": path.resolve(__dirname, "./src/types"),
"@utils": path.resolve(__dirname, "./src/utils"),
"@services": path.resolve(__dirname, "./src/services"),
},
},
server: {
Expand Down