Feat: change interval
This commit is contained in:
parent
98dad45fe7
commit
f42e8478e1
3 changed files with 37 additions and 5 deletions
|
@ -72,6 +72,9 @@
|
||||||
<details>
|
<details>
|
||||||
<summary>This room</summary>
|
<summary>This room</summary>
|
||||||
|
|
||||||
|
<label for="roomForm_interval">Interval (seconds):</label>
|
||||||
|
<input id="roomForm_interval" type="number" x-model.throttle.1000ms="$store.remoteState.interval" placeholder="Interval" />
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
Other players:
|
Other players:
|
||||||
<ul>
|
<ul>
|
||||||
|
|
14
js/index.js
14
js/index.js
|
@ -23,6 +23,20 @@ function JoinForm() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function RoomForm() {
|
||||||
|
return {
|
||||||
|
formData: {
|
||||||
|
interval: null,
|
||||||
|
},
|
||||||
|
init() {
|
||||||
|
this.formData.interval = Alpine.store("remoteState").interval;
|
||||||
|
},
|
||||||
|
submitForm() {
|
||||||
|
Alpine.store("remoteState").updateInterval(this.formData.interval);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function Timer() {
|
function Timer() {
|
||||||
return {
|
return {
|
||||||
time: 0,
|
time: 0,
|
||||||
|
|
|
@ -10,6 +10,7 @@ document.addEventListener("alpine:init", () => {
|
||||||
_client: null,
|
_client: null,
|
||||||
_currentPlayerTopic: null,
|
_currentPlayerTopic: null,
|
||||||
_gameStateTopic: null,
|
_gameStateTopic: null,
|
||||||
|
_lastGameState: null,
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
Alpine.effect(() => {
|
Alpine.effect(() => {
|
||||||
|
@ -52,6 +53,12 @@ document.addEventListener("alpine:init", () => {
|
||||||
Alpine.store("localState").nextPlayer = nextPlayer;
|
Alpine.store("localState").nextPlayer = nextPlayer;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Alpine.effect(() => {
|
||||||
|
if (this.connected) {
|
||||||
|
this.updateInterval(this.interval);
|
||||||
|
}
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
connect() {
|
connect() {
|
||||||
|
@ -95,6 +102,7 @@ document.addEventListener("alpine:init", () => {
|
||||||
if (!that.connected) {
|
if (!that.connected) {
|
||||||
that.connected = true;
|
that.connected = true;
|
||||||
}
|
}
|
||||||
|
that._lastGameState = data;
|
||||||
that.players = data.players;
|
that.players = data.players;
|
||||||
that.interval = data.interval;
|
that.interval = data.interval;
|
||||||
} else if (topic === that._currentPlayerTopic) {
|
} else if (topic === that._currentPlayerTopic) {
|
||||||
|
@ -131,6 +139,10 @@ document.addEventListener("alpine:init", () => {
|
||||||
this._updateCurrentPlayer(Alpine.store("localState").id);
|
this._updateCurrentPlayer(Alpine.store("localState").id);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
updateInterval(interval) {
|
||||||
|
this._updateGameState(this.players, interval);
|
||||||
|
},
|
||||||
|
|
||||||
_updatePlayers(players) {
|
_updatePlayers(players) {
|
||||||
if(!this.interval) {
|
if(!this.interval) {
|
||||||
this.interval = 30;
|
this.interval = 30;
|
||||||
|
@ -139,13 +151,16 @@ document.addEventListener("alpine:init", () => {
|
||||||
},
|
},
|
||||||
|
|
||||||
_updateGameState(players, interval) {
|
_updateGameState(players, interval) {
|
||||||
|
const newGameState = {
|
||||||
|
version: 1,
|
||||||
|
players: players,
|
||||||
|
interval: interval,
|
||||||
|
}
|
||||||
|
|
||||||
|
if(this._lastGameState && JSON.stringify(this._lastGameState) === JSON.stringify(newGameState)) return;
|
||||||
this._client.publish(
|
this._client.publish(
|
||||||
this._gameStateTopic,
|
this._gameStateTopic,
|
||||||
JSON.stringify({
|
JSON.stringify(newGameState),
|
||||||
version: 1,
|
|
||||||
players: players,
|
|
||||||
interval: interval,
|
|
||||||
}),
|
|
||||||
{ qos: 1, retain: true }
|
{ qos: 1, retain: true }
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue