Skip to content

Commit 00efb7e

Browse files
committed
Improve ergonomics of population classes
1 parent c67c93e commit 00efb7e

1 file changed

Lines changed: 31 additions & 61 deletions

File tree

rlapi/population.py

Lines changed: 31 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
__all__ = (
66
"PopulationPlaylist",
77
"KNOWN_POPULATION_PLAYLISTS",
8-
"PopulationEntry",
9-
"PlatformPopulation",
8+
"PlaylistPopulation",
109
"Population",
1110
)
1211

@@ -523,83 +522,45 @@ def __repr__(self) -> str:
523522
KNOWN_POPULATION_PLAYLISTS = {playlist.id: playlist for playlist in _KNOWN_PLAYLISTS}
524523

525524

526-
class PopulationEntry:
527-
"""PopulationEntry()
528-
Describes population for a Rocket League playlist on a single platform.
525+
class PlaylistPopulation:
526+
"""PlaylistPopulation()
527+
Describes population of a Rocket League playlist.
529528
530529
Attributes
531530
----------
532-
platform: `Platform`
533-
Platform that this entry's number of players refers to.
534-
playlist: `Playlist`
535-
Playlist that this entry's number of players refers to.
531+
playlist: `PopulationPlaylist`
532+
Playlist that this instance refers to.
536533
If the playlist is known, this will use a constant value
537534
with additional information.
538-
num_players: int
539-
Number of players who are currently online on this playlist.
535+
platforms: dict
536+
Mapping of platforms (`Platform`) to their number of online players (`int`)
537+
for this playlist.
540538
"""
541539

542-
__slots__ = ("platform", "playlist", "num_players")
540+
__slots__ = ("playlist", "platforms")
543541

544-
def __init__(self, platform: Platform, data: Dict[str, Any]) -> None:
545-
self.platform = platform
542+
def __init__(self, playlist_id: int) -> None:
546543
try:
547-
self.playlist = KNOWN_POPULATION_PLAYLISTS[data["PlaylistID"]]
544+
self.playlist = KNOWN_POPULATION_PLAYLISTS[playlist_id]
548545
except KeyError:
549-
self.playlist = PopulationPlaylist(
550-
data["PlaylistID"], "Unknown", is_known=False
551-
)
552-
self.num_players: int = data["NumPlayers"]
546+
self.playlist = PopulationPlaylist(playlist_id, "Unknown", is_known=False)
547+
self.platforms: dict[Platform, int] = {}
553548

554549
def __repr__(self) -> str:
555-
platform_repr = f"{self.platform.__class__.__name__}.{self.platform._name_}"
556550
return (
557551
f"<{self.__class__.__name__}"
558-
f" platform={platform_repr}"
559552
f" playlist={self.playlist!r}"
560553
f" num_players={self.num_players}"
561554
">"
562555
)
563556

564-
565-
class PlatformPopulation:
566-
"""PlatformPopulation()
567-
Describes population of a single Rocket League platform.
568-
569-
Attributes
570-
----------
571-
platform: `Platform`
572-
Platform that this population object refers to.
573-
playlists: dict
574-
Mapping of playlist IDs (`int`) to their entries (`PopulationEntry`).
575-
For ranked playlists, it is possible to do a lookup by their `PlaylistKey`.
576-
"""
577-
578-
__slots__ = ("platform", "playlists")
579-
580-
def __init__(self, platform: Platform, data: List[Dict[str, Any]]) -> None:
581-
self.platform = platform
582-
self.playlists = {}
583-
for raw_population_entry in data:
584-
entry = PopulationEntry(platform, raw_population_entry)
585-
self.playlists[entry.playlist.id] = entry
586-
587-
def __repr__(self) -> str:
588-
platform_repr = f"{self.platform.__class__.__name__}.{self.platform._name_}"
589-
return (
590-
f"<{self.__class__.__name__}"
591-
f" platform={platform_repr}"
592-
f" num_players={self.num_players}"
593-
">"
594-
)
595-
596557
@property
597558
def num_players(self) -> int:
598559
"""
599-
Total number of players who are currently online on the platform
600-
across all playlists.
560+
Total number of players who are currently online on this playlist
561+
across all platforms.
601562
"""
602-
return sum(entry.num_players for entry in self.playlists.values())
563+
return sum(num_players for num_players in self.platforms.values())
603564

604565

605566
class Population:
@@ -609,16 +570,25 @@ class Population:
609570
Attributes
610571
----------
611572
playlists: dict
612-
Mapping of platforms (`Platform`) to their populations (`PlatformPopulation`).
573+
Mapping of playlist IDs (`int`) to their population (`PlaylistPopulation`).
574+
For ranked playlists, it is possible to do a lookup by their `PlaylistKey`.
613575
"""
614576

615-
__slots__ = ("platforms",)
577+
__slots__ = ("playlists",)
616578

617579
def __init__(self, data: Dict[str, List[Dict[str, Any]]]) -> None:
618-
self.platforms = {}
580+
self.playlists: dict[int, PlaylistPopulation] = {}
619581
for raw_platform, raw_population in data.items():
620582
platform = Platform(raw_platform)
621-
self.platforms[platform] = PlatformPopulation(platform, raw_population)
583+
for raw_population_entry in raw_population:
584+
playlist_id = raw_population_entry["PlaylistID"]
585+
playlist_population = self.playlists.get(playlist_id)
586+
if playlist_population is None:
587+
playlist_population = PlaylistPopulation(playlist_id)
588+
self.playlists[playlist_id] = playlist_population
589+
590+
num_players = raw_population_entry["NumPlayers"]
591+
playlist_population.platforms[platform] = num_players
622592

623593
def __repr__(self) -> str:
624594
return f"<{self.__class__.__name__} num_players={self.num_players}>"
@@ -628,4 +598,4 @@ def num_players(self) -> int:
628598
"""
629599
Total number of players who are currently online on all platforms and playlists.
630600
"""
631-
return sum(population.num_players for population in self.platforms.values())
601+
return sum(population.num_players for population in self.playlists.values())

0 commit comments

Comments
 (0)