Merge branch 'master' into 'master'

refactor: play sound when moved

See merge request aiursoft/chessserver!69
This commit is contained in:
dvorak chen
2024-07-06 04:43:22 +00:00
2 changed files with 81 additions and 25 deletions
@@ -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
*/
@@ -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,
};