mat_competitors.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from __future__ import annotations
  2. import collections
  3. import dataclasses
  4. import json
  5. import sys
  6. import typing
  7. import cache
  8. import company
  9. import integration
  10. import planet_bases
  11. def main() -> None:
  12. cx, ticker = sys.argv[1:]
  13. (expertise,) = frozenset(iter_expertise(ticker))
  14. planets: dict[str, str] = {}
  15. for planet, cogc in company.iter_planet_cogc():
  16. if cogc == expertise:
  17. planets[planet['PlanetId']] = planet['PlanetName']
  18. print(len(planets), 'with', expertise, 'CoGC')
  19. with open('www/closest.json') as f:
  20. close_planet_ids = {planet_id for planet_id, closest_cx in json.load(f).items() if closest_cx == cx}
  21. coid_code_name: dict[str, tuple[str, str]] = {}
  22. coid_bases = collections.defaultdict(list)
  23. for planet_id, planet_name in planets.items():
  24. if planet_id in close_planet_ids:
  25. print(f'\t\033[32m{planet_name}\033[0m')
  26. else:
  27. print(f'\t\033[31m{planet_name}\033[0m')
  28. bases = planet_bases.get_bases(planet_name)
  29. for base in bases:
  30. if (code := base['OwnerCode']) is None:
  31. continue
  32. coid_code_name[base['OwnerId']] = code, base['OwnerName']
  33. coid_bases[base['OwnerId']].append(planet_id)
  34. coid_users: dict[str, str] = {company_id: d['Username']
  35. for company_id, d in cache.get('https://pmmg-products.github.io/reports/data/knownCompanies.json', expiry=cache.ONE_DAY).items()}
  36. competitors: list[Competitor] = []
  37. for company_id, co_production in integration.pmmg_monthly_report().items():
  38. if (mat_production := co_production.get(ticker)) is None:
  39. continue
  40. if planet_ids := coid_bases.get(company_id):
  41. code, co_name = coid_code_name[company_id]
  42. username = coid_users[company_id]
  43. competitors.append(Competitor(code, co_name, username, mat_production['amount'], planet_ids))
  44. competitors.sort(reverse=True)
  45. total = 0.0
  46. for c in competitors:
  47. close_planet_num = 0
  48. player_planets = []
  49. for planet_id in c.planet_ids:
  50. if planet_id in close_planet_ids:
  51. player_planets.append(f'\033[32m{planets[planet_id]}\033[0m')
  52. close_planet_num += 1
  53. else:
  54. player_planets.append(f'\033[31m{planets[planet_id]}\033[0m')
  55. if close_planet_num > 0:
  56. local_production = c.production * close_planet_num / len(c.planet_ids)
  57. total += local_production
  58. print(f'{c.code:4} {c.company_name:30} {c.username:20} {local_production:9,.1f} ', ' '.join(player_planets))
  59. print(f'total: {total:,.1f}')
  60. def iter_expertise(ticker: str) -> typing.Iterator[str]:
  61. buildings: typing.Sequence[company.Building] = cache.get('https://rest.fnar.net/building/allbuildings', expiry=cache.ONE_DAY)
  62. for building in buildings:
  63. for recipe in building['Recipes']:
  64. for output in recipe['Outputs']:
  65. if output['CommodityTicker'] == ticker:
  66. print(ticker, 'requires', building['Expertise'])
  67. yield building['Expertise']
  68. @dataclasses.dataclass(eq=False, frozen=True, slots=True)
  69. class Competitor:
  70. code: str
  71. company_name: str
  72. username: str
  73. production: float
  74. planet_ids: typing.Sequence[str]
  75. def __lt__(self, other: Competitor) -> bool:
  76. return self.production < other.production
  77. if __name__ == '__main__':
  78. main()