forked from MarseyWorld/MarseyWorld
i wish the person who wrote the initial version of this leaderboard code a very lovely day
i definitely don't want to cry myself to sleep because of this god awful mess it's a little bit better now though <3master
parent
aadd054bc3
commit
ff639ce94b
|
@ -344,101 +344,80 @@ def transfer_bux(v, username):
|
||||||
@app.get("/leaderboard")
|
@app.get("/leaderboard")
|
||||||
@auth_required
|
@auth_required
|
||||||
def leaderboard(v):
|
def leaderboard(v):
|
||||||
|
|
||||||
users = g.db.query(User)
|
users = g.db.query(User)
|
||||||
|
|
||||||
users1 = users.order_by(User.coins.desc()).limit(25).all()
|
def get_leaderboard(order_by, limit=25):
|
||||||
if v in users1:
|
leaderboard = users.order_by(order_by.desc()).limit(limit).all()
|
||||||
pos1 = None
|
position = None
|
||||||
else:
|
if v not in leaderboard:
|
||||||
sq = g.db.query(User.id, func.rank().over(order_by=User.coins.desc()).label("rank")).subquery()
|
sq = g.db.query(User.id, func.rank().over(order_by=order_by.desc()).label("rank")).subquery()
|
||||||
pos1 = g.db.query(sq.c.id, sq.c.rank).filter(sq.c.id == v.id).limit(1).one()[1]
|
position = g.db.query(sq.c.id, sq.c.rank).filter(sq.c.id == v.id).limit(1).one()[1]
|
||||||
|
return (leaderboard, position)
|
||||||
|
|
||||||
users2 = users.order_by(User.stored_subscriber_count.desc()).limit(25).all()
|
coins = get_leaderboard(User.coins)
|
||||||
if v in users2:
|
subscribers = get_leaderboard(User.stored_subscriber_count)
|
||||||
pos2 = None
|
posts = get_leaderboard(User.post_count)
|
||||||
else:
|
comments = get_leaderboard(User.comment_count)
|
||||||
sq = g.db.query(User.id, func.rank().over(order_by=User.stored_subscriber_count.desc()).label("rank")).subquery()
|
received_awards = get_leaderboard(User.received_award_count)
|
||||||
pos2 = g.db.query(sq.c.id, sq.c.rank).filter(sq.c.id == v.id).limit(1).one()[1]
|
coins_spent = get_leaderboard(User.coins_spent)
|
||||||
|
truecoins = get_leaderboard(User.truecoins)
|
||||||
|
|
||||||
users3 = users.order_by(User.post_count.desc()).limit(25).all()
|
def count_and_label(criteria):
|
||||||
if v in users3:
|
return func.count(criteria).label("count")
|
||||||
pos3 = None
|
|
||||||
else:
|
|
||||||
sq = g.db.query(User.id, func.rank().over(order_by=User.post_count.desc()).label("rank")).subquery()
|
|
||||||
pos3 = g.db.query(sq.c.id, sq.c.rank).filter(sq.c.id == v.id).limit(1).one()[1]
|
|
||||||
|
|
||||||
users4 = users.order_by(User.comment_count.desc()).limit(25).all()
|
def rank_filtered_rank_label_by_desc(criteria):
|
||||||
if v in users4:
|
return func.rank().over(order_by=func.count(criteria).desc()).label("rank")
|
||||||
pos4 = None
|
|
||||||
else:
|
|
||||||
sq = g.db.query(User.id, func.rank().over(order_by=User.comment_count.desc()).label("rank")).subquery()
|
|
||||||
pos4 = g.db.query(sq.c.id, sq.c.rank).filter(sq.c.id == v.id).limit(1).one()[1]
|
|
||||||
|
|
||||||
users5 = users.order_by(User.received_award_count.desc()).limit(25).all()
|
def get_leaderboard_2(lb_criteria, limit=25):
|
||||||
if v in users5:
|
sq = g.db.query(lb_criteria, count_and_label(lb_criteria), rank_filtered_rank_label_by_desc(lb_criteria)).group_by(lb_criteria).subquery()
|
||||||
pos5 = None
|
sq_criteria = None
|
||||||
else:
|
if lb_criteria == Badge.user_id:
|
||||||
sq = g.db.query(User.id, func.rank().over(order_by=User.received_award_count.desc()).label("rank")).subquery()
|
sq_criteria = User.id == sq.c.user_id
|
||||||
pos5 = g.db.query(sq.c.id, sq.c.rank).filter(sq.c.id == v.id).limit(1).one()[1]
|
elif lb_criteria == Marsey.author_id:
|
||||||
|
sq_criteria = User.id == sq.c.author_id
|
||||||
|
else:
|
||||||
|
raise ValueError("This leaderboard function only supports Badge.user_id and Marsey.author_id")
|
||||||
|
|
||||||
users7 = users.order_by(User.coins_spent.desc()).limit(25).all()
|
leaderboard = g.db.query(User, sq.c.count).join(sq, sq_criteria).order_by(sq.c.count.desc())
|
||||||
if v in users7:
|
position = g.db.query(User.id, sq.c.rank, sq.c.count).join(sq, sq_criteria).filter(User.id == v.id).one_or_none()
|
||||||
pos7 = None
|
if position: position = (position[1], position[2])
|
||||||
else:
|
else: position = (leaderboard.count() + 1, 0)
|
||||||
sq = g.db.query(User.id, func.rank().over(order_by=User.coins_spent.desc()).label("rank")).subquery()
|
leaderboard = leaderboard.limit(limit).all()
|
||||||
pos7 = g.db.query(sq.c.id, sq.c.rank).filter(sq.c.id == v.id).limit(1).one()[1]
|
return (leaderboard, position)
|
||||||
|
|
||||||
|
badges = get_leaderboard_2(Badge.user_id)
|
||||||
|
marseys = get_leaderboard_2(Marsey.author_id) if SITE_NAME == 'rDrama' else (None, None)
|
||||||
|
|
||||||
|
def get_leaderboard_3(lb_criteria, limit=25):
|
||||||
|
if lb_criteria != UserBlock.target_id:
|
||||||
|
raise ValueError("This leaderboard function only supports UserBlock.target_id")
|
||||||
|
sq = g.db.query(lb_criteria, count_and_label(lb_criteria)).group_by(lb_criteria).subquery()
|
||||||
|
leaderboard = g.db.query(sq.c.rank, sq.c.count).join(User, User.id == sq.c.target_id).filter(sq.c.target_id == v.id).limit(1).one_or_none()
|
||||||
|
|
||||||
users10 = users.order_by(User.truecoins.desc()).limit(25).all()
|
sq = g.db.query(lb_criteria, count_and_label(lb_criteria), rank_filtered_rank_label_by_desc(lb_criteria)).group_by(lb_criteria).subquery()
|
||||||
if v in users10:
|
position = g.db.query(sq.c.rank, sq.c.count).join(User, User.id == sq.c.target_id).filter(sq.c.target_id == v.id).limit(1).one_or_none()
|
||||||
pos10 = None
|
if not position: position = (leaderboard.count() + 1, 0)
|
||||||
else:
|
leaderboard = leaderboard.limit(limit).all()
|
||||||
sq = g.db.query(User.id, func.rank().over(order_by=User.truecoins.desc()).label("rank")).subquery()
|
return (leaderboard, position)
|
||||||
pos10 = g.db.query(sq.c.id, sq.c.rank).filter(sq.c.id == v.id).limit(1).one()[1]
|
|
||||||
|
|
||||||
sq = g.db.query(Badge.user_id, func.count(Badge.user_id).label("count"), func.rank().over(order_by=func.count(Badge.user_id).desc()).label("rank")).group_by(Badge.user_id).subquery()
|
blocks = get_leaderboard_3(UserBlock.target_id)
|
||||||
users11 = g.db.query(User, sq.c.count).join(sq, User.id==sq.c.user_id).order_by(sq.c.count.desc())
|
|
||||||
pos11 = g.db.query(User.id, sq.c.rank, sq.c.count).join(sq, User.id==sq.c.user_id).filter(User.id == v.id).one_or_none()
|
|
||||||
if pos11: pos11 = (pos11[1],pos11[2])
|
|
||||||
else: pos11 = (users11.count()+1, 0)
|
|
||||||
users11 = users11.limit(25).all()
|
|
||||||
|
|
||||||
if SITE_NAME == 'rDrama':
|
def get_leaderboard_4(lb_criteria, limit=25):
|
||||||
sq = g.db.query(Marsey.author_id, func.count(Marsey.author_id).label("count"), func.rank().over(order_by=func.count(Marsey.author_id).desc()).label("rank")).filter(Marsey.submitter_id==None).group_by(Marsey.author_id).subquery()
|
leaderboard = g.db.query(User.id, func.count(lb_criteria)).join(lb_criteria).group_by(User).order_by(func.count(lb_criteria).desc())
|
||||||
users12 = g.db.query(User, sq.c.count).join(sq, User.id==sq.c.author_id).order_by(sq.c.count.desc())
|
sq = g.db.query(User.id, count_and_label(lb_criteria), rank_filtered_rank_label_by_desc(lb_criteria)).join(lb_criteria).group_by(User).subquery()
|
||||||
pos12 = g.db.query(User.id, sq.c.rank, sq.c.count).join(sq, User.id==sq.c.author_id).filter(User.id == v.id).one_or_none()
|
position = g.db.query(sq.c.rank, sq.c.count).filter(sq.c.id).limit(1).one_or_none()
|
||||||
if pos12: pos12 = (pos12[1],pos12[2])
|
if not position: position = (leaderboard.count() + 1, 0)
|
||||||
else: pos12 = (users12.count()+1, 0)
|
leaderboard = leaderboard.limit(limit).all()
|
||||||
users12 = users12.limit(25).all()
|
return (leaderboard, position)
|
||||||
else:
|
|
||||||
users12 = None
|
|
||||||
pos12 = None
|
|
||||||
|
|
||||||
sq = g.db.query(UserBlock.target_id, func.count(UserBlock.target_id).label("count")).group_by(UserBlock.target_id).subquery()
|
owned_hats = get_leaderboard_4(User.owned_hats)
|
||||||
users16 = g.db.query(User, sq.c.count).join(User, User.id == sq.c.target_id).order_by(sq.c.count.desc())
|
designed_hats = get_leaderboard_4(User.designed_hats)
|
||||||
|
|
||||||
sq = g.db.query(UserBlock.target_id, func.count(UserBlock.target_id).label("count"), func.rank().over(order_by=func.count(UserBlock.target_id).desc()).label("rank")).group_by(UserBlock.target_id).subquery()
|
return render_template("leaderboard.html", v=v, users1=coins[0], pos1=coins[1], users2=subscribers[0], pos2=subscribers[1],
|
||||||
pos16 = g.db.query(sq.c.rank, sq.c.count).join(User, User.id == sq.c.target_id).filter(sq.c.target_id == v.id).limit(1).one_or_none()
|
users3=posts[0], pos3=posts[1], users4=comments[0], pos4=comments[1], users5=received_awards[0], pos5=received_awards[1],
|
||||||
if not pos16: pos16 = (users16.count()+1, 0)
|
users7=coins_spent[0], pos7=coins_spent[1], users10=truecoins[0], pos10=truecoins[1], users11=badges[0], pos11=badges[1],
|
||||||
users16 = users16.limit(25).all()
|
users12=marseys[0], pos12=marseys[1], users16=blocks[0], pos16=blocks[1], users17=owned_hats[0], pos17=owned_hats[1],
|
||||||
|
users18=designed_hats[0], pos18=designed_hats[1])
|
||||||
users17 = g.db.query(User, func.count(User.owned_hats)).join(User.owned_hats).group_by(User).order_by(func.count(User.owned_hats).desc())
|
|
||||||
sq = g.db.query(User.id, func.count(User.owned_hats).label("count"), func.rank().over(order_by=func.count(User.owned_hats).desc()).label("rank")).join(User.owned_hats).group_by(User).subquery()
|
|
||||||
pos17 = g.db.query(sq.c.rank, sq.c.count).filter(sq.c.id == v.id).limit(1).one_or_none()
|
|
||||||
if not pos17: pos17 = (users17.count()+1, 0)
|
|
||||||
users17 = users17.limit(25).all()
|
|
||||||
|
|
||||||
users18 = g.db.query(User, func.count(User.designed_hats)).join(User.designed_hats).group_by(User).order_by(func.count(User.designed_hats).desc())
|
|
||||||
sq = g.db.query(User.id, func.count(User.designed_hats).label("count"), func.rank().over(order_by=func.count(User.designed_hats).desc()).label("rank")).join(User.designed_hats).group_by(User).subquery()
|
|
||||||
pos18 = g.db.query(sq.c.rank, sq.c.count).filter(sq.c.id == v.id).limit(1).one_or_none()
|
|
||||||
if not pos18: pos18 = (users18.count()+1, 0)
|
|
||||||
users18 = users18.limit(25).all()
|
|
||||||
|
|
||||||
return render_template("leaderboard.html", v=v, users1=users1, pos1=pos1, users2=users2, pos2=pos2,
|
|
||||||
users3=users3, pos3=pos3, users4=users4, pos4=pos4, users5=users5, pos5=pos5,
|
|
||||||
users7=users7, pos7=pos7, users10=users10, pos10=pos10, users11=users11, pos11=pos11, users12=users12, pos12=pos12, users16=users16, pos16=pos16, users17=users17, pos17=pos17, users18=users18, pos18=pos18)
|
|
||||||
|
|
||||||
@app.get("/<id>/css")
|
@app.get("/<id>/css")
|
||||||
def get_css(id):
|
def get_css(id):
|
||||||
|
|
Loading…
Reference in New Issue