roi.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. from __future__ import annotations
  2. import dataclasses
  3. import json
  4. import typing
  5. import cache
  6. def main() -> None:
  7. recipes: list[Recipe] = cache.get('https://api.prunplanner.org/data/recipes')
  8. buildings: dict[str, Building] = {m['Ticker']: m for m in cache.get('https://api.prunplanner.org/data/buildings')}
  9. materials: dict[str, Material] = {m['Ticker']: m for m in cache.get('https://api.prunplanner.org/data/materials')}
  10. raw_prices: list[RawPrice] = cache.get('https://refined-prun.github.io/refined-prices/all.json')
  11. prices: dict[str, Price] = {
  12. p['MaterialTicker']: Price(p['VWAP7D'], p['AverageTraded7D']) for p in raw_prices # pyright: ignore[reportArgumentType]
  13. if p['ExchangeCode'] == 'IC1' and p['VWAP7D'] is not None
  14. }
  15. profits: list[Profit] = []
  16. for recipe in recipes:
  17. if profit := calc_profit(recipe, buildings, materials, prices):
  18. profits.append(profit)
  19. profits.sort()
  20. with open('www/roi.json', 'w') as f:
  21. json.dump([dataclasses.asdict(p) for p in profits], f, indent='\t')
  22. def calc_profit(recipe: Recipe, buildings: typing.Mapping[str, Building], materials: typing.Mapping[str, Material],
  23. prices: typing.Mapping[str, Price]) -> Profit | None:
  24. try:
  25. (output,) = recipe['Outputs']
  26. except ValueError: # skip recipes that don't have exactly 1 output
  27. return
  28. try:
  29. output_price = prices[output['Ticker']]
  30. cost = sum(prices[input['Ticker']].vwap * input['Amount'] for input in recipe['Inputs'])
  31. except KeyError: # skip recipes with thinly traded materials
  32. return
  33. revenue = output_price.vwap * output['Amount']
  34. building = buildings[recipe['BuildingTicker']]
  35. capex = sum(bm['Amount'] * prices[bm['CommodityTicker']].vwap
  36. for bm in building['BuildingCosts'])
  37. profit_per_run = revenue - cost
  38. runs_per_day = 24 * 60 * 60 * 1000 / recipe['TimeMs']
  39. if building['Ticker'] in ('FRM', 'ORC'):
  40. runs_per_day *= 1.1212 # promitor's fertility
  41. worker_consumable_daily_cost = building_daily_cost(building, prices)
  42. cost_per_day = cost * runs_per_day + worker_consumable_daily_cost
  43. output_per_day = output['Amount'] * runs_per_day
  44. logistics_per_area = max(
  45. sum(materials[input['Ticker']]['Weight'] * input['Amount'] for input in recipe['Inputs']),
  46. sum(materials[input['Ticker']]['Volume'] * input['Amount'] for input in recipe['Inputs']),
  47. materials[output['Ticker']]['Weight'] * output['Amount'],
  48. materials[output['Ticker']]['Volume'] * output['Amount'],
  49. ) * runs_per_day / building['AreaCost']
  50. return Profit(output['Ticker'], recipe['RecipeName'],
  51. expertise=building['Expertise'].replace('_', ' ').lower(),
  52. profit_per_day=(profit_per_run * runs_per_day - worker_consumable_daily_cost),
  53. area=building['AreaCost'],
  54. capex=capex,
  55. cost_per_day=cost_per_day,
  56. logistics_per_area=logistics_per_area,
  57. output_per_day=output_per_day,
  58. average_traded_7d=output_price.average_traded_7d)
  59. def building_daily_cost(building: Building, prices: typing.Mapping[str, Price]) -> float:
  60. consumption = {
  61. 'Pioneers': [('COF', 0.5), ('DW', 4), ('RAT', 4), ('OVE', 0.5), ('PWO', 0.2)],
  62. 'Settlers': [('DW', 5), ('RAT', 6), ('KOM', 1), ('EXO', 0.5), ('REP', 0.2), ('PT', 0.5)],
  63. 'Technicians': [('DW', 7.5), ('RAT', 7), ('ALE', 1), ('MED', 0.5), ('SC', 0.1), ('HMS', 0.5), ('SCN', 0.1)],
  64. 'Engineers': [('DW', 10), ('MED', 0.5), ('GIN', 1), ('FIM', 7), ('VG', 0.2), ('HSS', 0.2), ('PDA', 0.1)],
  65. 'Scientists': [('DW', 10), ('MED', 0.5), ('WIN', 1), ('MEA', 7), ('NST', 0.1), ('LC', 0.2), ('WS', 0.1)],
  66. }
  67. cost = 0
  68. for worker, mats in consumption.items():
  69. workers = building[worker]
  70. for mat, per_100 in mats:
  71. cost += prices[mat].vwap * workers * per_100 / 100
  72. return cost
  73. class Recipe(typing.TypedDict):
  74. RecipeName: str
  75. BuildingTicker: str
  76. Inputs: list[RecipeMat]
  77. Outputs: list[RecipeMat]
  78. TimeMs: int
  79. class RecipeMat(typing.TypedDict):
  80. Ticker: str
  81. Amount: int
  82. class Building(typing.TypedDict):
  83. Ticker: str
  84. Expertise: str
  85. AreaCost: int
  86. BuildingCosts: list[BuildingMat]
  87. Pioneers: int
  88. Settlers: int
  89. Technicians: int
  90. Engineers: int
  91. Scientists: int
  92. class BuildingMat(typing.TypedDict):
  93. CommodityTicker: str
  94. Amount: int
  95. class Material(typing.TypedDict):
  96. Ticker: str
  97. Weight: float
  98. Volume: float
  99. class RawPrice(typing.TypedDict):
  100. MaterialTicker: str
  101. ExchangeCode: str
  102. PriceAverage: int
  103. VWAP7D: float | None # volume-weighted average price over last 7 days
  104. AverageTraded7D: float | None # averaged daily traded volume over last 7 days
  105. @dataclasses.dataclass(eq=False, frozen=True, slots=True)
  106. class Price:
  107. vwap: float
  108. average_traded_7d: float
  109. @dataclasses.dataclass(eq=False, frozen=True, slots=True)
  110. class Profit:
  111. output: str
  112. recipe: str
  113. expertise: str
  114. profit_per_day: float
  115. area: float
  116. capex: float
  117. cost_per_day: float
  118. logistics_per_area: float
  119. output_per_day: float
  120. average_traded_7d: float
  121. def __lt__(self, other: Profit) -> bool:
  122. if (break_even := self.capex / self.profit_per_day) < 0:
  123. break_even = 10000 - self.profit_per_day
  124. if (other_break_even := other.capex / other.profit_per_day) < 0:
  125. other_break_even = 10000 - other.profit_per_day
  126. return break_even < other_break_even
  127. if __name__ == '__main__':
  128. main()