Fill out HighscoreData class with access functions for utils.py.
This commit is contained in:
+115
-11
@@ -38,23 +38,127 @@ class HighscoresData:
|
||||
|
||||
self._data[str(guild_id)] = skeleton
|
||||
|
||||
def get_guild_data(self, guild_id: int) -> dict:
|
||||
def add_score(
|
||||
self, guild_id: int, boss_name: str, category: str, user: str, score: str
|
||||
) -> None:
|
||||
"""
|
||||
Gets guild data from the guild id.
|
||||
Adds a new score to guilds highscore data.
|
||||
Ensures that there is no user duplicate and then sorts the scores
|
||||
|
||||
@param guild_id: guild id to retrieve data for
|
||||
@returns: dict of guild's highscore data
|
||||
@param guild_id: guild id to retrieve from.
|
||||
@param boss_name: boss to add score to.
|
||||
@param category: category to add score to.
|
||||
@param user: user that submitted score.
|
||||
@param score: score to add.
|
||||
"""
|
||||
return self._data[str(guild_id)]
|
||||
scores: "list[tuple]" = self._data[str(guild_id)]["tables"][boss_name]["categories"][
|
||||
category
|
||||
]
|
||||
ascending: bool = self._config["categories"][category]["ascending"]
|
||||
time_record: bool = self._config["categories"][category]["is_time_record"]
|
||||
|
||||
def set_guild_data(self, guild_id: int, data: dict) -> None:
|
||||
"""
|
||||
Sets guild data for given guild id, saves data.
|
||||
# create score tuple for either time or int
|
||||
# define sort index
|
||||
if time_record:
|
||||
if score.find(":") == -1:
|
||||
score_seconds = float(score)
|
||||
else:
|
||||
minutes = score.split(":")[0]
|
||||
seconds = score.split(":")[1]
|
||||
score_seconds = int(minutes) * 60 + float(seconds)
|
||||
score_tuple = (user, score, score_seconds)
|
||||
sort_index = 2
|
||||
else:
|
||||
score_tuple = (user, int(score))
|
||||
sort_index = 1
|
||||
|
||||
@param guild_id: guild id to save data for
|
||||
@param data: data to set and save
|
||||
# if user is in an existing tuple, delete it.
|
||||
users_scores: "list[tuple]" = [score_tuple]
|
||||
for tup in scores:
|
||||
if user == tup[0]:
|
||||
# Add tup to users_scores, remove from scores
|
||||
users_scores.append(tup)
|
||||
|
||||
# sort users_scores
|
||||
users_scores.sort(
|
||||
key=lambda x: x[sort_index],
|
||||
reverse=not ascending,
|
||||
)
|
||||
|
||||
for index, tup in enumerate(users_scores):
|
||||
if index == 0:
|
||||
# first score is the best score.
|
||||
if tup not in scores:
|
||||
scores.append(tup)
|
||||
else:
|
||||
# discard all other scores from scores list.
|
||||
if tup in scores:
|
||||
scores.remove(tup)
|
||||
|
||||
# sort scores.
|
||||
scores.sort(
|
||||
key=lambda x: x[sort_index],
|
||||
reverse=not ascending,
|
||||
)
|
||||
|
||||
self._data[str(guild_id)]["tables"][boss_name]["categories"][category] = scores
|
||||
save_highscores_data(self._data)
|
||||
|
||||
def get_categories_data(self, guild_id: int, table: str) -> "list[tuple]":
|
||||
"""
|
||||
self._data[str(guild_id)] = data
|
||||
Returns categories data in a list of tuples. Calls dict function items().
|
||||
|
||||
@param guild_id: guild's id to retrieve from.
|
||||
@param table: table to get category data from
|
||||
@returns: list of tuples containing all category and relative score data.
|
||||
"""
|
||||
return self._data[str(guild_id)][table]["categories"].items()
|
||||
|
||||
def get_config_highscore_size(self) -> int:
|
||||
"""
|
||||
Gets the defined size of highscore for config.
|
||||
|
||||
@returns: int highscore size
|
||||
"""
|
||||
return self._config["highscore_size"]
|
||||
|
||||
def get_guild_channel_id(self, guild_id: int) -> int:
|
||||
"""
|
||||
Returns the channel id to post highscore messages to.
|
||||
|
||||
@param guild_id: guild id to retrieve from
|
||||
"""
|
||||
return self._data[str(guild_id)]["highscore_channel_id"]
|
||||
|
||||
def get_guild_username_length(self, guild_id: int) -> int:
|
||||
"""
|
||||
Returns username length as set by each guild.
|
||||
|
||||
@param guild_id: guild id to retrieve from.
|
||||
@returns: int of username length.
|
||||
"""
|
||||
return self._data[str(guild_id)]["username_length"]
|
||||
|
||||
def get_table_message_id(self, guild_id: int, table: str) -> int:
|
||||
"""
|
||||
Returns the message id for given guild's table.
|
||||
|
||||
@param guild_id: guild id to retrieve from.
|
||||
@param table: the table to retreive from.
|
||||
@returns: int id of message for table's highscores.
|
||||
"""
|
||||
return self._data[str(guild_id)]["tables"][table]["message_id"]
|
||||
|
||||
def set_table_message_id(self, guild_id: int, table: str, message_id: int) -> None:
|
||||
"""
|
||||
Sets the message id for given guild's table.
|
||||
Saves highscores data.
|
||||
|
||||
@param guild_id: guild id to retrieve from.
|
||||
@param table: the table to retreive from.
|
||||
@param message_id: message id to set.
|
||||
"""
|
||||
self._data[str(guild_id)]["tables"][table]["message_id"] = message_id
|
||||
save_highscores_data(self._data)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user