Fixed sorting on either int or time record.

This commit is contained in:
alwayscompiling
2022-07-03 09:54:14 -04:00
parent c45606a7a4
commit 9e19e10b88
4 changed files with 33 additions and 39 deletions
+22 -29
View File
@@ -9,9 +9,7 @@ from utils import highscore_message # pylint: disable=import-error
from utils import data_storage # pylint: disable=import-error
async def submit_score_int(
self, ctx: commands.Context, boss_name: str, category: str, user: str, score: int
):
async def submit_score(self, ctx: commands.Context, boss_name: str, category: str, score: str):
"""
Adds a score to the high scores and sorts. Removes a score if larger than size limit.
@param boss: the boss to add score to
@@ -20,11 +18,29 @@ async def submit_score_int(
@param score: score (int) to add
"""
# TODO update user submission to not have dupe users
# create score tuple for either time or int
# define sort index
if highscores_config["categories"][category]["is_time_record"]:
score_list = score.split(":")
minutes = score_list[0]
seconds = score_list[1]
score_seconds = int(minutes) * 60 + int(seconds)
score_tuple = (ctx.author.display_name, score, score_seconds)
sort_index = 2
else:
score_tuple = (ctx.author.display_name, int(score))
sort_index = 1
scores = highscores_data[boss_name]["categories"][category]
score_tuple = (user, score)
scores.append(score_tuple)
ascending = highscores_config["categories"][category]
scores.sort(key=lambda x: x[1], reverse=not ascending)
# sort scores.
ascending = highscores_config["categories"][category]["ascending"]
scores.sort(key=lambda x: x[sort_index], reverse=not ascending)
# enforce highscore size
while len(scores) > highscores_config["highscore_size"]:
scores.pop()
highscores_data[boss_name]["categories"][category] = scores
@@ -42,26 +58,3 @@ async def submit_score_int(
# save data
data_storage.save_highscores_data(highscores_data)
def submit_score_str(boss: str, category: str, user: str, score: str):
"""
Adds a score to the high scores and sorts. Removes a score if larger than size limit.
@param boss: the boss to add score to
@param category: the category to add score to
@param user: user who submitted score
@param score: score (string) to add
"""
# TODO update user submission to not have dupe users
# TODO sort the strings properly, update this function for string usage
scores = highscores_data[boss][category]
score_tuple = (user, score)
scores.append(score_tuple)
ascending = highscores_config["categories"][category]
scores.sort(key=lambda x: x[1], reverse=not ascending)
while len(scores) > highscores_config["highscore_size"]:
scores.pop()
highscores_data[boss][category] = scores
# save data
data_storage.save_highscores_data(highscores_data)