Fixed sorting on either int or time record.
This commit is contained in:
@@ -15,7 +15,7 @@ class RegisterHighscoreChannel(commands.Cog, name="Score"):
|
|||||||
self.bot = bot
|
self.bot = bot
|
||||||
|
|
||||||
@commands.command(name="score")
|
@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
|
"""A command which calls a function to submit the score to highscores. Gathers required
|
||||||
information from command and passes on.
|
information from command and passes on.
|
||||||
Usage:
|
Usage:
|
||||||
@@ -23,9 +23,7 @@ class RegisterHighscoreChannel(commands.Cog, name="Score"):
|
|||||||
?score "Boss Name" "Category" "score"
|
?score "Boss Name" "Category" "score"
|
||||||
```
|
```
|
||||||
"""
|
"""
|
||||||
await highscore_manage.submit_score_int(
|
await highscore_manage.submit_score(self, ctx, boss_name, category, score)
|
||||||
self, ctx, boss_name, category, ctx.author.display_name, score
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
# This function will be called when this extension is loaded.
|
# This function will be called when this extension is loaded.
|
||||||
|
|||||||
@@ -184,15 +184,18 @@
|
|||||||
"categories": {
|
"categories": {
|
||||||
"Kill Count": {
|
"Kill Count": {
|
||||||
"hardmode": true,
|
"hardmode": true,
|
||||||
"ascending": false
|
"ascending": false,
|
||||||
|
"is_time_record": false
|
||||||
},
|
},
|
||||||
"Kill Time": {
|
"Kill Time": {
|
||||||
"hardmode": true,
|
"hardmode": true,
|
||||||
"ascending": true
|
"ascending": true,
|
||||||
|
"is_time_record": true
|
||||||
},
|
},
|
||||||
"Spooned Log": {
|
"Spooned Log": {
|
||||||
"hardmode": false,
|
"hardmode": false,
|
||||||
"ascending": true
|
"ascending": true,
|
||||||
|
"is_time_record": false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"highscore_size": 3
|
"highscore_size": 3
|
||||||
|
|||||||
+22
-29
@@ -9,9 +9,7 @@ from utils import highscore_message # pylint: disable=import-error
|
|||||||
from utils import data_storage # pylint: disable=import-error
|
from utils import data_storage # pylint: disable=import-error
|
||||||
|
|
||||||
|
|
||||||
async def submit_score_int(
|
async def submit_score(self, ctx: commands.Context, boss_name: str, category: str, score: str):
|
||||||
self, ctx: commands.Context, boss_name: str, category: str, user: str, score: int
|
|
||||||
):
|
|
||||||
"""
|
"""
|
||||||
Adds a score to the high scores and sorts. Removes a score if larger than size limit.
|
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 boss: the boss to add score to
|
||||||
@@ -20,11 +18,29 @@ async def submit_score_int(
|
|||||||
@param score: score (int) to add
|
@param score: score (int) to add
|
||||||
"""
|
"""
|
||||||
# TODO update user submission to not have dupe users
|
# 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]
|
scores = highscores_data[boss_name]["categories"][category]
|
||||||
score_tuple = (user, score)
|
|
||||||
scores.append(score_tuple)
|
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"]:
|
while len(scores) > highscores_config["highscore_size"]:
|
||||||
scores.pop()
|
scores.pop()
|
||||||
highscores_data[boss_name]["categories"][category] = scores
|
highscores_data[boss_name]["categories"][category] = scores
|
||||||
@@ -42,26 +58,3 @@ async def submit_score_int(
|
|||||||
|
|
||||||
# save data
|
# save data
|
||||||
data_storage.save_highscores_data(highscores_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)
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ def format_highscore_message(boss_name: str):
|
|||||||
|
|
||||||
# Gather all categories and add to string
|
# Gather all categories and add to string
|
||||||
for key, value in boss_data["categories"].items():
|
for key, value in boss_data["categories"].items():
|
||||||
ret_string += f"{key}".ljust(30, " ")
|
ret_string += f"{key}".ljust(50, " ")
|
||||||
ret_string += "\n"
|
ret_string += "\n"
|
||||||
|
|
||||||
highscore_size = highscores_config["highscore_size"]
|
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():
|
for key, value in boss_data["categories"].items():
|
||||||
if len(value) > i:
|
if len(value) > i:
|
||||||
score = 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:
|
else:
|
||||||
ret_string += f"{i+1}: submit your score".ljust(30, " ")
|
ret_string += f"{i+1}: submit your score".ljust(50, " ")
|
||||||
ret_string += "\n"
|
ret_string += "\n"
|
||||||
|
|
||||||
return ret_string
|
return ret_string
|
||||||
|
|||||||
Reference in New Issue
Block a user