whose-turn-is-it/index.js

234 lines
7.4 KiB
JavaScript
Raw Normal View History

2022-12-29 17:19:36 +01:00
function JoinForm() {
return {
formData: {
name: ""
},
submitForm() {
Alpine.store("localState").join(this.formData.name)
}
}
}
function uuidv4() {
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
}
function Timer() {
return {
time: 0,
init() {
2022-12-29 20:30:09 +01:00
setInterval(() => {
const lastPlayerSwitch = Alpine.store("remoteState").lastPlayerSwitch
console.log(lastPlayerSwitch)
if(!lastPlayerSwitch) {
this.time = null
}
else {
this.time = parseInt((90000 - ((new Date()).getTime() - lastPlayerSwitch)) / 1000);
}
}, 100);
2022-12-29 17:19:36 +01:00
},
}
2022-12-29 17:42:47 +01:00
}
2022-12-29 17:19:36 +01:00
document.addEventListener('alpine:init', () => {
console.log("Alpine.js is ready to go!")
Alpine.store("localState", {
joined: false,
nextPlayer: null,
name: "",
id: "",
init() {
Alpine.effect(() => {
if (this.joined) {
Alpine.store("remoteState").connect();
}
})
this.restore();
},
join(name) {
this.joined = true;
this.name = name;
this.id = uuidv4();
localStorage.clear();
localStorage.setItem("joined", this.joined)
localStorage.setItem("name", this.name);
localStorage.setItem("id", this.id)
},
leave() {
this.joined = false;
this.name = "";
localStorage.clear();
2022-12-29 20:30:09 +01:00
localStorage.setItem("id", this.id)
2022-12-29 17:19:36 +01:00
Alpine.store("remoteState").disconnect();
},
restore() {
const joined = localStorage.getItem("joined");
if (joined) {
this.joined = true;
this.name = localStorage.getItem("name");
this.id = localStorage.getItem("id");
}
},
})
Alpine.store("remoteState", {
players: [],
currentPlayer: null,
lastPlayerSwitch: null,
2022-12-29 17:19:36 +01:00
connected: false,
2022-12-29 17:59:24 +01:00
isMyTurn: false,
2022-12-29 17:19:36 +01:00
_client: null,
init() {
Alpine.effect(() => {
if (this.connected && Object.keys(this.players).indexOf(Alpine.store("localState").id) === -1) {
this._client.publish('im.dorian.whos-turn-is-it.players', JSON.stringify({
[Alpine.store("localState").id]: Alpine.store("localState").name,
...this.players
2022-12-29 20:30:09 +01:00
}), { qos: 1, retain: true })
2022-12-29 17:19:36 +01:00
}
})
Alpine.effect(() => {
2022-12-29 20:30:09 +01:00
if (this.currentPlayer == Alpine.store("localState").id) {
2022-12-29 17:59:24 +01:00
this.isMyTurn = true;
}
else {
this.isMyTurn = false;
2022-12-29 17:19:36 +01:00
}
})
Alpine.effect(() => {
2022-12-29 20:30:09 +01:00
if (Alpine.store("localState").nextPlayer == null || Alpine.store("localState").nextPlayer == Alpine.store("localState").id || !Object.keys(this.players).includes(Alpine.store("localState").nextPlayer)) {
2022-12-29 17:19:36 +01:00
const players = Object.keys(this.players).sort();
const nextPlayer = players[(players.indexOf(Alpine.store("localState").id) + 1) % players.length]
Alpine.store("localState").nextPlayer = nextPlayer;
}
})
2022-12-29 18:09:05 +01:00
Alpine.effect(() => {
2022-12-29 20:30:09 +01:00
if (this.isMyTurn) {
2022-12-29 20:26:40 +01:00
Alpine.store("audio").playDing()
2022-12-29 18:09:05 +01:00
}
})
2022-12-29 17:19:36 +01:00
},
connect() {
2022-12-29 17:24:58 +01:00
const url = 'wss://broker.emqx.io:8084/mqtt'
2022-12-29 17:19:36 +01:00
const options = {
// Clean session
clean: true,
connectTimeout: 4000,
// Authentication
clientId: Alpine.store("localState").id,
}
this._client = mqtt.connect(url, options)
const client = this._client;
this._client.on('connect', function () {
// Subscribe to a topic
client.subscribe('im.dorian.whos-turn-is-it.players')
client.subscribe('im.dorian.whos-turn-is-it.currentPlayer')
})
this._client.on('message', function (topic, message) {
// message is Buffer
console.log(topic, message.toString())
if (topic === "im.dorian.whos-turn-is-it.players") {
const data = JSON.parse(message.toString());
2022-12-29 20:30:09 +01:00
if (!Alpine.store("remoteState").connected) {
2022-12-29 17:19:36 +01:00
Alpine.store("remoteState").connected = true;
}
Alpine.store("remoteState").players = data;
}
2022-12-29 20:30:09 +01:00
else if (topic === "im.dorian.whos-turn-is-it.currentPlayer") {
const data = JSON.parse(message.toString());
Alpine.store("remoteState").currentPlayer = data.id;
Alpine.store("remoteState").lastPlayerSwitch = data.since;
2022-12-29 17:19:36 +01:00
}
})
},
giveTurnToNextPlayer() {
this._client.publish('im.dorian.whos-turn-is-it.currentPlayer', JSON.stringify({
id: Alpine.store("localState").nextPlayer,
since: new Date().getTime()
}), { qos: 1, retain: true })
2022-12-29 17:19:36 +01:00
},
disconnect() {
this._client.end(true);
},
clear() {
this._client.publish('im.dorian.whos-turn-is-it.players', JSON.stringify({
[Alpine.store("localState").id]: Alpine.store("localState").name,
2022-12-29 20:30:09 +01:00
}), { qos: 1, retain: true })
this._client.publish('im.dorian.whos-turn-is-it.currentPlayer', JSON.stringify({
id: Alpine.store("localState").id,
since: new Date().getTime()
}), { qos: 1, retain: true })
2022-12-29 17:19:36 +01:00
}
})
2022-12-29 20:26:40 +01:00
Alpine.store("audio", {
hasBeenTested: false,
isAvailable: false,
isEnabled: false,
audioPlayer: null,
init() {
this.audioPlayer = new Audio('silence.mp3');
Alpine.effect(() => {
localStorage.setItem("audio_enabled", this.isEnabled)
})
},
testPermission() {
2022-12-29 20:30:09 +01:00
if (this.isEnabled) return;
2022-12-29 20:26:40 +01:00
this.audioPlayer.src = 'silence.mp3';
this.audioPlayer.play().then(() => {
this.isAvailable = true
2022-12-29 20:30:09 +01:00
this.isEnabled = localStorage.getItem("audio_enabled") === false ? false : true
2022-12-29 20:26:40 +01:00
this.hasBeenTested = true
}).catch((error) => {
console.warn("Audio permission not granted!")
this.isAvailable = false
this.isEnabled = false
this.hasBeenTested = true
})
},
playDing() {
2022-12-29 20:30:09 +01:00
if (!this.isEnabled) return;
2022-12-29 20:26:40 +01:00
this.playSound('ding.mp3')
},
playSound(soundFile) {
if (!this.audioPlayer.paused || !this.isAvailable) {
return;
}
if (!this.audioPlayer.src.endsWith(soundFile)) {
this.audioPlayer.src = soundFile;
}
this.audioPlayer.play();
},
})
2022-12-29 17:19:36 +01:00
})