added support for custom roles in live sessions

This commit is contained in:
Steffen 2020-07-23 12:25:51 +02:00
parent 615ab168e2
commit af1717134c
No known key found for this signature in database
GPG Key ID: 764D74E98267DFC6
2 changed files with 47 additions and 18 deletions

View File

@ -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

View File

@ -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);
}
}