From cb71cbe1d55054fa827bc706b8829ac54cc4e542 Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Sat, 13 Aug 2022 20:08:57 -0400 Subject: [PATCH] Create update_username functions for category and table. --- classes/category.py | 24 ++++++++++++++++++++++++ classes/guild.py | 4 ++-- classes/table.py | 21 +++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/classes/category.py b/classes/category.py index e442f8b..dd34dcd 100644 --- a/classes/category.py +++ b/classes/category.py @@ -188,3 +188,27 @@ class Category: str: string of json representation of Category """ return json.dumps(self.to_dict(), indent=4) + + def update_username(self, old_username: str, new_username: str) -> bool: + """ + Checks score map for old username. Pops old username to remove from map. + Updates score's username and calls self.add_score with new score to add back + to map and sort. + + Args: + old_username (str): Username to replace + new_username (str): Username to replace with + + Returns: + bool: True indicating a score was updated, False otherwise + """ + + ret_val: bool = False + + if old_username in self._scores_map: + ret_val = True + score: Score = self._scores_map.pop(old_username) + score.user = new_username + self.add_score(score) + + return ret_val diff --git a/classes/guild.py b/classes/guild.py index da7ad63..dc7ede4 100644 --- a/classes/guild.py +++ b/classes/guild.py @@ -216,8 +216,8 @@ class Guild: to the table function to update username. Args: - old_username (str): Username to replace. Sent to table function - new_username (str): Username to replace with. Sent to table function + old_username (str): Username to replace. Sent to Table function + new_username (str): Username to replace with. Sent to Table function Returns: list[Table]: All the tables that had a username updated diff --git a/classes/table.py b/classes/table.py index 0f2af7e..3d3cc22 100644 --- a/classes/table.py +++ b/classes/table.py @@ -162,3 +162,24 @@ class Table: str: string of json representation of Table """ return json.dumps(self.to_dict(), indent=4) + + def update_username(self, old_username: str, new_username: str) -> bool: + """ + Calls Category update_username function for each category. Returns true if a category + was updated to inform if the table message needs rewritten. + + Args: + old_username (str): Username to replace. Sent to Category function + new_username (str): Username to replace with. Sent to Category function + + Returns: + bool: True if category was updated and requires message update + """ + + ret_val: bool = False + + for category in self.categories: + if category.update_username(old_username, new_username): + ret_val &= True + + return ret_val