Skip to content

Commit e4bfbb4

Browse files
committed
feature: add custom class support for war-related entities
1 parent 6390c3a commit e4bfbb4

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

coc/war_clans.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ class WarClan(BaseClan):
6666
:class:`int`: Total attacks used by clan this war.
6767
max_stars:
6868
:class:`int`: Total possible stars achievable.
69+
total_attacks:
70+
:class:`int`: Total possible number of attacks usable by clan this war
71+
member_cls: :class:`coc.ClanMember`
72+
The type which the members found in :attr:`Clan.members` will be of.
73+
Ensure any overriding of this inherits from :class:`coc.ClanMember`.
6974
"""
7075

7176
__slots__ = (
@@ -76,6 +81,7 @@ class WarClan(BaseClan):
7681
"exp_earned",
7782
"max_stars",
7883
"total_attacks",
84+
"member_cls",
7985
"_war",
8086
"_client",
8187

@@ -86,9 +92,10 @@ class WarClan(BaseClan):
8692
"_cs_members",
8793
)
8894

89-
def __init__(self, *, data, client, war):
95+
def __init__(self, *, data, client, war, **kwargs):
9096
self._war = war
9197
self._members = {}
98+
self.member_cls = kwargs.pop('member_cls', ClanWarMember)
9299

93100
super().__init__(data=data, client=client)
94101
self._from_data(data)
@@ -110,7 +117,7 @@ def _from_data(self, data: dict) -> None:
110117
self.total_attacks: int = (data_get("teamSize") or 0) * 3
111118

112119
self._iter_members = (
113-
ClanWarMember(data=mdata, client=self._client, war=self._war, clan=self)
120+
self.member_cls(data=mdata, client=self._client, war=self._war, clan=self)
114121
for mdata in data_get("members", [])
115122
)
116123

coc/war_members.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class ClanWarMember(BasePlayer):
5252
:class:`ClanWar`: The current war this member is in.
5353
clan:
5454
:class:`WarClan`: The member's clan.
55+
attack_cls: :class:`coc.WarAttack`
56+
The type which the attacks in :attr:`ClanWarMember.attacks` will be of.
57+
Ensure any overriding of this inherits from :class:`coc.WarAttack`.
5558
"""
5659

5760
__slots__ = (
@@ -65,16 +68,18 @@ class ClanWarMember(BasePlayer):
6568
"map_position",
6669
"war",
6770
"clan",
71+
"attack_cls",
6872
"_client",
6973
"_attacks",
7074
)
7175

72-
def __init__(self, *, data, client, war, clan):
76+
def __init__(self, *, data, client, war, clan, **kwargs):
7377
super().__init__(data=data, client=client)
7478
self._client = client
7579
self._attacks = []
7680
self.war = war # type: ClanWar
7781
self.clan = clan # type: WarClan
82+
self.attack_cls = kwargs.pop('attack_cls', WarAttack)
7883
self._from_data(data)
7984

8085
def _from_data(self, data):
@@ -87,7 +92,7 @@ def _from_data(self, data):
8792
self.defense_count: int = data_get("opponentAttacks")
8893

8994
self.__iter_attacks = (
90-
WarAttack(data=adata, client=self._client, war=self.war) for adata in data_get("attacks", [])
95+
self.attack_cls(data=adata, client=self._client, war=self.war) for adata in data_get("attacks", [])
9196
)
9297

9398
self._best_opponent_attacker: str = data_get("bestOpponentAttack", {}).get("attackerTag")

coc/wars.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ class ClanWar:
6666
:class:`ClanWarLeagueGroup`: The war's league group. This is ``None`` unless this is a Clan League War.
6767
attacks_per_member:
6868
:class:`int`: The number of attacks each member has this war.
69+
clan_cls: :class:`WarClan`
70+
the type `war.clan` and `war.opponent` will be of.
71+
Ensure any overriding of this inherits from :class:`coc.WarClan`.
6972
"""
7073

7174
__slots__ = (
@@ -81,6 +84,7 @@ class ClanWar:
8184
"opponent",
8285
"league_group",
8386
"attacks_per_member",
87+
"clan_cls",
8488
"_response_retry",
8589
"_raw_data"
8690
)
@@ -90,6 +94,7 @@ def __init__(self, *, data, client, **kwargs):
9094
self._client = client
9195
self._raw_data = data if client and client.raw_attribute else None
9296
self.clan_tag = kwargs.pop("clan_tag", None)
97+
self.clan_cls = kwargs.pop('clan_cls', WarClan)
9398
self._from_data(data)
9499

95100
self.clan_tag = self.clan and self.clan.tag or self.clan_tag
@@ -117,14 +122,14 @@ def _from_data(self, data: dict) -> None:
117122
# depending on the way the game stores it internally. This isn't very helpful as we always want it
118123
# from the perspective of the tag we provided, so switch them around if it isn't correct.
119124
if clan_data and clan_data.get("tag", self.clan_tag) == self.clan_tag:
120-
self.clan = try_enum(WarClan, data=clan_data, client=self._client,
125+
self.clan = try_enum(self.clan_cls, data=clan_data, client=self._client,
121126
war=self)
122-
self.opponent = try_enum(WarClan, data=data_get("opponent"),
127+
self.opponent = try_enum(self.clan_cls, data=data_get("opponent"),
123128
client=self._client, war=self)
124129
else:
125-
self.clan = try_enum(WarClan, data=data_get("opponent"),
130+
self.clan = try_enum(self.clan_cls, data=data_get("opponent"),
126131
client=self._client, war=self)
127-
self.opponent = try_enum(WarClan, data=clan_data,
132+
self.opponent = try_enum(self.clan_cls, data=clan_data,
128133
client=self._client, war=self)
129134

130135
@property

0 commit comments

Comments
 (0)