whose-turn-is-it/js/localState.js
2023-01-05 13:05:28 +01:00

52 lines
1.1 KiB
JavaScript

document.addEventListener("alpine:init", () => {
Alpine.store("localState", {
room: null,
nextPlayer: null,
name: "",
id: "",
init() {
Alpine.effect(() => {
const remoteState = Alpine.store("remoteState");
if (this.room) {
remoteState.connect();
} else if (remoteState && remoteState.connected) {
remoteState.disconnect();
}
});
this.restore();
Alpine.effect(() => {
// write stuff to local storage
if (this.room) {
localStorage.setItem("room", this.room);
} else {
localStorage.removeItem("room");
}
localStorage.setItem("name", this.name);
localStorage.setItem("id", this.id);
});
},
join(name, room) {
this.room = room;
this.name = name;
},
leave() {
this.room = null;
},
restore() {
this.id = localStorage.getItem("id");
if (!this.id) {
this.id = uuidv4();
}
this.name = localStorage.getItem("name");
this.room = localStorage.getItem("room");
},
});
});