company.py 2.5 KB

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