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')).filter(
                 Solves.team_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
Ejemplo n.º 2
0
    def get_standings():
        standings = scoreboard.get_standings()
        # TODO faster lookup here
        jstandings = []
        # print(standings)
        for team in standings:
            mode = utils.get_config("user_mode")
            if mode == "teams":
                teamid = Users.query.filter_by(id=team[0]).first_or_404().team_id
                solves = db.session.query(Solves.challenge_id.label('chalid'), Solves.date.label('date')).filter(
                    Solves.team_id == teamid)
            else:
                teamid = team[0]
                solves = db.session.query(Solves.challenge_id.label('chalid'), Solves.date.label('date')).filter(
                    Solves.user_id == teamid)

            freeze = utils.get_config('freeze')
            if freeze:
                freeze = utils.unix_time_to_utc(freeze)
                if teamid != session.get('id'):
                    solves = solves.filter(Solves.date < freeze)
            solves = solves.all()

            jsolves = []
            score = 0
            for solve in solves:
                cvalue = Challenges.query.filter_by(id=solve.chalid).first().value
                top = Solves.query.filter_by(challenge_id=solve.chalid, type='correct').order_by(Solves.date.asc()).all()
                if(solve.date == top[0].date):
                    solve = str(solve.chalid) + "-1"
                    score = score + int(cvalue * 1.3)
                elif(solve.date == top[1].date):
                    solve = str(solve.chalid) + "-2"
                    score = score + int(cvalue * 1.2)
                elif(solve.date == top[2].date):
                    solve = str(solve.chalid) + "-3"
                    score = score + int(cvalue * 1.1)
                else:
                    solve = str(solve.chalid) + "-0"
                    score = score + int(cvalue * 1)
                jsolves.append(solve)

            if mode == "teams":
                jstandings.append({'userid':"", 'teamid':team[0], 'score':score, 'name':team[2],'solves':jsolves})
            else:
                jstandings.append({'userid':team[0], 'teamid':"", 'score':score, 'name':team[2],'solves':jsolves})
        # 重新按分数排序
        jstandings.sort(key=lambda x: x["score"], reverse=True)
        db.session.close()
        return jstandings
    def classified_scores(classification):
        json = {'standings': []}
        if utils.get_config(
                'view_scoreboard_if_authed') and not utils.authed():
            return redirect(url_for('auth.login', next=request.path))
        if utils.hide_scores():
            return jsonify(json)

        standings = get_standings(classification=classification)

        for i, x in enumerate(standings):
            json['standings'].append({
                'pos': i + 1,
                'id': x.teamid,
                'team': x.name,
                'score': int(x.score)
            })
        return jsonify(json)
    def scoreboard_view():
        classifications = []
        for classification in db.session.query(
                Classification.classification).distinct():
            classifications.append(classification[0])
        db.session.close()

        classifications = sorted(classifications, reverse=True)

        # -=- For TAMUctf, but can be left in without any problems -=-
        try:
            tamu_test()
            tamu = ["tamu"]
        except:
            tamu = []
        #-=-
        try:
            current_user_class = Classification.query.filter_by(
                id=session.get('id')).first().classification
        except:
            current_user_class = "ALL"
        try:
            current_user_other = Classification.query.filter_by(
                id=session.get('id')).first().other
        except:
            current_user_other = 0

        if utils.get_config(
                'view_scoreboard_if_authed') and not utils.authed():
            return redirect(url_for('auth.login', next=request.path))
        if utils.hide_scores():
            return render_template('scoreboard.html',
                                   errors=['Scores are currently hidden'])
        standings = get_standings()

        return render_template('scoreboard.html',
                               teams=standings,
                               score_frozen=utils.is_scoreboard_frozen(),
                               classifications=classifications,
                               tamu=tamu,
                               current_user_class=current_user_class,
                               current_user_other=current_user_other)
Ejemplo n.º 5
0
def scoreboard_listing():
    standings = get_standings(admin=True)
    return render_template("admin/scoreboard.html", standings=standings)
def admin_scoreboard():
    standings = get_standings(admin=True)
    return render_template('admin/scoreboard.html', teams=standings)
    def classified_topteams(count, classification):
        json = {'places': {}}
        if utils.get_config(
                'view_scoreboard_if_authed') and not utils.authed():
            return redirect(url_for('auth.login', next=request.path))
        if utils.hide_scores():
            return jsonify(json)

        if count > 20 or count < 0:
            count = 10

        standings = get_standings(count=count, classification=classification)

        team_ids = [team.teamid for team in standings]

        solves = Solves.query.filter(Solves.teamid.in_(team_ids))
        awards = Awards.query.filter(Awards.teamid.in_(team_ids))

        freeze = utils.get_config('freeze')

        if freeze:
            solves = solves.filter(
                Solves.date < utils.unix_time_to_utc(freeze))
            awards = awards.filter(
                Awards.date < utils.unix_time_to_utc(freeze))

        solves = solves.all()
        awards = awards.all()

        for i, team in enumerate(team_ids):
            json['places'][i + 1] = {
                'id': standings[i].teamid,
                'name': standings[i].name,
                'solves': []
            }
            for solve in solves:
                if solve.teamid == team:
                    json['places'][i + 1]['solves'].append({
                        'chal':
                        solve.chalid,
                        'team':
                        solve.teamid,
                        'value':
                        solve.chal.value,
                        'time':
                        utils.unix_time(solve.date)
                    })
            for award in awards:
                if award.teamid == team:
                    json['places'][i + 1]['solves'].append({
                        'chal':
                        None,
                        'team':
                        award.teamid,
                        'value':
                        award.value,
                        'time':
                        utils.unix_time(award.date)
                    })
            json['places'][i + 1]['solves'] = sorted(
                json['places'][i + 1]['solves'], key=lambda k: k['time'])

        return jsonify(json)
    def classified():
        if request.method == 'POST':
            teamid = request.form['id']
            previous = Classification.query.filter_by(id=teamid)
            for x in previous:
                db.session.delete(x)

            errors = []
            classification = request.form['classification']

            if classification == 'other':
                classification = request.form['new_classification']

            classify = Classification(int(teamid), classification)
            db.session.add(classify)
            db.session.commit()
            db.session.close()

        if request.method == 'GET' or request.method == 'POST':

            classifications = Classification.query.all()

            teams = []
            scoring_teams = []
            standings = get_standings()

            # Competitors with a score
            for i, x in enumerate(standings):
                pushed = 0
                for classification in classifications:
                    if classification.teamid == x.teamid:
                        teams.append({
                            'id': x.teamid,
                            'name': x.name,
                            'class': classification.classification,
                            'score': x.score
                        })
                        pushed = 1
                        break
                scoring_teams.append(x.teamid)
                if (pushed == 0):
                    teams.append({
                        'id': x.teamid,
                        'name': x.name,
                        'class': '',
                        'score': x.score
                    })

            # Competitors with/without a score (limited to only without a score)
            for team in db.session.query(Teams.name, Teams.id,
                                         Teams.admin).all():
                if (team.admin == False):
                    pushed = 0
                    for classification in classifications:
                        if classification.teamid == team.id:
                            if (team.id not in scoring_teams):
                                teams.append({
                                    'id': team.id,
                                    'name': team.name,
                                    'class': classification.classification,
                                    'score': ''
                                })
                                pushed = 1
                    if (pushed == 0 and (team.id not in scoring_teams)):
                        teams.append({
                            'id': team.id,
                            'name': team.name,
                            'class': '',
                            'score': ''
                        })

            classf = []
            for clas in classifications:
                classf.append(clas.classification)
            classf = list(sorted(set(classf)))

            # -=- For TAMUctf, but can be left in without any problems -=-
            try:
                tamu_test()
                tamu = ["tamu"]
            except:
                tamu = []
            # -=-

            db.session.close()

            return render_template('config.html',
                                   teams=teams,
                                   classifications=classf,
                                   tamu=tamu)
Ejemplo n.º 9
0
def admin_scoreboard_view():
    standings = get_standings(admin=True)
    return render_template('admin/scoreboard.html', teams=standings)
Ejemplo n.º 10
0
    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, Challenges.category,
                db.func.sum(Challenges.value), db.func.max(Solves.date)).join(
                    Challenges, Solves.challenge_id == Challenges.id).group_by(
                        Challenges.category).filter(
                            Solves.team_id == teamid).filter(
                                Challenges.value != 1).filter(
                                    Challenges.value != 0))

            challenge = (db.session.query(Challenges.category,
                                          db.func.sum(
                                              Challenges.value)).group_by(
                                                  Challenges.category)).all()
            #print(team[2])
            chal_details = {}
            for i in challenge:
                chal_details.update({i[0]: i[1]})

            freeze = utils.get_config('freeze')
            if freeze:
                freeze = unix_time_to_utc(freeze)
                if teamid != session.get('id'):
                    #print(session.get('id'),teamid,freeze)
                    solves = solves.filter(Solves.date < freeze)

            solves = solves.all()
            score = []
            cat = get_challenges()["cat"]
            for i in solves:
                score.append({
                    "id": i[0],
                    "score": i[2],
                    "cat": i[1],
                    "date": i[3]
                })

            for i in cat:
                if i not in [j["cat"] for j in score]:
                    #score.append({"score":0,"cat":i,"date":datetime.datetime.utcfromtimestamp(111111111111)})
                    score.append({
                        "score": 0,
                        "cat": i,
                        "date": None,
                        "id": -1
                    })

            score = sorted(score, key=lambda i: i["cat"])

            maxscore = 0
            temp = []
            catfil = []
            count = 0

            for c, i in enumerate(score):
                if chal_details[i['cat']] == i['score']:
                    temp.append(i)
                    catfil.append(i['cat'])
                    maxscore += i['score']
                    count += 1

            if maxscore == 0:
                maxscore = {i["date"]: i["score"] for i in score}
                date = max(maxscore, key=maxscore.get)
                maxscore = maxscore[date]
                cat = {i["cat"]: i["score"] for i in score}
                cat = max(cat, key=cat.get)
                catfil.append(cat)
            else:
                date = sorted(temp, key=lambda i: i['date'],
                              reverse=True)[0]['date']

            if date == None:
                continue

            # Check for the cat with the least date if there are multiple max values

            jstandings.append({
                'teamid': team[0],
                'cat': catfil,
                'solves': score,
                'name': escape(team[2]),
                'date': date,
                'state': count,
                'score': maxscore
            })
            jstandings = sorted(jstandings, key=lambda i: i["date"])
            #for i in jstandings:
            #    print(teamid,i['date'],i['score'])
            jstandings = sorted(jstandings,
                                key=lambda i: i["score"],
                                reverse=True)
            jstandings = sorted(jstandings,
                                key=lambda i: i["state"],
                                reverse=True)
            #print('next sort')
            #for i in jstandings:
            #    print(i['date'],i['score'])

        db.session.close()
        return jstandings