| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- interface Recipe {
- RecipeId: string;
- BuildingTicker: string;
- RecipeName: string;
- TimeMs: number;
- Inputs: Array<{Ticker: string; Amount: number}>;
- Outputs: Array<{Ticker: string; Amount: number}>;
- }
- interface Price {
- MaterialTicker: string;
- ExchangeCode: string;
- PriceAverage: number;
- }
- async function fetchData(): Promise<{recipes: Recipe[]; prices: Price[]}> {
- const [recipesResponse, exchangesResponse] = await Promise.all([
- fetch('https://api.prunplanner.org/data/recipes'),
- fetch('https://api.prunplanner.org/data/exchanges'),
- ]);
- const [recipes, prices] = await Promise.all([
- recipesResponse.json(),
- exchangesResponse.json(),
- ]);
- return {recipes, prices};
- }
- function render({recipes, prices}: {recipes: Recipe[], prices: Price[]}) {
- const priceMap = new Map<string, number>();
- for (const price of prices)
- if (price.ExchangeCode == 'PP7D_IC1')
- priceMap.set(price.MaterialTicker, price.PriceAverage);
- const fmt = new Intl.NumberFormat(undefined, {maximumFractionDigits: 2});
- const tbody = document.querySelector('tbody') as HTMLTableSectionElement;
- tbody.innerHTML = '';
- for (const recipe of recipes) {
- if (recipe.BuildingTicker !== 'FRM')
- continue;
- const runsPerDay = 1000 * 60 * 60 * 24 / recipe.TimeMs;
- const revenuePerRun = recipe.Outputs.reduce((sum, output) => {
- const price = priceMap.get(output.Ticker);
- if (price === undefined)
- throw new Error(`missing price for ${output.Ticker}`);
- return sum + price * output.Amount;
- }, 0);
- const costPerRun = recipe.Inputs.reduce((sum, input) => {
- const price = priceMap.get(input.Ticker);
- if (price === undefined)
- throw new Error(`missing price for ${input.Ticker}`);
- return sum + price * input.Amount;
- }, 0);
- const row = document.createElement('tr');
- row.innerHTML = `
- <td>${recipe.RecipeName}</td>
- <td>${fmt.format(revenuePerRun * runsPerDay)}</td>
- <td>${fmt.format(costPerRun * runsPerDay)}</td>
- <td>${fmt.format((revenuePerRun - costPerRun) * runsPerDay)}</td>
- `;
- tbody.appendChild(row);
- }
- }
- fetchData().then(render);
|