mirror of
https://github.com/Fluffy-Bean/GameExpo23.git
synced 2025-05-19 01:44:51 +00:00
Show user only once on top scores
This commit is contained in:
parent
e08b31a430
commit
e1d22d502d
3 changed files with 34 additions and 7 deletions
|
@ -1,6 +1,9 @@
|
|||
from flask import Blueprint, request, render_template, abort
|
||||
from sqlalchemy import func
|
||||
|
||||
from .models import Scores, Users
|
||||
from .config import GAME_VERSION, MAX_TOP_SCORES
|
||||
from .extensions import db
|
||||
|
||||
|
||||
blueprint = Blueprint("views", __name__)
|
||||
|
@ -12,18 +15,29 @@ def index():
|
|||
ver_arg = request.args.get("ver", GAME_VERSION)
|
||||
user_arg = request.args.get("user", None)
|
||||
|
||||
scores = Scores.query.filter_by(difficulty=diff_arg).order_by(Scores.score.asc())
|
||||
scores = db.session.query(Scores).filter_by(difficulty=diff_arg)
|
||||
|
||||
subquery = (
|
||||
db.session.query(Scores.user_id, func.min(Scores.score).label('min'))
|
||||
.group_by(Scores.user_id)
|
||||
.subquery()
|
||||
)
|
||||
|
||||
if ver_arg:
|
||||
scores = scores.filter_by(version=ver_arg)
|
||||
if user_arg:
|
||||
|
||||
if not user_arg:
|
||||
scores = (
|
||||
scores.join(subquery, Scores.user_id == subquery.c.user_id)
|
||||
.filter(Scores.score == subquery.c.min)
|
||||
)
|
||||
else:
|
||||
if user := Users.query.filter_by(username=user_arg).first():
|
||||
scores = scores.filter_by(user_id=user.id)
|
||||
print(user.id)
|
||||
else:
|
||||
abort(404, "User not found")
|
||||
|
||||
scores = scores.limit(MAX_TOP_SCORES).all()
|
||||
scores = scores.order_by(Scores.score.asc()).limit(MAX_TOP_SCORES).all()
|
||||
|
||||
return render_template(
|
||||
"views/scores.html",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue