feat(gameplay): Adding % bonus for planet type
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { resources } from "./planetEconomy.js";
|
||||
import { resources, elements } from "./planetEconomy.js";
|
||||
|
||||
// ── Sort state ────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -64,6 +64,46 @@ export function computeTeamIncome(team, cells, resourceWorth) {
|
||||
return { total, byResource };
|
||||
}
|
||||
|
||||
// ── Element bonus calculation ─────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Reverse map: French element label → config key
|
||||
* e.g. "Matières premières" → "common", "Hydrocarbures" → "petrol"
|
||||
*/
|
||||
const ELEMENT_LABEL_TO_KEY = Object.fromEntries(
|
||||
Object.entries(elements).map(([key, label]) => [label, key])
|
||||
);
|
||||
|
||||
/**
|
||||
* Compute cumulative element bonus for a team based on their planets' production.
|
||||
* planet.production stores French label strings as keys (values from elements const).
|
||||
* bonus = sum_over_planets( sum_over_elements( elementShare% / 100 * elementWorth[key] ) )
|
||||
*
|
||||
* @param {string} team
|
||||
* @param {Map<string, { discoveredBy: string, hasPlanet: boolean, planet: object|null }>} cells
|
||||
* @param {object} elementWorth - { common: 1, petrol: 3, ... }
|
||||
* @returns {number} bonus value (use as %)
|
||||
*/
|
||||
export function computeTeamElementBonus(team, cells, elementWorth) {
|
||||
let bonus = 0;
|
||||
for (const [, meta] of cells) {
|
||||
if (meta.discoveredBy !== team) continue;
|
||||
if (!meta.hasPlanet || !meta.planet) continue;
|
||||
const { production } = meta.planet;
|
||||
if (!production) continue;
|
||||
for (const [elementLabel, pct] of Object.entries(production)) {
|
||||
// production keys are French labels; map back to config key
|
||||
const elementKey = ELEMENT_LABEL_TO_KEY[elementLabel] ?? elementLabel;
|
||||
const worth = elementWorth?.[elementKey] ?? 0;
|
||||
if (worth === 0) continue;
|
||||
bonus += (pct / 100) * worth;
|
||||
}
|
||||
}
|
||||
return bonus;
|
||||
}
|
||||
|
||||
export { elements };
|
||||
|
||||
// ── Resource table for the sidebar ───────────────────────────────────────────
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user