main.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // src/main.ts
  2. async function fetchData() {
  3. const [recipesResponse, exchangesResponse] = await Promise.all([
  4. fetch("https://api.prunplanner.org/data/recipes"),
  5. fetch("https://api.prunplanner.org/data/exchanges")
  6. ]);
  7. const [recipes, prices] = await Promise.all([
  8. recipesResponse.json(),
  9. exchangesResponse.json()
  10. ]);
  11. return { recipes, prices };
  12. }
  13. function render({ recipes, prices }) {
  14. const priceMap = new Map;
  15. for (const price of prices)
  16. if (price.ExchangeCode == "PP7D_IC1")
  17. priceMap.set(price.MaterialTicker, price.PriceAverage);
  18. const fmt = new Intl.NumberFormat(undefined, { maximumFractionDigits: 2 });
  19. const tbody = document.querySelector("tbody");
  20. tbody.innerHTML = "";
  21. for (const recipe of recipes) {
  22. if (recipe.BuildingTicker !== "FRM")
  23. continue;
  24. const runsPerDay = 1000 * 60 * 60 * 24 / recipe.TimeMs;
  25. const revenuePerRun = recipe.Outputs.reduce((sum, output) => {
  26. const price = priceMap.get(output.Ticker);
  27. if (price === undefined)
  28. throw new Error(`missing price for ${output.Ticker}`);
  29. return sum + price * output.Amount;
  30. }, 0);
  31. const costPerRun = recipe.Inputs.reduce((sum, input) => {
  32. const price = priceMap.get(input.Ticker);
  33. if (price === undefined)
  34. throw new Error(`missing price for ${input.Ticker}`);
  35. return sum + price * input.Amount;
  36. }, 0);
  37. const row = document.createElement("tr");
  38. row.innerHTML = `
  39. <td>${recipe.RecipeName}</td>
  40. <td>${fmt.format(revenuePerRun * runsPerDay)}</td>
  41. <td>${fmt.format(costPerRun * runsPerDay)}</td>
  42. <td>${fmt.format((revenuePerRun - costPerRun) * runsPerDay)}</td>
  43. `;
  44. tbody.appendChild(row);
  45. }
  46. }
  47. fetchData().then(render);
  48. //# debugId=E579798A6AFE522164756E2164756E21