86 lines
2.6 KiB
JavaScript
86 lines
2.6 KiB
JavaScript
import { EFFECT_TYPES } from './effects.js';
|
|
|
|
function byId(list, what) {
|
|
const map = new Map(list.map((item) => [item.id, item]));
|
|
if (map.size !== list.length) throw new Error(`Duplicate ${what} id`);
|
|
return map;
|
|
}
|
|
|
|
function checkEffect(owner, effect, generatorsById) {
|
|
if (!EFFECT_TYPES.includes(effect?.type)) {
|
|
throw new Error(`${owner}: unknown effect type "${effect?.type}"`);
|
|
}
|
|
if (effect.target && !generatorsById.has(effect.target)) {
|
|
throw new Error(`${owner}: unknown generator "${effect.target}"`);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Bundle the static game data (public/data/*.json) with lookup tables and
|
|
* validate it, so a typo in a JSON file fails loudly at startup (and in the
|
|
* tests) instead of mid-game.
|
|
*/
|
|
export function buildContext({
|
|
rules,
|
|
generators,
|
|
upgrades,
|
|
research,
|
|
legacy,
|
|
achievements,
|
|
investments,
|
|
tabs,
|
|
endless,
|
|
}) {
|
|
const generatorsById = byId(generators, 'generator');
|
|
const upgradesById = byId(upgrades, 'upgrade');
|
|
const researchById = byId(research, 'research');
|
|
const perksById = byId(legacy, 'legacy perk');
|
|
const achievementsById = byId(achievements, 'achievement');
|
|
const societiesById = byId(investments.societies, 'society');
|
|
const endlessById = byId(endless, 'endless line');
|
|
byId(tabs, 'tab');
|
|
|
|
for (const u of upgrades) {
|
|
checkEffect(`Upgrade "${u.id}"`, u.effect, generatorsById);
|
|
if (u.requires && !upgradesById.has(u.requires)) {
|
|
throw new Error(`Upgrade "${u.id}": unknown requirement "${u.requires}"`);
|
|
}
|
|
}
|
|
for (const node of research) {
|
|
checkEffect(`Research "${node.id}"`, node.effect, generatorsById);
|
|
for (const req of node.requires) {
|
|
if (!researchById.has(req)) throw new Error(`Research "${node.id}": unknown requirement "${req}"`);
|
|
}
|
|
}
|
|
for (const line of endless) {
|
|
checkEffect(`Endless "${line.id}"`, line.effect, generatorsById);
|
|
if (line.requires && !upgradesById.has(line.requires)) {
|
|
throw new Error(`Endless "${line.id}": unknown requirement "${line.requires}"`);
|
|
}
|
|
}
|
|
for (const perk of legacy) checkEffect(`Perk "${perk.id}"`, perk.effect, generatorsById);
|
|
for (const a of achievements) checkEffect(`Achievement "${a.id}"`, a.effect, generatorsById);
|
|
for (const s of investments.societies) {
|
|
if (!investments.tiers[s.tier]) throw new Error(`Society "${s.id}": unknown tier "${s.tier}"`);
|
|
}
|
|
|
|
return {
|
|
rules,
|
|
generators,
|
|
upgrades,
|
|
research,
|
|
legacy,
|
|
achievements,
|
|
investments,
|
|
tabs,
|
|
endless,
|
|
generatorsById,
|
|
upgradesById,
|
|
researchById,
|
|
perksById,
|
|
achievementsById,
|
|
societiesById,
|
|
endlessById,
|
|
};
|
|
}
|