Skip to content

Clan war log

ClanWarLog

Bases: IterBaseModel

clan war log model

can be iterated over

Source code in pyclasher/api/models/clan_war_log.py
 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
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
class ClanWarLog(IterBaseModel):
    _iter_rtype = ClanWarLogEntry
    __Criteria = Literal["team_size", "attacks_per_member", "result"]

    @property
    def average_team_size(self):
        try:
            return sum((war.team_size for war in self
                        if war.attacks_per_member == 2)) / len(self)
        except ZeroDivisionError:
            return None

    @property
    def average_destruction_percentage(self):
        try:
            return sum((war.clan.destruction_percentage for war in self
                        if war.attacks_per_member == 2)) / len(self)
        except ZeroDivisionError:
            return None

    @property
    def average_attacks(self):
        try:
            return sum((war.clan.attacks for war in self
                        if war.attacks_per_member == 2)) / len(self)
        except ZeroDivisionError:
            return None

    @property
    def average_stars(self):
        try:
            return sum((war.clan.stars for war in self
                        if war.attacks_per_member == 2)) / len(self)
        except ZeroDivisionError:
            return None

    @property
    def average_exp_earned(self):
        try:
            return sum((war.clan.exp_earned for war in self
                        if war.attacks_per_member == 2)) / len(self)
        except ZeroDivisionError:
            return None

    @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.to_dict_list(), list):
            raise PyClasherException("no value for `self._data`")
        self._data = sorted(self._data,
                            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 = [war
                      for war in self.to_dict_list()
                      if war[snake_to_camel(criteria)] == value]
        return

ClanWarLogEntry

Bases: BaseModel

clan war log entry model

Source code in pyclasher/api/models/clan_war_log.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class ClanWarLogEntry(BaseModel):
    @property
    def clan(self):
        return WarClan(self._get_data('clan'))

    @property
    def opponent(self):
        return WarClan(self._get_data('opponent'))

    @property
    def team_size(self):
        return self._get_data('teamSize')

    @property
    def attacks_per_member(self):
        return self._get_data('attacksPerMember')

    @property
    def end_time(self):
        return Time.from_str(self._get_data('endTime'))

    @property
    def result(self):
        return ClanWarResult(self._get_data('result'))

attacks_per_member: int property

attack count per member

:return: the attack count per member (usually 2 for regular war and 1 for clan war league) :rtype: int

clan: WarClan property

clan of the clan war

:return: the clan of the clan war :rtype: WarClan

end_time: Time property

end time of the clan war

:return: the end time of the clan war :rtype: Time

opponent: WarClan property

opponent of the clan war

:return: the opponent of the clan war :rtype: WarClan

result: ClanWarResult property

result of the clan war

:return: the result of the clan war :rtype: ClanWarResult

team_size: int property

clan war team size

:return: the clan war team size :rtype: int