roi.py 5.1 KB

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