From c689fcb4e7f228f08fcf64f362ef2f2f840c6986 Mon Sep 17 00:00:00 2001 From: Brad Brown Date: Mon, 20 Jun 2022 11:37:57 -0500 Subject: [PATCH] add leaderboard --- wordlinator/web/__init__.py | 49 +++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/wordlinator/web/__init__.py b/wordlinator/web/__init__.py index 2fd901f..1121b43 100644 --- a/wordlinator/web/__init__.py +++ b/wordlinator/web/__init__.py @@ -1,5 +1,5 @@ -import datetime import functools +import os import pathlib import time @@ -16,6 +16,9 @@ import wordlinator.utils import wordlinator.utils.scores import wordlinator.utils.web +TTL_TIME = 10 if os.getenv("DEBUG") else 600 +LEADERBOARD_COUNT = 20 + ################### # Setup Functions # ################### @@ -26,7 +29,7 @@ app = dash.Dash( ) -def get_ttl_hash(seconds=600): +def get_ttl_hash(seconds=TTL_TIME): return round(time.time() / seconds) @@ -72,6 +75,27 @@ def scores_from_db(round_id): ) +####################### +# Leaderboard helpers # +####################### + + +def get_leaderboard(round_id): + score_matrix = scores_from_db(round_id) + user_scores = score_matrix.by_user() + top_20 = dict( + list(sorted(user_scores.items(), key=lambda u: u[1].golf_score))[ + :LEADERBOARD_COUNT + ] + ) + return dash.dash_table.DataTable( + [{"Name": k, "Score": v.golf_score} for k, v in top_20.items()], + style_as_list_view=True, + style_table={"width": "40%", "margin": "auto"}, + style_cell={"textAlign": "center"}, + ) + + ################# # Score Helpers # ################# @@ -237,6 +261,15 @@ app.layout = dash.html.Div( id="round-selector", style={"maxWidth": "300px"}, ), + dash.html.Div( + [ + dash.html.H2( + f"Leaderboard - Top {LEADERBOARD_COUNT}", + style={"textAlign": "center"}, + ), + dash.html.Div("Loading...", id="leaderboard"), + ] + ), dash.html.Div( [ dash.html.H2("User Scores", style={"textAlign": "center"}), @@ -259,6 +292,18 @@ app.layout = dash.html.Div( ) +@app.long_callback( + output=dash.dependencies.Output("leaderboard", "children"), + inputs=[ + dash.dependencies.Input("title", "children"), + dash.dependencies.Input("round-selector-dropdown", "value"), + ], + manager=long_callback_manager, +) +def get_leaderboard_table(_, round_id): + return get_leaderboard(round_id) + + @app.long_callback( output=dash.dependencies.Output("user-scores", "children"), inputs=[