Skip to content

Commit 5e51714

Browse files
authored
Merge pull request #221 from mathsman5133/feature/custom-classes
Feature/custom classes
2 parents 0e5ee90 + e56fbec commit 5e51714

File tree

7 files changed

+44
-12
lines changed

7 files changed

+44
-12
lines changed

coc/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
SOFTWARE.
2323
"""
2424

25-
__version__ = "3.1.0"
25+
__version__ = "3.2.0"
2626

2727
from .abc import BasePlayer, BaseClan
2828
from .clans import RankedClan, Clan
@@ -54,7 +54,7 @@
5454
GatewayError,
5555
PrivateWarLog,
5656
)
57-
from .hero import Hero, Pet
57+
from .hero import Equipment, Hero, Pet
5858
from .http import BasicThrottler, BatchThrottler, HTTPClient
5959
from .iterators import (
6060
ClanIterator,

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

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
project = 'coc'
1616
copyright = '2022, mathsman5133'
1717
author = 'mathsman5133'
18-
release = '3.1.0'
18+
release = '3.2.0'
1919

2020
# -- General configuration ---------------------------------------------------
2121
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

docs/miscellaneous/changelog.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@ Changelog
77
This page keeps a fairly detailed, human readable version
88
of what has changed, and whats new for each version of the lib.
99

10+
v3.2.0
11+
------
12+
13+
Additions:
14+
~~~~~~~~~~
15+
- Added custom class support to a few more classes:
16+
17+
- :class:`ClanWarMember` now supports the :attr:`attack_cls` attribute to customize the type returned in
18+
:func:`ClanWarMember.attacks` (and, by extension also :func:`WarClan.attacks` and :func:`ClanWar.attacks`)
19+
- :class:`WarClan` now supports the :attr:`member_cls` attribute to customize the type returned in
20+
:func:`WarClan.members` (and, by extension also :func:`ClanWar.members`)
21+
- :class:`ClanWar` now supports the :attr:`clan_cls` attribute to customize the types of
22+
:func:`ClanWar.clan` and :func:`ClanWar.opponent`
23+
- the :class:`Equipment` class can now be imported as ``coc.Equipment``
24+
1025
v3.1.0
1126
------
1227

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ name = "coc.py"
77
authors = [{ name = "mathsman5133" }]
88
maintainers = [{ name = "majordoobie" }, { name = "MagicTheDev" }, { name = "Kuchenmampfer" },
99
{ name = "lukasthaler"}, { name = "doluk"}]
10-
version = "3.1.0"
10+
version = "3.2.0"
1111
description = "A python wrapper for the Clash of Clans API"
1212
requires-python = ">=3.7.3"
1313
readme = "README.rst"

0 commit comments

Comments
 (0)