diff --git a/files/routes/__init__.py b/files/routes/__init__.py index 5649270be..b2d1e376d 100644 --- a/files/routes/__init__.py +++ b/files/routes/__init__.py @@ -47,3 +47,4 @@ if FEATURES['ASSET_SUBMISSIONS']: from .asset_submissions import * if FEATURES['STREAMERS']: from .streamers import * +from .events import * diff --git a/files/routes/events.py b/files/routes/events.py new file mode 100644 index 000000000..f9be8836f --- /dev/null +++ b/files/routes/events.py @@ -0,0 +1,35 @@ +from flask import g, jsonify, render_template +from files.helpers.get import get_accounts_dict + +from files.routes.wrappers import auth_required +from files.__main__ import app + +@app.get('/events/worldcup2022/leaderboard') +@auth_required +def get_leaderboard(v): + result = g.db.execute('''WITH bet_votes AS ( + SELECT opt.id AS option_id, opt.exclusive, sov.user_id + FROM submission_option_votes sov + LEFT OUTER JOIN ( + SELECT so.id, so.exclusive + FROM submission_options so + JOIN submissions p ON so.submission_id = p.id + WHERE p.author_id = 30 AND p.created_utc > 1668953400 AND so.exclusive IN (2, 3) + ) AS opt ON opt.id = sov.option_id + WHERE opt.id IS NOT NULL +) +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 +ORDER BY bets_won DESC, bets_total ASC;''').all() + if g.is_api_or_xhr: return jsonify(result) + users = get_accounts_dict([r[0] for r in result], v=v, include_shadowbanned=False) + return render_template("event/worldcup22_leaderboard.html", v=v, result=result, users=users) diff --git a/files/templates/event/worldcup22_leaderboard.html b/files/templates/event/worldcup22_leaderboard.html new file mode 100644 index 000000000..db2f36340 --- /dev/null +++ b/files/templates/event/worldcup22_leaderboard.html @@ -0,0 +1,28 @@ +{%- extends 'default.html' -%} +{% block content %} +

World Cup 2022 Betting Leaderboard

+
+ + + + + + + + + + + + {% for r in result %} + {% set user = users.get(r[0]) %} + + + + + + {% endfor %} + +
#NameWinsBetsWins/Bets Ratio
{{loop.index}}{%- include 'user_in_table.html' -%}{{r[2]}}{{r[1]}}{{":. 0%" | format(r[2] / r[1]|float)}}
+
+ +{% endblock %}