Forráskód Böngészése

mat_competitors: sort and format

raylu 1 hete
szülő
commit
4dd5a4776e
2 módosított fájl, 23 hozzáadás és 7 törlés
  1. 22 6
      mat_competitors.py
  2. 1 1
      planet_bases.py

+ 22 - 6
mat_competitors.py

@@ -1,6 +1,7 @@
 from __future__ import annotations
 
 import collections
+import dataclasses
 import json
 import sys
 import typing
@@ -19,12 +20,12 @@ def main() -> None:
 		planet_ids = {planet_id for planet_id, closest_cx in json.load(f).items() if closest_cx == cx}
 
 	planets = []
-	print(len(planets), expertise, 'planets near', cx)
 	for planet, cogc in company.iter_planet_cogc():
 		if planet['PlanetId'] in planet_ids and cogc == expertise:
 			planets.append(planet['PlanetName'])
+	print(len(planets), expertise, 'planets near', cx)
 
-	coid_code: dict[str, str] = {}
+	coid_code_name: dict[str, tuple[str, str]] = {}
 	coid_bases = collections.defaultdict(list)
 	for planet in planets:
 		print('\t' + planet)
@@ -32,20 +33,24 @@ def main() -> None:
 		for base in bases:
 			if (code := base['OwnerCode']) is None:
 				continue
-			coid_code[base['OwnerId']] = code
+			coid_code_name[base['OwnerId']] = code, base['OwnerName']
 			coid_bases[base['OwnerId']].append(planet)
 
 	coid_users: dict[str, str] = {company_id: d['Username']
 		for company_id, d in cache.get('https://pmmg-products.github.io/reports/data/knownCompanies.json', expiry=cache.ONE_DAY).items()}
 
+	competitors: list[Competitor] = []
 	for company_id, co_production in integration.pmmg_monthly_report().items():
 		if (mat_production := co_production.get(ticker)) is None:
 			continue
 		if bases := coid_bases.get(company_id):
-			code = coid_code[company_id]
+			code, co_name = coid_code_name[company_id]
 			username = coid_users[company_id]
-			print(f'{code}\t{username}')
-			print(f'{mat_production["amount"]:10.1f}', ', '.join(bases))
+			competitors.append(Competitor(code, co_name, username, mat_production['amount'], bases))
+	competitors.sort(reverse=True)
+
+	for c in competitors:
+		print(f'{c.code:4} {c.company_name:30} {c.username:20} {c.production:9,.1f}', ', '.join(c.bases))
 
 def iter_expertise(ticker: str) -> typing.Iterator[str]:
 	buildings: typing.Sequence[company.Building] = cache.get('https://rest.fnar.net/building/allbuildings', expiry=cache.ONE_DAY)
@@ -56,5 +61,16 @@ def iter_expertise(ticker: str) -> typing.Iterator[str]:
 					print(ticker, 'requires', building['Expertise'])
 					yield building['Expertise']
 
+@dataclasses.dataclass(eq=False, frozen=True, slots=True)
+class Competitor:
+	code: str
+	company_name: str
+	username: str
+	production: float
+	bases: typing.Sequence[str]
+
+	def __lt__(self, other: Competitor) -> bool:
+		return self.production < other.production
+
 if __name__ == '__main__':
 	main()

+ 1 - 1
planet_bases.py

@@ -79,7 +79,7 @@ class Planet(typing.TypedDict):
 
 class Site(typing.TypedDict):
 	OwnerId: str
-	OwnerName: int
+	OwnerName: str
 	OwnerCode: str | None
 
 if __name__ == '__main__':