Skip to content

Commit

Permalink
Discard match results when activating previous stage (#995)
Browse files Browse the repository at this point in the history
  • Loading branch information
evroon authored Nov 7, 2024
1 parent 58470c4 commit a67db3a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
3 changes: 3 additions & 0 deletions backend/bracket/logic/scheduling/handle_stage_activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from bracket.models.db.stage_item_inputs import StageItemInputFinal, StageItemInputTentative
from bracket.models.db.team import Team
from bracket.models.db.util import StageWithStageItems
from bracket.sql.matches import clear_scores_for_matches_in_stage_item
from bracket.sql.rankings import get_ranking_for_stage_item
from bracket.sql.stage_item_inputs import (
get_stage_item_input_by_id,
Expand Down Expand Up @@ -131,6 +132,8 @@ async def update_matches_in_deactivated_stage(
Unsets the team_id for stage item inputs of the newly deactivated stage.
"""
for stage_item in deactivated_stage.stage_items:
await clear_scores_for_matches_in_stage_item(tournament_id, stage_item.id)

for stage_item_input in stage_item.inputs:
if stage_item_input.winner_from_stage_item_id is not None:
await sql_set_team_id_for_stage_item_input(tournament_id, stage_item_input.id, None)
25 changes: 24 additions & 1 deletion backend/bracket/sql/matches.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from bracket.database import database
from bracket.models.db.match import Match, MatchBody, MatchCreateBody
from bracket.models.db.tournament import Tournament
from bracket.utils.id_types import CourtId, MatchId, StageItemId
from bracket.utils.id_types import CourtId, MatchId, StageItemId, TournamentId


async def sql_delete_match(match_id: MatchId) -> None:
Expand Down Expand Up @@ -197,3 +197,26 @@ async def sql_get_match(match_id: MatchId) -> Match:
raise ValueError("Could not create stage")

return Match.model_validate(dict(result._mapping))


async def clear_scores_for_matches_in_stage_item(
tournament_id: TournamentId, stage_item_id: StageItemId
) -> None:
query = """
UPDATE matches
SET stage_item_input1_score = 0,
stage_item_input2_score = 0
FROM rounds
JOIN stage_items ON rounds.stage_item_id = stage_items.id
JOIN stages ON stages.id = stage_items.stage_id
WHERE rounds.id = matches.round_id
AND stages.tournament_id = :tournament_id
AND stage_items.id = :stage_item_id
"""
await database.execute(
query=query,
values={
"stage_item_id": stage_item_id,
"tournament_id": tournament_id,
},
)

0 comments on commit a67db3a

Please sign in to comment.