roi.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import {setupPopover} from './popover';
  2. const roiCache: Record<string, Promise<{lastModified: Date, profits: Profit[]}>> = {};
  3. async function getROI(cx: string) {
  4. const response = await fetch(`/roi_${cx.toLowerCase()}.json`);
  5. const lastModified = new Date(response.headers.get('last-modified')!);
  6. const profits = await response.json();
  7. return {lastModified, profits};
  8. }
  9. const lowVolume = document.querySelector('input#low-volume') as HTMLInputElement;
  10. const cxSelect = document.querySelector('select#cx') as HTMLSelectElement;
  11. const expertise = {
  12. AGRICULTURE: 'agri',
  13. CHEMISTRY: 'chem',
  14. CONSTRUCTION: 'const',
  15. ELECTRONICS: 'elec',
  16. FOOD_INDUSTRIES: 'food ind',
  17. FUEL_REFINING: 'fuel',
  18. MANUFACTURING: 'mfg',
  19. METALLURGY: 'metal',
  20. RESOURCE_EXTRACTION: 'res ext',
  21. } as const;
  22. const expertiseSelect = document.querySelector('select#expertise') as HTMLSelectElement;
  23. for (const key of Object.keys(expertise)) {
  24. const option = document.createElement('option');
  25. option.value = key;
  26. option.textContent = key.replace('_', ' ').toLowerCase();
  27. expertiseSelect.appendChild(option);
  28. }
  29. const buildingSelect = document.querySelector('select#building') as HTMLSelectElement;
  30. const formatDecimal = new Intl.NumberFormat(undefined,
  31. {maximumFractionDigits: 2, maximumSignificantDigits: 6, roundingPriority: 'lessPrecision'}).format;
  32. const formatWhole = new Intl.NumberFormat(undefined, {maximumFractionDigits: 0}).format;
  33. let currentSortKey: keyof Profit | 'outputs' = 'break_even';
  34. let currentSortAsc: boolean = true;
  35. let headersInitialized = false;
  36. async function render() {
  37. const tbody = document.querySelector('tbody')!;
  38. tbody.innerHTML = '';
  39. const cx = cxSelect.value;
  40. if (!roiCache[cx])
  41. roiCache[cx] = getROI(cx);
  42. const {lastModified, profits} = await roiCache[cx];
  43. if (!headersInitialized) {
  44. const ths = document.querySelectorAll('th');
  45. // EXTREME DETAIL: We swapped 'output_per_day' for 'market_capacity_area'.
  46. // Because this array index directly maps to the DOM headers, clicking the 8th
  47. // table header now natively routes to the new metric for dynamic sorting.
  48. const keys: (keyof Profit | 'outputs')[] = [
  49. 'outputs', 'expertise', 'profit_per_area', 'break_even',
  50. 'capex', 'cost_per_day', 'logistics_per_area', 'market_capacity_area'
  51. ];
  52. ths.forEach((th, i) => {
  53. if (keys[i]) {
  54. th.style.cursor = 'pointer';
  55. th.title = 'Click to sort';
  56. th.addEventListener('click', () => {
  57. if (currentSortKey === keys[i]) {
  58. currentSortAsc = !currentSortAsc;
  59. } else {
  60. currentSortKey = keys[i];
  61. currentSortAsc = keys[i] === 'break_even' ? true : false;
  62. }
  63. render();
  64. });
  65. }
  66. });
  67. headersInitialized = true;
  68. }
  69. const buildingTickers = new Set(profits.map(p => p.building));
  70. const buildings: {ticker: string, expertise: keyof typeof expertise}[] = Array.from(buildingTickers)
  71. .map((building) => ({ticker: building, expertise: profits.find(p => p.building === building)!.expertise}))
  72. .sort((a, b) => a.ticker.localeCompare(b.ticker));
  73. let selectedBuilding = buildingSelect.value;
  74. let buildingFound = false;
  75. buildingSelect.innerHTML = '<option value="">(all)</option>';
  76. for (const building of buildings)
  77. if (expertiseSelect.value === '' || expertiseSelect.value === building.expertise) {
  78. const option = document.createElement('option');
  79. option.value = building.ticker;
  80. option.textContent = building.ticker;
  81. if (building.ticker === selectedBuilding) {
  82. buildingFound = true;
  83. option.selected = true;
  84. }
  85. buildingSelect.appendChild(option);
  86. }
  87. if (!buildingFound)
  88. selectedBuilding = '';
  89. // EXTREME DETAIL: Even though we removed 'output_per_day' and 'average_traded_7d' from
  90. // the visual table, they are still present in the JSON backend structure.
  91. // This means our 'lowVolume' filter still works perfectly without requiring any math changes!
  92. const filteredProfits = profits.filter(p => {
  93. const volumeRatio = p.output_per_day / p.average_traded_7d;
  94. if (!lowVolume.checked && volumeRatio > 0.05) return false;
  95. if (expertiseSelect.value !== '' && p.expertise !== expertiseSelect.value) return false;
  96. if (selectedBuilding !== '' && p.building !== selectedBuilding) return false;
  97. return true;
  98. });
  99. filteredProfits.sort((a, b) => {
  100. let valA: any = a[currentSortKey as keyof Profit];
  101. let valB: any = b[currentSortKey as keyof Profit];
  102. if (currentSortKey === 'outputs') {
  103. valA = a.outputs.map(o => o.ticker).join(', ');
  104. valB = b.outputs.map(o => o.ticker).join(', ');
  105. }
  106. if (valA < valB) return currentSortAsc ? -1 : 1;
  107. if (valA > valB) return currentSortAsc ? 1 : -1;
  108. return 0;
  109. });
  110. for (const p of filteredProfits) {
  111. const tr = document.createElement('tr');
  112. // EXTREME DETAIL: We swapped the dual-line output code for a single <td> rendering the new
  113. // market_capacity_area metric. The color mapping scale ranges from 20 (Red) to 500 (Cyan).
  114. // For reference: a capacity of 20 means building 1 area captures exactly 5% of the market volume.
  115. tr.innerHTML = `
  116. <td>${p.outputs.map(o => o.ticker).join(', ')}</td>
  117. <td>${expertise[p.expertise]}</td>
  118. <td style="color: ${color(p.profit_per_area, 0, 300)}">${formatDecimal(p.profit_per_area)}</td>
  119. <td><span style="color: ${color(p.break_even, 30, 3)}">${formatDecimal(p.break_even)}</span>d</td>
  120. <td style="color: ${color(p.capex, 300_000, 40_000)}">${formatWhole(p.capex)}</td>
  121. <td style="color: ${color(p.cost_per_day, 40_000, 1_000)}">${formatWhole(p.cost_per_day)}</td>
  122. <td style="color: ${color(p.logistics_per_area, 2, 0.2)}">${formatDecimal(p.logistics_per_area)}</td>
  123. <td style="color: ${color(p.market_capacity_area, 20, 500)}">${formatWhole(p.market_capacity_area)}</td>
  124. `;
  125. const output = tr.querySelector('td')!;
  126. output.dataset.tooltip = p.recipe;
  127. const profitCell = tr.querySelectorAll('td')[2];
  128. const revenue = p.outputs.reduce((sum, o) => sum + o.amount * o.vwap_7d, 0);
  129. const inputCost = p.input_costs.reduce((sum, o) => sum + o.amount * o.vwap_7d, 0);
  130. profitCell.dataset.tooltip = formatMatPrices(p.outputs) + '\n\n' +
  131. formatMatPrices(p.input_costs) + '\n' +
  132. 'worker consumables: ' + formatWhole(p.worker_consumable_cost_per_day) + '\n\n' +
  133. `(${formatWhole(revenue)} - ${formatWhole(inputCost)}) × ${formatDecimal(p.runs_per_day)} runs ` +
  134. `- ${formatWhole(p.worker_consumable_cost_per_day)} = ${formatDecimal(p.profit_per_day)}\n` +
  135. `${formatDecimal(p.profit_per_day)} / ${formatWhole(p.area)} area = ${formatDecimal(p.profit_per_area)}`;
  136. // EXTREME DETAIL: Because we are condensing two variables into a single number, providing a tooltip
  137. // explaining exactly how the math breaks down is critical for the end-user experience.
  138. const marketCell = tr.querySelectorAll('td')[7];
  139. marketCell.dataset.tooltip = `Market Capacity: ${formatWhole(p.average_traded_7d)} traded/day ÷ ${formatDecimal(p.output_per_day / p.area)} produced/day/area = ${formatWhole(p.market_capacity_area)} equivalent areas`;
  140. tbody.appendChild(tr);
  141. }
  142. document.getElementById('last-updated')!.textContent =
  143. `last updated: ${lastModified.toLocaleString(undefined, {dateStyle: 'full', timeStyle: 'long', hour12: false})}`;
  144. }
  145. function color(n: number, low: number, high: number): string {
  146. const scale = Math.min(Math.max((n - low) / (high - low), 0), 1);
  147. return `color-mix(in xyz, #0aa ${scale * 100}%, #f80)`;
  148. }
  149. function formatMatPrices(matPrices: MatPrice[]): string {
  150. return matPrices.map(({ticker, amount, vwap_7d}) =>
  151. `${ticker}: ${amount} × ${formatDecimal(vwap_7d)} = ${formatWhole(amount * vwap_7d)}`).join('\n');
  152. }
  153. setupPopover();
  154. lowVolume.addEventListener('change', render);
  155. cxSelect.addEventListener('change', render);
  156. expertiseSelect.addEventListener('change', render);
  157. buildingSelect.addEventListener('change', render);
  158. render();
  159. interface Profit {
  160. outputs: MatPrice[]
  161. recipe: string
  162. expertise: keyof typeof expertise
  163. building: string
  164. profit_per_day: number
  165. area: number
  166. capex: number
  167. cost_per_day: number
  168. input_costs: MatPrice[]
  169. worker_consumable_cost_per_day: number
  170. runs_per_day: number
  171. logistics_per_area: number
  172. output_per_day: number
  173. average_traded_7d: number
  174. profit_per_area: number
  175. break_even: number
  176. market_capacity_area: number // Added pre-calculated property tracking
  177. }
  178. interface MatPrice {
  179. ticker: string
  180. amount: number
  181. vwap_7d: number
  182. }
  183. interface Building {
  184. building_type: 'INFRASTRUCTURE' | 'PLANETARY' | 'PRODUCTION';
  185. building_ticker: string;
  186. expertise: keyof typeof expertise;
  187. }