2023-01-05 12:23:15 +01:00
|
|
|
document.addEventListener("alpine:init", () => {
|
2023-01-05 13:05:28 +01:00
|
|
|
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();
|
2023-01-05 12:23:15 +01:00
|
|
|
}
|
2023-01-05 13:05:28 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
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");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|