Skip to content

Commit 3acf162

Browse files
committed
Rewrite League and BaseLeague, fix Player.get_pet()
1 parent 0c83742 commit 3acf162

File tree

6 files changed

+90
-124
lines changed

6 files changed

+90
-124
lines changed

coc/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@
7777
Timestamp,
7878
TimeDelta,
7979
Label,
80-
WarLeague,
81-
BuilderBaseLeague
80+
BaseLeague
8281
)
8382
from .players import Player, ClanMember, RankedPlayer
8483
from .player_clan import PlayerClan

coc/clans.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626

2727
from .players import ClanMember
28-
from .miscmodels import try_enum, ChatLanguage, Location, Label, WarLeague, CapitalDistrict
28+
from .miscmodels import try_enum, ChatLanguage, Location, Label, BaseLeague, CapitalDistrict
2929
from .utils import get, cached_property, correct_tag
3030
from .abc import BaseClan
3131

@@ -149,9 +149,9 @@ class Clan(BaseClan):
149149
The type which the clan capital districts found in
150150
:attr:`Clan.capital_districts` will be of. Ensure any overriding of
151151
this inherits from :class:`coc.CapitalDistrict`.
152-
war_league: :class:`coc.WarLeague`
152+
war_league: :class:`coc.BaseLeague`
153153
The clan's CWL league.
154-
capital_league: :class:`coc.WarLeague`
154+
capital_league: :class:`coc.BaseLeague`
155155
The clan's Clan Capital league.
156156
"""
157157

@@ -222,8 +222,8 @@ def _from_data(self, data: dict) -> None:
222222
self.war_losses: int = data_get("warLosses", -1)
223223
self.public_war_log: bool = data_get("isWarLogPublic")
224224
self.description: str = data_get("description")
225-
self.war_league = try_enum(WarLeague, data=data_get("warLeague"))
226-
self.capital_league = try_enum(WarLeague, data=data_get("capitalLeague"))
225+
self.war_league = try_enum(BaseLeague, data=data_get("warLeague"))
226+
self.capital_league = try_enum(BaseLeague, data=data_get("capitalLeague"))
227227
self.chat_language = try_enum(ChatLanguage, data=data_get("chatLanguage"))
228228
self.required_townhall = data_get("requiredTownhallLevel")
229229

coc/client.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from .clans import Clan, RankedClan
3535
from .errors import Forbidden, GatewayError, NotFound, PrivateWarLog
3636
from .enums import WarRound
37-
from .miscmodels import GoldPassSeason, Label, League, Location, LoadGameData
37+
from .miscmodels import BaseLeague, GoldPassSeason, Label, League, Location, LoadGameData
3838
from .hero import HeroHolder, PetHolder
3939
from .http import HTTPClient, BasicThrottler, BatchThrottler
4040
from .iterators import (
@@ -1614,7 +1614,8 @@ async def get_league_named(self, league_name: str) -> Optional[League]:
16141614
"""
16151615
return get(await self.search_leagues(), name=league_name)
16161616

1617-
async def search_builder_base_leagues(self, *, limit: int = None, before: str = None, after: str = None) -> List[League]:
1617+
async def search_builder_base_leagues(self, *, limit: int = None, before: str = None, after: str = None)-> List[
1618+
BaseLeague]:
16181619
"""Get list of builder base leagues.
16191620
16201621
Parameters
@@ -1638,13 +1639,13 @@ async def search_builder_base_leagues(self, *, limit: int = None, before: str =
16381639
16391640
Returns
16401641
--------
1641-
List[:class:`League`]
1642+
List[:class:`BaseLeague`]
16421643
The requested leagues.
16431644
"""
16441645
data = await self.http.search_builder_base_leagues(limit=limit, before=before, after=after)
1645-
return [League(data=n, client=self) for n in data["items"]]
1646+
return [BaseLeague(data=n, client=self) for n in data["items"]]
16461647

1647-
async def get_builder_base_league(self, league_id: int) -> League:
1648+
async def get_builder_base_league(self, league_id: int) -> BaseLeague:
16481649
"""
16491650
Get builder base league information
16501651
@@ -1667,13 +1668,13 @@ async def get_builder_base_league(self, league_id: int) -> League:
16671668
16681669
Returns
16691670
--------
1670-
:class:`League`
1671+
:class:`BaseLeague`
16711672
The league with the requested ID
16721673
"""
16731674
data = await self.http.get_builder_base_league(league_id)
1674-
return League(data=data, client=self)
1675+
return BaseLeague(data=data, client=self)
16751676

1676-
async def get_builder_base_league_named(self, league_name: str) -> Optional[League]:
1677+
async def get_builder_base_league_named(self, league_name: str) -> Optional[BaseLeague]:
16771678
"""Get a builder base league by name.
16781679
16791680
This is somewhat equivalent to
@@ -1699,12 +1700,12 @@ async def get_builder_base_league_named(self, league_name: str) -> Optional[Leag
16991700
17001701
Returns
17011702
--------
1702-
:class:`League`
1703+
:class:`BaseLeague`
17031704
The first league matching the league name. Could be ``None`` if not found.
17041705
"""
17051706
return get(await self.search_builder_base_leagues(), name=league_name)
17061707

1707-
async def search_war_leagues(self, *, limit: int = None, before: str = None, after: str = None) -> List[League]:
1708+
async def search_war_leagues(self, *, limit: int = None, before: str = None, after: str = None) -> List[BaseLeague]:
17081709
"""Get list of war leagues.
17091710
17101711
Parameters
@@ -1728,13 +1729,13 @@ async def search_war_leagues(self, *, limit: int = None, before: str = None, aft
17281729
17291730
Returns
17301731
--------
1731-
List[:class:`League`]
1732+
List[:class:`BaseLeague`]
17321733
The requested leagues.
17331734
"""
17341735
data = await self.http.search_war_leagues(limit=limit, before=before, after=after)
1735-
return [League(data=n, client=self) for n in data["items"]]
1736+
return [BaseLeague(data=n, client=self) for n in data["items"]]
17361737

1737-
async def get_war_league(self, league_id: int) -> League:
1738+
async def get_war_league(self, league_id: int) -> BaseLeague:
17381739
"""
17391740
Get war league information
17401741
@@ -1757,13 +1758,13 @@ async def get_war_league(self, league_id: int) -> League:
17571758
17581759
Returns
17591760
--------
1760-
:class:`League`
1761+
:class:`BaseLeague`
17611762
The league with the requested ID
17621763
"""
17631764
data = await self.http.get_war_league(league_id)
1764-
return League(data=data, client=self)
1765+
return BaseLeague(data=data, client=self)
17651766

1766-
async def get_war_league_named(self, league_name: str) -> Optional[League]:
1767+
async def get_war_league_named(self, league_name: str) -> Optional[BaseLeague]:
17671768
"""Get a war league by name.
17681769
17691770
This is somewhat equivalent to
@@ -1789,12 +1790,12 @@ async def get_war_league_named(self, league_name: str) -> Optional[League]:
17891790
17901791
Returns
17911792
--------
1792-
:class:`League`
1793+
:class:`BaseLeague`
17931794
The first league matching the league name. Could be ``None`` if not found.
17941795
"""
17951796
return get(await self.search_war_leagues(), name=league_name)
17961797

1797-
async def search_capital_leagues(self, *, limit: int = None, before: str = None, after: str = None) -> List[League]:
1798+
async def search_capital_leagues(self, *, limit: int = None, before: str = None, after: str = None) -> List[BaseLeague]:
17981799
"""Get list of capital leagues.
17991800
18001801
Parameters
@@ -1818,13 +1819,13 @@ async def search_capital_leagues(self, *, limit: int = None, before: str = None,
18181819
18191820
Returns
18201821
--------
1821-
List[:class:`League`]
1822+
List[:class:`BaseLeague`]
18221823
The requested leagues.
18231824
"""
18241825
data = await self.http.search_capital_leagues(limit=limit, before=before, after=after)
1825-
return [League(data=n, client=self) for n in data["items"]]
1826+
return [BaseLeague(data=n, client=self) for n in data["items"]]
18261827

1827-
async def get_capital_league(self, league_id: int) -> League:
1828+
async def get_capital_league(self, league_id: int) -> BaseLeague:
18281829
"""
18291830
Get capital league information
18301831
@@ -1847,13 +1848,13 @@ async def get_capital_league(self, league_id: int) -> League:
18471848
18481849
Returns
18491850
--------
1850-
:class:`League`
1851+
:class:`BaseLeague`
18511852
The league with the requested ID
18521853
"""
18531854
data = await self.http.get_capital_league(league_id)
1854-
return League(data=data, client=self)
1855+
return BaseLeague(data=data, client=self)
18551856

1856-
async def get_capital_league_named(self, league_name: str) -> Optional[League]:
1857+
async def get_capital_league_named(self, league_name: str) -> Optional[BaseLeague]:
18571858
"""Get a capital league by name.
18581859
18591860
This is somewhat equivalent to
@@ -1879,7 +1880,7 @@ async def get_capital_league_named(self, league_name: str) -> Optional[League]:
18791880
18801881
Returns
18811882
--------
1882-
:class:`League`
1883+
:class:`BaseLeague`
18831884
The first league matching the league name. Could be ``None`` if not found.
18841885
"""
18851886
return get(await self.search_capital_leagues(), name=league_name)

coc/miscmodels.py

Lines changed: 32 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,37 @@ def _from_data(self, data: dict) -> None:
242242
self.localised_name: str = data_get("localizedName")
243243

244244

245-
class League:
245+
class BaseLeague:
246+
"""Represents a basic league.
247+
248+
Attributes
249+
-----------
250+
id: :class:`int`: The league's unique ID
251+
name: :class:`str`: The league's name, as it appears in-game."""
252+
253+
__slots__ = (
254+
"id",
255+
"name",
256+
"_client"
257+
)
258+
259+
def __init__(self, *, data, client=None):
260+
# pylint: disable=invalid-name
261+
self.id: int = data["id"]
262+
self.name: str = data["name"]
263+
self._client = client
264+
265+
def __repr__(self):
266+
return "<%s id=%s name=%s>" % (self.__class__.__name__, self.id, self.name)
267+
268+
def __str__(self):
269+
return self.name
270+
271+
def __eq__(self, other):
272+
return isinstance(self, other.__class__) and other.id == self.id
273+
274+
275+
class League(BaseLeague):
246276
"""Represents a Clash of Clans League
247277
248278
Attributes
@@ -251,21 +281,12 @@ class League:
251281
:class:`int`: The league ID.
252282
name:
253283
:class:`str`: The league name.
254-
localised_name:
255-
:class:`str`: A localised name of the location. The extent of the use of this is unknown at present.
256-
localised_short_name:
257-
:class:`str`: A localised short name of the location. The extent of the use of this is unknown at present.
258284
icon:
259285
:class:`Icon`: The league's icon.
260286
"""
261287

262288
__slots__ = (
263-
"id",
264-
"name",
265-
"localised_short_name",
266-
"localised_name",
267289
"icon",
268-
"_client",
269290
)
270291

271292
def __str__(self):
@@ -279,17 +300,12 @@ def __eq__(self, other):
279300
return isinstance(other, self.__class__) and self.id == other.id
280301

281302
def __init__(self, *, data, client):
282-
self._client = client
303+
super().__init__(data=data, client=client)
283304
self._from_data(data)
284305

285306
def _from_data(self, data: dict) -> None:
286307
# pylint: disable=invalid-name
287308
data_get = data.get
288-
289-
self.id: int = data_get("id")
290-
self.name: str = data_get("name")
291-
self.localised_name: str = data_get("localizedName")
292-
self.localised_short_name: str = data_get("localizedShortName")
293309
self.icon = try_enum(Icon, data=data_get("iconUrls"), client=self._client)
294310

295311

@@ -613,63 +629,6 @@ def __init__(self, *, data, client):
613629
self.hall_level: int = data.get("districtHallLevel")
614630

615631

616-
class WarLeague:
617-
"""Represents a clan's CWL league.
618-
619-
Attributes
620-
-----------
621-
id: :class:`int`: The league's unique ID
622-
name: :class:`str`: The league's name, as it appears in-game."""
623-
624-
__slots__ = (
625-
"id",
626-
"name",
627-
)
628-
629-
def __init__(self, *, data):
630-
# pylint: disable=invalid-name
631-
self.id: int = data["id"]
632-
self.name: str = data["name"]
633-
634-
def __repr__(self):
635-
return "<%s id=%s name=%s>" % (self.__class__.__name__, self.id, self.name)
636-
637-
def __str__(self):
638-
return self.name
639-
640-
def __eq__(self, other):
641-
return isinstance(self, other.__class__) and other.id == self.id
642-
643-
644-
class BuilderBaseLeague:
645-
"""Represents a player's Builder Base league.
646-
647-
Attributes
648-
-----------
649-
id: :class:`int`: The league's unique ID
650-
name: :class:`str`: The league's name, as it appears in-game."""
651-
652-
__slots__ = (
653-
"id",
654-
"name",
655-
"_client"
656-
)
657-
658-
def __init__(self, *, data, client=None):
659-
# pylint: disable=invalid-name
660-
self.id: int = data["id"]
661-
self.name: str = data["name"]
662-
self._client = client
663-
664-
def __repr__(self):
665-
return "<%s id=%s name=%s>" % (self.__class__.__name__, self.id, self.name)
666-
667-
def __str__(self):
668-
return self.name
669-
670-
def __eq__(self, other):
671-
return isinstance(self, other.__class__) and other.id == self.id
672-
673632

674633
class ChatLanguage:
675634
"""Represents a clan's chat language.

0 commit comments

Comments
 (0)