Esempio n. 1
0
        def wrapper(user, team, challenge, request):
            chal_solve_func(user, team, challenge, request)

            notifier = get_configured_notifier()
            if notifier and bool(get_config('notifier_send_solves')):
                if get_mode_as_word() == TEAMS_MODE:
                    solver = team
                    solver_url = url_for("teams.public", team_id=solver.account_id, _external=True)
                else:
                    solver = user
                    solver_url = url_for("users.public", user_id=solver.account_id, _external=True)
                challenge_url = url_for('challenges.listing', _external=True, _anchor='{challenge.name}-{challenge.id}'.format(challenge=challenge))

                Model = get_model()
                solve_count = (
                    db.session.query(
                        db.func.count(Solves.id)
                    )
                    .filter(Solves.challenge_id == challenge.id)
                    .join(Model, Solves.account_id == Model.id)
                    .filter(Model.banned == False, Model.hidden == False)
                    .scalar()
                )

                max_solves = get_config('notifier_solve_count')
                max_solves = int(max_solves) if max_solves is not None else None

                if max_solves is None or solve_count <= max_solves:
                    notifier.notify_solve(get_config('notifier_solve_msg', '{solver} solved {challenge} ({solve_num} solve)'), solver.name, solver_url, challenge.name, challenge_url, solve_count)
def standings_to_string(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)
        teams = Teams.query.filter(Teams.id.in_(team_ids)).all()
        teams = [next(t for t in teams if t.id == id) for id in team_ids]
    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 = []
            for member in teams[i].members:
                members.append({
                    "id": member.id,
                    "oauth_id": member.oauth_id,
                    "name": member.name,
                    "score": int(member.score),
                })

            entry["members"] = members

        response.append(entry)
    return response
Esempio n. 3
0
    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_standings():
     standings = scoreboard.get_standings()
     # TODO faster lookup here
     jstandings = []
     for team in standings:
         teamid = team[0]
         solves = db.session.query(Solves.challenge_id.label('challenge_id'))
         if get_mode_as_word() == TEAMS_MODE:
             solves = solves.filter(Solves.team_id == teamid)
         else:
             solves = solves.filter(Solves.user_id == teamid)
         freeze = utils.get_config('freeze')
         if freeze:
             freeze = unix_time_to_utc(freeze)
             if teamid != session.get('id'):
                 solves = solves.filter(Solves.date < freeze)
         solves = solves.all()
         jsolves = []
         for solve in solves:
             jsolves.append(solve.challenge_id)
         jstandings.append({'teamid': team[0], 'score': team[3], 'name': team[2], 'solves': jsolves})
     db.session.close()
     return jstandings
Esempio n. 5
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}