From 415dd9cc4a2f22cb6a48a5ac98924f338230206d Mon Sep 17 00:00:00 2001 From: Aevann Date: Thu, 2 Feb 2023 02:27:38 +0200 Subject: [PATCH] Revert "remove worldcup leaderboard" This reverts commit 6dcb000c8f8de4dbb1813d2b4de385e847ff2082. --- files/routes/__init__.py | 1 + files/routes/special.py | 89 +++++++++++++++++++ .../special/worldcup22_leaderboard.html | 35 ++++++++ 3 files changed, 125 insertions(+) create mode 100644 files/routes/special.py create mode 100644 files/templates/special/worldcup22_leaderboard.html diff --git a/files/routes/__init__.py b/files/routes/__init__.py index 3af239d8d..3379152bb 100644 --- a/files/routes/__init__.py +++ b/files/routes/__init__.py @@ -45,4 +45,5 @@ if FEATURES['HATS']: from .hats import * if FEATURES['ASSET_SUBMISSIONS']: from .asset_submissions import * +from .special import * from .push_notifs import * diff --git a/files/routes/special.py b/files/routes/special.py new file mode 100644 index 000000000..4905cd282 --- /dev/null +++ b/files/routes/special.py @@ -0,0 +1,89 @@ +from flask import g, render_template +from files.helpers.get import get_accounts_dict + +from files.routes.wrappers import auth_required +from files.__main__ import app, cache + +_special_leaderboard_query = """ +WITH bet_options AS ( + SELECT p.id AS submission_id, so.id AS option_id, so.exclusive, cnt.count + FROM submission_options so + JOIN submissions p ON so.submission_id = p.id + JOIN ( + SELECT option_id, COUNT(*) FROM submission_option_votes + GROUP BY option_id + ) AS cnt ON so.id = cnt.option_id + WHERE p.author_id in (30,152) AND p.created_utc > 1668953400 + AND so.exclusive IN (2, 3) +), +submission_payouts AS ( + SELECT + sq_total.submission_id, + sq_winners.sum AS bettors, + floor((sq_total.sum * 200) / sq_winners.sum) AS winner_payout + FROM ( + SELECT submission_id, SUM(count) + FROM bet_options GROUP BY submission_id + ) AS sq_total + JOIN ( + SELECT submission_id, SUM(count) + FROM bet_options WHERE exclusive = 3 GROUP BY submission_id + ) AS sq_winners ON sq_total.submission_id = sq_winners.submission_id +), +bet_votes AS ( + SELECT + opt.option_id AS option_id, + opt.exclusive, + sov.user_id, + CASE + WHEN opt.exclusive = 2 THEN -200 + WHEN opt.exclusive = 3 THEN (submission_payouts.winner_payout - 200) + END payout + FROM submission_option_votes sov + LEFT OUTER JOIN bet_options AS opt + ON opt.option_id = sov.option_id + LEFT OUTER JOIN submission_payouts + ON opt.submission_id = submission_payouts.submission_id + WHERE opt.option_id IS NOT NULL +), +bettors AS ( + SELECT + COALESCE(bet_won.user_id, bet_lost.user_id) AS user_id, + (COALESCE(bet_won.count_won, 0) + + COALESCE(bet_lost.count_lost, 0)) AS bets_total, + COALESCE(bet_won.count_won, 0) AS bets_won + FROM ( + SELECT user_id, COUNT(*) AS count_won FROM bet_votes + WHERE exclusive = 3 GROUP BY user_id) AS bet_won + FULL OUTER JOIN ( + SELECT user_id, COUNT(*) AS count_lost FROM bet_votes + WHERE exclusive = 2 GROUP BY user_id + ) AS bet_lost ON bet_won.user_id = bet_lost.user_id +) +SELECT + bettors.user_id, + bettors.bets_won, + bettors.bets_total, + bet_payout.net AS payout +FROM bettors +LEFT OUTER JOIN ( + SELECT user_id, SUM(payout) AS net FROM bet_votes GROUP BY user_id +) AS bet_payout ON bettors.user_id = bet_payout.user_id +ORDER BY payout DESC, bets_won DESC, bets_total ASC; +""" + +@cache.memoize(timeout=60) +def _special_leaderboard_get(): + result = g.db.execute(_special_leaderboard_query).all() + return result + +@app.get('/events/worldcup2022/leaderboard') +@app.get('/special/worldcup2022/leaderboard') +@auth_required +def get_leaderboard(v): + result = _special_leaderboard_get() + if g.is_api_or_xhr: return result + users = get_accounts_dict([r[0] for r in result], + v=v, include_shadowbanned=False, graceful=True) + return render_template("special/worldcup22_leaderboard.html", + v=v, result=result, users=users) diff --git a/files/templates/special/worldcup22_leaderboard.html b/files/templates/special/worldcup22_leaderboard.html new file mode 100644 index 000000000..828f866c8 --- /dev/null +++ b/files/templates/special/worldcup22_leaderboard.html @@ -0,0 +1,35 @@ +{%- extends 'default.html' -%} +{% block pagetitle %}World Cup 2022 Betting Leaderboard{% endblock %} +{% block content %} +

World Cup 2022 Betting Leaderboard

+
+ + + + + + + + + + + + + {% for r in result %} + + {% set user = users.get(r[0]) %} + + + + + {% set ratio = r[1]/r[2] %} + {% set percent = (ratio*100)|string %} + + + + {% endfor %} + +
#NameWinsBetsWin rateWinnings
{{loop.index}}{%- include 'user_in_table.html' -%}{{r[1]}}{{r[2]}}{{percent[:3].replace('0.0', '0').rstrip('.')}}%{{r[3]}}
+
+ +{% endblock %}