3535 UNRANKED_LEAGUE_DATA ,
3636 ACHIEVEMENT_ORDER ,
3737 SUPER_TROOP_ORDER ,
38- HERO_PETS_ORDER ,
38+ PETS_ORDER ,
3939)
4040from .abc import BasePlayer
4141from .hero import Hero , Pet
@@ -237,6 +237,7 @@ class Player(ClanMember):
237237 "war_opted_in" ,
238238 "_achievements" ,
239239 "_heroes" ,
240+ "_pets" ,
240241 "_labels" ,
241242 "_spells" ,
242243 "_home_troops" ,
@@ -262,7 +263,7 @@ class Player(ClanMember):
262263 "_cs_home_troops" ,
263264 "_cs_builder_troops" ,
264265 "_cs_siege_machines" ,
265- "_cs_hero_pets " ,
266+ "_cs_pets " ,
266267 "_cs_super_troops" ,
267268 "_cs_heroes" ,
268269 "_cs_spells" ,
@@ -281,6 +282,7 @@ def __init__(self, *, data, client, load_game_data=None, **_):
281282 self ._home_troops : dict = {}
282283 self ._builder_troops : dict = {}
283284 self ._super_troops : list = []
285+ self ._pets = None # type: Optional[dict]
284286
285287 self .achievement_cls = Achievement
286288 self .hero_cls = Hero
@@ -335,7 +337,7 @@ def _from_data(self, data: dict) -> None:
335337 if self ._game_files_loaded :
336338 pet_lookup = [p .name for p in self ._client ._pet_holder .items ]
337339 else :
338- pet_lookup = HERO_PETS_ORDER
340+ pet_lookup = PETS_ORDER
339341
340342 self ._iter_labels = (label_cls (data = ldata , client = self ._client ) for ldata in data_get ("labels" , []))
341343 self ._iter_achievements = (achievement_cls (data = adata ) for adata in data_get ("achievements" , []))
@@ -400,11 +402,12 @@ def load_game_data(self):
400402 # if self._game_files_loaded:
401403 # return True
402404
403- holders = (self ._client ._troop_holder , self ._client ._hero_holder , self ._client ._spell_holder )
405+ holders = (self ._client ._troop_holder , self ._client ._hero_holder , self ._client ._spell_holder ,
406+ self ._client ._pet_holder )
404407 if not all (holder .loaded for holder in holders ):
405408 self ._client ._load_holders ()
406409
407- for items , holder in zip ((self .troops , self .heroes , self .spells ), holders ):
410+ for items , holder in zip ((self .troops , self .heroes , self .spells , self . pets ), holders ):
408411 for item in items :
409412 if not item .is_loaded :
410413 if isinstance (item , Troop ):
@@ -435,7 +438,7 @@ def achievements(self) -> List[Achievement]:
435438 return list (sorted_achievements .values ())
436439
437440 def get_achievement (self , name : str , default_value = None ) -> Optional [Achievement ]:
438- """Returns an achievement with the given name.
441+ """Gets an achievement with the given name.
439442
440443 Parameters
441444 -----------
@@ -544,18 +547,42 @@ def siege_machines(self) -> List[Troop]:
544547 troops = (t for t in self .troops if t .name in SIEGE_MACHINE_ORDER or t .is_siege_machine )
545548 return list (sorted (troops , key = lambda t : order .get (t .name , 0 )))
546549
547- @cached_property ("_cs_hero_pets " )
548- def hero_pets (self ) -> List [Pet ]:
550+ @cached_property ("_cs_pets " )
551+ def pets (self ) -> List [Pet ]:
549552 """List[:class:`Pet`]: A :class:`List` of the player's hero pets.
550553
551554 This will return hero pets in the order found in the Pet House in-game.
552555
553556 This includes:
554557 - Hero pets only.
555558 """
556- order = {k : v for v , k in enumerate (HERO_PETS_ORDER )}
559+ order = {k : v for v , k in enumerate (PETS_ORDER )}
557560 return list (sorted (self ._iter_pets , key = lambda t : order .get (t .name , 0 )))
558561
562+ def get_pet (self , name : str , default_value = None ) -> Optional [Pet ]:
563+ """Gets the pet with the given name.
564+
565+ Parameters
566+ -----------
567+ name: :class:`str`
568+ The name of a pet as found in-game.
569+ default_value:
570+ The default value to return if a pet with ``name`` is not found. Defaults to ``None``.
571+
572+ Returns
573+ --------
574+ Optional[:class:`Pet`]
575+ The returned pet or the ``default_value`` if not found, which defaults to ``None``.
576+
577+ """
578+ if not self ._pets :
579+ _ = self ._pets
580+
581+ try :
582+ return self ._pets [name ]
583+ except KeyError :
584+ return default_value
585+
559586 @cached_property ("_cs_super_troops" )
560587 def super_troops (self ) -> List [Troop ]:
561588 """List[:class:`Troop`]: A :class:`List` of the player's super troops.
@@ -573,7 +600,7 @@ def super_troops(self) -> List[Troop]:
573600 return list (sorted (self ._super_troops , key = lambda t : order .get (t .name , 0 )))
574601
575602 def get_troop (self , name : str , is_home_troop = None , default_value = None ) -> Optional [Troop ]:
576- """Returns a troop with the given name.
603+ """Gets a troop with the given name.
577604
578605 Parameters
579606 -----------
@@ -626,16 +653,19 @@ def heroes(self) -> List[Hero]:
626653 return list (sorted_heroes .values ())
627654
628655 def get_hero (self , name : str , default_value = None ) -> Optional [Hero ]:
629- """
630- Gets the player
656+ """Gets the hero with the given name.
657+
631658 Parameters
632- ----------
633- name
634- default_value
659+ -----------
660+ name: :class:`str`
661+ The name of a hero as found in-game.
662+ default_value:
663+ The default value to return if a hero with ``name`` is not found. Defaults to ``None``.
635664
636665 Returns
637- -------
638- Hero
666+ --------
667+ Optional[:class:`Hero`]
668+ The returned hero or the ``default_value`` if not found, which defaults to ``None``.
639669
640670 """
641671 if not self ._heroes :
@@ -661,7 +691,7 @@ def spells(self) -> List[Spell]:
661691 key = lambda s : order .get (s .name , 0 )))
662692
663693 def get_spell (self , name : str , default_value = None ) -> Optional [Spell ]:
664- """Returns a spell with the given name.
694+ """Gets the spell with the given name.
665695
666696 Parameters
667697 -----------
0 commit comments