Added seperate bracket/node logic

This commit is contained in:
2026-02-12 16:03:10 +01:00 Verified
parent 7cbb7faca8
commit bb68e326cd
13 changed files with 540 additions and 530 deletions
+13 -17
View File
@@ -13,7 +13,6 @@ from ...database import get_db
from . import router
# --- Helper: Centralize Auth Logic ---
def _check_auth(t: models.Tournament, user: Optional[str], code: Optional[str]):
is_admin = user is not None
code_matches = code is not None and str(code).strip() == str(t.code).strip()
@@ -44,11 +43,9 @@ async def report_score(
raise HTTPException(400, "No sets submitted")
_apply_score(match, report.sets)
flag_modified(match, "sets")
logic.refresh_bracket(t)
logic.update_schedule(t)
db.commit()
logic.advance_flow(db, t)
await send_ws_update(id)
return SUCCESS
@@ -63,8 +60,9 @@ async def edit_score(
user: Optional[str] = Depends(get_optional_user),
):
"""
Allows correcting a score without resetting the match status logic entirely,
or just re-applying the new sets.
Allows correcting a score.
Note: If the winner changes, 'advance_flow' might need to handle
undoing previous advancements, but for now we just re-run the flow.
"""
t = crud.get_tournament(db, id)
if not t:
@@ -80,9 +78,8 @@ async def edit_score(
_apply_score(match, report.sets)
flag_modified(match, "sets")
logic.refresh_bracket(t)
logic.update_schedule(t)
db.commit()
logic.advance_flow(db, t)
await send_ws_update(id)
return SUCCESS
@@ -106,14 +103,11 @@ async def clear_score(
if not match:
raise HTTPException(404, "Match not found")
match.winner = None
match.status = MatchStatus.PENDING.value
match.winner_team_id = None
match.status = MatchStatus.PENDING
match.sets = []
flag_modified(match, "sets")
logic.refresh_bracket(t)
logic.update_schedule(t)
db.commit()
await send_ws_update(id)
@@ -129,9 +123,9 @@ def _apply_score(match: models.Match, sets: List[schemas.SetScore]):
p2_wins = sum(1 for s in sets if s.p2 > s.p1)
if p1_wins > p2_wins:
match.winner = match.p1_name
match.winner_team_id = match.p1_team_id
elif p2_wins > p1_wins:
match.winner = match.p2_name
match.winner_team_id = match.p2_team_id
else:
p1_points = sum(s.p1 for s in sets)
p2_points = sum(s.p2 for s in sets)
@@ -139,7 +133,9 @@ def _apply_score(match: models.Match, sets: List[schemas.SetScore]):
if p1_points == p2_points:
raise HTTPException(400, "Absolute tie: Sets and Points are equal.")
match.winner = match.p1_name if p1_points > p2_points else match.p2_name
match.winner_team_id = (
match.p1_team_id if p1_points > p2_points else match.p2_team_id
)
match.status = MatchStatus.FINISHED.value
match.status = MatchStatus.FINISHED
match.sets = [s.model_dump() for s in sets]