roi.ts 4.4 KB

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