Files

42 lines
1.1 KiB
JavaScript

// Small helpers shared by the GameState mixins.
import { parseKey } from "../hex.js";
import { effectValue } from "../data/effects.js";
// Canonical key for an unordered pair of civilisations.
export function warKey(a, b) {
return a < b ? `${a}:${b}` : `${b}:${a}`;
}
// The value of a catalogue effect at `level`. Kept as a local name so the
// economy code reads naturally.
export function effectAt(e, level) {
return effectValue(e, level);
}
export function mergeModifiers(totals, additions) {
for (const stat of Object.keys(additions)) {
totals[stat] = (totals[stat] || 0) + additions[stat];
}
}
// Flattens a Map<"x,y", value> into [x, y, value] triples for the wire.
export function serializeCells(map) {
const result = [];
for (const [k, value] of map) {
const coords = parseKey(k);
result.push([coords.x, coords.y, value]);
}
return result;
}
// Flattens a Set<"x,y"> into [x, y, true] triples for the wire.
export function serializeSet(set) {
const result = [];
for (const k of set) {
const coords = parseKey(k);
result.push([coords.x, coords.y, true]);
}
return result;
}