Returns (team_id, match_id) of the winner at this round. The match_id can be any of the three matches in the previous game.
(conn, next_round_num, next_index, color)
| 111 | |
| 112 | |
| 113 | def get_next_team_and_from(conn, next_round_num, next_index, color): |
| 114 | """ |
| 115 | Returns (team_id, match_id) of the winner at this round. The match_id |
| 116 | can be any of the three matches in the previous game. |
| 117 | """ |
| 118 | prev_round_num = next_round_num - 1 |
| 119 | if color == COLOR_RED: |
| 120 | prev_index = next_index * 2 |
| 121 | if color == COLOR_BLUE: |
| 122 | prev_index = next_index * 2 + 1 |
| 123 | |
| 124 | cur = conn.cursor() |
| 125 | cur.execute('SELECT status, id, red_team, blue_team FROM {} \ |
| 126 | WHERE round=%s AND index=%s;' |
| 127 | .format(TABLE_NAME), |
| 128 | (prev_round_num, prev_index)) |
| 129 | |
| 130 | matches = cur.fetchall() |
| 131 | if len(matches) != 3: |
| 132 | print("{} {} {}", next_round_num, next_index, color) |
| 133 | print(matches) |
| 134 | |
| 135 | _, match_id, red_team, blue_team = matches[0] |
| 136 | match_winners = { |
| 137 | red_team: 0, |
| 138 | blue_team: 0, |
| 139 | } |
| 140 | |
| 141 | for status, _, _, _ in matches: |
| 142 | if status == 'redwon': |
| 143 | match_winners[red_team] += 1 |
| 144 | elif status == 'bluewon': |
| 145 | match_winners[blue_team] += 1 |
| 146 | else: |
| 147 | raise Exception("Match should be finished.") |
| 148 | |
| 149 | if match_winners[red_team] > match_winners[blue_team]: |
| 150 | match_winner = red_team |
| 151 | else: |
| 152 | match_winner = blue_team |
| 153 | |
| 154 | return (match_winner, match_id) |
| 155 | |
| 156 | |
| 157 | def generate_bracket(teams: List[Team]) -> List[Team]: |