Explorar o código

buy: include exporting planets' inventory

same as 94ac7a89 but in TS
raylu hai 3 semanas
pai
achega
e55ff52fa7
Modificáronse 1 ficheiros con 20 adicións e 7 borrados
  1. 20 7
      ts/buy.ts

+ 20 - 7
ts/buy.ts

@@ -20,21 +20,27 @@ document.querySelector('#fetch')!.addEventListener('click', async () => {
 setupPopover();
 
 async function calculate(username: string, apiKey: string, supplyForDays: number): Promise<void> {
-	const [prices, planets, warehouse, {bids, orders}] = await Promise.all([
+	const [prices, planets, avail, {bids, orders}] = await Promise.all([
 		getPrices(),
 		getPlanets(username, apiKey),
 		warehouseInventory(username, apiKey),
 		getBids(username, apiKey)
 	]);
 	const buy = new Map<string, number>();
-	for (const planet of planets)
+	for (const planet of planets) {
 		for (const [mat, amount] of planet.supplyForDays(supplyForDays))
 			buy.set(mat, (buy.get(mat) ?? 0) + amount);
+		for (const mat of planet.exporting) {
+			const planetExport = planet.inventory.get(mat);
+			if (planetExport)
+				avail.set(mat, (avail.get(mat) ?? 0) + planetExport);
+		}
+	}
 
 	// what's left to buy
 	const materials: Material[] = [];
 	for (const [mat, amount] of buy) {
-		const remaining = Math.max(amount - (bids.get(mat) ?? 0) - (warehouse.get(mat) ?? 0), 0);
+		const remaining = Math.max(amount - (bids.get(mat) ?? 0) - (avail.get(mat) ?? 0), 0);
 		const price = prices.get(mat);
 		if (!price || price.Bid === null || price.Ask === null) {
 			console.log(mat, 'has no bid/ask');
@@ -45,7 +51,7 @@ async function calculate(username: string, apiKey: string, supplyForDays: number
 			ticker: mat,
 			amount,
 			bids: bids.get(mat) ?? 0,
-			warehouse: warehouse.get(mat) ?? 0,
+			have: avail.get(mat) ?? 0,
 			spread,
 			savings: spread * remaining,
 		});
@@ -57,12 +63,12 @@ async function calculate(username: string, apiKey: string, supplyForDays: number
 	const format= new Intl.NumberFormat(undefined, {maximumFractionDigits: 0}).format;
 	for (const m of materials) {
 		const tr = document.createElement('tr');
-		const buyAmount = Math.max(m.amount - m.bids - m.warehouse, 0);
+		const buyAmount = Math.max(m.amount - m.bids - m.have, 0);
 		tr.innerHTML = `
 			<td>${m.ticker}</td>
 			<td>${format(m.amount)}</td>
 			<td>${format(m.bids)}</td>
-			<td>${format(m.warehouse)}</td>
+			<td>${format(m.have)}</td>
 			<td>${format(buyAmount)}</td>
 			<td>${format(m.spread)}</td>
 			<td>${format(m.savings)}</td>
@@ -126,6 +132,7 @@ class Planet {
 	name: string;
 	inventory: Map<string, number>;
 	netConsumption: Amount[];
+	exporting: Set<string>; // producing more than consumption
 
 	constructor(fioBurn: FIOBurn) {
 		this.name = fioBurn.PlanetName || fioBurn.PlanetNaturalId;
@@ -149,6 +156,12 @@ class Planet {
 			c.netConsumption = net;
 			this.netConsumption.push(c);
 		}
+
+		const consuming = new Set(this.netConsumption.map(item => item.MaterialTicker));
+		this.exporting = new Set<string>();
+		for (const item of fioBurn.OrderProduction)
+			if (!consuming.has(item.MaterialTicker))
+				this.exporting.add(item.MaterialTicker);
 	}
 
 	supplyForDays(targetDays: number): Map<string, number> {
@@ -169,7 +182,7 @@ interface Material {
 	ticker: string;
 	amount: number;
 	bids: number;
-	warehouse: number;
+	have: number;
 	spread: number;
 	savings: number;
 }