shipbuilding.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from __future__ import annotations
  2. import collections
  3. import typing
  4. import cache
  5. import roi
  6. BUY = frozenset([
  7. # definitely buy
  8. 'C', 'FLX', 'H', 'H2O', 'HAL', 'HCP', 'HE', 'LST', 'MG', 'N', 'NA', 'NCS', 'NS', 'O', 'PE', 'PG', 'S', 'TCL',
  9. # maybe buy
  10. 'AU', 'BRM', 'CU', 'FE', 'LI', 'RG', 'ROM', 'SI', 'TI',
  11. ])
  12. def main() -> None:
  13. blueprint = {
  14. 'FFC': 1,
  15. 'FSE': 1,
  16. 'LFE': 2,
  17. 'MFE': 2,
  18. 'QCR': 1,
  19. 'SFE': 1,
  20. 'LCB': 1,
  21. 'MFL': 1,
  22. 'MSL': 1,
  23. 'LHP': 94,
  24. 'SSC': 128,
  25. 'BR1': 1,
  26. 'CQM': 1,
  27. }
  28. prices: typing.Mapping[str, RawPrice] = {p['MaterialTicker']: p
  29. for p in cache.get('https://refined-prun.github.io/refined-prices/all.json') if p['ExchangeCode'] == 'IC1'}
  30. recipes = recipe_for_mats()
  31. production: dict[str, dict[str, float]] = collections.defaultdict(lambda: collections.defaultdict(float))
  32. cost = 0.0
  33. for mat, amount in blueprint.items():
  34. cost += analyze_mat(0, mat, amount, production, prices, recipes)
  35. print()
  36. print(f'total cost: {cost:,}')
  37. buildings: typing.Sequence[roi.Building] = cache.get('https://api.prunplanner.org/data/buildings/', expiry=cache.ONE_DAY)
  38. expertise = collections.defaultdict(list)
  39. for building in buildings:
  40. if building['building_ticker'] in production:
  41. expertise[building['expertise']].append(building['building_ticker'])
  42. for expertise, buildings in expertise.items():
  43. print(expertise)
  44. for building in buildings:
  45. print(f'\t{building:3}:', end='')
  46. for mat, amount in production[building].items():
  47. traded = prices[mat]['AverageTraded30D'] or 0
  48. if traded > amount * 2:
  49. print(f' \033[32m{mat}\033[0m', end='')
  50. else:
  51. print(f' \033[31m{mat}\033[0m', end='')
  52. print()
  53. def recipe_for_mats() -> dict[str, roi.Recipe]:
  54. all_recipes: list[roi.Recipe] = cache.get('https://api.prunplanner.org/data/recipes/', expiry=cache.ONE_DAY)
  55. mat_recipes = collections.defaultdict(list) # all ways to make a mat
  56. for recipe in all_recipes:
  57. for output in recipe['outputs']:
  58. mat_recipes[output['material_ticker']].append(recipe)
  59. mat_recipe: dict[str, roi.Recipe] = {} # mats for which there's only one recipe to make
  60. for mat, recipes in mat_recipes.items():
  61. if len(recipes) == 1:
  62. mat_recipe[mat] = recipes[0]
  63. return mat_recipe
  64. def analyze_mat(level: int, mat: str, amount: float, production: dict[str, dict[str, float]],
  65. prices: typing.Mapping[str, RawPrice], recipes: dict[str, roi.Recipe]) -> float:
  66. price = prices[mat]
  67. traded = price['AverageTraded30D'] or 0
  68. if mat in BUY:
  69. print('\t' * level + f'{amount:g}×{mat} buy: {price["Ask"]}, daily traded {traded:5.1f}')
  70. assert price['Ask'] is not None
  71. return price['Ask'] * amount
  72. else:
  73. if (recipe := recipes.get(mat)) is None:
  74. print('\t' * level + f'{amount:g}×{mat} make (unknown recipe)')
  75. return 0
  76. else:
  77. building = recipe['building_ticker']
  78. production[building][mat] += amount
  79. liquid = '\033[31mnot liquid\033[0m'
  80. if traded > amount * 2:
  81. liquid = '\033[32mliquid\033[0m'
  82. print('\t' * level + f'{amount:g}×{mat} make ({building}, {liquid})')
  83. total_cost = 0.0
  84. for input_mat in recipe['inputs']:
  85. input_amount = input_mat['material_amount'] * amount / recipe['outputs'][0]['material_amount']
  86. total_cost += analyze_mat(level + 1, input_mat['material_ticker'], input_amount, production, prices, recipes)
  87. print('\t' * level + f'\tcost: {total_cost:9,.2f}')
  88. return total_cost
  89. class RawPrice(typing.TypedDict):
  90. MaterialTicker: str
  91. ExchangeCode: str
  92. Ask: float | None
  93. AverageTraded30D: float | None # averaged daily traded volume over last 30 days
  94. Supply: int
  95. if __name__ == '__main__':
  96. main()