Feat: support interval in game state

This commit is contained in:
Dorian Zedler 2023-01-05 12:42:04 +01:00
parent 4a766adfda
commit 98dad45fe7
Signed by: dorian
GPG key ID: 989DE36109AFA354
2 changed files with 47 additions and 32 deletions

View file

@ -34,7 +34,7 @@ function Timer() {
this.time = null; this.time = null;
} else { } else {
this.time = parseInt( this.time = parseInt(
(90000 - (new Date().getTime() - lastPlayerSwitch)) / 1000 ((Alpine.store("remoteState").interval * 1000) - (new Date().getTime() - lastPlayerSwitch)) / 1000
); );
} }
}, 100); }, 100);

View file

@ -1,6 +1,7 @@
document.addEventListener("alpine:init", () => { document.addEventListener("alpine:init", () => {
Alpine.store("remoteState", { Alpine.store("remoteState", {
players: [], players: [],
interval: null,
currentPlayer: null, currentPlayer: null,
lastPlayerSwitch: null, lastPlayerSwitch: null,
connected: false, connected: false,
@ -17,14 +18,10 @@ document.addEventListener("alpine:init", () => {
Object.keys(this.players).indexOf(Alpine.store("localState").id) === Object.keys(this.players).indexOf(Alpine.store("localState").id) ===
-1 -1
) { ) {
this._client.publish( this._updatePlayers({
this._playersTopic, [Alpine.store("localState").id]: Alpine.store("localState").name,
JSON.stringify({ ...this.players
[Alpine.store("localState").id]: Alpine.store("localState").name, });
...this.players,
}),
{ qos: 1, retain: true }
);
} }
}); });
@ -78,8 +75,14 @@ document.addEventListener("alpine:init", () => {
this._client = mqtt.connect(url, options); this._client = mqtt.connect(url, options);
this._client.on("connect", () => { this._client.on("connect", () => {
// Subscribe to a topic setTimeout(() => {
that._client.subscribe(that._playersTopic); if(!that.connected) {
// reset game if not connected after 5 seconds
that.clear();
}
}, 1000 * 5);
that._client.subscribe(that._gameStateTopic);
that._client.subscribe(that._currentPlayerTopic); that._client.subscribe(that._currentPlayerTopic);
}); });
@ -87,12 +90,13 @@ document.addEventListener("alpine:init", () => {
// message is Buffer // message is Buffer
console.log(topic, message.toString()); console.log(topic, message.toString());
if (topic === that._playersTopic) { if (topic === that._gameStateTopic) {
const data = JSON.parse(message.toString()); const data = JSON.parse(message.toString());
if (!that.connected) { if (!that.connected) {
that.connected = true; that.connected = true;
} }
that.players = data; that.players = data.players;
that.interval = data.interval;
} else if (topic === that._currentPlayerTopic) { } else if (topic === that._currentPlayerTopic) {
const data = JSON.parse(message.toString()); const data = JSON.parse(message.toString());
that.currentPlayer = data.id; that.currentPlayer = data.id;
@ -102,14 +106,7 @@ document.addEventListener("alpine:init", () => {
}, },
giveTurnToNextPlayer() { giveTurnToNextPlayer() {
this._client.publish( this._updateCurrentPlayer(Alpine.store("localState").nextPlayer);
this._currentPlayerTopic,
JSON.stringify({
id: Alpine.store("localState").nextPlayer,
since: new Date().getTime(),
}),
{ qos: 1, retain: true }
);
}, },
disconnect() { disconnect() {
@ -122,28 +119,46 @@ document.addEventListener("alpine:init", () => {
this.isMyTurn = false; this.isMyTurn = false;
this._gameStateTopic = null; this._gameStateTopic = null;
this._playersTopic = null;
this._currentPlayerTopic = null; this._currentPlayerTopic = null;
Alpine.store("localState").nextPlayer = null; Alpine.store("localState").nextPlayer = null;
}, },
clear() { clear() {
this._updatePlayers({
[Alpine.store("localState").id]: Alpine.store("localState").name,
});
this._updateCurrentPlayer(Alpine.store("localState").id);
},
_updatePlayers(players) {
if(!this.interval) {
this.interval = 30;
}
this._updateGameState(players, this.interval);
},
_updateGameState(players, interval) {
this._client.publish( this._client.publish(
this._playersTopic, this._gameStateTopic,
JSON.stringify({ JSON.stringify({
[Alpine.store("localState").id]: Alpine.store("localState").name, version: 1,
}), players: players,
{ qos: 1, retain: true } interval: interval,
);
this._client.publish(
this._currentPlayerTopic,
JSON.stringify({
id: Alpine.store("localState").id,
since: new Date().getTime(),
}), }),
{ qos: 1, retain: true } { qos: 1, retain: true }
); );
}, },
_updateCurrentPlayer(id) {
this._client.publish(
this._currentPlayerTopic,
JSON.stringify({
id: id,
since: new Date().getTime(),
}),
{ qos: 1, retain: true }
);
}
}); });
}); });