roi.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // ts/popover.ts
  2. function setupPopover() {
  3. const main = document.querySelector("main");
  4. const popover = document.querySelector("#popover");
  5. main.addEventListener("mouseover", (event) => {
  6. const target = event.target;
  7. if (target.dataset.tooltip) {
  8. popover.textContent = target.dataset.tooltip;
  9. const rect = target.getBoundingClientRect();
  10. popover.style.left = `${rect.left}px`;
  11. popover.style.top = `${rect.bottom}px`;
  12. popover.showPopover();
  13. }
  14. });
  15. main.addEventListener("mouseout", (event) => {
  16. const target = event.target;
  17. if (target.dataset.tooltip)
  18. popover.hidePopover();
  19. });
  20. }
  21. // ts/roi.ts
  22. var roiCache = {};
  23. async function getROI(cx) {
  24. const response = await fetch(`/roi_${cx.toLowerCase()}.json`);
  25. const lastModified = new Date(response.headers.get("last-modified"));
  26. const profits = await response.json();
  27. return { lastModified, profits };
  28. }
  29. var lowVolume = document.querySelector("input#low-volume");
  30. var cxSelect = document.querySelector("select#cx");
  31. var expertise = {
  32. AGRICULTURE: "agri",
  33. CHEMISTRY: "chem",
  34. CONSTRUCTION: "const",
  35. ELECTRONICS: "elec",
  36. FOOD_INDUSTRIES: "food ind",
  37. FUEL_REFINING: "fuel",
  38. MANUFACTURING: "mfg",
  39. METALLURGY: "metal",
  40. RESOURCE_EXTRACTION: "res ext"
  41. };
  42. var expertiseSelect = document.querySelector("select#expertise");
  43. for (const key of Object.keys(expertise)) {
  44. const option = document.createElement("option");
  45. option.value = key;
  46. option.textContent = key.replace("_", " ").toLowerCase();
  47. expertiseSelect.appendChild(option);
  48. }
  49. var buildingSelect = document.querySelector("select#building");
  50. var formatDecimal = new Intl.NumberFormat(undefined, { maximumFractionDigits: 2, maximumSignificantDigits: 6, roundingPriority: "lessPrecision" }).format;
  51. var formatWhole = new Intl.NumberFormat(undefined, { maximumFractionDigits: 0 }).format;
  52. async function render() {
  53. const tbody = document.querySelector("tbody");
  54. tbody.innerHTML = "";
  55. const cx = cxSelect.value;
  56. if (!roiCache[cx])
  57. roiCache[cx] = getROI(cx);
  58. const { lastModified, profits } = await roiCache[cx];
  59. const buildingTickers = new Set(profits.map((p) => p.building));
  60. const buildings = Array.from(buildingTickers).map((building) => ({ ticker: building, expertise: profits.find((p) => p.building === building).expertise })).sort((a, b) => a.ticker.localeCompare(b.ticker));
  61. let selectedBuilding = buildingSelect.value;
  62. let buildingFound = false;
  63. buildingSelect.innerHTML = '<option value="">(all)</option>';
  64. for (const building of buildings)
  65. if (expertiseSelect.value === "" || expertiseSelect.value === building.expertise) {
  66. const option = document.createElement("option");
  67. option.value = building.ticker;
  68. option.textContent = building.ticker;
  69. if (building.ticker === selectedBuilding) {
  70. buildingFound = true;
  71. option.selected = true;
  72. }
  73. buildingSelect.appendChild(option);
  74. }
  75. if (!buildingFound)
  76. selectedBuilding = "";
  77. for (const p of profits) {
  78. const volumeRatio = p.output_per_day / p.average_traded_7d;
  79. if (!lowVolume.checked && volumeRatio > 0.05)
  80. continue;
  81. if (expertiseSelect.value !== "" && p.expertise !== expertiseSelect.value)
  82. continue;
  83. if (selectedBuilding !== "" && p.building !== selectedBuilding)
  84. continue;
  85. const tr = document.createElement("tr");
  86. const profitPerArea = p.profit_per_day / p.area;
  87. const breakEven = p.profit_per_day > 0 ? (p.capex + 3 * p.cost_per_day) / p.profit_per_day : Infinity;
  88. tr.innerHTML = `
  89. <td>${p.outputs.map((o) => o.ticker).join(", ")}</td>
  90. <td>${expertise[p.expertise]}</td>
  91. <td style="color: ${color(profitPerArea, 0, 300)}">${formatDecimal(profitPerArea)}</td>
  92. <td><span style="color: ${color(breakEven, 30, 3)}">${formatDecimal(breakEven)}</span>d</td>
  93. <td style="color: ${color(p.capex, 300000, 40000)}">${formatWhole(p.capex)}</td>
  94. <td style="color: ${color(p.cost_per_day, 40000, 1000)}">${formatWhole(p.cost_per_day)}</td>
  95. <td style="color: ${color(p.logistics_per_area, 2, 0.2)}">${formatDecimal(p.logistics_per_area)}</td>
  96. <td>
  97. ${formatDecimal(p.output_per_day)}<br>
  98. <span style="color: ${color(volumeRatio, 0.05, 0.002)}">${formatWhole(p.average_traded_7d)}</span>
  99. </td>
  100. `;
  101. const output = tr.querySelector("td");
  102. output.dataset.tooltip = p.recipe;
  103. const profitCell = tr.querySelectorAll("td")[2];
  104. const revenue = p.outputs.reduce((sum, o) => sum + o.amount * o.vwap_7d, 0);
  105. const inputCost = p.input_costs.reduce((sum, o) => sum + o.amount * o.vwap_7d, 0);
  106. profitCell.dataset.tooltip = formatMatPrices(p.outputs) + `
  107. ` + formatMatPrices(p.input_costs) + `
  108. ` + "worker consumables: " + formatWhole(p.worker_consumable_cost_per_day) + `
  109. ` + `(${formatWhole(revenue)} - ${formatWhole(inputCost)}) × ${formatDecimal(p.runs_per_day)} runs ` + `- ${formatWhole(p.worker_consumable_cost_per_day)} = ${formatDecimal(p.profit_per_day)}
  110. ` + `${formatDecimal(p.profit_per_day)} / ${formatWhole(p.area)} area = ${formatDecimal(profitPerArea)}`;
  111. tbody.appendChild(tr);
  112. }
  113. document.getElementById("last-updated").textContent = `last updated: ${lastModified.toLocaleString(undefined, { dateStyle: "full", timeStyle: "long", hour12: false })}`;
  114. }
  115. function color(n, low, high) {
  116. const scale = Math.min(Math.max((n - low) / (high - low), 0), 1);
  117. return `color-mix(in xyz, #0aa ${scale * 100}%, #f80)`;
  118. }
  119. function formatMatPrices(matPrices) {
  120. return matPrices.map(({ ticker, amount, vwap_7d }) => `${ticker}: ${amount} × ${formatDecimal(vwap_7d)} = ${formatWhole(amount * vwap_7d)}`).join(`
  121. `);
  122. }
  123. setupPopover();
  124. lowVolume.addEventListener("change", render);
  125. cxSelect.addEventListener("change", render);
  126. expertiseSelect.addEventListener("change", render);
  127. buildingSelect.addEventListener("change", render);
  128. render();
  129. //# debugId=55BEB0C86795158164756E2164756E21