roi.py 5.6 KB

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