From 554e5918e8c4b936f0760d85f5920db66ec008fe Mon Sep 17 00:00:00 2001 From: dvorakchen Date: Sat, 6 Jul 2024 12:36:48 +0800 Subject: [PATCH] refactor: play sound when moved --- .../wwwroot/scripts/chessboard/chessboard.js | 39 ++++------- .../wwwroot/scripts/chessboard/sound.js | 67 +++++++++++++++++++ 2 files changed, 81 insertions(+), 25 deletions(-) create mode 100644 src/Aiursoft.ChessServer/wwwroot/scripts/chessboard/sound.js diff --git a/src/Aiursoft.ChessServer/wwwroot/scripts/chessboard/chessboard.js b/src/Aiursoft.ChessServer/wwwroot/scripts/chessboard/chessboard.js index 16e3a59..5e7d51b 100644 --- a/src/Aiursoft.ChessServer/wwwroot/scripts/chessboard/chessboard.js +++ b/src/Aiursoft.ChessServer/wwwroot/scripts/chessboard/chessboard.js @@ -1,5 +1,10 @@ import { Chess } from "../../node_modules/chess.js/dist/esm/chess.js"; - +import { + initSoundForMobile, + initSoundForPC, + playSoundForPC, + playSoundForMobile, +} from "./sound.js"; import { buildOnDragStart, buildOnDrop, @@ -70,33 +75,17 @@ function AnduinChessBoard(color) { }; this._initSound = () => { - if (this.soundControl === null) { - return; - } - - this.soundControl.muted = true; - - const activeSound = () => { - if (this.soundControl.played.length === 0) { - this.soundControl.play(); - } else { - document.body.removeEventListener("click", activeSound); - } - }; - - document.body.addEventListener("click", activeSound); - }; - - this._playSound = () => { - if (this.soundControl !== null) { - this.soundControl.currentTime = 0; - this.soundControl.muted = false; - setTimeout(() => { - this.soundControl.muted = true; - }, 1500); + if (/Mobile/.test(navigator.userAgent)) { + initSoundForMobile.bind(this)(this); + this._playSound = playSoundForMobile.bind(this); + } else { + initSoundForPC.bind(this)(this); + this._playSound = playSoundForPC.bind(this); } }; + this._playSound = () => {}; + /** * render some styles, like highlight, red light */ diff --git a/src/Aiursoft.ChessServer/wwwroot/scripts/chessboard/sound.js b/src/Aiursoft.ChessServer/wwwroot/scripts/chessboard/sound.js new file mode 100644 index 0000000..0357e90 --- /dev/null +++ b/src/Aiursoft.ChessServer/wwwroot/scripts/chessboard/sound.js @@ -0,0 +1,67 @@ +const MINIMAL_BUT_NOT_MUTE = 0.0001; + +function initSoundForPC() { + if (this.soundControl === null) { + return; + } + + this.soundControl.volume = MINIMAL_BUT_NOT_MUTE; + + const activeSound = () => { + if (this.soundControl.played.length === 0) { + this.soundControl.play(); + } else { + document.body.removeEventListener("click", activeSound); + } + }; + + document.body.addEventListener("click", activeSound); +} + +function initSoundForMobile() { + if (this.soundControl === null) { + return; + } + + this.soundControl.muted = true; + + const activeSound = () => { + if (this.soundControl.played.length === 0) { + this.soundControl.play(); + } else { + document.body.removeEventListener("click", activeSound); + } + }; + + document.body.addEventListener("click", activeSound); +} + +function playSoundForPC() { + const START = 0; + // 'this' should be points anduinChessBoard + if (this.soundControl !== null) { + this.soundControl.currentTime = START; + this.soundControl.volume = 1; + setTimeout(() => { + this.soundControl.volume = MINIMAL_BUT_NOT_MUTE; + }, 500); + } +} + +function playSoundForMobile() { + // 'this' should be points anduinChessBoard + if (this.soundControl !== null) { + this.soundControl.currentTime = 0; + this.soundControl.muted = false; + setTimeout(() => { + this.soundControl.muted = true; + }, 500); + } +} + +export { + initSoundForPC, + initSoundForMobile, + playSoundForPC, + playSoundForMobile, +};