|
@@ -1,5 +1,12 @@
|
|
|
import {setupPopover} from './popover';
|
|
import {setupPopover} from './popover';
|
|
|
|
|
|
|
|
|
|
+const warehouseNames = {
|
|
|
|
|
+ 'AI1': 'ANT',
|
|
|
|
|
+ 'CI1': 'BEN',
|
|
|
|
|
+ 'IC1': 'HRT',
|
|
|
|
|
+ 'NC1': 'MOR',
|
|
|
|
|
+} as const;
|
|
|
|
|
+
|
|
|
const username = document.querySelector('#username') as HTMLInputElement;
|
|
const username = document.querySelector('#username') as HTMLInputElement;
|
|
|
const apiKey = document.querySelector('#api-key') as HTMLInputElement;
|
|
const apiKey = document.querySelector('#api-key') as HTMLInputElement;
|
|
|
{
|
|
{
|
|
@@ -14,7 +21,9 @@ const xitAct = document.querySelector('textarea#xit-act') as HTMLTextAreaElement
|
|
|
document.querySelector('#fetch')!.addEventListener('click', async () => {
|
|
document.querySelector('#fetch')!.addEventListener('click', async () => {
|
|
|
const supplyForDays = parseInt((document.querySelector('#days') as HTMLInputElement).value, 10);
|
|
const supplyForDays = parseInt((document.querySelector('#days') as HTMLInputElement).value, 10);
|
|
|
const cx = (document.querySelector('#cx') as HTMLInputElement).value;
|
|
const cx = (document.querySelector('#cx') as HTMLInputElement).value;
|
|
|
- const output = await calculate(username.value, apiKey.value, supplyForDays, cx);
|
|
|
|
|
|
|
+ if (!(cx in warehouseNames))
|
|
|
|
|
+ throw new Error(`invalid CX ${cx}`);
|
|
|
|
|
+ const output = await calculate(username.value, apiKey.value, supplyForDays, cx as keyof typeof warehouseNames);
|
|
|
console.log(output);
|
|
console.log(output);
|
|
|
localStorage.setItem('fio-username', username.value);
|
|
localStorage.setItem('fio-username', username.value);
|
|
|
localStorage.setItem('fio-api-key', apiKey.value);
|
|
localStorage.setItem('fio-api-key', apiKey.value);
|
|
@@ -24,12 +33,12 @@ document.querySelector('#copy-xit-act')!.addEventListener('click', () => {
|
|
|
});
|
|
});
|
|
|
setupPopover();
|
|
setupPopover();
|
|
|
|
|
|
|
|
-async function calculate(username: string, apiKey: string, supplyForDays: number, cx: string): Promise<void> {
|
|
|
|
|
|
|
+async function calculate(username: string, apiKey: string, supplyForDays: number, cx: keyof typeof warehouseNames): Promise<void> {
|
|
|
const [prices, planets, avail, {bids, orders}] = await Promise.all([
|
|
const [prices, planets, avail, {bids, orders}] = await Promise.all([
|
|
|
getPrices(cx),
|
|
getPrices(cx),
|
|
|
getPlanets(username, apiKey),
|
|
getPlanets(username, apiKey),
|
|
|
- warehouseInventory(username, apiKey),
|
|
|
|
|
- getBids(username, apiKey)
|
|
|
|
|
|
|
+ warehouseInventory(username, apiKey, warehouseNames[cx]),
|
|
|
|
|
+ getBids(username, apiKey, cx)
|
|
|
]);
|
|
]);
|
|
|
const buy = new Map<string, number>();
|
|
const buy = new Map<string, number>();
|
|
|
for (const planet of planets) {
|
|
for (const planet of planets) {
|
|
@@ -94,7 +103,7 @@ async function calculate(username: string, apiKey: string, supplyForDays: number
|
|
|
|
|
|
|
|
xitAct.value = JSON.stringify({
|
|
xitAct.value = JSON.stringify({
|
|
|
'actions': [
|
|
'actions': [
|
|
|
- {'name': 'BuyItems', 'type': 'CX Buy', 'group': 'A1', 'exchange': 'IC1',
|
|
|
|
|
|
|
+ {'name': 'BuyItems', 'type': 'CX Buy', 'group': 'A1', 'exchange': cx,
|
|
|
'priceLimits': priceLimits, 'buyPartial': true, 'allowUnfilled': true, 'useCXInv': false},
|
|
'priceLimits': priceLimits, 'buyPartial': true, 'allowUnfilled': true, 'useCXInv': false},
|
|
|
],
|
|
],
|
|
|
'global': {'name': `buy orders for ${supplyForDays} days`},
|
|
'global': {'name': `buy orders for ${supplyForDays} days`},
|
|
@@ -125,12 +134,12 @@ async function getPlanets(username: string, apiKey: string) {
|
|
|
return planets;
|
|
return planets;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-async function warehouseInventory(username: string, apiKey: string): Promise<Map<string, number>> {
|
|
|
|
|
|
|
+async function warehouseInventory(username: string, apiKey: string, whName: string): Promise<Map<string, number>> {
|
|
|
const warehouses: Warehouse[] = await fetch('https://rest.fnar.net/sites/warehouses/' + username,
|
|
const warehouses: Warehouse[] = await fetch('https://rest.fnar.net/sites/warehouses/' + username,
|
|
|
{headers: {'Authorization': apiKey}}).then(r => r.json());
|
|
{headers: {'Authorization': apiKey}}).then(r => r.json());
|
|
|
|
|
|
|
|
for (const warehouse of warehouses)
|
|
for (const warehouse of warehouses)
|
|
|
- if (warehouse.LocationNaturalId === 'HRT') {
|
|
|
|
|
|
|
+ if (warehouse.LocationNaturalId === whName) {
|
|
|
const storage: Storage = await fetch(`https://rest.fnar.net/storage/${username}/${warehouse.StoreId}`,
|
|
const storage: Storage = await fetch(`https://rest.fnar.net/storage/${username}/${warehouse.StoreId}`,
|
|
|
{headers: {'Authorization': apiKey}}).then(r => r.json());
|
|
{headers: {'Authorization': apiKey}}).then(r => r.json());
|
|
|
const inventory = new Map<string, number>();
|
|
const inventory = new Map<string, number>();
|
|
@@ -138,15 +147,14 @@ async function warehouseInventory(username: string, apiKey: string): Promise<Map
|
|
|
inventory.set(item.MaterialTicker, item.MaterialAmount);
|
|
inventory.set(item.MaterialTicker, item.MaterialAmount);
|
|
|
return inventory;
|
|
return inventory;
|
|
|
}
|
|
}
|
|
|
- throw new Error("couldn't find HRT warehouse");
|
|
|
|
|
|
|
+ throw new Error(`couldn't find ${whName} warehouse`);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-async function getBids(username: string, apiKey: string) {
|
|
|
|
|
|
|
+async function getBids(username: string, apiKey: string, cx: string) {
|
|
|
const allOrders: ExchangeOrder[] = await fetch('https://rest.fnar.net/cxos/' + username,
|
|
const allOrders: ExchangeOrder[] = await fetch('https://rest.fnar.net/cxos/' + username,
|
|
|
{headers: {'Authorization': apiKey}}).then(r => r.json());
|
|
{headers: {'Authorization': apiKey}}).then(r => r.json());
|
|
|
const orders = allOrders.filter(order =>
|
|
const orders = allOrders.filter(order =>
|
|
|
- order.OrderType === 'BUYING' && order.Status !== 'FILLED' && order.ExchangeCode === 'IC1');
|
|
|
|
|
-
|
|
|
|
|
|
|
+ order.OrderType === 'BUYING' && order.Status !== 'FILLED' && order.ExchangeCode === cx);
|
|
|
const bids = new Map<string, number>();
|
|
const bids = new Map<string, number>();
|
|
|
for (const order of orders)
|
|
for (const order of orders)
|
|
|
bids.set(order.MaterialTicker, (bids.get(order.MaterialTicker) ?? 0) + order.Amount);
|
|
bids.set(order.MaterialTicker, (bids.get(order.MaterialTicker) ?? 0) + order.Amount);
|