Skip to content

Commit

Permalink
fix: TTTK-55 statistics.db: getters raise now an ValueError if profil…
Browse files Browse the repository at this point in the history
…e doesn't exist instead of returning None, delete_statistics(uuid:str) and reset_statistics check for existance of stats, otherwise throws ValueError
  • Loading branch information
bananabr3d committed Mar 7, 2024
1 parent 2de511d commit 7fca9c2
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions Server/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ def delete_statistics(self, uuid: str) -> None:
DELETE FROM statistics
WHERE uuid = '{uuid}'
""")
else:
raise ValueError(f'Statistics for uuid: {uuid} does not exist')

def delete_all_statistics(self):
"""
Expand All @@ -127,7 +129,7 @@ def delete_all_statistics(self):
"""
def reset_statistics(self, uuid: str, arg: str):
"""
Resets the statistics of a profile by its uuid
Resets an argument value of a profile by its uuid to 0
:param uuid: uuid of the profile that is reset
:param arg: statistics that is get reseted, hast to be one of those:
'wins_first', 'wins_second', 'loses_first', 'loses_second', 'draws_first', 'draws_second', 'emojis', 'moves'
Expand All @@ -136,13 +138,15 @@ def reset_statistics(self, uuid: str, arg: str):
if arg not in ['wins_first', 'wins_second' 'loses_first', 'loses_second',
'draws_first', 'draws_secons', 'emojis', 'moves']:
raise ValueError('Invalid type')
with self.conn:
self.cursor.execute(f"""
UPDATE statistics
SET {arg} = 0
WHERE uuid = '{uuid}'
""")

if self._check_profile(uuid):
with self.conn:
self.cursor.execute(f"""
UPDATE statistics
SET {arg} = 0
WHERE uuid = '{uuid}'
""")
else:
raise ValueError(f'Statistics for uuid: {uuid} does not exist')
"""
________Get Methods___________
"""
Expand All @@ -169,7 +173,7 @@ def get_winrates(self, uuid: str, type: str = "all") -> float:
elif type == 'second':
return profile[2] / (profile[2] + profile[4])
else:
return None
raise ValueError(f'Statistics for uuid: {uuid} does not exist')

def get_emojis(self, uuid: str) -> int:
"""
Expand All @@ -181,7 +185,7 @@ def get_emojis(self, uuid: str) -> int:
profile = self._get_profile(uuid)
return profile[8]
else:
return None
raise ValueError(f'Statistics for uuid: {uuid} does not exist')

def get_moves(self, uuid: str) -> int:
"""
Expand Down Expand Up @@ -215,7 +219,7 @@ def get_wins(self, uuid: str, type='all') -> int:
elif type == 'second':
return profile[2]
else:
return None
raise ValueError(f'Statistics for uuid: {uuid} does not exist')

def get_loses(self, uuid: str, type='all') -> int:
"""
Expand All @@ -237,7 +241,7 @@ def get_loses(self, uuid: str, type='all') -> int:
elif type == 'second':
return profile[4]
else:
return None
raise ValueError(f'Statistics for uuid: {uuid} does not exist')

def get_draws(self, uuid: str, type: str = 'all') -> int:
"""
Expand All @@ -259,7 +263,7 @@ def get_draws(self, uuid: str, type: str = 'all') -> int:
elif type == 'second':
return profile[6]
else:
return None
raise ValueError(f'Statistics for uuid: {uuid} does not exist')

"""
________Private Methods_________
Expand Down Expand Up @@ -353,6 +357,8 @@ def _get_profile(self, uuid: str) -> tuple:
:param uuid: uuid of the profile that is searched
:return: profile statistics as a tuple
"""
if not self._check_profile(uuid):
raise ValueError(f'Statistics for uuid: {uuid} does not exist')
with self.conn:
self.cursor.execute(f"""
SELECT * FROM statistics
Expand Down Expand Up @@ -397,7 +403,7 @@ def _add_profile(self, uuid: str, wins_first: int = 0, wins_second: int = 0,
""", {'uuid': uuid, 'wins_first': wins_first, 'wins_second': wins_second,
'loses_first': loses_first, 'loses_second': loses_second, 'draws_first': draws_first, 'draws_second': draws_second, 'moves': moves, 'emojis': emojis})

# statistics = Statistics()
statistics = Statistics()
# statistics.update_statistics("test", "draws_first", 5)
# statistics.count_emojis("test", "🤔 🙈 me así, se 😌 ds 💕👭👙 hello 👩🏾‍🎓 emoji hello 👨‍👩‍👦‍👦 how are 😊 you today🙅🏽🙅🏽")
# print(statistics.get_statistics("test"))
Expand Down

0 comments on commit 7fca9c2

Please sign in to comment.