2020-05-09 19:47:00 +00:00
|
|
|
class LiveSession {
|
|
|
|
constructor(store) {
|
2020-11-29 21:17:02 +00:00
|
|
|
this._wss = "wss://live.clocktower.online:8080/";
|
2022-01-25 11:25:48 +00:00
|
|
|
// this._wss = "ws://localhost:8081/"; // uncomment if using local server with NODE_ENV=development
|
2020-05-24 20:13:52 +00:00
|
|
|
this._socket = null;
|
|
|
|
this._isSpectator = true;
|
|
|
|
this._gamestate = [];
|
|
|
|
this._store = store;
|
|
|
|
this._pingInterval = 30 * 1000; // 30 seconds between pings
|
|
|
|
this._pingTimer = null;
|
2020-09-21 09:19:46 +00:00
|
|
|
this._reconnectTimer = null;
|
2020-05-24 20:13:52 +00:00
|
|
|
this._players = {}; // map of players connected to a session
|
2020-09-16 09:39:36 +00:00
|
|
|
this._pings = {}; // map of player IDs to ping
|
2020-05-24 20:13:52 +00:00
|
|
|
// reconnect to previous session
|
|
|
|
if (this._store.state.session.sessionId) {
|
|
|
|
this.connect(this._store.state.session.sessionId);
|
|
|
|
}
|
2020-05-09 19:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open a new session for the passed channel.
|
|
|
|
* @param channel
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_open(channel) {
|
|
|
|
this.disconnect();
|
2020-09-21 09:19:46 +00:00
|
|
|
this._socket = new WebSocket(
|
2020-12-15 21:31:50 +00:00
|
|
|
this._wss +
|
|
|
|
channel +
|
|
|
|
"/" +
|
|
|
|
(this._isSpectator ? this._store.state.session.playerId : "host")
|
2020-09-21 09:19:46 +00:00
|
|
|
);
|
2020-05-24 20:13:52 +00:00
|
|
|
this._socket.addEventListener("message", this._handleMessage.bind(this));
|
|
|
|
this._socket.onopen = this._onOpen.bind(this);
|
2020-09-21 09:19:46 +00:00
|
|
|
this._socket.onclose = err => {
|
2020-05-24 20:13:52 +00:00
|
|
|
this._socket = null;
|
|
|
|
clearInterval(this._pingTimer);
|
|
|
|
this._pingTimer = null;
|
2020-09-21 09:19:46 +00:00
|
|
|
if (err.code !== 1000) {
|
|
|
|
// connection interrupted, reconnect after 3 seconds
|
|
|
|
this._store.commit("session/setReconnecting", true);
|
|
|
|
this._reconnectTimer = setTimeout(
|
|
|
|
() => this.connect(channel),
|
|
|
|
3 * 1000
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this._store.commit("session/setSessionId", "");
|
|
|
|
if (err.reason) alert(err.reason);
|
|
|
|
}
|
2020-05-09 19:47:00 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a message through the socket.
|
|
|
|
* @param command
|
|
|
|
* @param params
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_send(command, params) {
|
2020-05-24 20:13:52 +00:00
|
|
|
if (this._socket && this._socket.readyState === 1) {
|
|
|
|
this._socket.send(JSON.stringify([command, params]));
|
2020-05-09 19:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:54:27 +00:00
|
|
|
/**
|
|
|
|
* Send a message directly to a single playerId, if provided.
|
|
|
|
* Otherwise broadcast it.
|
|
|
|
* @param playerId player ID or "host", optional
|
|
|
|
* @param command
|
|
|
|
* @param params
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_sendDirect(playerId, command, params) {
|
|
|
|
if (playerId) {
|
|
|
|
this._send("direct", { [playerId]: [command, params] });
|
|
|
|
} else {
|
|
|
|
this._send(command, params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-09 19:47:00 +00:00
|
|
|
/**
|
|
|
|
* Open event handler for socket.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_onOpen() {
|
2020-05-24 20:13:52 +00:00
|
|
|
if (this._isSpectator) {
|
2021-01-29 20:54:27 +00:00
|
|
|
this._sendDirect(
|
|
|
|
"host",
|
|
|
|
"getGamestate",
|
|
|
|
this._store.state.session.playerId
|
|
|
|
);
|
2020-05-09 19:47:00 +00:00
|
|
|
} else {
|
|
|
|
this.sendGamestate();
|
|
|
|
}
|
2020-05-24 20:13:52 +00:00
|
|
|
this._ping();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a ping message with player ID and ST flag.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_ping() {
|
2020-05-27 20:55:33 +00:00
|
|
|
this._handlePing();
|
2020-09-16 09:39:36 +00:00
|
|
|
this._send("ping", [
|
2021-01-29 20:54:27 +00:00
|
|
|
this._isSpectator
|
|
|
|
? this._store.state.session.playerId
|
|
|
|
: Object.keys(this._players).length,
|
2020-09-21 09:19:46 +00:00
|
|
|
"latency"
|
2020-09-16 09:39:36 +00:00
|
|
|
]);
|
2020-05-24 20:13:52 +00:00
|
|
|
clearTimeout(this._pingTimer);
|
|
|
|
this._pingTimer = setTimeout(this._ping.bind(this), this._pingInterval);
|
2020-05-09 19:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle an incoming socket message.
|
|
|
|
* @param data
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_handleMessage({ data }) {
|
2020-05-09 19:56:51 +00:00
|
|
|
let command, params;
|
|
|
|
try {
|
|
|
|
[command, params] = JSON.parse(data);
|
|
|
|
} catch (err) {
|
|
|
|
console.log("unsupported socket message", data);
|
|
|
|
}
|
2020-05-09 19:47:00 +00:00
|
|
|
switch (command) {
|
2021-01-29 20:54:27 +00:00
|
|
|
case "getGamestate":
|
|
|
|
this.sendGamestate(params);
|
2020-05-09 19:47:00 +00:00
|
|
|
break;
|
2020-06-18 08:52:07 +00:00
|
|
|
case "edition":
|
|
|
|
this._updateEdition(params);
|
|
|
|
break;
|
2020-08-09 20:02:02 +00:00
|
|
|
case "fabled":
|
|
|
|
this._updateFabled(params);
|
|
|
|
break;
|
2020-05-09 19:47:00 +00:00
|
|
|
case "gs":
|
|
|
|
this._updateGamestate(params);
|
|
|
|
break;
|
|
|
|
case "player":
|
|
|
|
this._updatePlayer(params);
|
2020-05-24 20:13:52 +00:00
|
|
|
break;
|
2020-05-30 22:18:31 +00:00
|
|
|
case "claim":
|
|
|
|
this._updateSeat(params);
|
|
|
|
break;
|
2020-05-24 20:13:52 +00:00
|
|
|
case "ping":
|
|
|
|
this._handlePing(params);
|
|
|
|
break;
|
2020-06-03 20:25:23 +00:00
|
|
|
case "nomination":
|
2020-07-19 19:11:23 +00:00
|
|
|
if (!this._isSpectator) return;
|
2020-12-06 21:27:52 +00:00
|
|
|
if (!params) {
|
|
|
|
// create vote history record
|
|
|
|
this._store.commit(
|
|
|
|
"session/addHistory",
|
|
|
|
this._store.state.players.players
|
|
|
|
);
|
|
|
|
}
|
2020-06-30 08:16:56 +00:00
|
|
|
this._store.commit("session/nomination", { nomination: params });
|
2020-06-03 20:25:23 +00:00
|
|
|
break;
|
2020-07-19 19:11:23 +00:00
|
|
|
case "swap":
|
|
|
|
if (!this._isSpectator) return;
|
|
|
|
this._store.commit("players/swap", params);
|
|
|
|
break;
|
|
|
|
case "move":
|
|
|
|
if (!this._isSpectator) return;
|
|
|
|
this._store.commit("players/move", params);
|
|
|
|
break;
|
2021-03-19 19:09:13 +00:00
|
|
|
case "remove":
|
|
|
|
if (!this._isSpectator) return;
|
|
|
|
this._store.commit("players/remove", params);
|
|
|
|
break;
|
2021-05-15 18:07:54 +00:00
|
|
|
case "marked":
|
|
|
|
if (!this._isSpectator) return;
|
|
|
|
this._store.commit("session/setMarkedPlayer", params);
|
|
|
|
break;
|
2020-12-02 19:39:12 +00:00
|
|
|
case "isNight":
|
|
|
|
if (!this._isSpectator) return;
|
|
|
|
this._store.commit("toggleNight", params);
|
|
|
|
break;
|
2021-05-15 18:07:54 +00:00
|
|
|
case "isVoteHistoryAllowed":
|
|
|
|
if (!this._isSpectator) return;
|
|
|
|
this._store.commit("session/setVoteHistoryAllowed", params);
|
|
|
|
this._store.commit("session/clearVoteHistory");
|
|
|
|
break;
|
2020-06-18 08:00:32 +00:00
|
|
|
case "votingSpeed":
|
2020-07-19 19:11:23 +00:00
|
|
|
if (!this._isSpectator) return;
|
2020-06-18 08:00:32 +00:00
|
|
|
this._store.commit("session/setVotingSpeed", params);
|
|
|
|
break;
|
2020-12-22 10:32:00 +00:00
|
|
|
case "clearVoteHistory":
|
|
|
|
if (!this._isSpectator) return;
|
|
|
|
this._store.commit("session/clearVoteHistory");
|
|
|
|
break;
|
2020-12-05 21:24:55 +00:00
|
|
|
case "isVoteInProgress":
|
|
|
|
if (!this._isSpectator) return;
|
|
|
|
this._store.commit("session/setVoteInProgress", params);
|
|
|
|
break;
|
2020-06-03 20:25:23 +00:00
|
|
|
case "vote":
|
2020-06-05 20:12:51 +00:00
|
|
|
this._handleVote(params);
|
2020-06-03 20:25:23 +00:00
|
|
|
break;
|
2020-06-03 21:04:50 +00:00
|
|
|
case "lock":
|
2020-06-07 20:22:50 +00:00
|
|
|
this._handleLock(params);
|
2020-06-03 21:04:50 +00:00
|
|
|
break;
|
2020-05-24 20:13:52 +00:00
|
|
|
case "bye":
|
|
|
|
this._handleBye(params);
|
|
|
|
break;
|
2021-02-28 11:23:16 +00:00
|
|
|
case "pronouns":
|
|
|
|
this._updatePlayerPronouns(params);
|
2021-01-27 20:33:38 +00:00
|
|
|
break;
|
2020-05-09 19:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect to a new live session, either as host or spectator.
|
2020-05-27 20:33:51 +00:00
|
|
|
* Set a unique playerId if there isn't one yet.
|
2020-05-09 19:47:00 +00:00
|
|
|
* @param channel
|
|
|
|
*/
|
|
|
|
connect(channel) {
|
2020-05-27 20:33:51 +00:00
|
|
|
if (!this._store.state.session.playerId) {
|
|
|
|
this._store.commit(
|
2020-05-30 20:47:42 +00:00
|
|
|
"session/setPlayerId",
|
2020-05-27 20:33:51 +00:00
|
|
|
Math.random()
|
|
|
|
.toString(36)
|
|
|
|
.substr(2)
|
|
|
|
);
|
|
|
|
}
|
2020-09-16 09:39:36 +00:00
|
|
|
this._pings = {};
|
2020-05-30 20:47:42 +00:00
|
|
|
this._store.commit("session/setPlayerCount", 0);
|
2020-09-16 09:39:36 +00:00
|
|
|
this._store.commit("session/setPing", 0);
|
2020-05-24 20:13:52 +00:00
|
|
|
this._isSpectator = this._store.state.session.isSpectator;
|
2020-05-09 19:47:00 +00:00
|
|
|
this._open(channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close the current session, if any.
|
|
|
|
*/
|
|
|
|
disconnect() {
|
2020-09-16 09:39:36 +00:00
|
|
|
this._pings = {};
|
2020-05-30 20:47:42 +00:00
|
|
|
this._store.commit("session/setPlayerCount", 0);
|
2020-09-16 09:39:36 +00:00
|
|
|
this._store.commit("session/setPing", 0);
|
2020-09-21 09:19:46 +00:00
|
|
|
this._store.commit("session/setReconnecting", false);
|
|
|
|
clearTimeout(this._reconnectTimer);
|
2020-05-24 20:13:52 +00:00
|
|
|
if (this._socket) {
|
2021-01-29 20:54:27 +00:00
|
|
|
if (this._isSpectator) {
|
|
|
|
this._sendDirect("host", "bye", this._store.state.session.playerId);
|
|
|
|
}
|
2020-09-21 09:19:46 +00:00
|
|
|
this._socket.close(1000);
|
2020-05-24 20:13:52 +00:00
|
|
|
this._socket = null;
|
2020-05-09 19:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Publish the current gamestate.
|
2020-12-07 20:41:56 +00:00
|
|
|
* Optional param to reduce traffic. (send only player data)
|
2021-01-29 20:54:27 +00:00
|
|
|
* @param playerId
|
2020-12-07 20:41:56 +00:00
|
|
|
* @param isLightweight
|
2020-05-09 19:47:00 +00:00
|
|
|
*/
|
2021-01-29 20:54:27 +00:00
|
|
|
sendGamestate(playerId = "", isLightweight = false) {
|
2020-05-24 20:13:52 +00:00
|
|
|
if (this._isSpectator) return;
|
|
|
|
this._gamestate = this._store.state.players.players.map(player => ({
|
2020-05-09 19:47:00 +00:00
|
|
|
name: player.name,
|
2020-05-30 20:47:42 +00:00
|
|
|
id: player.id,
|
2020-05-09 19:47:00 +00:00
|
|
|
isDead: player.isDead,
|
|
|
|
isVoteless: player.isVoteless,
|
2021-02-28 11:23:16 +00:00
|
|
|
pronouns: player.pronouns,
|
2023-09-05 02:28:42 +00:00
|
|
|
...(player.role && player.role.team === "traveller"
|
2020-06-18 08:15:37 +00:00
|
|
|
? { roleId: player.role.id }
|
2020-05-09 19:47:00 +00:00
|
|
|
: {})
|
|
|
|
}));
|
2020-12-07 20:41:56 +00:00
|
|
|
if (isLightweight) {
|
2021-01-29 20:54:27 +00:00
|
|
|
this._sendDirect(playerId, "gs", {
|
|
|
|
gamestate: this._gamestate,
|
|
|
|
isLightweight
|
|
|
|
});
|
2020-12-07 20:41:56 +00:00
|
|
|
} else {
|
|
|
|
const { session, grimoire } = this._store.state;
|
|
|
|
const { fabled } = this._store.state.players;
|
2021-01-29 20:54:27 +00:00
|
|
|
this.sendEdition(playerId);
|
|
|
|
this._sendDirect(playerId, "gs", {
|
2020-12-07 20:41:56 +00:00
|
|
|
gamestate: this._gamestate,
|
|
|
|
isNight: grimoire.isNight,
|
2021-05-15 18:07:54 +00:00
|
|
|
isVoteHistoryAllowed: session.isVoteHistoryAllowed,
|
2020-12-07 20:41:56 +00:00
|
|
|
nomination: session.nomination,
|
|
|
|
votingSpeed: session.votingSpeed,
|
|
|
|
lockedVote: session.lockedVote,
|
|
|
|
isVoteInProgress: session.isVoteInProgress,
|
2021-05-15 18:07:54 +00:00
|
|
|
markedPlayer: session.markedPlayer,
|
|
|
|
fabled: fabled.map(f => (f.isCustom ? f : { id: f.id })),
|
2020-12-07 20:41:56 +00:00
|
|
|
...(session.nomination ? { votes: session.votes } : {})
|
|
|
|
});
|
|
|
|
}
|
2020-05-09 19:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the gamestate based on incoming data.
|
2020-06-18 08:00:32 +00:00
|
|
|
* @param data
|
2020-05-09 19:47:00 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2020-06-18 08:00:32 +00:00
|
|
|
_updateGamestate(data) {
|
2020-05-27 20:33:51 +00:00
|
|
|
if (!this._isSpectator) return;
|
2020-12-01 21:42:21 +00:00
|
|
|
const {
|
|
|
|
gamestate,
|
2020-12-07 20:41:56 +00:00
|
|
|
isLightweight,
|
2020-12-02 19:39:12 +00:00
|
|
|
isNight,
|
2021-05-15 18:07:54 +00:00
|
|
|
isVoteHistoryAllowed,
|
2020-12-01 21:42:21 +00:00
|
|
|
nomination,
|
|
|
|
votingSpeed,
|
|
|
|
votes,
|
|
|
|
lockedVote,
|
2020-12-05 21:24:55 +00:00
|
|
|
isVoteInProgress,
|
2021-05-15 18:07:54 +00:00
|
|
|
markedPlayer,
|
2020-12-01 21:42:21 +00:00
|
|
|
fabled
|
|
|
|
} = data;
|
2020-05-24 20:13:52 +00:00
|
|
|
const players = this._store.state.players.players;
|
2020-05-09 19:47:00 +00:00
|
|
|
// adjust number of players
|
|
|
|
if (players.length < gamestate.length) {
|
|
|
|
for (let x = players.length; x < gamestate.length; x++) {
|
2020-05-24 20:13:52 +00:00
|
|
|
this._store.commit("players/add", gamestate[x].name);
|
2020-05-09 19:47:00 +00:00
|
|
|
}
|
|
|
|
} else if (players.length > gamestate.length) {
|
|
|
|
for (let x = players.length; x > gamestate.length; x--) {
|
2020-05-24 20:13:52 +00:00
|
|
|
this._store.commit("players/remove", x - 1);
|
2020-05-09 19:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// update status for each player
|
|
|
|
gamestate.forEach((state, x) => {
|
|
|
|
const player = players[x];
|
2020-06-18 08:15:37 +00:00
|
|
|
const { roleId } = state;
|
2020-05-30 20:47:42 +00:00
|
|
|
// update relevant properties
|
2021-02-28 11:23:16 +00:00
|
|
|
["name", "id", "isDead", "isVoteless", "pronouns"].forEach(property => {
|
2020-05-30 20:47:42 +00:00
|
|
|
const value = state[property];
|
|
|
|
if (player[property] !== value) {
|
|
|
|
this._store.commit("players/update", { player, property, value });
|
|
|
|
}
|
|
|
|
});
|
2023-09-05 02:28:42 +00:00
|
|
|
// roles are special, because of travellers
|
2020-06-18 08:15:37 +00:00
|
|
|
if (roleId && player.role.id !== roleId) {
|
2020-12-24 14:38:28 +00:00
|
|
|
const role =
|
|
|
|
this._store.state.roles.get(roleId) ||
|
|
|
|
this._store.getters.rolesJSONbyId.get(roleId);
|
|
|
|
if (role) {
|
|
|
|
this._store.commit("players/update", {
|
|
|
|
player,
|
|
|
|
property: "role",
|
|
|
|
value: role
|
|
|
|
});
|
|
|
|
}
|
2023-09-05 02:28:42 +00:00
|
|
|
} else if (!roleId && player.role.team === "traveller") {
|
2020-05-24 20:13:52 +00:00
|
|
|
this._store.commit("players/update", {
|
2020-05-09 19:47:00 +00:00
|
|
|
player,
|
|
|
|
property: "role",
|
|
|
|
value: {}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2020-12-07 20:41:56 +00:00
|
|
|
if (!isLightweight) {
|
|
|
|
this._store.commit("toggleNight", !!isNight);
|
2021-05-15 18:07:54 +00:00
|
|
|
this._store.commit("session/setVoteHistoryAllowed", isVoteHistoryAllowed);
|
2020-12-07 20:41:56 +00:00
|
|
|
this._store.commit("session/nomination", {
|
|
|
|
nomination,
|
|
|
|
votes,
|
|
|
|
votingSpeed,
|
|
|
|
lockedVote,
|
|
|
|
isVoteInProgress
|
|
|
|
});
|
2021-05-15 18:07:54 +00:00
|
|
|
this._store.commit("session/setMarkedPlayer", markedPlayer);
|
2020-12-07 20:41:56 +00:00
|
|
|
this._store.commit("players/setFabled", {
|
2021-05-15 18:07:54 +00:00
|
|
|
fabled: fabled.map(f => this._store.state.fabled.get(f.id) || f)
|
2020-12-07 20:41:56 +00:00
|
|
|
});
|
|
|
|
}
|
2020-05-09 19:47:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 08:52:07 +00:00
|
|
|
/**
|
|
|
|
* Publish an edition update. ST only
|
2021-01-29 20:54:27 +00:00
|
|
|
* @param playerId
|
2020-06-18 08:52:07 +00:00
|
|
|
*/
|
2021-01-29 20:54:27 +00:00
|
|
|
sendEdition(playerId = "") {
|
2020-06-18 08:52:07 +00:00
|
|
|
if (this._isSpectator) return;
|
|
|
|
const { edition } = this._store.state;
|
|
|
|
let roles;
|
2020-12-19 21:15:42 +00:00
|
|
|
if (!edition.isOfficial) {
|
2021-02-03 21:12:38 +00:00
|
|
|
roles = this._store.getters.customRolesStripped;
|
2020-06-18 08:52:07 +00:00
|
|
|
}
|
2021-01-29 20:54:27 +00:00
|
|
|
this._sendDirect(playerId, "edition", {
|
2021-02-03 21:12:38 +00:00
|
|
|
edition: edition.isOfficial ? { id: edition.id } : edition,
|
2020-06-18 08:52:07 +00:00
|
|
|
...(roles ? { roles } : {})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update edition and roles for custom editions.
|
|
|
|
* @param edition
|
|
|
|
* @param roles
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_updateEdition({ edition, roles }) {
|
|
|
|
if (!this._isSpectator) return;
|
|
|
|
this._store.commit("setEdition", edition);
|
|
|
|
if (roles) {
|
2021-02-03 21:12:38 +00:00
|
|
|
this._store.commit("setCustomRoles", roles);
|
2020-12-10 21:30:34 +00:00
|
|
|
if (this._store.state.roles.size !== roles.length) {
|
|
|
|
const missing = [];
|
2021-02-18 16:22:46 +00:00
|
|
|
roles.forEach(({ id }) => {
|
2020-12-10 21:30:34 +00:00
|
|
|
if (!this._store.state.roles.get(id)) {
|
|
|
|
missing.push(id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
alert(
|
|
|
|
`This session contains custom characters that can't be found. ` +
|
|
|
|
`Please load them before joining! ` +
|
|
|
|
`Missing roles: ${missing.join(", ")}`
|
|
|
|
);
|
2020-12-23 20:07:35 +00:00
|
|
|
this.disconnect();
|
|
|
|
this._store.commit("toggleModal", "edition");
|
2020-12-10 21:30:34 +00:00
|
|
|
}
|
2020-06-18 08:52:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-09 20:02:02 +00:00
|
|
|
/**
|
|
|
|
* Publish a fabled update. ST only
|
|
|
|
*/
|
|
|
|
sendFabled() {
|
|
|
|
if (this._isSpectator) return;
|
2020-11-29 21:17:02 +00:00
|
|
|
const { fabled } = this._store.state.players;
|
2020-08-09 20:02:02 +00:00
|
|
|
this._send(
|
|
|
|
"fabled",
|
2021-05-15 18:07:54 +00:00
|
|
|
fabled.map(f => (f.isCustom ? f : { id: f.id }))
|
2020-08-09 20:02:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update fabled roles.
|
|
|
|
* @param fabled
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_updateFabled(fabled) {
|
|
|
|
if (!this._isSpectator) return;
|
2020-11-29 21:17:02 +00:00
|
|
|
this._store.commit("players/setFabled", {
|
2021-05-15 18:07:54 +00:00
|
|
|
fabled: fabled.map(f => this._store.state.fabled.get(f.id) || f)
|
2020-08-09 20:02:02 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-09 19:47:00 +00:00
|
|
|
/**
|
|
|
|
* Publish a player update.
|
|
|
|
* @param player
|
|
|
|
* @param property
|
|
|
|
* @param value
|
|
|
|
*/
|
|
|
|
sendPlayer({ player, property, value }) {
|
2020-05-24 20:13:52 +00:00
|
|
|
if (this._isSpectator || property === "reminders") return;
|
|
|
|
const index = this._store.state.players.players.indexOf(player);
|
2020-05-09 19:47:00 +00:00
|
|
|
if (property === "role") {
|
2023-09-05 02:28:42 +00:00
|
|
|
if (value.team && value.team === "traveller") {
|
|
|
|
// update local gamestate to remember this player as a traveller
|
2020-06-18 08:15:37 +00:00
|
|
|
this._gamestate[index].roleId = value.id;
|
2020-05-09 19:47:00 +00:00
|
|
|
this._send("player", {
|
|
|
|
index,
|
|
|
|
property,
|
2020-06-18 08:15:37 +00:00
|
|
|
value: value.id
|
2020-05-09 19:47:00 +00:00
|
|
|
});
|
2020-06-18 08:15:37 +00:00
|
|
|
} else if (this._gamestate[index].roleId) {
|
2023-09-05 02:28:42 +00:00
|
|
|
// player was previously a traveller
|
2020-06-18 08:15:37 +00:00
|
|
|
delete this._gamestate[index].roleId;
|
|
|
|
this._send("player", { index, property, value: "" });
|
2020-05-09 19:47:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this._send("player", { index, property, value });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-24 19:09:56 +00:00
|
|
|
* Update a player based on incoming data. Player only.
|
2020-05-09 19:47:00 +00:00
|
|
|
* @param index
|
|
|
|
* @param property
|
|
|
|
* @param value
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_updatePlayer({ index, property, value }) {
|
2020-12-24 19:09:56 +00:00
|
|
|
if (!this._isSpectator) return;
|
2020-05-24 20:13:52 +00:00
|
|
|
const player = this._store.state.players.players[index];
|
2020-05-09 19:47:00 +00:00
|
|
|
if (!player) return;
|
2023-09-05 02:28:42 +00:00
|
|
|
// special case where a player stops being a traveller
|
2020-06-18 08:15:37 +00:00
|
|
|
if (property === "role") {
|
2023-09-05 02:28:42 +00:00
|
|
|
if (!value && player.role.team === "traveller") {
|
2020-06-18 08:15:37 +00:00
|
|
|
// reset to an unknown role
|
|
|
|
this._store.commit("players/update", {
|
|
|
|
player,
|
|
|
|
property: "role",
|
|
|
|
value: {}
|
|
|
|
});
|
|
|
|
} else {
|
2021-01-22 21:05:16 +00:00
|
|
|
// load role, first from session, the global, then fail gracefully
|
|
|
|
const role =
|
|
|
|
this._store.state.roles.get(value) ||
|
|
|
|
this._store.getters.rolesJSONbyId.get(value) ||
|
|
|
|
{};
|
2020-06-18 08:15:37 +00:00
|
|
|
this._store.commit("players/update", {
|
|
|
|
player,
|
|
|
|
property: "role",
|
|
|
|
value: role
|
|
|
|
});
|
|
|
|
}
|
2020-05-09 19:47:00 +00:00
|
|
|
} else {
|
|
|
|
// just update the player otherwise
|
2020-05-24 20:13:52 +00:00
|
|
|
this._store.commit("players/update", { player, property, value });
|
2020-05-09 19:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-24 20:13:52 +00:00
|
|
|
|
2021-02-28 11:00:41 +00:00
|
|
|
/**
|
2021-02-28 11:23:16 +00:00
|
|
|
* Publish a player pronouns update
|
2021-02-28 11:00:41 +00:00
|
|
|
* @param player
|
2021-02-28 13:17:40 +00:00
|
|
|
* @param value
|
2021-03-16 19:46:52 +00:00
|
|
|
* @param isFromSockets
|
2021-02-28 11:00:41 +00:00
|
|
|
*/
|
2021-03-16 19:46:52 +00:00
|
|
|
sendPlayerPronouns({ player, value, isFromSockets }) {
|
2021-02-28 16:08:31 +00:00
|
|
|
//send pronoun only for the seated player or storyteller
|
2021-03-16 15:14:51 +00:00
|
|
|
//Do not re-send pronoun data for an update that was recieved from the sockets layer
|
|
|
|
if (
|
2021-03-16 19:46:52 +00:00
|
|
|
isFromSockets ||
|
2021-03-16 15:14:51 +00:00
|
|
|
(this._isSpectator && this._store.state.session.playerId !== player.id)
|
|
|
|
)
|
2021-02-28 16:08:31 +00:00
|
|
|
return;
|
2021-01-27 20:33:38 +00:00
|
|
|
const index = this._store.state.players.players.indexOf(player);
|
2021-03-16 15:14:51 +00:00
|
|
|
this._send("pronouns", [index, value]);
|
2021-01-27 20:33:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-16 15:14:51 +00:00
|
|
|
* Update a pronouns based on incoming data.
|
2021-01-27 20:33:38 +00:00
|
|
|
* @param index
|
2021-02-28 13:17:40 +00:00
|
|
|
* @param value
|
2021-01-27 20:33:38 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2021-03-16 15:14:51 +00:00
|
|
|
_updatePlayerPronouns([index, value]) {
|
2021-01-27 20:33:38 +00:00
|
|
|
const player = this._store.state.players.players[index];
|
2021-03-16 15:14:51 +00:00
|
|
|
|
|
|
|
this._store.commit("players/update", {
|
|
|
|
player,
|
|
|
|
property: "pronouns",
|
|
|
|
value,
|
2021-03-16 19:46:52 +00:00
|
|
|
isFromSockets: true
|
2021-03-16 15:14:51 +00:00
|
|
|
});
|
2021-01-27 20:33:38 +00:00
|
|
|
}
|
|
|
|
|
2020-05-24 20:13:52 +00:00
|
|
|
/**
|
|
|
|
* Handle a ping message by another player / storyteller
|
2021-01-27 21:05:32 +00:00
|
|
|
* @param playerIdOrCount
|
|
|
|
* @param latency
|
2020-05-24 20:13:52 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2021-01-29 20:54:27 +00:00
|
|
|
_handlePing([playerIdOrCount = 0, latency] = []) {
|
2020-05-24 20:13:52 +00:00
|
|
|
const now = new Date().getTime();
|
2021-01-27 21:05:32 +00:00
|
|
|
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) {
|
|
|
|
delete this._players[player];
|
|
|
|
delete this._pings[player];
|
|
|
|
}
|
2020-05-31 21:42:08 +00:00
|
|
|
}
|
2021-01-27 21:05:32 +00:00
|
|
|
// remove claimed seats from players that are no longer connected
|
|
|
|
this._store.state.players.players.forEach(player => {
|
|
|
|
if (player.id && !this._players[player.id]) {
|
|
|
|
this._store.commit("players/update", {
|
|
|
|
player,
|
|
|
|
property: "id",
|
|
|
|
value: ""
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// store new player data
|
|
|
|
if (playerIdOrCount) {
|
|
|
|
this._players[playerIdOrCount] = now;
|
|
|
|
const ping = parseInt(latency, 10);
|
|
|
|
if (ping && ping > 0 && ping < 30 * 1000) {
|
2020-09-22 07:10:35 +00:00
|
|
|
// ping to Players
|
2021-01-27 21:05:32 +00:00
|
|
|
this._pings[playerIdOrCount] = ping;
|
2020-09-22 07:10:35 +00:00
|
|
|
const pings = Object.values(this._pings);
|
|
|
|
this._store.commit(
|
|
|
|
"session/setPing",
|
|
|
|
Math.round(pings.reduce((a, b) => a + b, 0) / pings.length)
|
|
|
|
);
|
2020-09-21 09:19:46 +00:00
|
|
|
}
|
2020-09-16 09:39:36 +00:00
|
|
|
}
|
2021-01-27 21:05:32 +00:00
|
|
|
} else if (latency) {
|
|
|
|
// ping to ST
|
|
|
|
this._store.commit("session/setPing", parseInt(latency, 10));
|
2020-09-16 09:39:36 +00:00
|
|
|
}
|
2021-01-29 20:54:27 +00:00
|
|
|
// update player count
|
|
|
|
if (!this._isSpectator || playerIdOrCount) {
|
|
|
|
this._store.commit(
|
|
|
|
"session/setPlayerCount",
|
|
|
|
this._isSpectator ? playerIdOrCount : Object.keys(this._players).length
|
|
|
|
);
|
2020-09-16 09:39:36 +00:00
|
|
|
}
|
2020-05-24 20:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-29 20:54:27 +00:00
|
|
|
* Handle a player leaving the sessions. ST only
|
2020-05-24 20:13:52 +00:00
|
|
|
* @param playerId
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_handleBye(playerId) {
|
2021-01-29 20:54:27 +00:00
|
|
|
if (this._isSpectator) return;
|
2020-05-24 20:13:52 +00:00
|
|
|
delete this._players[playerId];
|
2020-05-30 20:47:42 +00:00
|
|
|
this._store.commit(
|
|
|
|
"session/setPlayerCount",
|
|
|
|
Object.keys(this._players).length
|
|
|
|
);
|
2020-05-24 20:13:52 +00:00
|
|
|
}
|
2020-05-30 22:18:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Claim a seat, needs to be confirmed by the Storyteller.
|
2020-12-15 14:59:43 +00:00
|
|
|
* Seats already occupied can't be claimed.
|
|
|
|
* @param seat either -1 to vacate or the index of the seat claimed
|
2020-05-30 22:18:31 +00:00
|
|
|
*/
|
|
|
|
claimSeat(seat) {
|
|
|
|
if (!this._isSpectator) return;
|
2020-12-15 14:59:43 +00:00
|
|
|
const players = this._store.state.players.players;
|
|
|
|
if (players.length > seat && (seat < 0 || !players[seat].id)) {
|
2020-05-30 22:18:31 +00:00
|
|
|
this._send("claim", [seat, this._store.state.session.playerId]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a player id associated with that seat.
|
|
|
|
* @param index seat index or -1
|
|
|
|
* @param value playerId to add / remove
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_updateSeat([index, value]) {
|
2020-06-03 20:25:23 +00:00
|
|
|
if (this._isSpectator) return;
|
2020-05-30 22:18:31 +00:00
|
|
|
const property = "id";
|
2020-06-03 20:25:23 +00:00
|
|
|
const players = this._store.state.players.players;
|
2020-05-30 22:18:31 +00:00
|
|
|
// remove previous seat
|
2020-06-03 20:25:23 +00:00
|
|
|
const oldIndex = players.findIndex(({ id }) => id === value);
|
|
|
|
if (oldIndex >= 0 && oldIndex !== index) {
|
|
|
|
this._store.commit("players/update", {
|
|
|
|
player: players[oldIndex],
|
|
|
|
property,
|
|
|
|
value: ""
|
|
|
|
});
|
2020-05-30 22:18:31 +00:00
|
|
|
}
|
|
|
|
// add playerId to new seat
|
|
|
|
if (index >= 0) {
|
2020-06-03 20:25:23 +00:00
|
|
|
const player = players[index];
|
2020-05-30 22:18:31 +00:00
|
|
|
if (!player) return;
|
|
|
|
this._store.commit("players/update", { player, property, value });
|
|
|
|
}
|
2020-06-03 20:25:23 +00:00
|
|
|
// update player session list as if this was a ping
|
2020-12-24 19:09:56 +00:00
|
|
|
this._handlePing([true, value, 0]);
|
2020-06-03 20:25:23 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 21:31:50 +00:00
|
|
|
/**
|
|
|
|
* Distribute player roles to all seated players in a direct message.
|
|
|
|
* This will be split server side so that each player only receives their own (sub)message.
|
|
|
|
*/
|
|
|
|
distributeRoles() {
|
|
|
|
if (this._isSpectator) return;
|
|
|
|
const message = {};
|
|
|
|
this._store.state.players.players.forEach((player, index) => {
|
|
|
|
if (player.id && player.role) {
|
|
|
|
message[player.id] = [
|
|
|
|
"player",
|
|
|
|
{ index, property: "role", value: player.role.id }
|
|
|
|
];
|
|
|
|
}
|
|
|
|
});
|
2020-12-22 10:32:00 +00:00
|
|
|
if (Object.keys(message).length) {
|
|
|
|
this._send("direct", message);
|
|
|
|
}
|
2020-12-15 21:31:50 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 20:25:23 +00:00
|
|
|
/**
|
2020-06-03 21:04:50 +00:00
|
|
|
* A player nomination. ST only
|
2020-06-18 08:00:32 +00:00
|
|
|
* This also syncs the voting speed to the players.
|
2021-05-15 18:07:54 +00:00
|
|
|
* Payload can be an object with {nomination} property or just the nomination itself, or undefined.
|
|
|
|
* @param payload [nominator, nominee]|{nomination}
|
2020-06-03 20:25:23 +00:00
|
|
|
*/
|
2021-05-15 18:07:54 +00:00
|
|
|
nomination(payload) {
|
2020-06-03 20:25:23 +00:00
|
|
|
if (this._isSpectator) return;
|
2021-05-15 18:07:54 +00:00
|
|
|
const nomination = payload ? payload.nomination || payload : payload;
|
2020-06-03 20:25:23 +00:00
|
|
|
const players = this._store.state.players.players;
|
|
|
|
if (
|
|
|
|
!nomination ||
|
|
|
|
(players.length > nomination[0] && players.length > nomination[1])
|
|
|
|
) {
|
2020-06-18 08:00:32 +00:00
|
|
|
this.setVotingSpeed(this._store.state.session.votingSpeed);
|
2020-06-03 20:25:23 +00:00
|
|
|
this._send("nomination", nomination);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-05 21:24:55 +00:00
|
|
|
/**
|
|
|
|
* Set the isVoteInProgress status. ST only
|
|
|
|
*/
|
|
|
|
setVoteInProgress() {
|
|
|
|
if (this._isSpectator) return;
|
|
|
|
this._send("isVoteInProgress", this._store.state.session.isVoteInProgress);
|
|
|
|
}
|
|
|
|
|
2020-12-02 19:39:12 +00:00
|
|
|
/**
|
|
|
|
* Send the isNight status. ST only
|
|
|
|
*/
|
|
|
|
setIsNight() {
|
|
|
|
if (this._isSpectator) return;
|
|
|
|
this._send("isNight", this._store.state.grimoire.isNight);
|
|
|
|
}
|
2020-12-22 10:32:00 +00:00
|
|
|
|
2021-05-15 18:07:54 +00:00
|
|
|
/**
|
|
|
|
* Send the isVoteHistoryAllowed state. ST only
|
|
|
|
*/
|
|
|
|
setVoteHistoryAllowed() {
|
|
|
|
if (this._isSpectator) return;
|
|
|
|
this._send(
|
|
|
|
"isVoteHistoryAllowed",
|
|
|
|
this._store.state.session.isVoteHistoryAllowed
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-18 08:00:32 +00:00
|
|
|
/**
|
|
|
|
* Send the voting speed. ST only
|
|
|
|
* @param votingSpeed voting speed in seconds, minimum 1
|
|
|
|
*/
|
|
|
|
setVotingSpeed(votingSpeed) {
|
|
|
|
if (this._isSpectator) return;
|
|
|
|
if (votingSpeed) {
|
|
|
|
this._send("votingSpeed", votingSpeed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:07:54 +00:00
|
|
|
/**
|
|
|
|
* Set which player is on the block. ST only
|
|
|
|
* @param playerIndex, player id or -1 for empty
|
|
|
|
*/
|
|
|
|
setMarked(playerIndex) {
|
|
|
|
if (this._isSpectator) return;
|
|
|
|
this._send("marked", playerIndex);
|
|
|
|
}
|
|
|
|
|
2020-12-22 10:32:00 +00:00
|
|
|
/**
|
|
|
|
* Clear the vote history for everyone. ST only
|
|
|
|
*/
|
|
|
|
clearVoteHistory() {
|
|
|
|
if (this._isSpectator) return;
|
|
|
|
this._send("clearVoteHistory");
|
|
|
|
}
|
|
|
|
|
2020-06-03 20:25:23 +00:00
|
|
|
/**
|
2020-06-16 08:58:36 +00:00
|
|
|
* Send a vote. Player or ST
|
2020-06-05 20:12:51 +00:00
|
|
|
* @param index Seat of the player
|
2020-06-16 08:58:36 +00:00
|
|
|
* @param sync Flag whether to sync this vote with others or not
|
2020-06-03 20:25:23 +00:00
|
|
|
*/
|
|
|
|
vote([index]) {
|
2020-06-05 20:12:51 +00:00
|
|
|
const player = this._store.state.players.players[index];
|
2020-06-16 08:58:36 +00:00
|
|
|
if (
|
|
|
|
this._store.state.session.playerId === player.id ||
|
|
|
|
!this._isSpectator
|
|
|
|
) {
|
|
|
|
// send vote only if it is your own vote or you are the storyteller
|
2020-09-16 09:39:36 +00:00
|
|
|
this._send("vote", [
|
|
|
|
index,
|
|
|
|
this._store.state.session.votes[index],
|
|
|
|
!this._isSpectator
|
|
|
|
]);
|
2020-06-05 20:12:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-16 09:39:36 +00:00
|
|
|
* Handle an incoming vote, but only if it is from ST or unlocked.
|
2020-06-16 08:58:36 +00:00
|
|
|
* @param index
|
2020-06-05 20:12:51 +00:00
|
|
|
* @param vote
|
2020-09-16 09:39:36 +00:00
|
|
|
* @param fromST
|
2020-06-05 20:12:51 +00:00
|
|
|
*/
|
2020-09-16 09:39:36 +00:00
|
|
|
_handleVote([index, vote, fromST]) {
|
|
|
|
const { session, players } = this._store.state;
|
|
|
|
const playerCount = players.players.length;
|
|
|
|
const indexAdjusted =
|
|
|
|
(index - 1 + playerCount - session.nomination[1]) % playerCount;
|
|
|
|
if (fromST || indexAdjusted >= session.lockedVote - 1) {
|
|
|
|
this._store.commit("session/vote", [index, vote]);
|
|
|
|
}
|
2020-05-30 22:18:31 +00:00
|
|
|
}
|
2020-06-03 21:04:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Lock a vote. ST only
|
|
|
|
*/
|
|
|
|
lockVote() {
|
|
|
|
if (this._isSpectator) return;
|
2020-06-07 20:22:50 +00:00
|
|
|
const { lockedVote, votes, nomination } = this._store.state.session;
|
|
|
|
const { players } = this._store.state.players;
|
|
|
|
const index = (nomination[1] + lockedVote - 1) % players.length;
|
|
|
|
this._send("lock", [this._store.state.session.lockedVote, votes[index]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-25 13:09:05 +00:00
|
|
|
* Update vote lock and the locked vote, if it differs. Player only
|
2020-06-07 20:22:50 +00:00
|
|
|
* @param lock
|
|
|
|
* @param vote
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_handleLock([lock, vote]) {
|
2020-12-25 13:09:05 +00:00
|
|
|
if (!this._isSpectator) return;
|
2020-06-07 20:22:50 +00:00
|
|
|
this._store.commit("session/lockVote", lock);
|
|
|
|
if (lock > 1) {
|
|
|
|
const { lockedVote, nomination } = this._store.state.session;
|
|
|
|
const { players } = this._store.state.players;
|
|
|
|
const index = (nomination[1] + lockedVote - 1) % players.length;
|
|
|
|
if (this._store.state.session.votes[index] !== vote) {
|
|
|
|
this._store.commit("session/vote", [index, vote]);
|
|
|
|
}
|
|
|
|
}
|
2020-06-03 21:04:50 +00:00
|
|
|
}
|
2020-07-19 19:11:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Swap two player seats. ST only
|
|
|
|
* @param payload
|
|
|
|
*/
|
|
|
|
swapPlayer(payload) {
|
|
|
|
if (this._isSpectator) return;
|
|
|
|
this._send("swap", payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move a player to another seat. ST only
|
|
|
|
* @param payload
|
|
|
|
*/
|
|
|
|
movePlayer(payload) {
|
|
|
|
if (this._isSpectator) return;
|
|
|
|
this._send("move", payload);
|
|
|
|
}
|
2021-03-19 19:09:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a player. ST only
|
|
|
|
* @param payload
|
|
|
|
*/
|
|
|
|
removePlayer(payload) {
|
|
|
|
if (this._isSpectator) return;
|
|
|
|
this._send("remove", payload);
|
|
|
|
}
|
2020-05-09 19:47:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 08:15:37 +00:00
|
|
|
export default store => {
|
2020-05-08 17:33:29 +00:00
|
|
|
// setup
|
2020-05-09 19:47:00 +00:00
|
|
|
const session = new LiveSession(store);
|
2020-05-08 17:33:29 +00:00
|
|
|
|
|
|
|
// listen to mutations
|
2021-03-14 19:42:39 +00:00
|
|
|
store.subscribe(({ type, payload }, state) => {
|
2020-05-09 19:47:00 +00:00
|
|
|
switch (type) {
|
2020-05-30 20:47:42 +00:00
|
|
|
case "session/setSessionId":
|
2021-03-14 19:42:39 +00:00
|
|
|
if (state.session.sessionId) {
|
|
|
|
session.connect(state.session.sessionId);
|
2020-05-09 19:47:00 +00:00
|
|
|
} else {
|
2020-05-12 18:48:00 +00:00
|
|
|
window.location.hash = "";
|
2020-05-09 19:47:00 +00:00
|
|
|
session.disconnect();
|
|
|
|
}
|
|
|
|
break;
|
2020-05-30 22:18:31 +00:00
|
|
|
case "session/claimSeat":
|
|
|
|
session.claimSeat(payload);
|
|
|
|
break;
|
2020-12-15 21:31:50 +00:00
|
|
|
case "session/distributeRoles":
|
|
|
|
if (payload) {
|
|
|
|
session.distributeRoles();
|
|
|
|
}
|
|
|
|
break;
|
2020-06-03 20:25:23 +00:00
|
|
|
case "session/nomination":
|
2021-05-15 18:07:54 +00:00
|
|
|
case "session/setNomination":
|
2020-06-03 20:25:23 +00:00
|
|
|
session.nomination(payload);
|
|
|
|
break;
|
2020-12-05 21:24:55 +00:00
|
|
|
case "session/setVoteInProgress":
|
|
|
|
session.setVoteInProgress(payload);
|
|
|
|
break;
|
2020-06-16 08:58:36 +00:00
|
|
|
case "session/voteSync":
|
2020-06-03 20:25:23 +00:00
|
|
|
session.vote(payload);
|
|
|
|
break;
|
2020-06-03 21:04:50 +00:00
|
|
|
case "session/lockVote":
|
|
|
|
session.lockVote();
|
|
|
|
break;
|
2020-06-18 08:00:32 +00:00
|
|
|
case "session/setVotingSpeed":
|
|
|
|
session.setVotingSpeed(payload);
|
|
|
|
break;
|
2020-12-22 10:32:00 +00:00
|
|
|
case "session/clearVoteHistory":
|
|
|
|
session.clearVoteHistory();
|
|
|
|
break;
|
2021-05-15 18:07:54 +00:00
|
|
|
case "session/setVoteHistoryAllowed":
|
|
|
|
session.setVoteHistoryAllowed();
|
|
|
|
break;
|
2020-12-02 19:39:12 +00:00
|
|
|
case "toggleNight":
|
|
|
|
session.setIsNight();
|
|
|
|
break;
|
2020-06-18 08:52:07 +00:00
|
|
|
case "setEdition":
|
|
|
|
session.sendEdition();
|
|
|
|
break;
|
2020-12-13 20:50:44 +00:00
|
|
|
case "players/setFabled":
|
2020-08-09 20:02:02 +00:00
|
|
|
session.sendFabled();
|
|
|
|
break;
|
2021-05-15 18:07:54 +00:00
|
|
|
case "session/setMarkedPlayer":
|
|
|
|
session.setMarked(payload);
|
|
|
|
break;
|
2020-05-12 20:22:36 +00:00
|
|
|
case "players/swap":
|
2020-07-19 19:11:23 +00:00
|
|
|
session.swapPlayer(payload);
|
|
|
|
break;
|
2020-05-19 13:21:38 +00:00
|
|
|
case "players/move":
|
2020-07-19 19:11:23 +00:00
|
|
|
session.movePlayer(payload);
|
|
|
|
break;
|
2021-03-19 19:09:13 +00:00
|
|
|
case "players/remove":
|
|
|
|
session.removePlayer(payload);
|
|
|
|
break;
|
2020-07-19 19:11:23 +00:00
|
|
|
case "players/set":
|
2020-05-09 19:47:00 +00:00
|
|
|
case "players/clear":
|
|
|
|
case "players/add":
|
2021-01-29 20:54:27 +00:00
|
|
|
session.sendGamestate("", true);
|
2020-05-09 19:47:00 +00:00
|
|
|
break;
|
|
|
|
case "players/update":
|
2021-02-28 13:17:40 +00:00
|
|
|
if (payload.property === "pronouns") {
|
|
|
|
session.sendPlayerPronouns(payload);
|
2021-03-02 19:16:46 +00:00
|
|
|
} else {
|
|
|
|
session.sendPlayer(payload);
|
2021-02-28 13:17:40 +00:00
|
|
|
}
|
2020-05-09 19:47:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-05-08 17:33:29 +00:00
|
|
|
});
|
2020-05-12 18:48:00 +00:00
|
|
|
|
|
|
|
// check for session Id in hash
|
2020-12-14 20:49:40 +00:00
|
|
|
const sessionId = window.location.hash.substr(1);
|
|
|
|
if (sessionId) {
|
2020-05-30 20:47:42 +00:00
|
|
|
store.commit("session/setSpectator", true);
|
2020-12-14 20:49:40 +00:00
|
|
|
store.commit("session/setSessionId", sessionId);
|
2021-03-30 19:34:09 +00:00
|
|
|
store.commit("toggleGrimoire", false);
|
2020-05-12 18:48:00 +00:00
|
|
|
}
|
2020-05-08 17:33:29 +00:00
|
|
|
};
|