whose-turn-is-it/js/remoteState.js

183 lines
4.7 KiB
JavaScript
Raw Normal View History

2022-12-30 13:28:28 +01:00
document.addEventListener("alpine:init", () => {
Alpine.store("remoteState", {
players: [],
2023-01-05 12:42:04 +01:00
interval: null,
2022-12-30 13:28:28 +01:00
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
_currentPlayerTopic: null,
_gameStateTopic: null,
2023-01-05 12:58:08 +01:00
_lastGameState: 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
) {
2023-01-05 12:42:04 +01:00
this._updatePlayers({
[Alpine.store("localState").id]: Alpine.store("localState").name,
2023-01-05 13:05:28 +01:00
...this.players,
2023-01-05 12:42:04 +01:00
});
2022-12-30 13:28:28 +01:00
}
});
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;
}
});
2023-01-05 12:58:08 +01:00
Alpine.effect(() => {
if (this.connected) {
this.updateInterval(this.interval);
}
});
2022-12-30 13:28:28 +01:00
},
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 13:05:28 +01:00
const topicPrefix = `im.dorian.whos-turn-is-it.${btoa(
Alpine.store("localState").room
)}`;
2023-01-05 10:22:17 +01:00
this._gameStateTopic = `${topicPrefix}.gameState`;
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", () => {
2023-01-05 12:42:04 +01:00
setTimeout(() => {
2023-01-05 13:05:28 +01:00
if (!that.connected) {
2023-01-05 12:42:04 +01:00
// reset game if not connected after 5 seconds
that.clear();
}
}, 1000 * 5);
that._client.subscribe(that._gameStateTopic);
2023-01-05 10:22:17 +01:00
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 12:42:04 +01:00
if (topic === that._gameStateTopic) {
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 12:58:08 +01:00
that._lastGameState = data;
2023-01-05 12:42:04 +01:00
that.players = data.players;
that.interval = data.interval;
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() {
2023-01-05 12:42:04 +01:00
this._updateCurrentPlayer(Alpine.store("localState").nextPlayer);
2022-12-30 13:28:28 +01:00
},
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._currentPlayerTopic = null;
Alpine.store("localState").nextPlayer = null;
2022-12-30 13:28:28 +01:00
},
clear() {
2023-01-05 12:42:04 +01:00
this._updatePlayers({
[Alpine.store("localState").id]: Alpine.store("localState").name,
});
this._updateCurrentPlayer(Alpine.store("localState").id);
},
2023-01-05 12:58:08 +01:00
updateInterval(interval) {
this._updateGameState(this.players, interval);
},
2023-01-05 12:42:04 +01:00
_updatePlayers(players) {
2023-01-05 13:05:28 +01:00
if (!this.interval) {
2023-01-05 12:42:04 +01:00
this.interval = 30;
}
this._updateGameState(players, this.interval);
},
_updateGameState(players, interval) {
2023-01-05 12:58:08 +01:00
const newGameState = {
version: 1,
players: players,
interval: interval,
2023-01-05 13:05:28 +01:00
};
2023-01-05 12:58:08 +01:00
2023-01-05 13:05:28 +01:00
if (
this._lastGameState &&
JSON.stringify(this._lastGameState) === JSON.stringify(newGameState)
)
return;
this._client.publish(this._gameStateTopic, JSON.stringify(newGameState), {
qos: 1,
retain: true,
});
2023-01-05 12:42:04 +01:00
},
_updateCurrentPlayer(id) {
2022-12-30 13:28:28 +01:00
this._client.publish(
2023-01-05 10:22:17 +01:00
this._currentPlayerTopic,
2022-12-30 13:28:28 +01:00
JSON.stringify({
2023-01-05 12:42:04 +01:00
id: id,
2022-12-30 13:28:28 +01:00
since: new Date().getTime(),
}),
{ qos: 1, retain: true }
);
2023-01-05 13:05:28 +01:00
},
2022-12-30 13:28:28 +01:00
});
2023-01-05 13:05:28 +01:00
});