roi.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. async function render() {
  34. const tbody = document.querySelector('tbody')!;
  35. tbody.innerHTML = '';
  36. const cx = cxSelect.value;
  37. if (!roiCache[cx])
  38. roiCache[cx] = getROI(cx);
  39. const {lastModified, profits} = await roiCache[cx];
  40. const buildingTickers = new Set(profits.map(p => p.building));
  41. const buildings: {ticker: string, expertise: keyof typeof expertise}[] = Array.from(buildingTickers)
  42. .map((building) => ({ticker: building, expertise: profits.find(p => p.building === building)!.expertise}))
  43. .sort((a, b) => a.ticker.localeCompare(b.ticker));
  44. let selectedBuilding = buildingSelect.value;
  45. let buildingFound = false;
  46. buildingSelect.innerHTML = '<option value="">(all)</option>';
  47. for (const building of buildings)
  48. if (expertiseSelect.value === '' || expertiseSelect.value === building.expertise) {
  49. const option = document.createElement('option');
  50. option.value = building.ticker;
  51. option.textContent = building.ticker;
  52. if (building.ticker === selectedBuilding) {
  53. buildingFound = true;
  54. option.selected = true;
  55. }
  56. buildingSelect.appendChild(option);
  57. }
  58. if (!buildingFound)
  59. selectedBuilding = '';
  60. for (const p of profits) {
  61. const volumeRatio = p.output_per_day / p.average_traded_7d;
  62. if (!lowVolume.checked && volumeRatio > 0.05)
  63. continue;
  64. if (expertiseSelect.value !== '' && p.expertise !== expertiseSelect.value)
  65. continue;
  66. if (selectedBuilding !== '' && p.building !== selectedBuilding)
  67. continue;
  68. const tr = document.createElement('tr');
  69. const profitPerArea = p.profit_per_day / p.area;
  70. const breakEven = p.profit_per_day > 0 ? p.capex / p.profit_per_day : Infinity;
  71. tr.innerHTML = `
  72. <td>${p.outputs.map(o => o.ticker).join(', ')}</td>
  73. <td>${expertise[p.expertise]}</td>
  74. <td style="color: ${color(profitPerArea, 0, 250)}">${formatDecimal(profitPerArea)}</td>
  75. <td><span style="color: ${color(breakEven, 30, 3)}">${formatDecimal(breakEven)}</span>d</td>
  76. <td style="color: ${color(p.capex, 300_000, 40_000)}">${formatWhole(p.capex)}</td>
  77. <td style="color: ${color(p.cost_per_day, 40_000, 1_000)}">${formatWhole(p.cost_per_day)}</td>
  78. <td style="color: ${color(p.logistics_per_area, 2, 0.2)}">${formatDecimal(p.logistics_per_area)}</td>
  79. <td>
  80. ${formatDecimal(p.output_per_day)}<br>
  81. <span style="color: ${color(volumeRatio, 0.05, 0.002)}">${formatWhole(p.average_traded_7d)}</span>
  82. </td>
  83. `;
  84. const output = tr.querySelector('td')!;
  85. output.dataset.tooltip = p.recipe;
  86. const profitCell = tr.querySelectorAll('td')[2];
  87. const revenue = p.outputs.reduce((sum, o) => sum + o.amount * o.vwap_7d, 0);
  88. const inputCost = p.input_costs.reduce((sum, o) => sum + o.amount * o.vwap_7d, 0);
  89. profitCell.dataset.tooltip = formatMatPrices(p.outputs) + '\n\n' +
  90. formatMatPrices(p.input_costs) + '\n' +
  91. 'worker consumables: ' + formatWhole(p.worker_consumable_cost_per_day) + '\n\n' +
  92. `(${formatWhole(revenue)} - ${formatWhole(inputCost)}) × ${formatDecimal(p.runs_per_day)} runs ` +
  93. `- ${formatWhole(p.worker_consumable_cost_per_day)} = ${formatDecimal(p.profit_per_day)}\n` +
  94. `${formatDecimal(p.profit_per_day)} / ${formatWhole(p.area)} area = ${formatDecimal(profitPerArea)}`;
  95. tbody.appendChild(tr);
  96. }
  97. document.getElementById('last-updated')!.textContent =
  98. `last updated: ${lastModified.toLocaleString(undefined, {dateStyle: 'full', timeStyle: 'long', hour12: false})}`;
  99. }
  100. function color(n: number, low: number, high: number): string {
  101. // scale n from low..high to 0..1 clamped
  102. const scale = Math.min(Math.max((n - low) / (high - low), 0), 1);
  103. return `color-mix(in xyz, #0aa ${scale * 100}%, #f80)`;
  104. }
  105. function formatMatPrices(matPrices: MatPrice[]): string {
  106. return matPrices.map(({ticker, amount, vwap_7d}) =>
  107. `${ticker}: ${amount} × ${formatDecimal(vwap_7d)} = ${formatWhole(amount * vwap_7d)}`).join('\n');
  108. }
  109. setupPopover();
  110. lowVolume.addEventListener('change', render);
  111. cxSelect.addEventListener('change', render);
  112. expertiseSelect.addEventListener('change', render);
  113. buildingSelect.addEventListener('change', render);
  114. render();
  115. interface Profit {
  116. outputs: MatPrice[]
  117. recipe: string
  118. expertise: keyof typeof expertise
  119. building: string
  120. profit_per_day: number
  121. area: number
  122. capex: number
  123. cost_per_day: number
  124. input_costs: MatPrice[]
  125. worker_consumable_cost_per_day: number
  126. runs_per_day: number
  127. logistics_per_area: number
  128. output_per_day: number
  129. average_traded_7d: number
  130. }
  131. interface MatPrice {
  132. ticker: string
  133. amount: number
  134. vwap_7d: number
  135. }
  136. interface Building {
  137. building_type: 'INFRASTRUCTURE' | 'PLANETARY' | 'PRODUCTION';
  138. building_ticker: string;
  139. expertise: keyof typeof expertise;
  140. }