1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| def get_score_by_challenge_id(challenges, challenge_id): for challenge in challenges: if challenge.id == challenge_id: return challenge.value return None
def get_matrix_standings(): challenges = Challenges.query.filter(*[]).order_by(Challenges.id.asc()).all() solves = db.session.query(Solves.date.label('date'), Solves.challenge_id.label('challenge_id'), Solves.user_id.label('user_id'), Solves.team_id.label('team_id')).all() awards = db.session.query(Awards.user_id.label('user_id'), Awards.team_id.label('team_id'), Awards.value.label('value'), Awards.date.label('date')).all() teams = db.session.query(Teams.id.label('team_id'), Teams.name.label('name'), Teams.hidden.label('hidden'), Teams.banned.label('banned')).all() users = db.session.query(Users.id.label('user_id'), Users.name.label('name'), Users.sid.label('sid'), Users.hidden.label('hidden'), Users.banned.label('banned')).all() freeze = utils.get_config('freeze') mode = get_config("user_mode") if freeze: freeze = unix_time_to_utc(freeze) solves = solves.filter(Solves.date < freeze) awards = awards.filter(Awards.date < freeze) top_solves = defaultdict(list) sorted_solves = sorted(solves, key=lambda x: (x.challenge_id, x.date)) for solve in sorted_solves: challenge_id = solve.challenge_id if len(top_solves[challenge_id]) >= 3: continue user = next((user for user in users if user.user_id == solve.user_id), None) if not user: continue if user.hidden or user.banned: continue
if mode == TEAMS_MODE: team = next((team for team in teams if team.team_id == solve.team_id), None) if not team: continue if team.hidden or team.banned: continue
top_solves[challenge_id].append({ 'date': solve.date, 'user_id': solve.user_id, 'team_id': solve.team_id }) if mode == TEAMS_MODE: matrix_scores = [] for team in teams: if team.hidden or team.banned: continue team_solves = [solve for solve in solves if solve[3] == team.team_id] total_score = 0 team_status = [] for solve in team_solves: challenge_id = solve[1] rank = 4 for index, top_solve in enumerate(top_solves[challenge_id]): if top_solve['team_id'] == team.team_id: rank = index + 1 score = get_score_by_challenge_id(challenges, challenge_id) if rank == 1: score *= Decimal('1.1') elif rank == 2: score *= Decimal('1.05') elif rank == 3: score *= Decimal('1.03') total_score += score
team_status.append({'challenge_id': challenge_id, 'rank': rank})
award_value = 0 for award in awards: if award.team_id == team.team_id: total_score += award.value award_value += award.value
matrix_scores.append( {'name': team.name, 'id': team.team_id, 'total_score': total_score, 'challenge_solved': team_status, 'award_value': award_value}) matrix_scores.sort(key=lambda x: x['total_score'], reverse=True) return matrix_scores else: matrix_scores = [] for user in users: if user.hidden or user.banned: continue user_solves = [solve for solve in solves if solve[2] == user.user_id] total_score = 0 user_status = [] for solve in user_solves: challenge_id = solve[1] rank = 4 for index, top_solve in enumerate(top_solves[challenge_id]): if top_solve['user_id'] == user.user_id: rank = index + 1 score = get_score_by_challenge_id(challenges, challenge_id) if rank == 1: score *= Decimal('1.1') elif rank == 2: score *= Decimal('1.05') elif rank == 3: score *= Decimal('1.03') total_score += score
user_status.append({'challenge_id': challenge_id, 'rank': rank}) award_value = 0 if get_config("matrix:score_switch"): if user.sid: if str(user.sid[:4]) in str(get_config("matrix:score_grade")): total_score += get_config("matrix:score_num") award_value += get_config("matrix:score_num") for award in awards: if award.user_id == user.user_id: total_score += award.value award_value += award.value
matrix_scores.append( {'name': user.name, 'id': user.user_id, 'total_score': total_score, 'challenge_solved': user_status, 'award_value': award_value}) matrix_scores.sort(key=lambda x: x['total_score'], reverse=True) return matrix_scores
|