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,13 +464,13 @@ 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();
if (!this._isSpectator) {
// remove players that haven't sent a ping in twice the timespan // remove players that haven't sent a ping in twice the timespan
for (let player in this._players) { for (let player in this._players) {
if (now - this._players[player] > this._pingInterval * 2) { if (now - this._players[player] > this._pingInterval * 2) {
@ -478,7 +480,7 @@ class LiveSession {
} }
// 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 (!this._isSpectator && player.id && !this._players[player.id]) { if (player.id && !this._players[player.id]) {
this._store.commit("players/update", { this._store.commit("players/update", {
player, player,
property: "id", property: "id",
@ -487,16 +489,12 @@ class LiveSession {
} }
}); });
// store new player data // store new player data
if (playerId) { if (playerIdOrCount) {
this._players[playerId] = now; this._players[playerIdOrCount] = 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
); );
} }