adding store methods to get role by id and roles not in json

This commit is contained in:
Dave 2023-05-27 14:31:48 +01:00
parent 8a481dfbf3
commit 9da7d45515

View file

@ -15,9 +15,9 @@ Vue.use(Vuex);
const getRolesByEdition = (edition = editionJSON[0]) => { const getRolesByEdition = (edition = editionJSON[0]) => {
return new Map( return new Map(
rolesJSON rolesJSON
.filter(r => r.edition === edition.id || edition.roles.includes(r.id)) .filter((r) => r.edition === edition.id || edition.roles.includes(r.id))
.sort((a, b) => b.team.localeCompare(a.team)) .sort((a, b) => b.team.localeCompare(a.team))
.map(role => [role.id, role]) .map((role) => [role.id, role])
); );
}; };
@ -25,20 +25,42 @@ const getTravelersNotInEdition = (edition = editionJSON[0]) => {
return new Map( return new Map(
rolesJSON rolesJSON
.filter( .filter(
r => (r) =>
r.team === "traveler" && r.team === "traveler" &&
r.edition !== edition.id && r.edition !== edition.id &&
!edition.roles.includes(r.id) !edition.roles.includes(r.id)
) )
.map(role => [role.id, role]) .map((role) => [role.id, role])
); );
}; };
const set = key => ({ grimoire }, val) => { const getRolesNotInEdition = (edition = editionJSON[0]) => {
return new Map(
rolesJSON
.filter(
(r) =>
r.team !== "traveler" &&
r.edition !== edition.id &&
!edition.roles.includes(r.id)
)
.map((role) => [role.id, role.name])
);
};
const getRoleById = (id) => {
return rolesJSON.find((r) => r.id === id);
``;
};
const set =
(key) =>
({ grimoire }, val) => {
grimoire[key] = val; grimoire[key] = val;
}; };
const toggle = key => ({ grimoire }, val) => { const toggle =
(key) =>
({ grimoire }, val) => {
if (val === true || val === false) { if (val === true || val === false) {
grimoire[key] = val; grimoire[key] = val;
} else { } else {
@ -46,14 +68,14 @@ const toggle = key => ({ grimoire }, val) => {
} }
}; };
const clean = id => id.toLocaleLowerCase().replace(/[^a-z0-9]/g, ""); const clean = (id) => id.toLocaleLowerCase().replace(/[^a-z0-9]/g, "");
// global data maps // global data maps
const editionJSONbyId = new Map( const editionJSONbyId = new Map(
editionJSON.map(edition => [edition.id, edition]) editionJSON.map((edition) => [edition.id, edition])
); );
const rolesJSONbyId = new Map(rolesJSON.map(role => [role.id, role])); const rolesJSONbyId = new Map(rolesJSON.map((role) => [role.id, role]));
const fabled = new Map(fabledJSON.map(role => [role.id, role])); const fabled = new Map(fabledJSON.map((role) => [role.id, role]));
// jinxes // jinxes
let jinxes = {}; let jinxes = {};
@ -65,7 +87,7 @@ try {
jinxes = new Map( jinxes = new Map(
jinxesJSON.map(({ id, hatred }) => [ jinxesJSON.map(({ id, hatred }) => [
clean(id), clean(id),
new Map(hatred.map(({ id, reason }) => [clean(id), reason])) new Map(hatred.map(({ id, reason }) => [clean(id), reason])),
]) ])
); );
// }); // });
@ -88,13 +110,13 @@ const customRole = {
remindersGlobal: [], remindersGlobal: [],
setup: false, setup: false,
team: "townsfolk", team: "townsfolk",
isCustom: true isCustom: true,
}; };
export default new Vuex.Store({ export default new Vuex.Store({
modules: { modules: {
players, players,
session session,
}, },
state: { state: {
grimoire: { grimoire: {
@ -106,7 +128,7 @@ export default new Vuex.Store({
isMuted: false, isMuted: false,
isImageOptIn: false, isImageOptIn: false,
zoom: 0, zoom: 0,
background: "" background: "",
}, },
modals: { modals: {
edition: false, edition: false,
@ -117,13 +139,14 @@ export default new Vuex.Store({
reminder: false, reminder: false,
role: false, role: false,
roles: false, roles: false,
voteHistory: false voteHistory: false,
}, },
edition: editionJSONbyId.get("tb"), edition: editionJSONbyId.get("tb"),
roles: getRolesByEdition(), roles: getRolesByEdition(),
otherTravelers: getTravelersNotInEdition(), otherTravelers: getTravelersNotInEdition(),
otherRoles: getTravelersNotInEdition(),
fabled, fabled,
jinxes jinxes,
}, },
getters: { getters: {
/** /**
@ -138,9 +161,9 @@ export default new Vuex.Store({
const strippedProps = [ const strippedProps = [
"firstNightReminder", "firstNightReminder",
"otherNightReminder", "otherNightReminder",
"isCustom" "isCustom",
]; ];
roles.forEach(role => { roles.forEach((role) => {
if (!role.isCustom) { if (!role.isCustom) {
customRoles.push({ id: role.id }); customRoles.push({ id: role.id });
} else { } else {
@ -159,7 +182,8 @@ export default new Vuex.Store({
}); });
return customRoles; return customRoles;
}, },
rolesJSONbyId: () => rolesJSONbyId rolesJSONbyId: () => rolesJSONbyId,
roleById: getRoleById,
}, },
mutations: { mutations: {
setZoom: set("zoom"), setZoom: set("zoom"),
@ -188,7 +212,7 @@ export default new Vuex.Store({
setCustomRoles(state, roles) { setCustomRoles(state, roles) {
const processedRoles = roles const processedRoles = roles
// replace numerical role object keys with matching key names // replace numerical role object keys with matching key names
.map(role => { .map((role) => {
if (role[0]) { if (role[0]) {
const customKeys = Object.keys(customRole); const customKeys = Object.keys(customRole);
const mappedRole = {}; const mappedRole = {};
@ -203,19 +227,19 @@ export default new Vuex.Store({
} }
}) })
// clean up role.id // clean up role.id
.map(role => { .map((role) => {
role.id = clean(role.id); role.id = clean(role.id);
return role; return role;
}) })
// map existing roles to base definition or pre-populate custom roles to ensure all properties // map existing roles to base definition or pre-populate custom roles to ensure all properties
.map( .map(
role => (role) =>
rolesJSONbyId.get(role.id) || rolesJSONbyId.get(role.id) ||
state.roles.get(role.id) || state.roles.get(role.id) ||
Object.assign({}, customRole, role) Object.assign({}, customRole, role)
) )
// default empty icons and placeholders, clean up firstNight / otherNight // default empty icons and placeholders, clean up firstNight / otherNight
.map(role => { .map((role) => {
if (rolesJSONbyId.get(role.id)) return role; if (rolesJSONbyId.get(role.id)) return role;
role.imageAlt = // map team to generic icon role.imageAlt = // map team to generic icon
{ {
@ -223,32 +247,36 @@ export default new Vuex.Store({
outsider: "outsider", outsider: "outsider",
minion: "minion", minion: "minion",
demon: "evil", demon: "evil",
fabled: "fabled" fabled: "fabled",
}[role.team] || "custom"; }[role.team] || "custom";
role.firstNight = Math.abs(role.firstNight); role.firstNight = Math.abs(role.firstNight);
role.otherNight = Math.abs(role.otherNight); role.otherNight = Math.abs(role.otherNight);
return role; return role;
}) })
// filter out roles that don't match an existing role and also don't have name/ability/team // filter out roles that don't match an existing role and also don't have name/ability/team
.filter(role => role.name && role.ability && role.team) .filter((role) => role.name && role.ability && role.team)
// sort by team // sort by team
.sort((a, b) => b.team.localeCompare(a.team)); .sort((a, b) => b.team.localeCompare(a.team));
// convert to Map without Fabled // convert to Map without Fabled
state.roles = new Map( state.roles = new Map(
processedRoles processedRoles
.filter(role => role.team !== "fabled") .filter((role) => role.team !== "fabled")
.map(role => [role.id, role]) .map((role) => [role.id, role])
); );
// update Fabled to include custom Fabled from this script // update Fabled to include custom Fabled from this script
state.fabled = new Map([ state.fabled = new Map([
...processedRoles.filter(r => r.team === "fabled").map(r => [r.id, r]), ...processedRoles
...fabledJSON.map(role => [role.id, role]) .filter((r) => r.team === "fabled")
.map((r) => [r.id, r]),
...fabledJSON.map((role) => [role.id, role]),
]); ]);
// update extraTravelers map to only show travelers not in this script // update extraTravelers map to only show travelers not in this script
state.otherTravelers = new Map( state.otherTravelers = new Map(
rolesJSON rolesJSON
.filter(r => r.team === "traveler" && !roles.some(i => i.id === r.id)) .filter(
.map(role => [role.id, role]) (r) => r.team === "traveler" && !roles.some((i) => i.id === r.id)
)
.map((role) => [role.id, role])
); );
}, },
setEdition(state, edition) { setEdition(state, edition) {
@ -260,7 +288,7 @@ export default new Vuex.Store({
state.edition = edition; state.edition = edition;
} }
state.modals.edition = false; state.modals.edition = false;
}
}, },
plugins: [persistence, socket] },
plugins: [persistence, socket],
}); });