Skip to content

Commit

Permalink
fixup! fixup! fixup! Assign teams to subsequent elimination rounds
Browse files Browse the repository at this point in the history
  • Loading branch information
evroon committed Nov 8, 2024
1 parent 193237e commit 446a88c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 38 deletions.
74 changes: 38 additions & 36 deletions backend/bracket/logic/ranking/elimination.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
from typing import TypeVar

from bracket.logic.ranking.statistics import START_ELO, TeamStatistics
from bracket.models.db.match import MatchWithDetails, MatchWithDetailsDefinitive
from bracket.models.db.match import Match, MatchWithDetailsDefinitive
from bracket.models.db.ranking import Ranking
from bracket.models.db.stage_item import StageType
from bracket.models.db.util import RoundWithMatches, StageItemWithRounds
from bracket.sql.matches import (
sql_set_input_id_for_match_winner_input,
sql_set_input_ids_for_match,
)
from bracket.utils.id_types import (
MatchId,
PlayerId,
StageItemInputId,
TeamId,
Expand Down Expand Up @@ -111,42 +112,43 @@ def determine_team_ranking_for_stage_item(
async def update_teams_in_subsequent_elimination_rounds(
current_round: RoundWithMatches,
stage_item: StageItemWithRounds,
match_ids: set[MatchId],
) -> None:
affected_match_ids = {match.id for match in current_round.matches}
affected_matches: dict[MatchId, Match] = {

Check warning on line 117 in backend/bracket/logic/ranking/elimination.py

View check run for this annotation

Codecov / codecov/patch

backend/bracket/logic/ranking/elimination.py#L117

Added line #L117 was not covered by tests
match.id: match for match in current_round.matches if match.id in match_ids
}
subsequent_rounds = [round_ for round_ in stage_item.rounds if round_.id > current_round.id]
subsequent_rounds.sort(key=lambda round_: round_.id)

Check warning on line 121 in backend/bracket/logic/ranking/elimination.py

View check run for this annotation

Codecov / codecov/patch

backend/bracket/logic/ranking/elimination.py#L120-L121

Added lines #L120 - L121 were not covered by tests

for round_ in subsequent_rounds:
for match in round_.matches:
updated_input_ids = [match.stage_item_input1_id, match.stage_item_input2_id]

if match.stage_item_input1_winner_from_match_id in affected_match_ids:
referenced_match: MatchWithDetailsDefinitive | MatchWithDetails | None = next(
(
earlier_match
for earlier_match in current_round.matches
if match.stage_item_input1_winner_from_match_id == earlier_match.id
),
None,
for subsequent_match in round_.matches:
updated_input_ids: list[StageItemInputId | None] = [

Check warning on line 125 in backend/bracket/logic/ranking/elimination.py

View check run for this annotation

Codecov / codecov/patch

backend/bracket/logic/ranking/elimination.py#L123-L125

Added lines #L123 - L125 were not covered by tests
subsequent_match.stage_item_input1_id,
subsequent_match.stage_item_input2_id,
]
updated = False

Check warning on line 129 in backend/bracket/logic/ranking/elimination.py

View check run for this annotation

Codecov / codecov/patch

backend/bracket/logic/ranking/elimination.py#L129

Added line #L129 was not covered by tests

if subsequent_match.stage_item_input1_winner_from_match_id in affected_matches:
updated = True
winner = affected_matches[

Check warning on line 133 in backend/bracket/logic/ranking/elimination.py

View check run for this annotation

Codecov / codecov/patch

backend/bracket/logic/ranking/elimination.py#L131-L133

Added lines #L131 - L133 were not covered by tests
subsequent_match.stage_item_input1_winner_from_match_id
].get_winner()
updated_input_ids[0] = winner.id if winner is not None else None

Check warning on line 136 in backend/bracket/logic/ranking/elimination.py

View check run for this annotation

Codecov / codecov/patch

backend/bracket/logic/ranking/elimination.py#L136

Added line #L136 was not covered by tests

if subsequent_match.stage_item_input2_winner_from_match_id in affected_matches:
updated = True
winner = affected_matches[

Check warning on line 140 in backend/bracket/logic/ranking/elimination.py

View check run for this annotation

Codecov / codecov/patch

backend/bracket/logic/ranking/elimination.py#L138-L140

Added lines #L138 - L140 were not covered by tests
subsequent_match.stage_item_input2_winner_from_match_id
].get_winner()
updated_input_ids[1] = winner.id if winner is not None else None

Check warning on line 143 in backend/bracket/logic/ranking/elimination.py

View check run for this annotation

Codecov / codecov/patch

backend/bracket/logic/ranking/elimination.py#L143

Added line #L143 was not covered by tests

if updated:
await sql_set_input_ids_for_match(round_.id, subsequent_match.id, updated_input_ids)

Check warning on line 146 in backend/bracket/logic/ranking/elimination.py

View check run for this annotation

Codecov / codecov/patch

backend/bracket/logic/ranking/elimination.py#L145-L146

Added lines #L145 - L146 were not covered by tests

# Matches from this round also affect matches of the next round
affected_matches[subsequent_match.id] = subsequent_match.model_copy(

Check warning on line 149 in backend/bracket/logic/ranking/elimination.py

View check run for this annotation

Codecov / codecov/patch

backend/bracket/logic/ranking/elimination.py#L149

Added line #L149 was not covered by tests
update={
"stage_item_input1_id": updated_input_ids[0],
"stage_item_input2_id": updated_input_ids[1],
}
)
if referenced_match is not None:
winner = referenced_match.get_winner()
updated_input_ids[0] = winner.id if winner is not None else None

if match.stage_item_input2_winner_from_match_id in affected_match_ids:
referenced_match = next(
(
earlier_match
for earlier_match in current_round.matches
if match.stage_item_input2_winner_from_match_id == earlier_match.id
),
None,
)
if referenced_match is not None:
winner = referenced_match.get_winner()
updated_input_ids[1] = winner.id if winner is not None else None

await sql_set_input_id_for_match_winner_input(round_.id, match.id, updated_input_ids)

# Matches from this round also affect matches of the next round
for match in round_.matches:
affected_match_ids.add(match.id)
2 changes: 1 addition & 1 deletion backend/bracket/routes/matches.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,6 @@ async def update_match_by_id(
await reorder_matches_for_court(tournament, scheduled_matches, assert_some(match.court_id))

if stage_item.type == StageType.SINGLE_ELIMINATION:
await update_teams_in_subsequent_elimination_rounds(round_, stage_item)
await update_teams_in_subsequent_elimination_rounds(round_, stage_item, {match_id})

Check warning on line 176 in backend/bracket/routes/matches.py

View check run for this annotation

Codecov / codecov/patch

backend/bracket/routes/matches.py#L176

Added line #L176 was not covered by tests

return SuccessResponse()
2 changes: 1 addition & 1 deletion backend/bracket/sql/matches.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ async def sql_update_match(match_id: MatchId, match: MatchBody, tournament: Tour
)


async def sql_set_input_id_for_match_winner_input(
async def sql_set_input_ids_for_match(
round_id: RoundId, match_id: MatchId, input_ids: list[StageItemInputId | None]
) -> None:
query = """

Check warning on line 124 in backend/bracket/sql/matches.py

View check run for this annotation

Codecov / codecov/patch

backend/bracket/sql/matches.py#L124

Added line #L124 was not covered by tests
Expand Down

0 comments on commit 446a88c

Please sign in to comment.