Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions coc/entry_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,17 @@ async def __anext__(self) -> Union[ClanWarLogEntry, RaidLogEntry]:

# If index request is within range of the war_logs, return item
if self._min_index <= self._async_index < self._max_index:
ret = self._logs[self._async_index % len(self._logs)]
ret = self._logs[self._async_index - self._min_index]

# Iteration has reached the end of the array, fetch the next
# set of logs from the endpoint
else:
elif self._next_page:
await self._paginate()
self._min_index = self._max_index
self._max_index = self._max_index + len(self._logs)
ret = self._logs[self._async_index % len(self._logs)]
ret = self._logs[self._async_index - self._min_index]
else:
raise StopAsyncIteration

self._async_index += 1
return self._model(data=ret, client=self._client)
Expand Down
7 changes: 2 additions & 5 deletions coc/raid.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def __repr__(self):
def __eq__(self, other):
if isinstance(other, RaidMember):
if (self.tag == other.tag
and self.name == other.name
and self.attack_count == other.attack_count
and self.attack_limit == other.attack_limit
and self.bonus_attack_limit == other.bonus_attack_limit
Expand Down Expand Up @@ -156,7 +155,6 @@ def __repr__(self):
def __eq__(self, other):
if isinstance(other, RaidAttack):
if (self.attacker_tag == other.attacker_tag
and self.attacker_name == other.attacker_name
and self.destruction == other.destruction
and self.raid_clan == other.raid_clan
and self.district == other.district
Expand Down Expand Up @@ -215,7 +213,7 @@ def __eq__(self, other):
and self.destruction == other.destruction
and self.looted == other.looted
and self.raid_clan == other.raid_clan
and self.attacks == other.attack_count
and self.attack_count == other.attack_count
):
return True
return False
Expand Down Expand Up @@ -321,6 +319,7 @@ def __eq__(self, other):
and self.attack_count == other.attack_count
and self.district_count == other.district_count
and self.destroyed_district_count == other.destroyed_district_count
and self.raid_log_entry.start_time == other.raid_log_entry.start_time
and self.attacks == other.attacks)

def __repr__(self):
Expand Down Expand Up @@ -428,13 +427,11 @@ def __repr__(self):
def __eq__(self, other):
if isinstance(other, RaidLogEntry):
if (self.start_time == other.start_time
and self.end_time == other.end_time
and self.completed_raid_count == other.completed_raid_count
and self.destroyed_district_count == other.destroyed_district_count
and self.attack_count == other.attack_count
and self.attack_log == other.attack_log
and self.defense_log == other.defense_log
and self.members == other.members
):
return True

Expand Down
32 changes: 14 additions & 18 deletions examples/war_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os

import coc
from coc.raid import RaidLogEntry


async def get_warlog_for_clans(client: coc.Client, clan_tags: list):
Expand All @@ -23,14 +24,16 @@ async def test_raidlog(client: coc.Client, clan_tag: str):
# Limit is set to None retrieving all values
raid_no_page = await client.get_raidlog(clan_tag)
limit = len(raid_no_page)
page_limit = 11

# Enable pagination, by default it will only cache 10 logs using limit
# once you iterate beyond the cached amount, it will fetch the next set
raid_with_page = await client.get_raidlog(clan_tag, page=True, limit=11)
raid_with_page = await client.get_raidlog(clan_tag, page=True, limit=page_limit)

# Iterate over warlogs like the current version of coc.py
for i, e in enumerate(raid_no_page):
print(f"[{i}]-sync limit: {limit} page: False")
e: RaidLogEntry
print(f"[{i}]-sync limit: {limit} page: False {e.start_time.time}")

# Option to async for loop a non paginated object
count = 0
Expand All @@ -39,23 +42,19 @@ async def test_raidlog(client: coc.Client, clan_tag: str):
count += 1

for i, e in enumerate(raid_with_page):
print(f"[{i}]-sync limit: 10 page: True")
print(f"[{i}]-sync limit: {page_limit} page: True")

# Set `paginate=True` to enable fetching beyond the limit value until
# there are more values to fetch
count = 0
async for i in raid_with_page:
print(f"[{count}]-async limit: {limit} page: True")
print(f"[{count}]-async limit: {page_limit} page: True {i.start_time.time}")
count += 1
if count == 20:
break


# Simple test comparing the two data sets
count = 0
async for async_log in raid_with_page:
if count == limit:
break

if async_log != raid_no_page[count]:
raise AssertionError(f"{id(async_log)} does not match {id(raid_no_page[count])} at index {count}")
count += 1
Expand All @@ -65,10 +64,11 @@ async def test_warlog(client: coc.Client, clan_tag: str):
# Limit is set to None retrieving all values
warlogs_no_page = await client.get_warlog(clan_tag)
limit = len(warlogs_no_page)
pagination_limit = 11

# Enable pagination, by default it will only cache 10 logs using limit
# once you iterate beyond the cached amount, it will fetch the next set
warlogs_with_page = await client.get_warlog(clan_tag, page=True, limit=15)
warlogs_with_page = await client.get_warlog(clan_tag, page=True, limit=pagination_limit)

# Iterate over warlogs like the current version of coc.py
for i, e in enumerate(warlogs_no_page):
Expand All @@ -81,26 +81,22 @@ async def test_warlog(client: coc.Client, clan_tag: str):
count += 1

for i, e in enumerate(warlogs_with_page):
print(f"[{i}]-sync limit: 10 page: True")
print(f"[{i}]-sync limit: {pagination_limit} page: True")

# Set `paginate=True` to enable fetching beyond the limit value until
# there are more values to fetch
count = 0
async for i in warlogs_with_page:
print(f"[{count}]-async limit: {limit} page: True")
print(f"[{count}]-async limit: {pagination_limit} page: True {i.end_time.time}")
count += 1
if count == 20:
break

# Simple test comparing the two data sets
count = 0
async for async_log in warlogs_with_page:
if count == limit:
break

if async_log != warlogs_no_page[count]:
raise AssertionError(f"{id(async_log)} does not match {id(warlogs_no_page[count])} at index {count}")
count += 1
print(count)



Expand Down Expand Up @@ -140,7 +136,7 @@ async def main():

# await get_warlog_opponents_from_clan_name(coc_client, "Reddit Zulu", 5)
await test_warlog(coc_client, "#2Y28CGP8")
# await test_raidlog(coc_client, "#2Y28CGP8")
await test_raidlog(coc_client, "#2Y28CGP8")
await coc_client.close()


Expand Down