feat: New features (research, invest, gambling, new game+, trophies)
This commit is contained in:
+64
-16
@@ -1,27 +1,75 @@
|
||||
import { EFFECT_TYPES } from './effects.js';
|
||||
|
||||
/**
|
||||
* Bundle the static game data (data/*.json) with lookup tables and validate it,
|
||||
* so a typo in a JSON file fails loudly at startup instead of mid-game.
|
||||
*/
|
||||
export function buildContext({ rules, generators, upgrades }) {
|
||||
const generatorsById = new Map(generators.map((g) => [g.id, g]));
|
||||
const upgradesById = new Map(upgrades.map((u) => [u.id, u]));
|
||||
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;
|
||||
}
|
||||
|
||||
if (generatorsById.size !== generators.length) throw new Error('Duplicate generator id');
|
||||
if (upgradesById.size !== upgrades.length) throw new Error('Duplicate upgrade id');
|
||||
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,
|
||||
}) {
|
||||
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');
|
||||
byId(tabs, 'tab');
|
||||
|
||||
for (const u of upgrades) {
|
||||
if (!EFFECT_TYPES.includes(u.effect?.type)) {
|
||||
throw new Error(`Upgrade "${u.id}": unknown effect type "${u.effect?.type}"`);
|
||||
}
|
||||
if (u.effect.target && !generatorsById.has(u.effect.target)) {
|
||||
throw new Error(`Upgrade "${u.id}": unknown generator "${u.effect.target}"`);
|
||||
}
|
||||
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 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, generatorsById, upgradesById };
|
||||
return {
|
||||
rules,
|
||||
generators,
|
||||
upgrades,
|
||||
research,
|
||||
legacy,
|
||||
achievements,
|
||||
investments,
|
||||
tabs,
|
||||
generatorsById,
|
||||
upgradesById,
|
||||
researchById,
|
||||
perksById,
|
||||
achievementsById,
|
||||
societiesById,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user