roi.py 5.3 KB

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