2 Commits 85d56aea91 ... a2059c0dcb

Author SHA1 Message Date
  raylu a2059c0dcb shipbuilding: calculate consumables cost 1 month ago
  raylu 85d56aea91 shipbuilding: calculate consumables cost 1 month ago
1 changed files with 9 additions and 10 deletions
  1. 9 10
      ts/shipbuilding.ts

+ 9 - 10
ts/shipbuilding.ts

@@ -44,7 +44,7 @@ async function render() {
 		throw new Error('missing shipbuilding container');
 	main.innerHTML = '<p>Loading...</p>';
 
-	const [allPrices, recipes, buildings, knownCompanies, companyProductionById] = await Promise.all([
+	const [allPrices, recipes, buildingList, knownCompanies, companyProductionById] = await Promise.all([
 		cachedFetchJSON('https://refined-prun.github.io/refined-prices/all.json') as Promise<RawPrice[]>,
 		recipeForMats(),
 		cachedFetchJSON('https://api.prunplanner.org/data/buildings/') as Promise<Building[]>,
@@ -53,6 +53,7 @@ async function render() {
 	]);
 	const prices = Object.fromEntries(allPrices.filter((price) => price.ExchangeCode === 'IC1')
 			.map((price) => [price.MaterialTicker, price]));
+	const buildings = Object.fromEntries(buildingList.map((b) => [b.building_ticker, b]));
 
 	const production: Production = {};
 	const extract: Record<string, number> = {};
@@ -71,9 +72,8 @@ async function render() {
 		for (const [mat, amount] of Object.entries(buildingProduction))
 			requiredMats[mat] = (requiredMats[mat] ?? 0) + amount;
 
-	const buildingsByTicker = Object.fromEntries(buildings.map((b) => [b.building_ticker, b]));
 	const expertiseGroups: Record<string, string[]> = {};
-	for (const building of buildings) {
+	for (const building of buildingList) {
 		if (!(building.building_ticker in production))
 			continue;
 		if (!expertiseGroups[building.expertise])
@@ -85,7 +85,7 @@ async function render() {
 	main.append(
 		renderAnalysis(analysisNodes),
 		element('p', {textContent: `total cost: ${formatWhole(cost)}`}),
-		renderProduction(expertiseGroups, production, prices, recipes, buildingsByTicker),
+		renderProduction(expertiseGroups, production, prices, recipes, buildings),
 		renderMatList('extract', extract),
 		renderMatList('buy', buy),
 		renderShipbuilders(requiredMats, knownCompanies, companyProductionById),
@@ -261,16 +261,15 @@ function buildingDailyCost(building: Building, prices: Record<string, RawPrice>)
 	return cost;
 }
 
-function renderProduction(expertiseGroups: Record<string, string[]>, production: Production,
-		prices: Record<string, RawPrice>, recipes: Record<string, Recipe>,
-		buildingsByTicker: Record<string, Building>): HTMLElement {
+function renderProduction(expertiseGroups: Record<string, string[]>, production: Production, prices: Record<string, RawPrice>,
+	recipes: Record<string, Recipe>, buildings: Record<string, Building>): HTMLElement {
 	const section = element('section');
 	section.append(element('h2', {textContent: 'production'}));
 
 	let totalConsumablesCost = 0;
-	for (const [expertise, buildings] of Object.entries(expertiseGroups)) {
+	for (const [expertise, productionBuildings] of Object.entries(expertiseGroups)) {
 		section.append(element('h3', {textContent: expertise}));
-		for (const building of buildings) {
+		for (const building of productionBuildings) {
 			const buildingMats = element('div');
 			const mats = Object.entries(production[building]);
 			let buildingMins = 0;
@@ -289,7 +288,7 @@ function renderProduction(expertiseGroups: Record<string, string[]>, production:
 				buildingMins += amount / outputPerRun * (recipe.time_ms / 1000 / 60);
 			}
 			const numBuildings = buildingMins / (24*60) / 5 / 1.605; // one ship every 5 days, 160.5% efficiency
-			const consumablesCost = buildingDailyCost(buildingsByTicker[building], prices) * Math.round(Math.max(1, numBuildings));
+			const consumablesCost = buildingDailyCost(buildings[building], prices) * Math.round(Math.max(1, numBuildings));
 			totalConsumablesCost += consumablesCost;
 			const buildingRow = element('div', {className: 'building-row',
 				textContent: `${formatFixed(numBuildings, 1)}x${building} (${formatWhole(consumablesCost)}/d)`});