roi.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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['building_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. hq_levels_raw = cache.get('https://raw.githubusercontent.com/PRUNplanner/frontend/ec2ab897624121186f7de8e6c2e28ebf292f4432/src/features/hq_upgrade_calculator/hq_levels.json')
  12. for cx in ['AI1', 'CI1', 'IC1', 'NC1']:
  13. profits = calc_for_cx(cx, recipes, buildings, materials, raw_prices, hq_levels_raw)
  14. with open(f'www/roi_{cx.lower()}.json', 'w') as f:
  15. json.dump([dataclasses.asdict(p) for p in profits], f, indent='\t')
  16. def calc_for_cx(cx: str, recipes: typing.Collection[Recipe], buildings: typing.Mapping[str, Building],
  17. materials: typing.Mapping[str, Material], raw_prices: typing.Collection[RawPrice],
  18. hq_levels_raw: dict) -> typing.Sequence[Profit]:
  19. prices: dict[str, Price] = {
  20. p['MaterialTicker']: Price(p['VWAP7D'], p['AverageTraded7D'], p['VWAP30D'], p['Bid'], p['Ask']) for p in raw_prices # pyright: ignore[reportArgumentType]
  21. if p['ExchangeCode'] == cx
  22. }
  23. hq_costs: dict[str, dict[str, float]] = {}
  24. for level_str, mats in hq_levels_raw.items():
  25. cost = {'vwap': 0.0, 'bid': 0.0, 'ask': 0.0}
  26. for mat in mats:
  27. if mat['ticker'] in prices:
  28. m_metrics = get_metrics(mat['amount'], prices[mat['ticker']])
  29. for k in cost: cost[k] += m_metrics[k]
  30. hq_costs[level_str] = cost
  31. habitation: typing.Mapping[Worker, str] = {
  32. 'pioneers': 'HB1',
  33. 'settlers': 'HB2',
  34. 'technicians': 'HB3',
  35. 'engineers': 'HB4',
  36. 'scientists': 'HB5',
  37. }
  38. hab_area_cost: dict[Worker, float] = {}
  39. hab_capex: dict[Worker, dict[str, float]] = {}
  40. for worker, hab in habitation.items():
  41. hab_area_cost[worker] = buildings[hab]['area_cost'] / 100
  42. base_capex = building_construction_cost(buildings[hab], prices)
  43. hab_capex[worker] = {k: v / 100 for k, v in base_capex.items()}
  44. profits: list[Profit] = []
  45. for recipe in recipes:
  46. if profit := calc_profit(recipe, buildings, hab_area_cost, hab_capex, materials, prices, hq_costs):
  47. profits.append(profit)
  48. profits.sort()
  49. return profits
  50. def get_metrics(amount: float, price: Price) -> dict[str, float]:
  51. v = price.vwap_7d or price.vwap_30d or 0.0
  52. b = price.bid if price.bid is not None else v
  53. a = price.ask if price.ask is not None else v
  54. return {'vwap': amount * v, 'bid': amount * b, 'ask': amount * a}
  55. def calc_profit(recipe: Recipe, buildings: typing.Mapping[str, Building], hab_area_cost: typing.Mapping[Worker, float],
  56. hab_capex: typing.Mapping[Worker, dict[str, float]], materials: typing.Mapping[str, Material],
  57. prices: typing.Mapping[str, Price], hq_costs: dict[str, dict[str, float]]) -> Profit | None:
  58. if len(recipe['outputs']) == 0:
  59. return
  60. building = buildings[recipe['building_ticker']]
  61. area = building['area_cost'] + sum(hab_area_cost[worker] * building[worker] for worker in hab_area_cost)
  62. runs_per_day = 24 * 60 * 60 * 1000 / recipe['time_ms'] * 1.25 # assume CoGC
  63. if building['building_ticker'] in ('FRM', 'ORC'):
  64. runs_per_day *= 1.1212 # promitor's fertility
  65. outputs: list[MatPrice] = []
  66. revenue = {'vwap': 0.0, 'bid': 0.0, 'ask': 0.0}
  67. output_prices: dict[str, PriceNonNull] = {}
  68. for output in recipe['outputs']:
  69. price = prices[output['material_ticker']]
  70. if price.vwap_7d is None or price.average_traded_7d is None:
  71. return # skip recipes with thinly traded outputs
  72. output_prices[output['material_ticker']] = typing.cast(PriceNonNull, price)
  73. m = get_metrics(output['material_amount'] * runs_per_day, price)
  74. for k in revenue: revenue[k] += m[k]
  75. outputs.append(MatPrice(output['material_ticker'], output['material_amount'], price.vwap_7d, price.bid, price.ask))
  76. input_costs: list[MatPrice] = []
  77. opex = {'vwap': 0.0, 'bid': 0.0, 'ask': 0.0}
  78. for input in recipe['inputs']:
  79. price = prices[input['material_ticker']]
  80. if price.vwap_7d is None:
  81. return # skip recipes with thinly traded inputs
  82. m = get_metrics(input['material_amount'] * runs_per_day, price)
  83. for k in opex: opex[k] += m[k]
  84. input_costs.append(MatPrice(input['material_ticker'], input['material_amount'], price.vwap_7d, price.bid, price.ask))
  85. worker_consumable = building_daily_cost(building, prices)
  86. for k in opex: opex[k] += worker_consumable[k]
  87. capex = building_construction_cost(building, prices)
  88. for worker, hab_cost in hab_capex.items():
  89. workers = building[worker]
  90. if workers > 0:
  91. for k in capex: capex[k] += hab_cost[k] * workers
  92. lowest_liquidity = min(recipe['outputs'],
  93. key=lambda output: output['material_amount'] / output_prices[output['material_ticker']].average_traded_7d)
  94. output_per_day = lowest_liquidity['material_amount'] * runs_per_day
  95. average_traded_7d = output_prices[lowest_liquidity['material_ticker']].average_traded_7d
  96. output_per_base = output_per_day / (area / 500)
  97. market_capacity_base = average_traded_7d / output_per_base
  98. in_w = sum(materials[input['material_ticker']]['weight'] * input['material_amount'] for input in recipe['inputs'])
  99. in_v = sum(materials[input['material_ticker']]['volume'] * input['material_amount'] for input in recipe['inputs'])
  100. out_w = sum(materials[output['material_ticker']]['weight'] * output['material_amount'] for output in recipe['outputs'])
  101. out_v = sum(materials[output['material_ticker']]['volume'] * output['material_amount'] for output in recipe['outputs'])
  102. runs_per_base = runs_per_day / (area / 500)
  103. # EXTREME DETAIL: We export the normalized daily ship fraction, but we have deleted the static
  104. # ship_capex calculation since the frontend UI now completely controls the Round Trip Time parameter.
  105. normalized_logistics_per_base = max(in_w / 3000, in_v / 1000, out_w / 3000, out_v / 1000) * runs_per_base
  106. bottlenecks = [
  107. (in_w, 't (I)'),
  108. (in_v, 'm³ (I)'),
  109. (out_w, 't (O)'),
  110. (out_v, 'm³ (O)')
  111. ]
  112. max_logistics, logistics_bottleneck = max(bottlenecks, key=lambda x: x[0])
  113. logistics_per_base = max_logistics * runs_per_base
  114. return Profit(outputs, recipe['recipe_name'],
  115. expertise=building['expertise'],
  116. building=building['building_ticker'],
  117. area=area,
  118. capex=capex,
  119. opex=opex,
  120. revenue=revenue,
  121. input_costs=input_costs,
  122. runs_per_day=runs_per_day,
  123. logistics_per_base=logistics_per_base,
  124. normalized_logistics_per_base=normalized_logistics_per_base,
  125. logistics_bottleneck=logistics_bottleneck,
  126. output_per_day=output_per_day,
  127. average_traded_7d=average_traded_7d,
  128. market_capacity_base=market_capacity_base,
  129. hq_costs=hq_costs)
  130. def building_construction_cost(building: Building, prices: typing.Mapping[str, Price]) -> dict[str, float]:
  131. cost = {'vwap': 0.0, 'bid': 0.0, 'ask': 0.0}
  132. for bc in building['costs']:
  133. m = get_metrics(bc['material_amount'], prices[bc['material_ticker']])
  134. for k in cost: cost[k] += m[k]
  135. # https://handbook.apex.prosperousuniverse.com/wiki/building-costs/#rocky-planets
  136. mcg = get_metrics(building['area_cost'] * 4, prices['MCG'])
  137. for k in cost: cost[k] += mcg[k]
  138. return cost
  139. def building_daily_cost(building: Building, prices: typing.Mapping[str, Price]) -> dict[str, float]:
  140. consumption = {
  141. 'pioneers': [('COF', 0.5), ('DW', 4), ('RAT', 4), ('OVE', 0.5), ('PWO', 0.2)],
  142. 'settlers': [('DW', 5), ('RAT', 6), ('KOM', 1), ('EXO', 0.5), ('REP', 0.2), ('PT', 0.5)],
  143. 'technicians': [('DW', 7.5), ('RAT', 7), ('ALE', 1), ('MED', 0.5), ('SC', 0.1), ('HMS', 0.5), ('SCN', 0.1)],
  144. 'engineers': [('DW', 10), ('MED', 0.5), ('GIN', 1), ('FIM', 7), ('VG', 0.2), ('HSS', 0.2), ('PDA', 0.1)],
  145. 'scientists': [('DW', 10), ('MED', 0.5), ('WIN', 1), ('MEA', 7), ('NST', 0.1), ('LC', 0.2), ('WS', 0.05)],
  146. }
  147. cost = {'vwap': 0.0, 'bid': 0.0, 'ask': 0.0}
  148. for worker, mats in consumption.items():
  149. workers = building[worker]
  150. for mat, per_100 in mats:
  151. m = get_metrics(workers * per_100 / 100, prices[mat])
  152. for k in cost: cost[k] += m[k]
  153. return cost
  154. Worker = typing.Literal['pioneers', 'settlers', 'technicians', 'engineers', 'scientists']
  155. class Recipe(typing.TypedDict):
  156. recipe_name: str
  157. building_ticker: str
  158. inputs: list[RecipeMat]
  159. outputs: list[RecipeMat]
  160. time_ms: int
  161. class RecipeMat(typing.TypedDict):
  162. material_ticker: str
  163. material_amount: int
  164. class Building(typing.TypedDict):
  165. building_ticker: str
  166. expertise: str
  167. area_cost: int
  168. costs: list[BuildingMat]
  169. pioneers: int
  170. settlers: int
  171. technicians: int
  172. engineers: int
  173. scientists: int
  174. class BuildingMat(typing.TypedDict):
  175. material_ticker: str
  176. material_amount: int
  177. class Material(typing.TypedDict):
  178. ticker: str
  179. weight: float
  180. volume: float
  181. class RawPrice(typing.TypedDict):
  182. MaterialTicker: str
  183. ExchangeCode: str
  184. VWAP7D: float | None
  185. AverageTraded7D: float | None
  186. VWAP30D: float | None
  187. Bid: float | None
  188. Ask: float | None
  189. @dataclasses.dataclass(eq=False, frozen=True, slots=True)
  190. class Price:
  191. vwap_7d: float | None
  192. average_traded_7d: float | None
  193. vwap_30d: float | None
  194. bid: float | None
  195. ask: float | None
  196. @dataclasses.dataclass(eq=False, frozen=True, slots=True)
  197. class PriceNonNull:
  198. vwap_7d: float
  199. average_traded_7d: float
  200. @dataclasses.dataclass(eq=False, frozen=True, slots=True)
  201. class Profit:
  202. outputs: typing.Collection[MatPrice]
  203. recipe: str
  204. expertise: str
  205. building: str
  206. area: float
  207. capex: dict[str, float]
  208. opex: dict[str, float]
  209. revenue: dict[str, float]
  210. input_costs: typing.Collection[MatPrice]
  211. runs_per_day: float
  212. logistics_per_base: float
  213. normalized_logistics_per_base: float
  214. logistics_bottleneck: str
  215. output_per_day: float
  216. average_traded_7d: float
  217. market_capacity_base: float
  218. hq_costs: dict[str, dict[str, float]]
  219. def __lt__(self, other: Profit) -> bool:
  220. bases_a = self.area / 500
  221. p_a = (self.revenue['vwap'] - self.opex['vwap']) / bases_a
  222. c_a = self.capex['vwap'] / bases_a
  223. o_a = self.opex['vwap'] / bases_a
  224. be_a = (c_a + 3 * o_a) / p_a if p_a > 0 else 10000 - p_a
  225. bases_b = other.area / 500
  226. p_b = (other.revenue['vwap'] - other.opex['vwap']) / bases_b
  227. c_b = other.capex['vwap'] / bases_b
  228. o_b = other.opex['vwap'] / bases_b
  229. be_b = (c_b + 3 * o_b) / p_b if p_b > 0 else 10000 - p_b
  230. return be_a < be_b
  231. @dataclasses.dataclass(eq=False, frozen=True, slots=True)
  232. class MatPrice:
  233. ticker: str
  234. amount: int
  235. vwap_7d: float
  236. bid: float | None
  237. ask: float | None
  238. if __name__ == '__main__':
  239. main()