company.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. report_by_expertise: dict[str | None, dict[str, int]] = collections.defaultdict(dict)
  26. for mat, production in company_report.items():
  27. expertise = experts.get(mat)
  28. report_by_expertise[expertise][mat] = production['amount']
  29. for expertise, sub_report in report_by_expertise.items():
  30. print(expertise, end=' ')
  31. if expertise is not None:
  32. print(', '.join(cogc_planets.get(expertise, [])), end='')
  33. print()
  34. for mat, amount in sub_report.items():
  35. print(f'\t{mat:3} {amount:9,.1f}')
  36. def iter_planet_cogc() -> typing.Iterator[tuple[Planet, Expertise | None]]:
  37. all_planets: typing.Collection[Planet] = cache.get('https://universemap.taiyibureau.de/planet_data.json',
  38. expiry=cache.ONE_DAY)
  39. for planet in all_planets:
  40. cogc = None
  41. if len(cogcs := planet['COGCPrograms']) > 1:
  42. cogcs.sort(key=lambda c: c['StartEpochMs'], reverse=True)
  43. cogc = cogcs[1]['ProgramType']
  44. if cogc is not None:
  45. cogc = cogc.removeprefix('ADVERTISING_')
  46. yield planet, typing.cast(Expertise | None, cogc)
  47. Expertise = typing.Literal['AGRICULTURE', 'CHEMISTRY', 'CONSTRUCTION', 'ELECTRONICS', 'FOOD_INDUSTRIES',
  48. 'FUEL_REFINING', 'MANUFACTURING', 'METALLURGY', 'RESOURCE_EXTRACTION']
  49. class Company(typing.TypedDict):
  50. CompanyId: str
  51. Planets: typing.Sequence[CompanyPlanet]
  52. class CompanyPlanet(typing.TypedDict):
  53. PlanetNaturalId: str
  54. PlanetName: str
  55. class Planet(typing.TypedDict):
  56. PlanetId: str
  57. PlanetName: str
  58. COGCPrograms: list[PlanetCOGC]
  59. class PlanetCOGC(typing.TypedDict):
  60. ProgramType: str | None
  61. StartEpochMs: float
  62. class Building(typing.TypedDict):
  63. Recipes: typing.Sequence[BuildingRecipe]
  64. Expertise: str
  65. class BuildingRecipe(typing.TypedDict):
  66. Outputs: typing.Sequence[BuildingMat]
  67. class BuildingMat(typing.TypedDict):
  68. CommodityTicker: str
  69. Amount: int
  70. if __name__ == '__main__':
  71. main()