Feat: keep if after leaving

This commit is contained in:
Dorian Zedler 2022-12-29 20:30:09 +01:00
parent d1c7f15bd7
commit 196cbc26e2
Signed by: dorian
GPG key ID: 989DE36109AFA354

View file

@ -59,8 +59,8 @@ document.addEventListener('alpine:init', () => {
leave() { leave() {
this.joined = false; this.joined = false;
this.name = ""; this.name = "";
this.id = "";
localStorage.clear(); localStorage.clear();
localStorage.setItem("id", this.id)
Alpine.store("remoteState").disconnect(); Alpine.store("remoteState").disconnect();
}, },
@ -88,13 +88,13 @@ document.addEventListener('alpine:init', () => {
this._client.publish('im.dorian.whos-turn-is-it.players', JSON.stringify({ this._client.publish('im.dorian.whos-turn-is-it.players', JSON.stringify({
[Alpine.store("localState").id]: Alpine.store("localState").name, [Alpine.store("localState").id]: Alpine.store("localState").name,
...this.players ...this.players
}), { qos:1, retain: true }) }), { qos: 1, retain: true })
} }
}) })
Alpine.effect(() => { Alpine.effect(() => {
this.lastPlayerSwitch = new Date().getTime(); this.lastPlayerSwitch = new Date().getTime();
if(this.currentPlayer == Alpine.store("localState").id) { if (this.currentPlayer == Alpine.store("localState").id) {
this.isMyTurn = true; this.isMyTurn = true;
} }
else { else {
@ -103,7 +103,7 @@ document.addEventListener('alpine:init', () => {
}) })
Alpine.effect(() => { Alpine.effect(() => {
if(Alpine.store("localState").nextPlayer == null || Alpine.store("localState").nextPlayer == Alpine.store("localState").id || !Object.keys(this.players).includes(Alpine.store("localState").nextPlayer)) { if (Alpine.store("localState").nextPlayer == null || Alpine.store("localState").nextPlayer == Alpine.store("localState").id || !Object.keys(this.players).includes(Alpine.store("localState").nextPlayer)) {
const players = Object.keys(this.players).sort(); const players = Object.keys(this.players).sort();
const nextPlayer = players[(players.indexOf(Alpine.store("localState").id) + 1) % players.length] const nextPlayer = players[(players.indexOf(Alpine.store("localState").id) + 1) % players.length]
Alpine.store("localState").nextPlayer = nextPlayer; Alpine.store("localState").nextPlayer = nextPlayer;
@ -111,7 +111,7 @@ document.addEventListener('alpine:init', () => {
}) })
Alpine.effect(() => { Alpine.effect(() => {
if(this.isMyTurn) { if (this.isMyTurn) {
Alpine.store("audio").playDing() Alpine.store("audio").playDing()
} }
}) })
@ -143,19 +143,19 @@ document.addEventListener('alpine:init', () => {
if (topic === "im.dorian.whos-turn-is-it.players") { if (topic === "im.dorian.whos-turn-is-it.players") {
const data = JSON.parse(message.toString()); const data = JSON.parse(message.toString());
if(!Alpine.store("remoteState").connected) { if (!Alpine.store("remoteState").connected) {
Alpine.store("remoteState").connected = true; Alpine.store("remoteState").connected = true;
} }
Alpine.store("remoteState").players = data; Alpine.store("remoteState").players = data;
} }
else if(topic === "im.dorian.whos-turn-is-it.currentPlayer") { else if (topic === "im.dorian.whos-turn-is-it.currentPlayer") {
Alpine.store("remoteState").currentPlayer = message.toString(); Alpine.store("remoteState").currentPlayer = message.toString();
} }
}) })
}, },
giveTurnToNextPlayer() { giveTurnToNextPlayer() {
this._client.publish('im.dorian.whos-turn-is-it.currentPlayer', Alpine.store("localState").nextPlayer, { qos:1, retain: true }) this._client.publish('im.dorian.whos-turn-is-it.currentPlayer', Alpine.store("localState").nextPlayer, { qos: 1, retain: true })
}, },
@ -166,8 +166,8 @@ document.addEventListener('alpine:init', () => {
clear() { clear() {
this._client.publish('im.dorian.whos-turn-is-it.players', JSON.stringify({ this._client.publish('im.dorian.whos-turn-is-it.players', JSON.stringify({
[Alpine.store("localState").id]: Alpine.store("localState").name, [Alpine.store("localState").id]: Alpine.store("localState").name,
}), { qos:1, retain: true }) }), { qos: 1, retain: true })
this._client.publish('im.dorian.whos-turn-is-it.currentPlayer', Alpine.store("localState").id, { qos:1, retain: true }) this._client.publish('im.dorian.whos-turn-is-it.currentPlayer', Alpine.store("localState").id, { qos: 1, retain: true })
} }
}) })
@ -186,11 +186,11 @@ document.addEventListener('alpine:init', () => {
}, },
testPermission() { testPermission() {
if(this.isEnabled) return; if (this.isEnabled) return;
this.audioPlayer.src = 'silence.mp3'; this.audioPlayer.src = 'silence.mp3';
this.audioPlayer.play().then(() => { this.audioPlayer.play().then(() => {
this.isAvailable = true this.isAvailable = true
this.isEnabled = localStorage.getItem("audio_enabled") === false ? false:true this.isEnabled = localStorage.getItem("audio_enabled") === false ? false : true
this.hasBeenTested = true this.hasBeenTested = true
}).catch((error) => { }).catch((error) => {
console.warn("Audio permission not granted!") console.warn("Audio permission not granted!")
@ -201,7 +201,7 @@ document.addEventListener('alpine:init', () => {
}, },
playDing() { playDing() {
if(!this.isEnabled) return; if (!this.isEnabled) return;
this.playSound('ding.mp3') this.playSound('ding.mp3')
}, },