main.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 tbody = document.querySelector("tbody");
  19. tbody.innerHTML = "";
  20. for (const recipe of recipes) {
  21. if (recipe.BuildingTicker !== "FRM")
  22. continue;
  23. const runsPerDay = 1000 * 60 * 60 * 24 / recipe.TimeMs;
  24. let costPerRun = 0;
  25. for (const input of recipe.Inputs) {
  26. const price = priceMap.get(input.Ticker);
  27. if (price === undefined)
  28. throw new Error(`missing price for ${input.Ticker}`);
  29. costPerRun += price * input.Amount;
  30. }
  31. const row = document.createElement("tr");
  32. row.innerHTML = `
  33. <td>${recipe.RecipeName}</td>
  34. <td>${(costPerRun * runsPerDay).toFixed(2)}</td>
  35. `;
  36. tbody.appendChild(row);
  37. }
  38. }
  39. fetchData().then(render);
  40. //# debugId=2420FEED6C51286B64756E2164756E21