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