def load_team_score(tid): """Get the score for a team. Looks for a cached team score, if not found we query all correct submissions by the team and add up their basescores if they exist. Cache the result. """ score = cache.get('teamscore_' + tid) if score is None: problems = problem.load_problems() pscore = {p['pid']: p['basescore'] for p in problems} solved = problem.get_solved_problems(tid) score = dict() score['score'] = sum(pscore[pid] for pid in solved) # TODO: calculate time penalty submission = list(db.submissions.find( { "tid": tid, "correct": True, "pid": {"$ne": "wait_re"}, "timestamp": {"$gt": ctf_start}, "timestamp": {"$lt": ctf_end} }, { "_id": 0, "pid": 1, "timestamp": 1 })) time_penalty = max([0] + [s['timestamp'] for s in submission]) score['time_penalty'] = time_penalty cache.set('teamscore_' + tid, json.dumps(score), 60 * 60) else: score = json.loads(score) return score
def get_teams_scoreboard_cached(teams, cache_key): """Gets the cached scoreboard of teams. Kind of a hack, tells the front end to look for a static page scoreboard rather than sending a 2000+ length array that the front end must parse. """ scoreboard = cache.get(cache_key) if scoreboard is None: scoreboard = dict() problems = problem.load_problems() problems = [{ 'pid': p['pid'], 'displayname': p['displayname'] } for p in problems] pids = [p['pid'] for p in problems] team_scores = [{ "teamname": t['teamname'], "score": load_team_score(t['tid']), "solved": [pids.index(p) for p in problem.get_solved_problems(t['tid'])] } for t in teams] team_scores.sort(key=lambda x: (-x['score']['score'], x['score']['time_penalty'])) scoreboard['problems'] = problems scoreboard['teamname'] = [ts['teamname'] for ts in team_scores] scoreboard['score'] = [ts['score']['score'] for ts in team_scores] scoreboard['solved'] = [ts['solved'] for ts in team_scores] cache.set(cache_key, json.dumps(scoreboard), 60 * 60) else: scoreboard = json.loads(scoreboard) return scoreboard
def get_solved_problems_hook(): return problem.get_solved_problems(session['tid'])