Add score submission command. Add highscore management function. Change how config file and data file are defined.

This commit is contained in:
alwayscompiling
2022-06-29 19:52:19 -04:00
parent ed62836b7f
commit f204be8d52
5 changed files with 91 additions and 11 deletions
+52
View File
@@ -0,0 +1,52 @@
"""
Contains various functions for managing the highscores data
"""
from highscores import highscores_config # pylint: disable=import-error
from highscores import highscores_data # pylint: disable=import-error
from utils import data_storage # pylint: disable=import-error
def submit_score_int(boss: str, category: str, user: str, score: int):
"""
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 (int) to add
"""
# TODO update user submission to not have dupe users
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)
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)
+1 -2
View File
@@ -56,8 +56,7 @@ def format_highscore_message(boss):
boss_category_scores = boss_data[f"{hardmode_category}_hardmode"]
if len(boss_category_scores) > i:
score = boss_category_scores[i]
for key, value in score.items():
ret_string += f"{i+1}: {key} - {value}".ljust(30, " ")
ret_string += f"{i+1}: {score[0]} - {score[1]}".ljust(30, " ")
else:
ret_string += f"{i+1}: submit your score".ljust(30, " ")
ret_string += "\n"