sell.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from __future__ import annotations
  2. import collections
  3. import math
  4. import sys
  5. import typing
  6. import cache
  7. from config import config
  8. import market
  9. import supply
  10. if typing.TYPE_CHECKING:
  11. import supply
  12. def main() -> None:
  13. (ship,) = sys.argv[1:]
  14. ship_inventory = get_ship_storage(ship)
  15. warehouse_inventory = supply.warehouse_inventory()
  16. raw_prices: typing.Mapping[str, market.RawPrice] = {p['MaterialTicker']: p
  17. for p in cache.get('https://refined-prun.github.io/refined-prices/all.json') if p['ExchangeCode'] == 'IC1'}
  18. consumption = get_consumption()
  19. for item in ship_inventory:
  20. ticker = item['MaterialTicker']
  21. print(f'{item["MaterialAmount"]:5} {ticker:3}:', end=' ')
  22. have = item['MaterialAmount'] + warehouse_inventory.get(ticker, 0)
  23. need = math.ceil(consumption.get(ticker, 0) * 14)
  24. sell = min(have - need, item['MaterialAmount'])
  25. keep = item['MaterialAmount'] - sell
  26. if sell > 0:
  27. print('sell', sell, end=' ')
  28. if keep > 0:
  29. print('keep', keep, end= ' ')
  30. print()
  31. def get_ship_storage(ship_name: str) -> typing.Sequence[market.StorageItem]:
  32. ship_name = ship_name.casefold()
  33. stores: typing.Sequence[market.Storage] = cache.get('https://rest.fnar.net/storage/' + config.username,
  34. headers={'Authorization': config.fio_api_key})
  35. for store in stores:
  36. if store['Type'] == 'SHIP_STORE' and store['Name'].casefold() == ship_name:
  37. return store['StorageItems']
  38. raise Exception(f'ship storage {ship_name} not found')
  39. def get_consumption() -> typing.Mapping[str, float]:
  40. '''get consumption on planets that consume each mat'''
  41. planets = [supply.Planet(fio_burn) for fio_burn in cache.get('https://rest.fnar.net/fioweb/burn/user/' + config.username,
  42. headers={'Authorization': config.fio_api_key})]
  43. consumption: dict[str, float] = collections.defaultdict(float)
  44. for planet in planets:
  45. supply_config = config.supply_config(planet.name)
  46. for mat, amount in planet.net_consumption.items():
  47. if mat not in supply_config.ignore_materials and amount > 0:
  48. consumption[mat] += amount
  49. return consumption
  50. if __name__ == '__main__':
  51. main()