|
@@ -65,16 +65,22 @@ async function calcForCX(username: string, apiKey: string, supplyForDays: number
|
|
|
const prices = await getPrices(cx);
|
|
const prices = await getPrices(cx);
|
|
|
const avail = await warehouseInventory(username, apiKey, warehouseNames[cx]);
|
|
const avail = await warehouseInventory(username, apiKey, warehouseNames[cx]);
|
|
|
const {bids, orders} = await getBids(username, apiKey, cx);
|
|
const {bids, orders} = await getBids(username, apiKey, cx);
|
|
|
- const buy = new Map<string, number>();
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // what do we need and have
|
|
|
|
|
+ const totalConsumption = 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 [ticker, consumption] of planet.netConsumption)
|
|
|
|
|
+ totalConsumption.set(ticker, (totalConsumption.get(ticker) ?? 0) + consumption);
|
|
|
for (const mat of planet.exporting) {
|
|
for (const mat of planet.exporting) {
|
|
|
const planetExport = planet.inventory.get(mat);
|
|
const planetExport = planet.inventory.get(mat);
|
|
|
if (planetExport)
|
|
if (planetExport)
|
|
|
avail.set(mat, (avail.get(mat) ?? 0) + planetExport);
|
|
avail.set(mat, (avail.get(mat) ?? 0) + planetExport);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ const buy = new Map<string, number>();
|
|
|
|
|
+ for (const [mat, amount] of totalConsumption)
|
|
|
|
|
+ if (amount > 0)
|
|
|
|
|
+ buy.set(mat, (buy.get(mat) ?? 0) + amount * supplyForDays);
|
|
|
|
|
|
|
|
// what's left to buy
|
|
// what's left to buy
|
|
|
const materials: Material[] = [];
|
|
const materials: Material[] = [];
|
|
@@ -215,7 +221,7 @@ class Planet {
|
|
|
id: string;
|
|
id: string;
|
|
|
name: string;
|
|
name: string;
|
|
|
inventory: Map<string, number>;
|
|
inventory: Map<string, number>;
|
|
|
- netConsumption: Amount[];
|
|
|
|
|
|
|
+ netConsumption: Map<string, number>;
|
|
|
exporting: Set<string>; // producing more than consumption
|
|
exporting: Set<string>; // producing more than consumption
|
|
|
|
|
|
|
|
constructor(fioBurn: FIOBurn) {
|
|
constructor(fioBurn: FIOBurn) {
|
|
@@ -225,42 +231,19 @@ class Planet {
|
|
|
for (const item of fioBurn.Inventory)
|
|
for (const item of fioBurn.Inventory)
|
|
|
this.inventory.set(item.MaterialTicker, item.MaterialAmount);
|
|
this.inventory.set(item.MaterialTicker, item.MaterialAmount);
|
|
|
|
|
|
|
|
- const producing = new Map<string, Amount>();
|
|
|
|
|
|
|
+ this.netConsumption = new Map<string, number>();
|
|
|
for (const item of fioBurn.OrderProduction)
|
|
for (const item of fioBurn.OrderProduction)
|
|
|
- producing.set(item.MaterialTicker, item);
|
|
|
|
|
|
|
+ this.netConsumption.set(item.MaterialTicker, -item.DailyAmount);
|
|
|
|
|
|
|
|
- this.netConsumption = [];
|
|
|
|
|
for (const c of [...fioBurn.OrderConsumption, ...fioBurn.WorkforceConsumption]) {
|
|
for (const c of [...fioBurn.OrderConsumption, ...fioBurn.WorkforceConsumption]) {
|
|
|
- let net = c.DailyAmount;
|
|
|
|
|
- const production = producing.get(c.MaterialTicker);
|
|
|
|
|
- if (production) {
|
|
|
|
|
- net -= production.DailyAmount;
|
|
|
|
|
- if (net < 0)
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
- c.netConsumption = net;
|
|
|
|
|
- this.netConsumption.push(c);
|
|
|
|
|
|
|
+ this.netConsumption.set(c.MaterialTicker, (this.netConsumption.get(c.MaterialTicker) ?? 0) + c.DailyAmount);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const consuming = new Set(this.netConsumption.map(item => item.MaterialTicker));
|
|
|
|
|
this.exporting = new Set<string>();
|
|
this.exporting = new Set<string>();
|
|
|
for (const item of fioBurn.OrderProduction)
|
|
for (const item of fioBurn.OrderProduction)
|
|
|
- if (!consuming.has(item.MaterialTicker))
|
|
|
|
|
|
|
+ if ((this.netConsumption.get(item.MaterialTicker) ?? 0) < 0)
|
|
|
this.exporting.add(item.MaterialTicker);
|
|
this.exporting.add(item.MaterialTicker);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- supplyForDays(targetDays: number): Map<string, number> {
|
|
|
|
|
- const buy = new Map<string, number>();
|
|
|
|
|
- for (const consumption of this.netConsumption) {
|
|
|
|
|
- const ticker = consumption.MaterialTicker;
|
|
|
|
|
- const avail = this.inventory.get(ticker) ?? 0;
|
|
|
|
|
- const dailyConsumption = consumption.netConsumption!;
|
|
|
|
|
- const days = avail / dailyConsumption;
|
|
|
|
|
- if (days < targetDays)
|
|
|
|
|
- buy.set(ticker, Math.ceil((targetDays - days) * dailyConsumption));
|
|
|
|
|
- }
|
|
|
|
|
- return buy;
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
interface Material {
|
|
interface Material {
|
|
@@ -310,7 +293,6 @@ interface Warehouse {
|
|
|
interface Amount {
|
|
interface Amount {
|
|
|
MaterialTicker: string;
|
|
MaterialTicker: string;
|
|
|
DailyAmount: number;
|
|
DailyAmount: number;
|
|
|
- netConsumption?: number;
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
interface FIOBurn {
|
|
interface FIOBurn {
|