fix: Economy factors taken in charge in websockets

This commit is contained in:
gauvainboiche
2026-04-02 15:51:59 +02:00
parent 3cdb6385a7
commit 0b56e0deb0
+21 -2
View File
@@ -70,6 +70,24 @@ const ELEMENT_LABEL_TO_KEY = Object.fromEntries(
// ── Income computation ──────────────────────────────────────────────────────── // ── Income computation ────────────────────────────────────────────────────────
/**
* Population-based income multiplier (mirrors public/src/economy.js).
* Base factor = billions / 10, plus an efficiency boost for smaller populations:
* 010 B → ×10, 10100 B → ×5, 1001000 B → ×2.5, >1000 B → ×1
*
* @param {number} billions
* @returns {number}
*/
function populationFactor(billions) {
const base = billions / 10;
let boost;
if (billions < 10) boost = 10;
else if (billions < 100) boost = 5;
else if (billions < 1000) boost = 2.5;
else boost = 1;
return base * boost;
}
/** /**
* Compute total income per second for a team from DB grid rows. * Compute total income per second for a team from DB grid rows.
* *
@@ -83,14 +101,15 @@ export function computeTeamIncome(team, rows, resourceWorth) {
for (const row of rows) { for (const row of rows) {
if (row.discovered_by !== team) continue; if (row.discovered_by !== team) continue;
if (!row.has_planet || !row.planet_json) continue; if (!row.has_planet || !row.planet_json) continue;
const { naturalResources } = row.planet_json; const { naturalResources, population } = row.planet_json;
if (!naturalResources) continue; if (!naturalResources) continue;
const popFactor = population ? populationFactor(population.billions) : 1;
for (const [label, pct] of Object.entries(naturalResources)) { for (const [label, pct] of Object.entries(naturalResources)) {
const info = LABEL_TO_RESOURCE.get(label); const info = LABEL_TO_RESOURCE.get(label);
if (!info) continue; if (!info) continue;
const worth = resourceWorth?.[info.cat]?.[info.key] ?? 0; const worth = resourceWorth?.[info.cat]?.[info.key] ?? 0;
if (worth === 0) continue; if (worth === 0) continue;
total += (pct / 100) * worth; total += (pct / 100) * worth * popFactor;
} }
} }
return total; return total;