def get(self): standings = get_standings() response = [] mode = get_config("user_mode") account_type = get_mode_as_word() if mode == TEAMS_MODE: r = db.session.execute( select([ Users.id, Users.name, Users.oauth_id, Users.team_id, Users.hidden, Users.banned, ]).where(Users.team_id.isnot(None))) users = r.fetchall() membership = defaultdict(dict) for u in users: if u.hidden is False and u.banned is False: membership[u.team_id][u.id] = { "id": u.id, "oauth_id": u.oauth_id, "name": u.name, "score": 0, } # Get user_standings as a dict so that we can more quickly get member scores user_standings = get_user_standings() for u in user_standings: membership[u.team_id][u.user_id]["score"] = int(u.score) for i, x in enumerate(standings): entry = { "pos": i + 1, "account_id": x.account_id, "account_url": generate_account_url(account_id=x.account_id), "account_type": account_type, "oauth_id": x.oauth_id, "name": x.name, "score": int(x.score), } if mode == TEAMS_MODE: entry["members"] = list(membership[x.account_id].values()) response.append(entry) return {"success": True, "data": response}
def get_place(self, admin=False, numeric=False): """ This method is generally a clone of CTFd.scoreboard.get_standings. The point being that models.py must be self-reliant and have little to no imports within the CTFd application as importing from the application itself will result in a circular import. """ from CTFd.utils.scores import get_user_standings standings = get_user_standings(admin=admin) try: n = standings.index((self.id,)) + 1 if numeric: return n return ordinalize(n) except ValueError: return None
def get_place(self, admin=False, numeric=False): """ This method is generally a clone of CTFd.scoreboard.get_standings. The point being that models.py must be self-reliant and have little to no imports within the CTFd application as importing from the application itself will result in a circular import. """ from CTFd.utils.scores import get_user_standings standings = get_user_standings(admin=admin) for i, user in enumerate(standings): if user.user_id == self.id: n = i + 1 if numeric: return n return ordinalize(n) else: return None
def get_place(self, admin=False, numeric=False): """ This method is generally a clone of CTFd.scoreboard.get_standings. The point being that models.py must be self-reliant and have little to no imports within the CTFd application as importing from the application itself will result in a circular import. """ from CTFd.utils.scores import get_user_standings standings = get_user_standings(admin=admin) # http://codegolf.stackexchange.com/a/4712 try: i = standings.index((self.id, )) + 1 if numeric: return i else: k = i % 10 return "%d%s" % (i, "tsnrhtdd"[(i / 10 % 10 != 1) * (k < 4) * k::4]) except ValueError: return 0
def get(self): standings = get_standings() response = [] mode = get_config("user_mode") account_type = get_mode_as_word() if mode == TEAMS_MODE: team_ids = [] for team in standings: team_ids.append(team.account_id) # Get team objects with members explicitly loaded in teams = (Teams.query.options(joinedload(Teams.members)).filter( Teams.id.in_(team_ids)).all()) # Sort according to team_ids order teams = [next(t for t in teams if t.id == id) for id in team_ids] # Get user_standings as a dict so that we can more quickly get member scores user_standings = get_user_standings() users = {} for u in user_standings: users[u.user_id] = u for i, x in enumerate(standings): entry = { "pos": i + 1, "account_id": x.account_id, "account_url": generate_account_url(account_id=x.account_id), "account_type": account_type, "oauth_id": x.oauth_id, "name": x.name, "score": int(x.score), } if mode == TEAMS_MODE: members = [] # This code looks like it would be slow # but it is faster than accessing each member's score individually for member in teams[i].members: user = users.get(member.id) if user: members.append({ "id": user.user_id, "oauth_id": user.oauth_id, "name": user.name, "score": int(user.score), }) else: if member.hidden is False and member.banned is False: members.append({ "id": member.id, "oauth_id": member.oauth_id, "name": member.name, "score": 0, }) entry["members"] = members response.append(entry) return {"success": True, "data": response}
def scoreboard_listing(): standings = get_standings(admin=True) user_standings = get_user_standings(admin=True) if is_teams_mode() else None return render_template( "admin/scoreboard.html", standings=standings, user_standings=user_standings )