diff --git a/src/lib/chess.ts b/src/lib/chess.ts index 15ca5f5..ef0d7d0 100644 --- a/src/lib/chess.ts +++ b/src/lib/chess.ts @@ -317,11 +317,10 @@ export const getCapturedPieces = ( new RegExp(piece, "g") )?.length; const newPiece = pieceFenToSymbol[piece] ?? piece; - if (!count) return { piece: newPiece, count }; return { piece: newPiece, - count: count - (piecesLeftCount ?? 0), + count: Math.max(0, count - (piecesLeftCount ?? 0)), }; }); }; diff --git a/src/lib/sounds.ts b/src/lib/sounds.ts index 1c34639..705d6fd 100644 --- a/src/lib/sounds.ts +++ b/src/lib/sounds.ts @@ -1,10 +1,16 @@ import { Move } from "chess.js"; import { getWhoIsCheckmated, isCheck } from "./chess"; +let audioContext: AudioContext | null = null; let audio: HTMLAudioElement | null = null; const playSound = async (url: string) => { - if (!audio) audio = new Audio(); + if (!audio) { + audioContext = new AudioContext(); + audio = new Audio(); + const source = audioContext.createMediaElementSource(audio); + source.connect(audioContext.destination); + } audio.src = url; try {