Convert add_score to have scores as a dict.
This commit is contained in:
@@ -66,40 +66,17 @@ class HighscoresData:
|
||||
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))
|
||||
score_tuple = (score, score_seconds)
|
||||
sort_index = 1
|
||||
else:
|
||||
score_tuple = int(score)
|
||||
sort_index = 0
|
||||
|
||||
# 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)
|
||||
# Will not ensure new score is better than old one. Allows for correcting mistakes.
|
||||
|
||||
# sort users_scores
|
||||
users_scores.sort(
|
||||
key=lambda x: x[sort_index],
|
||||
reverse=not ascending,
|
||||
)
|
||||
scores[user] = score_tuple
|
||||
|
||||
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,
|
||||
)
|
||||
scores = dict(sorted(scores.items(), key=lambda x: x[1][sort_index], reverse=not ascending))
|
||||
|
||||
self._data[str(guild_id)]["tables"][boss_name]["categories"][category] = scores
|
||||
save_highscores_data(self._data)
|
||||
|
||||
Reference in New Issue
Block a user