load traveler data in live sessions

This commit is contained in:
Steffen 2020-06-18 10:15:37 +02:00
parent 2218b10331
commit 395619afb4
No known key found for this signature in database
GPG Key ID: 764D74E98267DFC6
1 changed files with 31 additions and 31 deletions

View File

@ -1,3 +1,5 @@
import rolesJSON from "../roles.json";
class LiveSession { class LiveSession {
constructor(store) { constructor(store) {
// this._wss = "ws://localhost:8081/"; // this._wss = "ws://localhost:8081/";
@ -159,13 +161,7 @@ class LiveSession {
isDead: player.isDead, isDead: player.isDead,
isVoteless: player.isVoteless, isVoteless: player.isVoteless,
...(player.role && player.role.team === "traveler" ...(player.role && player.role.team === "traveler"
? { ? { roleId: player.role.id }
role: {
id: player.role.id,
team: "traveler",
name: player.role.name
}
}
: {}) : {})
})); }));
this._send("gs", { this._send("gs", {
@ -201,7 +197,7 @@ class LiveSession {
// update status for each player // update status for each player
gamestate.forEach((state, x) => { gamestate.forEach((state, x) => {
const player = players[x]; const player = players[x];
const { role } = state; const { roleId } = state;
// update relevant properties // update relevant properties
["name", "id", "isDead", "isVoteless"].forEach(property => { ["name", "id", "isDead", "isVoteless"].forEach(property => {
const value = state[property]; const value = state[property];
@ -210,13 +206,14 @@ class LiveSession {
} }
}); });
// roles are special, because of travelers // roles are special, because of travelers
if (role && player.role.id !== role.id) { if (roleId && player.role.id !== roleId) {
const role = rolesJSON.find(r => r.id === roleId);
this._store.commit("players/update", { this._store.commit("players/update", {
player, player,
property: "role", property: "role",
value: role value: role
}); });
} else if (!role && player.role.team === "traveler") { } else if (!roleId && player.role.team === "traveler") {
this._store.commit("players/update", { this._store.commit("players/update", {
player, player,
property: "role", property: "role",
@ -238,19 +235,16 @@ class LiveSession {
if (property === "role") { if (property === "role") {
if (value.team && value.team === "traveler") { if (value.team && value.team === "traveler") {
// update local gamestate to remember this player as a traveler // update local gamestate to remember this player as a traveler
this._gamestate[index].role = { this._gamestate[index].roleId = value.id;
id: player.role.id,
team: "traveler",
name: player.role.name
};
this._send("player", { this._send("player", {
index, index,
property, property,
value: this._gamestate[index].role value: value.id
}); });
} else if (this._gamestate[index].role) { } else if (this._gamestate[index].roleId) {
delete this._gamestate[index].role; // player was previously a traveler
this._send("player", { index, property, value: {} }); delete this._gamestate[index].roleId;
this._send("player", { index, property, value: "" });
} }
} else { } else {
this._send("player", { index, property, value }); this._send("player", { index, property, value });
@ -268,17 +262,23 @@ class LiveSession {
const player = this._store.state.players.players[index]; const player = this._store.state.players.players[index];
if (!player) return; if (!player) return;
// special case where a player stops being a traveler // special case where a player stops being a traveler
if ( if (property === "role") {
property === "role" && if (!value && player.role.team === "traveler") {
value.team !== "traveler" &&
player.role.team === "traveler"
) {
// reset to an unknown role // reset to an unknown role
this._store.commit("players/update", { this._store.commit("players/update", {
player, player,
property: "role", property: "role",
value: {} value: {}
}); });
} else {
// load traveler role
const role = rolesJSON.find(r => r.id === value);
this._store.commit("players/update", {
player,
property: "role",
value: role
});
}
} else { } else {
// just update the player otherwise // just update the player otherwise
this._store.commit("players/update", { player, property, value }); this._store.commit("players/update", { player, property, value });
@ -457,7 +457,7 @@ class LiveSession {
} }
} }
module.exports = store => { export default store => {
// setup // setup
const session = new LiveSession(store); const session = new LiveSession(store);