Skip to content
This repository has been archived by the owner on Jul 14, 2024. It is now read-only.

Format code with yapf #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
43 changes: 27 additions & 16 deletions clashstat.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import coc #type: ignore
import nest_asyncio #type: ignore
import coc #type: ignore
import nest_asyncio #type: ignore
import asyncio
from typing import List, Dict, Optional, Union, Any


class PlayerStats:
def __init__(self, ID: str, Password: str, filename: str) -> None:
nest_asyncio.apply()
Expand All @@ -28,7 +29,10 @@ async def GetUserTrophies(self) -> Dict[str, Dict[str, Union[str, int]]]:
return list of dict[playertag, dict[player name, tag, trophies]]
why?: to find playertag within O(1)
"""
tasks = list(map(lambda player: asyncio.create_task(self.client.get_player(player)), self.PlayersTag))
tasks = list(
map(
lambda player: asyncio.create_task(
self.client.get_player(player)), self.PlayersTag))

PlayersInfo = await asyncio.gather(*tasks)

Expand Down Expand Up @@ -59,46 +63,52 @@ def ComparePlayerData(self, NewPlayersInfo: Dict[str, Dict[str, Union[str, int]]
def IsItSameTrophies(profile_1: Optional[Dict[str, Union[str, int]]],\
profile_2: Optional[Dict[str, Union[str, int]]]) -> bool:

if isinstance(profile_1, type(None)) or isinstance(profile_2, type(None)):
if isinstance(profile_1, type(None)) or isinstance(
profile_2, type(None)):
return False
return profile_1.get('trophies') == profile_2.get('trophies') #type: ignore

return profile_1.get('trophies') == profile_2.get(
'trophies') #type: ignore

#If nothing is in the prev player info list
#return every player info
if len(self.PrevPlayersFullInfo.keys()) == 0:
return NewPlayersInfo

UpdateRequiredInfo: Dict[str, Dict[str, Union[str, int]]] = {}
for tag in self.PlayersTag:
if not IsItSameTrophies(NewPlayersInfo.get(tag), self.PrevPlayersFullInfo.get(tag)):
if not IsItSameTrophies(NewPlayersInfo.get(tag),
self.PrevPlayersFullInfo.get(tag)):
UpdateRequiredInfo[tag] = NewPlayersInfo[tag]

return UpdateRequiredInfo

def FindTrophyDifferenceAndUpdate(self, NewPlayersInfo: Dict[str, Dict[str, Union[str, int]]]) -> Dict[str, Dict[str, Union[str, int]]]:
def FindTrophyDifferenceAndUpdate(
self, NewPlayersInfo: Dict[str, Dict[str, Union[str, int]]]
) -> Dict[str, Dict[str, Union[str, int]]]:
"""
Get the difference in trophy and return the information
"""
if len(self.PrevPlayersFullInfo.keys()) == 0:
self.PrevPlayersFullInfo = NewPlayersInfo #type: ignore
self.PrevPlayersFullInfo = NewPlayersInfo #type: ignore
return {}

def FindTrophyDifference(CurrInfo, PastInfo):
if isinstance(CurrInfo, type(None)) or isinstance(PastInfo, type(None)):
if isinstance(CurrInfo, type(None)) or isinstance(
PastInfo, type(None)):
print(f"Error!: CurrInfo: {CurrInfo}, PrevInfo: {PastInfo}")
return 0
return CurrInfo.get('trophies') - PastInfo.get('trophies')

TrophyDifferenceCollection = {}
for tag in NewPlayersInfo.keys():
TrophyDifference = FindTrophyDifference(NewPlayersInfo.get(tag), self.PrevPlayersFullInfo.get(tag))
TrophyDifference = FindTrophyDifference(
NewPlayersInfo.get(tag), self.PrevPlayersFullInfo.get(tag))
TrophyDifferenceCollection[tag] = \
{'trophies': TrophyDifference, 'name': NewPlayersInfo[tag].get('name'), 'tag': tag}


self.PrevPlayersFullInfo = NewPlayersInfo #type: ignore
self.PrevPlayersFullInfo = NewPlayersInfo #type: ignore
return TrophyDifferenceCollection

async def Run(self):
"""
IMPORTANT: Call self.GetPlayerList() first before calling this function.
Expand All @@ -111,7 +121,8 @@ async def Run(self):
print(self.PrevPlayersFullInfo)
DifferenceDetectedPlayers = self.ComparePlayerData(NewPlayersInfo)
print("Compare Player Data")
TrophyDifference = self.FindTrophyDifferenceAndUpdate(DifferenceDetectedPlayers)
TrophyDifference = self.FindTrophyDifferenceAndUpdate(
DifferenceDetectedPlayers)
print("Find Trophy Difference")
print(TrophyDifference)
return TrophyDifference
return TrophyDifference