소스 검색

buy: don't hardcode IC1

raylu 2 주 전
부모
커밋
bc0f408a05
1개의 변경된 파일19개의 추가작업 그리고 11개의 파일을 삭제
  1. 19 11
      ts/buy.ts

+ 19 - 11
ts/buy.ts

@@ -1,5 +1,12 @@
 import {setupPopover} from './popover';
 
+const warehouseNames = {
+	'AI1': 'ANT',
+	'CI1': 'BEN',
+	'IC1': 'HRT',
+	'NC1': 'MOR',
+} as const;
+
 const username = document.querySelector('#username') 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 () => {
 	const supplyForDays = parseInt((document.querySelector('#days') as HTMLInputElement).value, 10);
 	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);
 	localStorage.setItem('fio-username', username.value);
 	localStorage.setItem('fio-api-key', apiKey.value);
@@ -24,12 +33,12 @@ document.querySelector('#copy-xit-act')!.addEventListener('click', () => {
 });
 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([
 		getPrices(cx),
 		getPlanets(username, apiKey),
-		warehouseInventory(username, apiKey),
-		getBids(username, apiKey)
+		warehouseInventory(username, apiKey, warehouseNames[cx]),
+		getBids(username, apiKey, cx)
 	]);
 	const buy = new Map<string, number>();
 	for (const planet of planets) {
@@ -94,7 +103,7 @@ async function calculate(username: string, apiKey: string, supplyForDays: number
 
 	xitAct.value = JSON.stringify({
 		'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},
 		],
 		'global': {'name': `buy orders for ${supplyForDays} days`},
@@ -125,12 +134,12 @@ async function getPlanets(username: string, apiKey: string) {
 	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,
 		{headers: {'Authorization': apiKey}}).then(r => r.json());
 	
 	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}`,
 				{headers: {'Authorization': apiKey}}).then(r => r.json());
 			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);
 			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,
 		{headers: {'Authorization': apiKey}}).then(r => r.json());
 	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>();
 	for (const order of orders)
 		bids.set(order.MaterialTicker, (bids.get(order.MaterialTicker) ?? 0) + order.Amount);