Skip to content

Commit 39fd751

Browse files
fix: fixed sound of already made moves (#12)
1 parent a56bb87 commit 39fd751

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

game/src/app/game/[slug]/page.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export default function Game(props: GameProps) {
3636

3737
const [game, setGame] = useState(new Chess());
3838
const lastPlayedMoveRef = useRef<number>(-1);
39+
const isFirstLoadRef = useRef(true);
3940

4041
const [selfMove] = useSound("../../../sounds/move-self.mp3");
4142
const [capture] = useSound("../../../sounds/capture.mp3");
@@ -81,10 +82,13 @@ export default function Game(props: GameProps) {
8182
const currentMoveNumber = numberOfMoves(board);
8283
setGame(board);
8384

84-
if (currentMoveNumber > lastPlayedMoveRef.current) {
85+
if (
86+
currentMoveNumber > lastPlayedMoveRef.current &&
87+
!isFirstLoadRef.current
88+
) {
8589
playSound(board, oldBoard);
86-
lastPlayedMoveRef.current = currentMoveNumber;
8790
}
91+
lastPlayedMoveRef.current = currentMoveNumber;
8892
},
8993
[playSound]
9094
);
@@ -98,6 +102,10 @@ export default function Game(props: GameProps) {
98102
if (numberOfMoves(remoteBoard) > numberOfMoves(game)) {
99103
updateBoard(remoteBoard, game);
100104
}
105+
106+
if (isFirstLoadRef.current) {
107+
isFirstLoadRef.current = false;
108+
}
101109
}, [remoteGame, game, updateBoard]);
102110

103111
const makeAMove = (move: Move | string) => {
@@ -119,6 +127,7 @@ export default function Game(props: GameProps) {
119127
return false;
120128
}
121129

130+
isFirstLoadRef.current = false;
122131
submit("move", { gameId: slug, move: move.san });
123132
return true;
124133
}

0 commit comments

Comments
 (0)