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
*/
_ping() {
this._send("ping", [
this._isSpectator,
this._store.state.session.playerId,
"latency"
]);
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);
this._pingTimer = setTimeout(this._ping.bind(this), this._pingInterval);
}
@ -462,13 +464,13 @@ class LiveSession {
/**
* Handle a ping message by another player / storyteller
* @param isSpectator
* @param playerId
* @param timestamp
* @param playerIdOrCount
* @param latency
* @private
*/
_handlePing([isSpectator, playerId, latency] = []) {
_handlePing([playerIdOrCount, latency] = []) {
const now = new Date().getTime();
if (!this._isSpectator) {
// remove players that haven't sent a ping in twice the timespan
for (let player in this._players) {
if (now - this._players[player] > this._pingInterval * 2) {
@ -478,7 +480,7 @@ class LiveSession {
}
// remove claimed seats from players that are no longer connected
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", {
player,
property: "id",
@ -487,16 +489,12 @@ class LiveSession {
}
});
// store new player data
if (playerId) {
this._players[playerId] = now;
if (playerIdOrCount) {
this._players[playerIdOrCount] = now;
const ping = parseInt(latency, 10);
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
this._pings[playerId] = ping;
this._pings[playerIdOrCount] = ping;
const pings = Object.values(this._pings);
this._store.commit(
"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(
"session/setPlayerCount",
Object.keys(this._players).length
this._isSpectator
? playerIdOrCount || 0
: Object.keys(this._players).length
);
}