import {cachedFetchJSON} from './cache'; const renderTarget = document.querySelector('#gov')!; const planetInput = document.querySelector('#planet') as HTMLInputElement; const popSelect = document.querySelector('#pop') as HTMLSelectElement; function serializeToHash(planet: string, pop: Pop): void { const params = new URLSearchParams({'planet': planet, 'pop': pop}); document.location.hash = params.toString(); } function deserializeFromHash(): {planet: string, pop: Pop} | null { const params = new URLSearchParams(document.location.hash.substring(1)); const planet = params.get('planet'); const pop = params.get('pop'); if (planet && pop && ['pio', 'set', 'tec', 'eng', 'sci'].includes(pop)) { planetInput.value = planet; popSelect.value = pop; return {planet, pop: pop as Pop}; } else return null; } document.querySelector('form')!.addEventListener('submit', async (event) => { event.preventDefault(); const planet = planetInput.value; const pop = popSelect.value as Pop; if (planet && pop) { await render(planet, pop); serializeToHash(planet, pop); } }); { const deserialized = deserializeFromHash(); if (deserialized) void render(deserialized.planet, deserialized.pop); } async function render(planetName: string, pop: Pop) { const loader = document.querySelector('#loader') as HTMLElement; loader.style.display = 'block'; renderTarget.innerHTML = ''; try { await _render(planetName, pop); } catch (e) { renderTarget.textContent = (e as Error).message; } loader.style.display = 'none'; } async function _render(planetName: string, pop: Pop) { const encodedPlanetName = encodeURIComponent(planetName); const [planet, siteCounts, infras, allPrices]: [Planet, SiteCount[], Infrastructure[], Price[]] = await Promise.all([ cachedFetchJSON(`https://api.fnar.net/planet/${encodedPlanetName}?include_population_reports=true`), cachedFetchJSON(`https://api.fnar.net/planet/sitecount?planet=${encodedPlanetName}`), cachedFetchJSON(`https://api.fnar.net/infrastructure?infrastructure=${encodedPlanetName}&include_upkeeps=true`), cachedFetchJSON('https://api.prunplanner.org/data/exchanges'), ]); let lastPOPR = null; for (const report of planet.PopulationReports) { if (!lastPOPR || report.SimulationPeriod > lastPOPR.SimulationPeriod) lastPOPR = report; } if (lastPOPR === null) { renderTarget.textContent = `no POPR for ${planetName}`; return; } const lastPOPRts = Math.floor(new Date(lastPOPR.ReportTimestamp).getTime() / 1000); const nextPOPRts = lastPOPRts + 7 * 24 * 60 * 60; let currentPop = 0; if (pop == 'pio') currentPop = lastPOPR.NextPopulationPioneer; else if (pop == 'set') currentPop = lastPOPR.NextPopulationSettler; else if (pop == 'tec') currentPop = lastPOPR.NextPopulationTechnician; else if (pop == 'eng') currentPop = lastPOPR.NextPopulationEngineer; else if (pop == 'sci') currentPop = lastPOPR.NextPopulationScientist; const siteCount = siteCounts[0].Count; const totalNeeds: Record = { 'safety': calcTotalNeeds(lastPOPR, 'safety'), 'health': calcTotalNeeds(lastPOPR, 'health'), 'comfort': calcTotalNeeds(lastPOPR, 'comfort'), 'culture': calcTotalNeeds(lastPOPR, 'culture'), 'education': calcTotalNeeds(lastPOPR, 'education'), }; const currentPOPIFilled: POPIFill = new Map(); for (const infra of infras) { const filled = calcPOPIFilled(infra); if (filled !== null) currentPOPIFilled.set(infra.Type, {tier: infra.CurrentLevel, numMats: filled}); } const prices: Map = new Map(); for (const price of allPrices) if (price.exchange_code === 'IC1') prices.set(price.ticker, price.vwap_30d); renderTarget.innerHTML = `last POPR: ${lastPOPR.ReportTimestamp} (${lastPOPRts})
next POPR: ${new Date(nextPOPRts * 1000).toISOString()} (${nextPOPRts})
pio set tec eng sci
population ${lastPOPR.NextPopulationPioneer}
${formatDelta(lastPOPR.PopulationDifferencePioneer)}
${lastPOPR.NextPopulationSettler}
${formatDelta(lastPOPR.PopulationDifferenceSettler)}
${lastPOPR.NextPopulationTechnician}
${formatDelta(lastPOPR.PopulationDifferenceTechnician)}
${lastPOPR.NextPopulationEngineer}
${formatDelta(lastPOPR.PopulationDifferenceEngineer)}
${lastPOPR.NextPopulationScientist}
${formatDelta(lastPOPR.PopulationDifferenceScientist)}
unemployed ${unemployed(lastPOPR.NextPopulationPioneer, lastPOPR.PopulationDifferencePioneer, lastPOPR.OpenJobsPioneer, lastPOPR.UnemploymentRatePioneer)} ${unemployed(lastPOPR.NextPopulationSettler, lastPOPR.PopulationDifferenceSettler, lastPOPR.OpenJobsSettler, lastPOPR.UnemploymentRateSettler)} ${unemployed(lastPOPR.NextPopulationTechnician, lastPOPR.PopulationDifferenceTechnician, lastPOPR.OpenJobsTechnician, lastPOPR.UnemploymentRateTechnician)} ${unemployed(lastPOPR.NextPopulationEngineer, lastPOPR.PopulationDifferenceEngineer, lastPOPR.OpenJobsEngineer, lastPOPR.UnemploymentRateEngineer)} ${unemployed(lastPOPR.NextPopulationScientist, lastPOPR.PopulationDifferenceScientist, lastPOPR.OpenJobsScientist, lastPOPR.UnemploymentRateScientist)}

needs

${siteCount} bases
safety health comfort culture education
last POPR ${formatPct(lastPOPR.NeedFulfillmentSafety)} ${formatPct(lastPOPR.NeedFulfillmentHealth)} ${formatPct(lastPOPR.NeedFulfillmentComfort)} ${formatPct(lastPOPR.NeedFulfillmentCulture)} ${formatPct(lastPOPR.NeedFulfillmentEducation)}
total needed ${formatNum(totalNeeds['safety'])} ${formatNum(totalNeeds['health'])} ${formatNum(totalNeeds['comfort'])} ${formatNum(totalNeeds['culture'])} ${formatNum(totalNeeds['education'])}

current POPI

${infras.map((infra) => { if (infra.CurrentLevel === 0) return ''; const fill = currentPOPIFilled.get(infra.Type)!; return ``; }).join('')}
${infra.Type} T${infra.CurrentLevel} ${fill.numMats}/${infra.Upkeeps.length}
current projected ${pop.toUpperCase()} happiness: ${formatPct(projectedHappiness(pop, totalNeeds, siteCount, currentPOPIFilled))}

options

${paretoFront(pop, totalNeeds, siteCount, currentPOPIFilled, currentPop, prices).map((result) => { let unitCost = ''; if (result.cost > 0) unitCost = formatNum(result.cost / result.migration); return ``; }).join('')}
config projected migration cost/day unit cost
${[...result.config.entries()].map(([building, fill]) => `${building}: ${fill.numMats}`).join(', ')} ${formatDelta(result.migration)} ${formatNum(result.cost)} ${unitCost}
`; } const formatNum = new Intl.NumberFormat(undefined, {maximumFractionDigits: 2}).format; const formatPct = new Intl.NumberFormat(undefined, {style: 'percent', maximumFractionDigits: 2}).format; function formatDelta(n: number, withPlus: boolean = true): string { if (n > 0) return `${withPlus ? '+' : ''}${formatNum(n)}`; else if (n < 0) return `${formatNum(n)}`; else return n.toString(); } function unemployed(people: number, change: number, openJobs: number, unemploymentRate: number): string { let nextUnemployed; if (openJobs <= people + change) { let prevUnemploymentRate = unemploymentRate; if (openJobs > 0) if (people - change > 0) prevUnemploymentRate = -openJobs / (people - change); else prevUnemploymentRate = -1 const prevUnemployed = prevUnemploymentRate * (people - change); nextUnemployed = prevUnemployed + change; } else nextUnemployed = -openJobs + change; return formatDelta(Math.round(nextUnemployed), false); } function calcTotalNeeds(popReport: POPR, need: Need): number { return popReport.NextPopulationPioneer * NEEDS.pio[need] + popReport.NextPopulationSettler * NEEDS.set[need] + popReport.NextPopulationTechnician * NEEDS.tec[need] + popReport.NextPopulationEngineer * NEEDS.eng[need] + popReport.NextPopulationScientist * NEEDS.sci[need]; } function calcPOPIFilled(infra: Infrastructure): number | null { if (infra.CurrentLevel === 0) return null; let filled = 0; for (const upkeep of infra.Upkeeps) { const nextConsumptionAmount = upkeep.StoreCapacity / 30 * upkeep.Duration; // # capacity is always 30 days if (upkeep.Stored >= nextConsumptionAmount) filled++; } return filled; } function paretoFront(pop: Pop, totalNeeds: Record, siteCount: number, currentPOPIFilled: POPIFill, currentPop: number, prices: Map): {config: POPIFill, migration: number, cost: number}[] { const results: {config: POPIFill, migration: number, cost: number}[] = []; for (const config of popiFillCombinations(currentPOPIFilled)) { const happiness = projectedHappiness(pop, totalNeeds, siteCount, config); let migration = 0; if (happiness > 0.7) migration = currentPop * (happiness - 0.7); else if (happiness < 0.5) migration = 0.8 * currentPop * (happiness - 0.5); // TODO: education let cost = calcCost(config, prices); // is any result better than this one? if (results.some((result) => (result.migration >= migration && result.cost < cost) || (result.migration > migration && result.cost <= cost))) continue; // are any results worse than this one? for (let i = results.length - 1; i >= 0; i--) if ((results[i].migration <= migration && results[i].cost > cost) || (results[i].migration < migration && results[i].cost >= cost)) results.splice(i, 1); results.push({config, migration, cost}); } return results; } function* popiFillCombinations(currentPOPIFilled: POPIFill): Generator { const entries = [...currentPOPIFilled.keys()]; function* helper(index: number, current: POPIFill): Generator { if (index === entries.length) { yield new Map(current); return; } const building = entries[index]; const tier = currentPOPIFilled.get(building)!.tier; const maxBuildingMats = Object.keys(POPI[building].mats).length; for (let i = 0; i <= maxBuildingMats; i++) { current.set(building, {tier, numMats: i}); yield* helper(index + 1, current); } } yield* helper(0, new Map()); } function projectedHappiness(pop: Pop, totalNeeds: Record, siteCount: number, popiFilled: POPIFill): number { let totalProvided: Record = {'safety': 50 * siteCount, 'health': 50 * siteCount, 'comfort': 0, 'culture': 0, 'education': 0}; for (const [building, fill] of popiFilled.entries()) { const {needs} = POPI[building]; const maxBuildingMats = Object.keys(POPI[building].mats).length; for (const {need, supplied} of needs) { const provided = fill.numMats / maxBuildingMats * fill.tier * supplied; totalProvided[need] += provided; } } const satisfaction: Map = new Map(); // percentage of needs fulfilled for (const _need in totalProvided) { const need = _need as Need; satisfaction.set(need, Math.min(totalProvided[need] / totalNeeds[need], 1)); } const safetyHealthCap = Math.min(satisfaction.get('safety')!, satisfaction.get('health')!); if (satisfaction.get('comfort')! > safetyHealthCap) satisfaction.set('comfort', safetyHealthCap); if (satisfaction.get('culture')! > safetyHealthCap) satisfaction.set('culture', safetyHealthCap); const comfortCultureCap = Math.min(satisfaction.get('comfort')!, satisfaction.get('culture')!); if (satisfaction.get('education')! > comfortCultureCap) satisfaction.set('education', comfortCultureCap); const weights = NEEDS[pop]; let happiness = 1 - Object.values(weights).reduce((sum, weight) => sum + weight, 0); // assume 100% life support for (const [need, s] of satisfaction.entries()) happiness += weights[need] * s; return happiness; } function calcCost(config: POPIFill, prices: Map): number { let cost = 0; for (const [building, fill] of config.entries()) { const {mats} = POPI[building]; const matPrices: {ticker: string, costPerDay: number}[] = []; for (const [mat, amount] of Object.entries(mats)) { const matCost = prices.get(mat)!; if (matCost === undefined) throw new Error('no price for ' + mat); matPrices.push({ticker: mat, costPerDay: matCost * amount}); } matPrices.sort((a, b) => a.costPerDay - b.costPerDay); for (let i = 0; i < fill.numMats; i++) cost += matPrices[i].costPerDay * fill.tier; } return cost; } const NEEDS: Record> = { 'pio': {'safety': 0.25, 'health': 0.15, 'comfort': 0.03, 'culture': 0.02, 'education': 0.01}, 'set': {'safety': 0.30, 'health': 0.20, 'comfort': 0.03, 'culture': 0.03, 'education': 0.03}, 'tec': {'safety': 0.20, 'health': 0.30, 'comfort': 0.20, 'culture': 0.10, 'education': 0.05}, 'eng': {'safety': 0.10, 'health': 0.15, 'comfort': 0.35, 'culture': 0.20, 'education': 0.10}, 'sci': {'safety': 0.10, 'health': 0.10, 'comfort': 0.20, 'culture': 0.25, 'education': 0.30}, } const POPI: Record}> = { 'SAFETY_STATION': { needs: [{need: 'safety', supplied: 2500}], mats: {'DW': 10, 'OFF': 10, 'SUN': 2}, }, 'SECURITY_DRONE_POST': { needs: [{need: 'safety', supplied: 5000}], mats: {'POW': 1, 'RAD': 0.47, 'CCD': 0.07, 'SUD': 0.07}, }, 'EMERGENCY_CENTER': { needs: [{need: 'safety', supplied: 1000}, {need: 'health', supplied: 1000}], mats: {'PK': 2, 'POW': 0.4, 'BND': 4, 'RED': 0.07, 'BSC': 0.07}, }, 'INFIRMARY': { needs: [{need: 'health', supplied: 2500}], mats: {'OFF': 10, 'TUB': 6.67, 'STR': 0.67}, }, 'HOSPITAL': { needs: [{need: 'health', supplied: 5000}], mats: {'PK': 2, 'SEQ': 0.4, 'BND': 4, 'SDR': 0.07, 'RED': 0.07, 'BSC': 0.13}, }, 'WELLNESS_CENTER': { needs: [{need: 'health', supplied: 1000}, {need: 'comfort', supplied: 1000}], mats: {'KOM': 4, 'OLF': 2, 'DW': 6, 'DEC': 0.67, 'PFE': 2.67, 'SOI': 6.67}, }, 'WILDLIFE_PARK': { needs: [{need: 'comfort', supplied: 2500}], mats: {'DW': 10, 'FOD': 6, 'PFE': 2, 'SOI': 3.33, 'DEC': 0.33}, }, 'ARCADES': { needs: [{need: 'comfort', supplied: 5000}], mats: {'POW': 2, 'MHP': 2, 'OLF': 4, 'BID': 0.2, 'HOG': 0.2, 'EDC': 0.2}, }, 'ART_CAFE': { needs: [{need: 'comfort', supplied: 1000}, {need: 'culture', supplied: 1000}], mats: {'MHP': 1, 'HOG': 1, 'UTS': 0.67, 'DEC': 0.67}, }, 'ART_GALLERY': { needs: [{need: 'culture', supplied: 2500}], mats: {'MHP': 1, 'HOG': 1, 'UTS': 0.67, 'DEC': 0.67}, }, 'THEATER': { needs: [{need: 'culture', supplied: 5000}], mats: {'POW': 1.4, 'MHP': 2, 'HOG': 1.4, 'OLF': 4, 'BID': 0.33, 'DEC': 0.67}, }, 'PLANETARY_BROADCASTING_HUB': { needs: [{need: 'culture', supplied: 1000}, {need: 'education', supplied: 1000}], mats: {'OFF': 10, 'MHP': 1, 'SP': 1.33, 'AAR': 0.67, 'EDC': 0.27, 'IDC': 0.13}, }, 'LIBRARY': { needs: [{need: 'education', supplied: 2500}], mats: {'MHP': 1, 'HOG': 1, 'CD': 0.33, 'DIS': 0.33, 'BID': 0.2}, }, 'UNIVERSITY': { needs: [{need: 'education', supplied: 5000}], mats: {'COF': 10, 'REA': 10, 'TUB': 10, 'BID': 0.33, 'HD': 0.67, 'IDC': 0.2}, }, }; type Pop = 'pio' | 'set' | 'tec' | 'eng' | 'sci'; type Need = 'safety' | 'health' | 'comfort' | 'culture' | 'education'; type POPIBuilding = 'SAFETY_STATION' | 'SECURITY_DRONE_POST' | 'EMERGENCY_CENTER' | 'INFIRMARY' | 'HOSPITAL' | 'WELLNESS_CENTER' | 'WILDLIFE_PARK' | 'ARCADES' | 'ART_CAFE' | 'ART_GALLERY' | 'THEATER' | 'PLANETARY_BROADCASTING_HUB' | 'LIBRARY' | 'UNIVERSITY'; type POPIFill = Map; interface Planet { PopulationReports: POPR[] } interface POPR { SimulationPeriod: number; ReportTimestamp: string; NeedFulfillmentSafety: number; NeedFulfillmentHealth: number; NeedFulfillmentComfort: number; NeedFulfillmentCulture: number; NeedFulfillmentEducation: number; NextPopulationPioneer: number; NextPopulationSettler: number; NextPopulationTechnician: number; NextPopulationEngineer: number; NextPopulationScientist: number; PopulationDifferencePioneer: number; PopulationDifferenceSettler: number; PopulationDifferenceTechnician: number; PopulationDifferenceEngineer: number; PopulationDifferenceScientist: number; OpenJobsPioneer: number; OpenJobsSettler: number; OpenJobsTechnician: number; OpenJobsEngineer: number; OpenJobsScientist: number; UnemploymentRatePioneer: number; UnemploymentRateSettler: number; UnemploymentRateTechnician: number; UnemploymentRateEngineer: number; UnemploymentRateScientist: number; } interface SiteCount { Count: number; } interface Infrastructure { Type: POPIBuilding; CurrentLevel: number; Upkeeps: {Stored: number, StoreCapacity: number, 'Duration': number}[]; } interface Price { ticker: string exchange_code: string vwap_30d: number }