Added seperate bracket/node logic
This commit is contained in:
@@ -3,4 +3,4 @@ from fastapi import APIRouter
|
||||
|
||||
router = APIRouter(prefix="/tournaments", tags=["Tournaments"])
|
||||
|
||||
from . import report, courts, matches, teams, tournaments
|
||||
from . import tournaments, teams, courts, matches, report, bracket
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
# backend/app/routes/tournaments/bracket.py
|
||||
from fastapi import Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ... import crud, schemas
|
||||
from ...database import get_db
|
||||
from . import router
|
||||
|
||||
|
||||
@router.get("/{id}/bracket", response_model=list[schemas.BracketNodeOut])
|
||||
def get_tournament_bracket(id: str, db: Session = Depends(get_db)):
|
||||
t = crud.get_tournament(db, id)
|
||||
if not t:
|
||||
raise HTTPException(404, "Tournament not found")
|
||||
return t.nodes
|
||||
@@ -1,12 +1,11 @@
|
||||
# backend/app/routes/tournaments/courts.py
|
||||
|
||||
from fastapi import Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ... import crud, schemas
|
||||
from ...constants import SUCCESS
|
||||
from ...core.websocket_manager import send_ws_update
|
||||
from ...core.auth import get_current_user
|
||||
from ...core.websocket_manager import send_ws_update
|
||||
from ...database import get_db
|
||||
from . import router
|
||||
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
# backend/app/routes/tournaments/teams.py
|
||||
|
||||
from fastapi import Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ... import crud, schemas
|
||||
from ...constants import SUCCESS
|
||||
from ...core.websocket_manager import send_ws_update
|
||||
from ...core.auth import get_current_user
|
||||
from ...core.websocket_manager import send_ws_update
|
||||
from ...database import get_db
|
||||
from . import router
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ from ...core.auth import get_current_user
|
||||
from . import router
|
||||
|
||||
|
||||
@router.post("", response_model=schemas.TournamentOut)
|
||||
@router.post("", response_model=schemas.TournamentUpdateResponse)
|
||||
async def create_tournament(
|
||||
data: schemas.TournamentCreate,
|
||||
db: Session = Depends(get_db),
|
||||
|
||||
Reference in New Issue
Block a user