Skip to content

Clan war log

ClanWarLogRequest

Bases: IterRequestModel

Retrieve clan's clan war log.

Source code in pyclasher/api/requests/clan_war_log.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
class ClanWarLogRequest(IterRequestModel):
    """
    Retrieve clan's clan war log.
    """
    clan_tag: str = None
    _iter_rtype = ClanWarLogEntry
    _list_rtype = ClanWarLog
    __Criteria = Literal["team_size", "attacks_per_member", "result"]

    def __init__(self, clan_tag, limit=None, after=None, before=None):
        """
        initialisation of the clan request
        :param clan_tag:    Tag of the clan.
        :type clan_tag:     str
        :param limit:       Limit the number of items returned in the response.
        :type limit:        int
        :param after:       Return only items that occur after this marker. Before marker can be found from the response,
                            inside the 'paging' property. Note that only after or before can be specified for a request, not both.
        :type after:        str
        :param before:      Return only items that occur before this marker. Before marker can be found from the response,
                            inside the 'paging' property. Note that only after or before can be specified for a request, not both.
        :type before:       str
        """

        self.clan_tag = clan_tag
        IterRequestModel.__init__(self,
                                  "clans/{clan_tag}/warlog",
                                  clan_tag=self.clan_tag,
                                  kwargs={
                                      'limit': limit,
                                      'after': after,
                                      'before': before
                                  })
        return

    @staticmethod
    def __sort_key(item, key):
        if key == "result":
            if item[snake_to_camel(key)] == ClanWarResult.WIN.value:
                return 3
            if item[snake_to_camel(key)] == ClanWarResult.LOSE.value:
                return 1
            if item[snake_to_camel(key)] == ClanWarResult.TIE.value:
                return 2
            if item[snake_to_camel(key)] == ClanWarResult.NONE.value:
                return 0
        else:
            return item[snake_to_camel(key)]

    def sort(self, criteria, descending=True):
        if not isinstance(self._data, dict):
            raise RequestNotDone
        self._data['items'] = sorted(
            self._data['items'],
            key=lambda war: self.__sort_key(war, criteria),
            reverse=descending
        )
        return

    def filter(self, criteria, value):
        if isinstance(value, ClanWarResult):
            value = value.value

        self._data['items'] = [war
                               for war in self._data['items']
                               if war[snake_to_camel(criteria)] == value]
        return

    @property
    def average_team_size(self):
        return self.items.average_team_size

    @property
    def average_destruction_percentage(self):
        return self.items.average_destruction_percentage

    @property
    def average_attacks(self):
        return self.items.average_attacks

    @property
    def average_stars(self):
        return self.items.average_stars

    @property
    def average_exp_earned(self):
        return self.items.average_exp_earned

__init__(clan_tag, limit=None, after=None, before=None)

initialisation of the clan request :param clan_tag: Tag of the clan. :type clan_tag: str :param limit: Limit the number of items returned in the response. :type limit: int :param after: Return only items that occur after this marker. Before marker can be found from the response, inside the 'paging' property. Note that only after or before can be specified for a request, not both. :type after: str :param before: Return only items that occur before this marker. Before marker can be found from the response, inside the 'paging' property. Note that only after or before can be specified for a request, not both. :type before: str

Source code in pyclasher/api/requests/clan_war_log.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def __init__(self, clan_tag, limit=None, after=None, before=None):
    """
    initialisation of the clan request
    :param clan_tag:    Tag of the clan.
    :type clan_tag:     str
    :param limit:       Limit the number of items returned in the response.
    :type limit:        int
    :param after:       Return only items that occur after this marker. Before marker can be found from the response,
                        inside the 'paging' property. Note that only after or before can be specified for a request, not both.
    :type after:        str
    :param before:      Return only items that occur before this marker. Before marker can be found from the response,
                        inside the 'paging' property. Note that only after or before can be specified for a request, not both.
    :type before:       str
    """

    self.clan_tag = clan_tag
    IterRequestModel.__init__(self,
                              "clans/{clan_tag}/warlog",
                              clan_tag=self.clan_tag,
                              kwargs={
                                  'limit': limit,
                                  'after': after,
                                  'before': before
                              })
    return