company.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from __future__ import annotations
  2. import collections
  3. import datetime
  4. import sys
  5. import typing
  6. import cache
  7. import integration
  8. def main() -> None:
  9. code = sys.argv[1]
  10. company: Company = cache.get('https://rest.fnar.net/company/code/' + code)
  11. planets = {cp['PlanetName'] for cp in company['Planets']}
  12. cogc_planets: dict[str, list[str]] = collections.defaultdict(list)
  13. for planet, cogc in iter_planet_cogc():
  14. if planet['PlanetName'] in planets:
  15. print(planet['PlanetName'], cogc, sep='\t\t')
  16. if cogc is not None:
  17. cogc_planets[cogc].append(planet['PlanetName'])
  18. buildings: typing.Sequence[Building] = cache.get('https://rest.fnar.net/building/allbuildings', expiry=cache.ONE_DAY)
  19. experts: dict[str, str] = {}
  20. for building in buildings:
  21. for recipe in building['Recipes']:
  22. for output in recipe['Outputs']:
  23. experts[output['CommodityTicker']] = building['Expertise']
  24. print()
  25. company_report = integration.pmmg_monthly_report()[company['CompanyId']]
  26. for mat, production in company_report.items():
  27. expertise = experts.get(mat)
  28. print(f'{mat:3} {production["amount"]:8,.0f} {expertise or '':19}',
  29. ', '.join(cogc_planets.get(expertise, []))) # pyright: ignore[reportArgumentType, reportCallIssue]
  30. def iter_planet_cogc() -> typing.Iterator[tuple[Planet, Expertise | None]]:
  31. all_planets: typing.Collection[Planet] = cache.get('https://universemap.taiyibureau.de/planet_data.json',
  32. expiry=cache.ONE_DAY)
  33. for planet in all_planets:
  34. cogc = None
  35. if len(cogcs := planet['COGCPrograms']) > 1:
  36. cogcs.sort(key=lambda c: c['StartEpochMs'], reverse=True)
  37. cogc = cogcs[1]['ProgramType']
  38. if cogc is not None:
  39. cogc = cogc.removeprefix('ADVERTISING_')
  40. yield planet, typing.cast(Expertise | None, cogc)
  41. Expertise = typing.Literal['AGRICULTURE', 'CHEMISTRY', 'CONSTRUCTION', 'ELECTRONICS', 'FOOD_INDUSTRIES',
  42. 'FUEL_REFINING', 'MANUFACTURING', 'METALLURGY', 'RESOURCE_EXTRACTION']
  43. class Company(typing.TypedDict):
  44. CompanyId: str
  45. Planets: typing.Sequence[CompanyPlanet]
  46. class CompanyPlanet(typing.TypedDict):
  47. PlanetNaturalId: str
  48. PlanetName: str
  49. class Planet(typing.TypedDict):
  50. PlanetId: str
  51. PlanetName: str
  52. COGCPrograms: list[PlanetCOGC]
  53. class PlanetCOGC(typing.TypedDict):
  54. ProgramType: str | None
  55. StartEpochMs: float
  56. class Building(typing.TypedDict):
  57. Recipes: typing.Sequence[BuildingRecipe]
  58. Expertise: str
  59. class BuildingRecipe(typing.TypedDict):
  60. Outputs: typing.Sequence[BuildingMat]
  61. class BuildingMat(typing.TypedDict):
  62. CommodityTicker: str
  63. Amount: int
  64. if __name__ == '__main__':
  65. main()