Bladeren bron

shipbuilding: handle missing prices better

import RAD
raylu 1 week geleden
bovenliggende
commit
7158ddecb4
1 gewijzigde bestanden met toevoegingen van 14 en 12 verwijderingen
  1. 14 12
      ts/shipbuilding.ts

+ 14 - 12
ts/shipbuilding.ts

@@ -7,7 +7,7 @@ const BUY = new Set([
 	'AIR', 'AU', 'BE', 'BRM', 'BOR', 'BTS', 'CU', 'FAN', 'FC', 'FE', 'HCC', 'HD', 'LDI', 'LI', 'MFK', 'MWF',
 	'REA', 'RG', 'RGO', 'ROM', 'SFK', 'SI', 'STL', 'TCO', 'TPU',
 	// import
-	'AAR', 'AWF', 'CAP', 'CF',
+	'AAR', 'AWF', 'CAP', 'CF', 'RAD',
 	// skip
 	'LFE', 'LHP', 'MFE', 'SFE', 'SSC',
 ])
@@ -36,14 +36,16 @@ const shipbuilders = [
 	'TRUEnterprises',
 ];
 
-render();
-
+const main = document.querySelector('main.shipbuilding')!;
+(async () => {
+	main.innerHTML = 'loading...';
+	try {
+		await render();
+	} catch (e) {
+		main.innerHTML = e instanceof Error ? e.message : String(e);
+	}
+})();
 async function render() {
-	const main = document.querySelector('main.shipbuilding');
-	if (!main)
-		throw new Error('missing shipbuilding container');
-	main.innerHTML = '<p>Loading...</p>';
-
 	const [allPrices, recipes, buildingList, knownCompanies, companyProductionById] = await Promise.all([
 		cachedFetchJSON('https://refined-prun.github.io/refined-prices/all.json') as Promise<RawPrice[]>,
 		recipeForMats(),
@@ -181,12 +183,12 @@ function analyzeMat(mat: string, amount: number, production: Production, extract
 		throw new Error(`missing price for ${mat}`);
 	const traded = price.AverageTraded30D ?? 0;
 	if (BUY.has(mat)) {
-		if (price.Ask == null)
-			throw new Error(`missing ask price for ${mat}`);
+		const matPrice = price.VWAP30D ?? price.Ask;
+		if (matPrice == null) throw new Error(`missing ask price for ${mat}`);
 		buy[mat] = (buy[mat] ?? 0) + amount;
 		return {
-			cost: price.Ask * amount,
-			node: { text: `${formatAmount(amount)}x${mat} buy: ${formatAmount(price.Ask)}, daily traded ${formatFixed(traded, 1)}`, children: [] },
+			cost: matPrice * amount,
+			node: { text: `${formatAmount(amount)}x${mat} buy: ${formatAmount(matPrice)}, daily traded ${formatFixed(traded, 1)}`, children: [] },
 		};
 	}