supply.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. from __future__ import annotations
  2. import sys
  3. import tomllib
  4. import typing
  5. import cache
  6. def main() -> None:
  7. planet = sys.argv[1].casefold()
  8. burn, raw_materials = fio_data(planet)
  9. inventory = {item['MaterialTicker']: item['MaterialAmount'] for item in burn['Inventory']}
  10. materials = {mat['Ticker']: mat for mat in raw_materials}
  11. producing = {item['MaterialTicker']: item for item in burn['OrderProduction']}
  12. net_consumption = []
  13. for c in burn['OrderConsumption'] + burn['WorkforceConsumption']:
  14. net = c['DailyAmount']
  15. if production := producing.get(c['MaterialTicker']):
  16. net -= production['DailyAmount']
  17. if net < 0:
  18. continue
  19. c['net_consumption'] = net
  20. net_consumption.append(c)
  21. vol_per_day = 0.0
  22. weight_per_day = 0.0
  23. target_days = float('inf')
  24. for consumption in net_consumption:
  25. ticker = consumption['MaterialTicker']
  26. vol_per_day += materials[ticker]['Volume'] * consumption['net_consumption']
  27. weight_per_day += materials[ticker]['Weight'] * consumption['net_consumption']
  28. days = inventory.get(ticker, 0) / consumption['net_consumption']
  29. if days < target_days:
  30. target_days = days
  31. print(f'consuming {vol_per_day:.1f}㎥/d')
  32. print(f'consuming {weight_per_day:.1f}t/d')
  33. limiting = 'Volume'
  34. if weight_per_day > vol_per_day:
  35. limiting = 'Weight'
  36. target_days = round(target_days + 0.05, 1)
  37. while True:
  38. space_used = 0
  39. buy: dict[str, float] = {}
  40. for consumption in net_consumption:
  41. ticker = consumption['MaterialTicker']
  42. avail = inventory.get(ticker, 0)
  43. daily_consumption = consumption['net_consumption']
  44. days = avail / daily_consumption
  45. if days < target_days:
  46. buy[ticker] = (target_days - days) * daily_consumption
  47. space_used += buy[ticker] * materials[ticker][limiting]
  48. if space_used > 500:
  49. break
  50. optimal = buy
  51. target_days += 0.1
  52. print('supply for', round(target_days, 1), 'days')
  53. for consumption in net_consumption:
  54. ticker = consumption['MaterialTicker']
  55. avail = inventory.get(ticker, 0)
  56. daily_consumption = consumption['net_consumption']
  57. days = avail / daily_consumption
  58. print(f'{ticker:>3}: {avail:5d} ({daily_consumption:8.2f}/d) {days:4.1f} d', end='')
  59. if need := optimal.get(ticker): # pyright: ignore[reportPossiblyUnboundVariable]
  60. print(f' | {need:8.1f}')
  61. else:
  62. print()
  63. def fio_data(planet: str) -> tuple[PlanetData, typing.Sequence[Material]]:
  64. with open('config.toml', 'rb') as f:
  65. config = tomllib.load(f)
  66. planets: list[PlanetData] = cache.get('https://rest.fnar.net/fioweb/burn/user/' + config['username'],
  67. headers={'Authorization': config['fio_api_key']})
  68. for planet_data in planets:
  69. name = planet_data['PlanetName']
  70. if name.casefold() == planet:
  71. assert planet_data['Error'] is None
  72. break
  73. else:
  74. raise ValueError(planet + ' not found')
  75. materials: list[Material] = cache.get('https://rest.fnar.net/material/allmaterials')
  76. return planet_data, materials
  77. class PlanetData(typing.TypedDict):
  78. PlanetName: str
  79. Error: typing.Any
  80. OrderConsumption: list[Amount]
  81. WorkforceConsumption: list[Amount]
  82. Inventory: list[Inventory]
  83. OrderProduction: list[Amount]
  84. class Amount(typing.TypedDict):
  85. MaterialTicker: str
  86. DailyAmount: float
  87. net_consumption: float
  88. class Inventory(typing.TypedDict):
  89. MaterialTicker: str
  90. MaterialAmount: int
  91. class Material(typing.TypedDict):
  92. Ticker: str
  93. Weight: float
  94. Volume: float
  95. if __name__ == '__main__':
  96. main()