67 lines
1.8 KiB
TypeScript
67 lines
1.8 KiB
TypeScript
import {marketApi} from '@/mammon';
|
|
import {getMarketTypes, MarketType} from '../type';
|
|
import {MarketTypePrice} from './MarketTypePrice';
|
|
|
|
export type LocationPrice = {
|
|
buyUnit: number;
|
|
sellUnit: number;
|
|
buy: number;
|
|
sell: number;
|
|
};
|
|
|
|
export type ComparedItem = {
|
|
typeId: number;
|
|
name: string;
|
|
quantity: number;
|
|
volume: number;
|
|
prices: Record<number, LocationPrice>;
|
|
};
|
|
|
|
export type PricesForLocation = (types: MarketType[], locationId: number) => Promise<MarketTypePrice[]>;
|
|
|
|
export const compareAppraisal = async (
|
|
raw: string,
|
|
locationIds: number[],
|
|
getPrices: PricesForLocation
|
|
): Promise<ComparedItem[]> => {
|
|
if (!raw?.trim() || locationIds.length === 0) {
|
|
return [];
|
|
}
|
|
|
|
const stacks = await marketApi.parseStacks(raw).then(r => r.data);
|
|
|
|
if (stacks.length === 0) {
|
|
return [];
|
|
}
|
|
|
|
const quantities = new Map(stacks.map(s => [s.marketTypeId, s.quantity]));
|
|
const types = await getMarketTypes(stacks.map(s => s.marketTypeId));
|
|
|
|
const rows = new Map<number, ComparedItem>(types.map(t => [t.id, {
|
|
typeId: t.id,
|
|
name: t.name,
|
|
quantity: quantities.get(t.id) ?? 0,
|
|
volume: t.volume,
|
|
prices: {},
|
|
}]));
|
|
|
|
const pricesByLocation = await Promise.all(
|
|
locationIds.map(async locationId => [locationId, await getPrices(types, locationId)] as const)
|
|
);
|
|
|
|
pricesByLocation.forEach(([locationId, prices]) => prices.forEach(p => {
|
|
const row = rows.get(p.type.id);
|
|
|
|
if (row) {
|
|
row.prices[locationId] = {
|
|
buyUnit: p.buy,
|
|
sellUnit: p.sell,
|
|
buy: p.buy * row.quantity,
|
|
sell: p.sell * row.quantity,
|
|
};
|
|
}
|
|
}));
|
|
|
|
return [...rows.values()];
|
|
};
|