shipbuilding.py 3.6 KB

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