ping changed to direct message

This commit is contained in:
Steffen 2021-01-27 22:05:32 +01:00
parent 7b301ffa73
commit ea0e6d2d72
1 changed files with 39 additions and 36 deletions

View File

@ -80,12 +80,14 @@ class LiveSession {
* @private * @private
*/ */
_ping() { _ping() {
this._send("ping", [
this._isSpectator,
this._store.state.session.playerId,
"latency"
]);
this._handlePing(); this._handlePing();
if (this._isSpectator) {
this._send("direct", {
host: [this._store.state.session.playerId, "latency"]
});
} else {
this._send("ping", [Object.keys(this._players).length, "latency"]);
}
clearTimeout(this._pingTimer); clearTimeout(this._pingTimer);
this._pingTimer = setTimeout(this._ping.bind(this), this._pingInterval); this._pingTimer = setTimeout(this._ping.bind(this), this._pingInterval);
} }
@ -462,41 +464,37 @@ class LiveSession {
/** /**
* Handle a ping message by another player / storyteller * Handle a ping message by another player / storyteller
* @param isSpectator * @param playerIdOrCount
* @param playerId * @param latency
* @param timestamp
* @private * @private
*/ */
_handlePing([isSpectator, playerId, latency] = []) { _handlePing([playerIdOrCount, latency] = []) {
const now = new Date().getTime(); const now = new Date().getTime();
// remove players that haven't sent a ping in twice the timespan if (!this._isSpectator) {
for (let player in this._players) { // remove players that haven't sent a ping in twice the timespan
if (now - this._players[player] > this._pingInterval * 2) { for (let player in this._players) {
delete this._players[player]; if (now - this._players[player] > this._pingInterval * 2) {
delete this._pings[player]; delete this._players[player];
delete this._pings[player];
}
} }
} // remove claimed seats from players that are no longer connected
// remove claimed seats from players that are no longer connected this._store.state.players.players.forEach(player => {
this._store.state.players.players.forEach(player => { if (player.id && !this._players[player.id]) {
if (!this._isSpectator && player.id && !this._players[player.id]) { this._store.commit("players/update", {
this._store.commit("players/update", { player,
player, property: "id",
property: "id", value: ""
value: "" });
}); }
} });
}); // store new player data
// store new player data if (playerIdOrCount) {
if (playerId) { this._players[playerIdOrCount] = now;
this._players[playerId] = now; const ping = parseInt(latency, 10);
const ping = parseInt(latency, 10); if (ping && ping > 0 && ping < 30 * 1000) {
if (ping && ping > 0 && ping < 30 * 1000) {
if (this._isSpectator && !isSpectator) {
// ping to ST
this._store.commit("session/setPing", ping);
} else if (!this._isSpectator) {
// ping to Players // ping to Players
this._pings[playerId] = ping; this._pings[playerIdOrCount] = ping;
const pings = Object.values(this._pings); const pings = Object.values(this._pings);
this._store.commit( this._store.commit(
"session/setPing", "session/setPing",
@ -504,10 +502,15 @@ class LiveSession {
); );
} }
} }
} else if (latency) {
// ping to ST
this._store.commit("session/setPing", parseInt(latency, 10));
} }
this._store.commit( this._store.commit(
"session/setPlayerCount", "session/setPlayerCount",
Object.keys(this._players).length this._isSpectator
? playerIdOrCount || 0
: Object.keys(this._players).length
); );
} }