company.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. print()
  18. experts = mat_expertise()
  19. company_report = integration.pmmg_monthly_report()[company['CompanyId']]
  20. report_by_expertise: dict[str | None, dict[str, int]] = collections.defaultdict(dict)
  21. for mat, production in company_report.items():
  22. expertise = experts.get(mat)
  23. report_by_expertise[expertise][mat] = production['amount']
  24. for expertise, sub_report in report_by_expertise.items():
  25. print(expertise, end=' ')
  26. if expertise is not None:
  27. print(', '.join(cogc_planets.get(expertise, [])), end='')
  28. print()
  29. for mat, amount in sub_report.items():
  30. print(f'\t{mat:3} {amount:9,.1f}')
  31. def iter_planet_cogc() -> typing.Iterator[tuple[Planet, Expertise | None]]:
  32. all_planets: typing.Collection[Planet] = cache.get('https://universemap.taiyibureau.de/planet_data.json',
  33. expiry=cache.ONE_DAY)
  34. for planet in all_planets:
  35. cogc = None
  36. if len(cogcs := planet['COGCPrograms']) > 1:
  37. cogcs.sort(key=lambda c: c['StartEpochMs'], reverse=True)
  38. cogc = cogcs[1]['ProgramType']
  39. if cogc is not None:
  40. cogc = cogc.removeprefix('ADVERTISING_')
  41. yield planet, typing.cast(Expertise | None, cogc)
  42. def mat_expertise() -> dict[str, Expertise]:
  43. expertise: dict[str, Expertise] = {
  44. 'AL': 'METALLURGY',
  45. 'DRF': 'ELECTRONICS',
  46. 'FE': 'METALLURGY',
  47. }
  48. materials: typing.Sequence[Material] = cache.get('https://rest.fnar.net/material/allmaterials', expiry=cache.ONE_DAY)
  49. for material in materials:
  50. if material['CategoryName'] in ('ores', 'minerals', 'liquids', 'gases'):
  51. expertise[material['Ticker']] = 'RESOURCE_EXTRACTION'
  52. buildings: typing.Sequence[Building] = cache.get('https://rest.fnar.net/building/allbuildings', expiry=cache.ONE_DAY)
  53. for building in buildings:
  54. for recipe in building['Recipes']:
  55. for output in recipe['Outputs']:
  56. expertise.setdefault(output['CommodityTicker'], building['Expertise'])
  57. return expertise
  58. Expertise = typing.Literal['AGRICULTURE', 'CHEMISTRY', 'CONSTRUCTION', 'ELECTRONICS', 'FOOD_INDUSTRIES',
  59. 'FUEL_REFINING', 'MANUFACTURING', 'METALLURGY', 'RESOURCE_EXTRACTION']
  60. class Company(typing.TypedDict):
  61. CompanyId: str
  62. Planets: typing.Sequence[CompanyPlanet]
  63. class CompanyPlanet(typing.TypedDict):
  64. PlanetNaturalId: str
  65. PlanetName: str
  66. class Planet(typing.TypedDict):
  67. PlanetId: str
  68. PlanetName: str
  69. COGCPrograms: list[PlanetCOGC]
  70. class PlanetCOGC(typing.TypedDict):
  71. ProgramType: str | None
  72. StartEpochMs: float
  73. class Building(typing.TypedDict):
  74. Recipes: typing.Sequence[BuildingRecipe]
  75. Expertise: str
  76. class BuildingRecipe(typing.TypedDict):
  77. Outputs: typing.Sequence[BuildingMat]
  78. class BuildingMat(typing.TypedDict):
  79. CommodityTicker: str
  80. Amount: int
  81. class Material(typing.TypedDict):
  82. Ticker: str
  83. CategoryName: str
  84. if __name__ == '__main__':
  85. main()