From af1717134c7bda01ed1a5290c12344dd3f337fea Mon Sep 17 00:00:00 2001 From: Steffen Date: Thu, 23 Jul 2020 12:25:51 +0200 Subject: [PATCH] added support for custom roles in live sessions --- src/store/index.js | 58 +++++++++++++++++++++++++++++++++++---------- src/store/socket.js | 7 ++---- 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/src/store/index.js b/src/store/index.js index 5130d87..6a3ce76 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -24,6 +24,23 @@ const getRolesByEdition = (edition = "tb") => { ); }; +// base definition for custom roles +const imageBase = + "https://raw.githubusercontent.com/bra1n/townsquare/main/src/assets/icons/"; +const customRole = { + image: "", + edition: "custom", + firstNight: 0, + firstNightReminder: "", + otherNight: 0, + otherNightReminder: "", + reminders: [], + remindersGlobal: [], + setup: false, + team: "townsfolk", + isCustom: true +}; + export default new Vuex.Store({ modules: { players, @@ -51,6 +68,34 @@ export default new Vuex.Store({ edition: "tb", roles: getRolesByEdition() }, + getters: { + /** + * Return all custom roles, with default values stripped. + * @param roles + * @returns {[]} + */ + customRoles: ({ roles }) => { + const customRoles = []; + roles.forEach(role => { + if (!role.isCustom) { + customRoles.push({ id: role.id }); + } else { + const strippedRole = {}; + for (let prop in role) { + const value = role[prop]; + if (prop === "image" && value.match(new RegExp("^" + imageBase))) { + continue; + } + if (prop !== "isCustom" && value !== customRole[prop]) { + strippedRole[prop] = value; + } + } + customRoles.push(strippedRole); + } + }); + return customRoles; + } + }, mutations: { toggleMenu({ grimoire }) { grimoire.isMenuOpen = !grimoire.isMenuOpen; @@ -105,19 +150,6 @@ export default new Vuex.Store({ * @param roles Array of role IDs or full role definitions */ setCustomRoles(state, roles) { - const imageBase = - "https://raw.githubusercontent.com/bra1n/townsquare/main/src/assets/icons/"; - const customRole = { - image: "", - edition: "custom", - firstNight: 0, - firstNightReminder: "", - otherNight: 0, - otherNightReminder: "", - reminders: [], - remindersGlobal: [], - setup: false - }; state.roles = new Map( roles // map existing roles to base definition or pre-populate custom roles to ensure all properties diff --git a/src/store/socket.js b/src/store/socket.js index af22ec6..1cea03b 100644 --- a/src/store/socket.js +++ b/src/store/socket.js @@ -250,7 +250,7 @@ class LiveSession { const { edition } = this._store.state; let roles; if (edition === "custom") { - roles = Array.from(this._store.state.roles.keys()); + roles = this._store.getters.customRoles; } this._send("edition", { edition, @@ -268,10 +268,7 @@ class LiveSession { if (!this._isSpectator) return; this._store.commit("setEdition", edition); if (roles) { - this._store.commit( - "setCustomRoles", - roles.map(id => ({ id })) - ); + this._store.commit("setCustomRoles", roles); } }