2023-02-02 00:27:38 +00:00
|
|
|
from flask import g, render_template
|
2023-03-02 00:27:17 +00:00
|
|
|
from sqlalchemy.sql import text
|
|
|
|
|
2023-02-02 00:27:38 +00:00
|
|
|
from files.helpers.get import get_accounts_dict
|
2023-02-26 08:41:04 +00:00
|
|
|
from files.helpers.config.const import *
|
2023-02-02 00:27:38 +00:00
|
|
|
|
2023-02-26 08:41:04 +00:00
|
|
|
from files.routes.wrappers import *
|
|
|
|
|
|
|
|
from files.__main__ import app, cache, limiter
|
2023-02-02 00:27:38 +00:00
|
|
|
|
2023-03-02 00:27:17 +00:00
|
|
|
_special_leaderboard_query = text("""
|
2023-02-02 00:27:38 +00:00
|
|
|
WITH bet_options AS (
|
2023-03-02 00:36:41 +00:00
|
|
|
SELECT p.id AS parent_id, so.id AS option_id, so.exclusive, cnt.count
|
2023-02-02 00:27:38 +00:00
|
|
|
FROM submission_options so
|
2023-03-02 00:36:41 +00:00
|
|
|
JOIN submissions p ON so.parent_id = p.id
|
2023-02-02 00:27:38 +00:00
|
|
|
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
|
2023-03-02 00:36:41 +00:00
|
|
|
sq_total.parent_id,
|
2023-02-02 00:27:38 +00:00
|
|
|
sq_winners.sum AS bettors,
|
|
|
|
floor((sq_total.sum * 200) / sq_winners.sum) AS winner_payout
|
|
|
|
FROM (
|
2023-03-02 00:36:41 +00:00
|
|
|
SELECT parent_id, SUM(count)
|
|
|
|
FROM bet_options GROUP BY parent_id
|
2023-02-02 00:27:38 +00:00
|
|
|
) AS sq_total
|
|
|
|
JOIN (
|
2023-03-02 00:36:41 +00:00
|
|
|
SELECT parent_id, SUM(count)
|
|
|
|
FROM bet_options WHERE exclusive = 3 GROUP BY parent_id
|
|
|
|
) AS sq_winners ON sq_total.parent_id = sq_winners.parent_id
|
2023-02-02 00:27:38 +00:00
|
|
|
),
|
|
|
|
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
|
2023-03-02 00:36:41 +00:00
|
|
|
ON opt.parent_id = submission_payouts.parent_id
|
2023-02-02 00:27:38 +00:00
|
|
|
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;
|
2023-03-02 00:27:17 +00:00
|
|
|
""")
|
2023-02-02 00:27:38 +00:00
|
|
|
|
2023-02-02 00:37:03 +00:00
|
|
|
@cache.memoize()
|
2023-02-02 00:27:38 +00:00
|
|
|
def _special_leaderboard_get():
|
2023-03-16 06:27:58 +00:00
|
|
|
result = g.db.execute(_special_leaderboard_query).all()
|
2023-02-02 00:27:38 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
@app.get('/events/worldcup2022/leaderboard')
|
|
|
|
@app.get('/special/worldcup2022/leaderboard')
|
2023-02-26 08:41:04 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT)
|
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
|
2023-02-02 00:27:38 +00:00
|
|
|
@auth_required
|
|
|
|
def get_leaderboard(v):
|
2023-02-21 14:51:22 +00:00
|
|
|
if SITE_NAME != 'rDrama':
|
|
|
|
abort(404)
|
|
|
|
|
2023-02-02 00:27:38 +00:00
|
|
|
result = _special_leaderboard_get()
|
|
|
|
if g.is_api_or_xhr: return result
|
2023-02-27 15:38:12 +00:00
|
|
|
users = get_accounts_dict([r[0] for r in result], v=v, graceful=True)
|
2023-02-02 00:27:38 +00:00
|
|
|
return render_template("special/worldcup22_leaderboard.html",
|
|
|
|
v=v, result=result, users=users)
|