roi.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 expertise = {
  11. AGRICULTURE: 'agri',
  12. CHEMISTRY: 'chem',
  13. CONSTRUCTION: 'const',
  14. ELECTRONICS: 'elec',
  15. FOOD_INDUSTRIES: 'food ind',
  16. FUEL_REFINING: 'fuel',
  17. MANUFACTURING: 'mfg',
  18. METALLURGY: 'metal',
  19. RESOURCE_EXTRACTION: 'res ext',
  20. } as const;
  21. const expertiseSelect = document.querySelector('select#expertise') as HTMLSelectElement;
  22. for (const key of Object.keys(expertise)) {
  23. const option = document.createElement('option');
  24. option.value = key;
  25. option.textContent = key.replace('_', ' ').toLowerCase();
  26. expertiseSelect.appendChild(option);
  27. }
  28. const cxSelect = document.querySelector('select#cx') as HTMLSelectElement;
  29. const formatDecimal = new Intl.NumberFormat(undefined,
  30. {maximumFractionDigits: 2, maximumSignificantDigits: 6, roundingPriority: 'lessPrecision'}).format;
  31. const formatWhole = new Intl.NumberFormat(undefined, {maximumFractionDigits: 0}).format;
  32. async function render() {
  33. const tbody = document.querySelector('tbody')!;
  34. tbody.innerHTML = '';
  35. const cx = cxSelect.value;
  36. if (!roiCache[cx])
  37. roiCache[cx] = getROI(cx);
  38. const {lastModified, profits} = await roiCache[cx];
  39. for (const p of profits) {
  40. const volumeRatio = p.output_per_day / p.average_traded_7d;
  41. if (!lowVolume.checked && volumeRatio > 0.05)
  42. continue;
  43. if (expertiseSelect.value !== '' && p.expertise !== expertiseSelect.value)
  44. continue;
  45. const tr = document.createElement('tr');
  46. const profitPerArea = p.profit_per_day / p.area;
  47. const breakEven = p.profit_per_day > 0 ? p.capex / p.profit_per_day : Infinity;
  48. tr.innerHTML = `
  49. <td>${p.outputs.map(o => o.ticker).join(', ')}</td>
  50. <td>${expertise[p.expertise]}</td>
  51. <td style="color: ${color(profitPerArea, 0, 250)}">${formatDecimal(profitPerArea)}</td>
  52. <td><span style="color: ${color(breakEven, 30, 3)}">${formatDecimal(breakEven)}</span>d</td>
  53. <td style="color: ${color(p.capex, 300_000, 40_000)}">${formatWhole(p.capex)}</td>
  54. <td style="color: ${color(p.cost_per_day, 40_000, 1_000)}">${formatWhole(p.cost_per_day)}</td>
  55. <td style="color: ${color(p.logistics_per_area, 2, 0.2)}">${formatDecimal(p.logistics_per_area)}</td>
  56. <td>
  57. ${formatDecimal(p.output_per_day)}<br>
  58. <span style="color: ${color(volumeRatio, 0.05, 0.002)}">${formatWhole(p.average_traded_7d)}</span>
  59. </td>
  60. `;
  61. const output = tr.querySelector('td')!;
  62. output.dataset.tooltip = p.recipe;
  63. const profitCell = tr.querySelectorAll('td')[2];
  64. const revenue = p.outputs.reduce((sum, o) => sum + o.amount * o.vwap_7d, 0);
  65. const inputCost = p.input_costs.reduce((sum, o) => sum + o.amount * o.vwap_7d, 0);
  66. profitCell.dataset.tooltip = formatMatPrices(p.outputs) + '\n\n' +
  67. formatMatPrices(p.input_costs) + '\n' +
  68. 'worker consumables: ' + formatWhole(p.worker_consumable_cost_per_day) + '\n\n' +
  69. `(${formatWhole(revenue)} - ${formatWhole(inputCost)}) × ${formatDecimal(p.runs_per_day)} runs ` +
  70. `- ${formatWhole(p.worker_consumable_cost_per_day)} = ${formatDecimal(p.profit_per_day)}\n` +
  71. `${formatDecimal(p.profit_per_day)} / ${formatWhole(p.area)} area = ${formatDecimal(profitPerArea)}`;
  72. tbody.appendChild(tr);
  73. }
  74. document.getElementById('last-updated')!.textContent =
  75. `last updated: ${lastModified.toLocaleString(undefined, {dateStyle: 'full', timeStyle: 'long', hour12: false})}`;
  76. }
  77. function color(n: number, low: number, high: number): string {
  78. // scale n from low..high to 0..1 clamped
  79. const scale = Math.min(Math.max((n - low) / (high - low), 0), 1);
  80. return `color-mix(in xyz, #0aa ${scale * 100}%, #f80)`;
  81. }
  82. function formatMatPrices(matPrices: MatPrice[]): string {
  83. return matPrices.map(({ticker, amount, vwap_7d}) =>
  84. `${ticker}: ${amount} × ${formatDecimal(vwap_7d)} = ${formatWhole(amount * vwap_7d)}`).join('\n');
  85. }
  86. setupPopover();
  87. lowVolume.addEventListener('change', render);
  88. expertiseSelect.addEventListener('change', render);
  89. cxSelect.addEventListener('change', render);
  90. render();
  91. interface Profit {
  92. outputs: MatPrice[]
  93. recipe: string
  94. expertise: keyof typeof expertise
  95. profit_per_day: number
  96. area: number
  97. capex: number
  98. cost_per_day: number
  99. input_costs: MatPrice[]
  100. worker_consumable_cost_per_day: number
  101. runs_per_day: number
  102. logistics_per_area: number
  103. output_per_day: number
  104. average_traded_7d: number
  105. }
  106. interface MatPrice {
  107. ticker: string
  108. amount: number
  109. vwap_7d: number
  110. }