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
+2 -4
View File
@@ -15,7 +15,7 @@ class RegisterHighscoreChannel(commands.Cog, name="Score"):
self.bot = bot
@commands.command(name="score")
async def score(self, ctx: commands.Context, boss_name: str, category: str, score):
async def score(self, ctx: commands.Context, boss_name: str, category: str, score: str):
"""A command which calls a function to submit the score to highscores. Gathers required
information from command and passes on.
Usage:
@@ -23,9 +23,7 @@ class RegisterHighscoreChannel(commands.Cog, name="Score"):
?score "Boss Name" "Category" "score"
```
"""
await highscore_manage.submit_score_int(
self, ctx, boss_name, category, ctx.author.display_name, score
)
await highscore_manage.submit_score(self, ctx, boss_name, category, score)
# This function will be called when this extension is loaded.
+6 -3
View File
@@ -184,15 +184,18 @@
"categories": {
"Kill Count": {
"hardmode": true,
"ascending": false
"ascending": false,
"is_time_record": false
},
"Kill Time": {
"hardmode": true,
"ascending": true
"ascending": true,
"is_time_record": true
},
"Spooned Log": {
"hardmode": false,
"ascending": true
"ascending": true,
"is_time_record": false
}
},
"highscore_size": 3
+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)
+3 -3
View File
@@ -18,7 +18,7 @@ def format_highscore_message(boss_name: str):
# Gather all categories and add to string
for key, value in boss_data["categories"].items():
ret_string += f"{key}".ljust(30, " ")
ret_string += f"{key}".ljust(50, " ")
ret_string += "\n"
highscore_size = highscores_config["highscore_size"]
@@ -28,9 +28,9 @@ def format_highscore_message(boss_name: str):
for key, value in boss_data["categories"].items():
if len(value) > i:
score = value[i]
ret_string += f"{i+1}: {score[0]} - {score[1]}".ljust(30, " ")
ret_string += f"{i+1}: {score[0]} - {score[1]}".ljust(50, " ")
else:
ret_string += f"{i+1}: submit your score".ljust(30, " ")
ret_string += f"{i+1}: submit your score".ljust(50, " ")
ret_string += "\n"
return ret_string