Better bracket logic

This commit is contained in:
2026-02-14 01:04:54 +01:00 Verified
parent 87d3ea5ef0
commit 7b878b3abe
32 changed files with 1326 additions and 1348 deletions
+7 -12
View File
@@ -7,23 +7,18 @@ class TournamentTypes(str, Enum):
DOUBLE = "Double"
class BracketType(str, Enum):
WINNERS = "Winners"
LOSERS = "Losers"
FINALS = "Finals"
class MatchSourceType(str, Enum):
WINNER = "Winner"
LOSER = "Loser"
class MatchStatus(str, Enum):
PENDING = "Pending"
SCHEDULED = "Scheduled"
PENDING = "Pending"
FINISHED = "Finished"
class BracketTypes(str, Enum):
WB = "Winner"
LB = "Loser"
FINALS = "Finals"
class WinnerSide(str, Enum):
P1 = "p1"
P2 = "p2"
+79 -68
View File
@@ -1,22 +1,11 @@
# backend/app/core/brackets.py
import math
from typing import Optional
class Match:
def __init__(self, match_id: int, name: str):
self.id = match_id
self.name = name
self.next_win: Optional["Match"] = None
self.next_loss: Optional["Match"] = None
self.players: list[Optional[int]] = [None, None]
self.is_bye: bool = False
self.ghost_count: int = 0
from ..constants import BracketTypes
from .structures import Match
class BracketGenerator:
def __init__(self):
self.matches_dict = {}
self.match_counter = 1
@@ -30,23 +19,24 @@ class BracketGenerator:
size = 2 ** math.ceil(math.log2(num_players))
seeds = self._generate_seeding_indices(num_players)
bracket = self._create_tree_bracket(size, "WB")
# Create Winners Bracket
bracket = self._create_tree_bracket(size)
for i, match in enumerate(bracket[0]):
match.players = [seeds[i * 2], seeds[i * 2 + 1]]
match.teams = [seeds[i * 2], seeds[i * 2 + 1]]
lb_rounds = []
if double_elimination:
lb_rounds = self._create_strict_lb(size, bracket)
if double_elimination:
self._create_de_finals(bracket, lb_rounds)
else:
self._create_se_third_place(bracket)
self._apply_ghost_logic(bracket[0], num_players)
# Propagate ghosts (this marks matches as is_bye=True)
self._resolve_byes(num_players)
all_matches = sorted(self.matches_dict.values(), key=lambda x: x.id)
# Filter out byes so they don't become DB rows
return [m for m in all_matches if not m.is_bye]
def _generate_seeding_indices(self, num_players: int) -> list[int]:
@@ -58,7 +48,7 @@ class BracketGenerator:
curr *= 2
return seeds
def _create_tree_bracket(self, size: int, prefix: str) -> list[list[Match]]:
def _create_tree_bracket(self, size: int) -> list[list[Match]]:
rounds: list[list[Match]] = []
current_size = size // 2
r_num = 1
@@ -66,8 +56,7 @@ class BracketGenerator:
while current_size >= 1:
round_matches: list[Match] = []
for _ in range(current_size):
name = f"{prefix} Round {r_num}"
m = Match(self.match_counter, name)
m = Match(self.match_counter, BracketTypes.WB, r_num)
self.matches_dict[m.id] = m
round_matches.append(m)
self.match_counter += 1
@@ -75,15 +64,10 @@ class BracketGenerator:
current_size //= 2
r_num += 1
if rounds:
rounds[-1][0].name = "Grand Final"
if len(rounds) > 1:
for m in rounds[-2]:
m.name = f"{prefix} Semifinal"
for r in range(len(rounds) - 1):
for i, match in enumerate(rounds[r]):
match.next_win = rounds[r + 1][i // 2]
match.next_win_slot = i % 2
return rounds
@@ -104,102 +88,129 @@ class BracketGenerator:
round_matches = []
for _ in range(current_lb_size):
m = Match(self.match_counter, f"LB Round {r}")
m = Match(self.match_counter, BracketTypes.LB, r)
self.matches_dict[m.id] = m
round_matches.append(m)
self.match_counter += 1
lb_rounds.append(round_matches)
if lb_rounds:
lb_rounds[-1][0].name = "Losers Final (3rd Place Match)"
# Link LB rounds
for r in range(len(lb_rounds) - 1):
curr = lb_rounds[r]
next_r = lb_rounds[r + 1]
for i, match in enumerate(curr):
if len(curr) == len(next_r):
# 1-to-1 Mapping case (e.g. 4 matches -> 4 matches)
match.next_win = next_r[i]
# FIX: Always use slot 1 (Slot 0 is taken by WB Drop-down)
match.next_win_slot = 1
else:
# 2-to-1 Mapping case (e.g. 4 matches -> 2 matches)
match.next_win = next_r[i // 2]
match.next_win_slot = i % 2
# Link WB Losers -> LB
for r in range(num_wb_rounds):
wb_round = wb_rounds[r]
if r == 0:
lb_target_idx = 0
else:
lb_target_idx = (r * 2) - 1
lb_target_idx = 0 if r == 0 else (r * 2) - 1
if lb_target_idx >= len(lb_rounds):
break
lb_round = lb_rounds[lb_target_idx]
for i, wb_match in enumerate(wb_round):
if r == 0:
target = lb_round[i // 2]
wb_match.next_loss_slot = i % 2
else:
# Drop-in round
target = lb_round[i] if i < len(lb_round) else lb_round[0]
# Drop-ins take Slot 0
wb_match.next_loss_slot = 0
wb_match.next_loss = target
return lb_rounds
def _create_se_third_place(self, wb_rounds: list[list[Match]]) -> None:
"""Creates a standalone 3rd place match for Single Elim"""
if len(wb_rounds) < 2:
return
semis = wb_rounds[-2]
third_place_match = Match(self.match_counter, "3rd Place Match")
final_round_idx = wb_rounds[-1][0].round_number
third_place_match = Match(
self.match_counter, BracketTypes.FINALS, final_round_idx
)
self.matches_dict[third_place_match.id] = third_place_match
self.match_counter += 1
for m in semis:
m.next_loss = third_place_match
for i, sm in enumerate(semis):
sm.next_loss = third_place_match
sm.next_loss_slot = i % 2
def _create_de_finals(
self, wb_rounds: list[list[Match]], lb_rounds: list[list[Match]]
) -> None:
"""Links Double Elim Grand Final"""
wb_final = wb_rounds[-1][0]
wb_final.name = "Winners Final"
final_round_idx = wb_final.round_number + 1
gf = Match(self.match_counter, "Grand Final")
gf = Match(self.match_counter, BracketTypes.FINALS, final_round_idx)
self.matches_dict[gf.id] = gf
self.match_counter += 1
wb_final.next_win = gf
wb_final.next_win_slot = 0
if lb_rounds:
lb_rounds[-1][0].next_win = gf
reset = Match(self.match_counter, "Grand Final Reset")
lb_rounds[-1][0].next_win_slot = 1
reset = Match(self.match_counter, BracketTypes.FINALS, final_round_idx + 1)
self.matches_dict[reset.id] = reset
self.match_counter += 1
gf.next_loss = reset
gf.next_loss_slot = 0
def _apply_ghost_logic(
self, round_one_matches: list[Match], num_players: int
) -> None:
def _resolve_byes(self, num_players: int) -> None:
"""
Injects ghosts into WB Round 1 and lets them flow recursively.
Iterates through all matches to identify 'Ghost' players (seeds > num_players).
If a match has a ghost, it is marked as a Bye, and the real player is
automatically pushed to the next match.
"""
for m in round_one_matches:
p1, p2 = m.players
if p1 and p1 > num_players:
self._add_ghost(m)
if p2 and p2 > num_players:
self._add_ghost(m)
all_matches: list[Match] = sorted(
self.matches_dict.values(), key=lambda x: x.id
)
def _add_ghost(self, match: Match) -> None:
"""
Recursively propagates a ghost through the bracket.
"""
match.ghost_count += 1
match.is_bye = True
for m in all_matches:
if m.is_bye:
continue
if match.next_loss:
self._add_ghost(match.next_loss)
p1, p2 = m.teams
if match.ghost_count == 2:
if match.next_win:
self._add_ghost(match.next_win)
p1_is_ghost = p1 is not None and p1 > num_players
p2_is_ghost = p2 is not None and p2 > num_players
if p1_is_ghost or p2_is_ghost:
m.is_bye = True
winner = None
loser = None # Ghost
if p1_is_ghost and p2_is_ghost:
winner = None
loser = None
elif p1_is_ghost:
winner = p2
loser = p1
elif p2_is_ghost:
winner = p1
loser = p2
# Propagate Winner (Advance to next round)
if m.next_win:
m.next_win.teams[m.next_win_slot] = winner
# Propagate Loser (Drop to LB)
# Important: We push the GHOST to the LB.
# When the loop reaches that LB match later, it will see the ghost
# and mark THAT match as a bye too, recursively cleaning the bracket.
if m.next_loss:
m.next_loss.teams[m.next_loss_slot] = loser
+19
View File
@@ -0,0 +1,19 @@
# backend/app/core/structures.py
from dataclasses import dataclass, field
from typing import Optional
from ..constants import BracketTypes
@dataclass
class Match:
id: int
bracket_type: BracketTypes
round_number: int
next_win: Optional["Match"] = None
next_loss: Optional["Match"] = None
next_win_slot: int = 0
next_loss_slot: int = 0
teams: list[Optional[int]] = field(default_factory=lambda: [None, None])
is_bye: bool = False
ghost_count: int = 0
+2 -2
View File
@@ -58,6 +58,6 @@ class MermaidLive:
@staticmethod
def _print_node(m: Match, style: str) -> None:
label = m.name
if m.players[0] and m.players[1]:
label += f" ({m.players[0]} vs {m.players[1]})"
if m.teams[0] and m.teams[1]:
label += f" ({m.teams[0]} vs {m.teams[1]})"
print(f' M{m.id}["{label}"]:::{style}')
+18 -33
View File
@@ -7,39 +7,16 @@ from . import logic, models, schemas
def _rebuild_structure(db: Session, t: models.Tournament):
"""
Nukes existing nodes/matches and regenerates them based on current teams.
Used when teams are added/removed.
"""
t.matches = []
for m in t.matches:
db.delete(m)
db.flush()
t.nodes = []
db.flush()
node_data = logic.generate_bracket_nodes(t)
for nd in node_data:
db.add(models.BracketNode(**nd, tournament_id=t.id))
logic.generate_bracket(db, t)
db.flush()
db.refresh(t)
logic.initialize_seeding(db, t)
logic.update_schedule_times(db, t)
def get_tournaments(db: Session):
return db.query(models.Tournament).all()
def get_tournament(db: Session, tournament_id: str):
return (
db.query(models.Tournament)
.filter(models.Tournament.id == tournament_id)
.first()
)
def create_tournament(db: Session, data: schemas.TournamentCreate):
t_id = str(uuid4())[:8]
new_t = models.Tournament(
@@ -56,19 +33,27 @@ def create_tournament(db: Session, data: schemas.TournamentCreate):
db.add(new_t)
db.commit()
node_data = logic.generate_bracket_nodes(new_t)
for nd in node_data:
db.add(models.BracketNode(**nd, tournament_id=t_id))
db.commit()
db.refresh(new_t)
logic.initialize_seeding(db, new_t)
# Generate Bracket
logic.generate_bracket(db, new_t)
logic.update_schedule_times(db, new_t)
db.commit()
db.refresh(new_t)
return new_t
def get_tournaments(db: Session):
return db.query(models.Tournament).all()
def get_tournament(db: Session, tournament_id: str):
return (
db.query(models.Tournament)
.filter(models.Tournament.id == tournament_id)
.first()
)
def delete_tournament(db: Session, tournament_id: str) -> bool:
t = get_tournament(db, tournament_id)
if not t:
+179 -302
View File
@@ -1,333 +1,210 @@
# backend/app/logic.py
import math
from datetime import datetime, timedelta
from typing import Optional
from collections import defaultdict, deque
from datetime import timedelta
from uuid import uuid4
from sqlalchemy.orm import Session
from . import models
from .constants import (
BracketType,
MatchSourceType,
MatchStatus,
TournamentTypes,
WinnerSide,
)
from .constants import MatchStatus, TournamentTypes
from .core.brackets import BracketGenerator
def get_seeded_positions(num_slots, teams):
seeds = [1, 2]
while len(seeds) < num_slots:
next_seeds = []
for s in seeds:
next_seeds.append(s)
next_seeds.append(2 * len(seeds) + 1 - s)
seeds = next_seeds
return [teams[s - 1] if s <= len(teams) else None for s in seeds]
def generate_bracket_nodes(t: models.Tournament) -> list[dict]:
"""
Generates the skeleton (BracketNodes). Does NOT create Matches.
"""
def generate_bracket(db: Session, t: models.Tournament):
teams = t.teams
count = len(teams)
if count < 2:
return []
if not teams:
return
power = math.ceil(math.log2(count)) if count > 0 else 1
size = 2**power
gen = BracketGenerator()
is_double = t.type == TournamentTypes.DOUBLE
abstract_matches = gen.generate(len(teams), double_elimination=is_double)
id_map = {m.id: str(uuid4()) for m in abstract_matches}
nodes = []
display_counter = 1
def resolve_target(match_node):
curr = match_node
while curr and curr.is_bye:
curr = curr.next_win
return curr
def make_id():
return str(uuid4())
db_matches = []
friendly_counter = 1
class NodeRef:
def __init__(self, bracket, round_n):
self.id = make_id()
self.bracket = bracket
self.round = round_n
self.display_num = 0
for m in abstract_matches:
real_win = resolve_target(m.next_win)
real_loss = resolve_target(m.next_loss)
self.next_win: Optional["NodeRef"] = None
self.next_loss: Optional["NodeRef"] = None
self.src_p1: Optional["NodeRef"] = None
self.src_p2: Optional["NodeRef"] = None
initial_status = MatchStatus.SCHEDULED
p1_id = None
p2_id = None
self.src_p1_type: Optional[MatchSourceType] = None
self.src_p2_type: Optional[MatchSourceType] = None
p1_seed = m.teams[0]
p2_seed = m.teams[1]
def to_dict(self):
return {
"id": self.id,
"bracket_type": self.bracket,
"round_number": self.round,
"display_number": self.display_num,
"winner_next_node_id": self.next_win.id if self.next_win else None,
"loser_next_node_id": self.next_loss.id if self.next_loss else None,
"source_p1_node_id": self.src_p1.id if self.src_p1 else None,
"source_p2_node_id": self.src_p2.id if self.src_p2 else None,
"source_p1_type": self.src_p1_type,
"source_p2_type": self.src_p2_type,
}
if p1_seed and p1_seed <= len(teams):
p1_id = teams[p1_seed - 1].id
if p2_seed and p2_seed <= len(teams):
p2_id = teams[p2_seed - 1].id
wb_rounds = power
wb_layers = {r: [] for r in range(1, wb_rounds + 1)}
if p1_id and p2_id:
initial_status = MatchStatus.PENDING
for r in range(1, wb_rounds + 1):
for _ in range(size // (2**r)):
n = NodeRef(BracketType.WINNERS, r)
wb_layers[r].append(n)
new_match = models.Match(
id=id_map[m.id],
tournament_id=t.id,
match_number=friendly_counter,
bracket_type=m.bracket_type,
round_number=m.round_number,
status=initial_status,
p1_team_id=p1_id,
p2_team_id=p2_id,
winner_next_match_id=id_map[real_win.id] if real_win else None,
loser_next_match_id=id_map[real_loss.id] if real_loss else None,
)
db_matches.append(new_match)
friendly_counter += 1
# Link Winners
for r in range(1, wb_rounds):
for i, node in enumerate(wb_layers[r]):
target = wb_layers[r + 1][i // 2]
node.next_win = target
if i % 2 == 0:
target.src_p1 = node
target.src_p1_type = MatchSourceType.WINNER
else:
target.src_p2 = node
target.src_p2_type = MatchSourceType.WINNER
lb_layers = {}
if t.type == TournamentTypes.DOUBLE and size >= 4:
lb_rounds = (wb_rounds - 1) * 2
lb_layers = {r: [] for r in range(1, lb_rounds + 1)}
current_count = size // 4
for r in range(1, lb_rounds + 1):
for _ in range(current_count):
n = NodeRef(BracketType.LOSERS, r)
lb_layers[r].append(n)
if r % 2 == 0:
current_count //= 2
for r in range(1, lb_rounds):
for i, node in enumerate(lb_layers[r]):
target = lb_layers[r + 1][i] if r % 2 != 0 else lb_layers[r + 1][i // 2]
node.next_win = target
if r % 2 != 0:
target.src_p1 = node
target.src_p1_type = MatchSourceType.WINNER
else:
if i % 2 == 0:
target.src_p1 = node
target.src_p1_type = MatchSourceType.WINNER
else:
target.src_p2 = node
target.src_p2_type = MatchSourceType.WINNER
# Link Drop-down (Winners -> Losers)
for r in range(1, wb_rounds):
drop_round = 1 if r == 1 else (r - 1) * 2
wb_layer_nodes = wb_layers[r]
lb_layer_nodes = lb_layers[drop_round]
for i, wb_node in enumerate(wb_layer_nodes):
target = None
if r == 1:
target = lb_layer_nodes[i // 2]
else:
if i < len(lb_layer_nodes):
target = lb_layer_nodes[i]
else:
target = lb_layer_nodes[-1]
wb_node.next_loss = target
slot = WinnerSide.P1 if (r == 1 and i % 2 == 0) else WinnerSide.P2
if slot == WinnerSide.P1:
target.src_p1 = wb_node
target.src_p1_type = MatchSourceType.LOSER
else:
target.src_p2 = wb_node
target.src_p2_type = MatchSourceType.LOSER
# Finals
final_node = NodeRef(BracketType.FINALS, 1)
wb_final = wb_layers[wb_rounds][0]
lb_final = lb_layers[lb_rounds][0]
wb_final.next_loss = lb_final
wb_final.next_win = final_node
lb_final.next_win = final_node
final_node.src_p1 = wb_final
final_node.src_p1_type = MatchSourceType.WINNER
final_node.src_p2 = lb_final
final_node.src_p2_type = MatchSourceType.WINNER
all_nodes = []
for r in sorted(wb_layers.keys()):
all_nodes.extend(wb_layers[r])
for r in sorted(lb_layers.keys()):
all_nodes.extend(lb_layers[r])
all_nodes.append(final_node)
else:
all_nodes = []
for r in sorted(wb_layers.keys()):
all_nodes.extend(wb_layers[r])
for n in all_nodes:
n.display_num = display_counter
display_counter += 1
nodes.append(n.to_dict())
return nodes
def initialize_seeding(db: Session, t: models.Tournament):
seeded_teams = get_seeded_positions(
2 ** math.ceil(math.log2(t.team_count)), t.teams
)
r1_nodes = [
n
for n in t.nodes
if n.bracket_type == BracketType.WINNERS and n.round_number == 1
]
r1_nodes.sort(key=lambda x: x.display_number)
for i, node in enumerate(r1_nodes):
t1 = seeded_teams[i * 2]
t2 = seeded_teams[i * 2 + 1]
node.p1_team_id = t1.id if t1 else None
node.p2_team_id = t2.id if t2 else None
advance_flow(db, t)
def advance_flow(db: Session, t: models.Tournament):
changes = True
while changes:
changes = False
for node in t.nodes:
if node.round_number == 1 and node.bracket_type == BracketType.WINNERS:
if node.p1_team_id and not node.p2_team_id:
if _move_team(
db,
t,
node.p1_team_id,
node.winner_next_node_id,
MatchSourceType.WINNER,
):
pass
elif node.p2_team_id and not node.p1_team_id:
if _move_team(
db,
t,
node.p2_team_id,
node.winner_next_node_id,
MatchSourceType.WINNER,
):
pass
# 2. MATCH CREATION
if node.p1_team_id and node.p2_team_id:
if not node.match:
m_id = str(uuid4())
new_match = models.Match(
id=m_id,
tournament_id=t.id,
node_id=node.id,
p1_team_id=node.p1_team_id,
p2_team_id=node.p2_team_id,
status=MatchStatus.PENDING,
court_id=node.planned_court_id,
start_time=node.planned_start_time,
)
db.add(new_match)
db.commit()
db.refresh(node)
changes = True
# 3. MATCH RESULT PROPAGATION
elif (
node.match.status == MatchStatus.FINISHED
and node.match.winner_team_id
):
winner_id = node.match.winner_team_id
loser_id = (
node.match.p1_team_id
if winner_id == node.match.p2_team_id
else node.match.p2_team_id
)
if _move_team(
db,
t,
winner_id,
node.winner_next_node_id,
MatchSourceType.WINNER,
):
changes = True
if _move_team(
db, t, loser_id, node.loser_next_node_id, MatchSourceType.LOSER
):
changes = True
def _move_team(db, t, team_id, target_node_id, source_type):
if not target_node_id or not team_id:
return False
target = next((n for n in t.nodes if n.id == target_node_id), None)
if not target:
return False
updated = False
if target.source_p1_type == source_type and not target.p1_team_id:
target.p1_team_id = team_id
updated = True
elif target.source_p2_type == source_type and not target.p2_team_id:
target.p2_team_id = team_id
updated = True
elif not target.p1_team_id:
target.p1_team_id = team_id
updated = True
elif not target.p2_team_id:
target.p2_team_id = team_id
updated = True
if updated:
db.add(target)
db.commit()
return updated
db.add_all(db_matches)
db.flush()
def update_schedule_times(db: Session, t: models.Tournament):
nodes = sorted(t.nodes, key=lambda n: n.display_number)
current_time = t.timestamp
courts = t.courts
if not courts:
if not t.courts:
return
court_timers: dict[int, datetime] = {c.id: current_time for c in courts}
matches = t.matches
if not matches:
return
for node in nodes:
if node.planned_start_time:
continue
# 1. Build Dependency Graph
adj = defaultdict(list)
in_degree = {m.id: 0 for m in matches}
match_map = {m.id: m for m in matches}
best_court_id = min(court_timers, key=lambda k: court_timers[k])
start = court_timers[best_court_id]
for m in matches:
if m.id not in adj:
adj[m.id] = []
node.planned_court_id = best_court_id
node.planned_start_time = start
if m.winner_next_match_id and m.winner_next_match_id in match_map:
adj[m.id].append(m.winner_next_match_id)
in_degree[m.winner_next_match_id] += 1
court_timers[best_court_id] = start + timedelta(minutes=t.duration)
if m.loser_next_match_id and m.loser_next_match_id in match_map:
adj[m.id].append(m.loser_next_match_id)
in_degree[m.loser_next_match_id] += 1
if node.match and node.match.status == MatchStatus.PENDING:
node.match.court_id = best_court_id
node.match.timestamp = start
# 2. Initialize constraints
# FIX: Ensure tournament_start is naive (no timezone) to match SQLite DB datetimes
tournament_start = t.timestamp
if tournament_start.tzinfo is not None:
tournament_start = tournament_start.replace(tzinfo=None)
match_earliest_start = {m.id: tournament_start for m in matches}
court_timers = {c.id: tournament_start for c in t.courts}
# 3. Topological Sort
queue = deque([m.id for m in matches if in_degree[m.id] == 0])
# Sort initial batch to prioritize logical order
initial_order = sorted(
list(queue),
key=lambda mid: (match_map[mid].bracket_type, match_map[mid].match_number),
)
queue = deque(initial_order)
while queue:
current_id = queue.popleft()
match = match_map[current_id]
# FIX: Ensure DB start_time is treated as naive
m_start = match.start_time
if m_start and m_start.tzinfo is not None:
m_start = m_start.replace(tzinfo=None)
if match.status == MatchStatus.FINISHED:
# If finished, propagate actual time
if not m_start:
m_start = tournament_start
actual_end = m_start + timedelta(minutes=t.duration)
else:
# Schedule: Must wait for dependencies (min_start) AND court availability
min_start = match_earliest_start[current_id]
best_court_id = min(
court_timers, key=lambda k: max(court_timers[k], min_start)
)
scheduled_start = max(court_timers[best_court_id], min_start)
match.court_id = best_court_id
match.start_time = scheduled_start
actual_end = scheduled_start + timedelta(minutes=t.duration)
court_timers[best_court_id] = actual_end
for child_id in adj[current_id]:
if actual_end > match_earliest_start[child_id]:
match_earliest_start[child_id] = actual_end
in_degree[child_id] -= 1
if in_degree[child_id] == 0:
queue.append(child_id)
db.commit()
def advance_winner(db: Session, match: models.Match, winner_id: int):
if not winner_id:
return
loser_id = match.p1_team_id if match.p1_team_id != winner_id else match.p2_team_id
def update_next_match(next_match, team_id):
if not next_match:
return
if not next_match.p1_team_id:
next_match.p1_team_id = team_id
elif not next_match.p2_team_id:
next_match.p2_team_id = team_id
if (
next_match.p1_team_id
and next_match.p2_team_id
and next_match.status == MatchStatus.SCHEDULED
):
next_match.status = MatchStatus.PENDING
db.add(next_match)
update_next_match(match.winner_next_match, winner_id)
if match.loser_next_match and loser_id:
update_next_match(match.loser_next_match, loser_id)
db.commit()
def undo_advancement(db: Session, match: models.Match):
if not match.winner_team_id:
return
winner_id = match.winner_team_id
loser_id = match.p1_team_id if match.p1_team_id != winner_id else match.p2_team_id
def clear_from_next(next_match, team_id):
if not next_match:
return
if next_match.p1_team_id == team_id:
next_match.p1_team_id = None
elif next_match.p2_team_id == team_id:
next_match.p2_team_id = None
if next_match.status == MatchStatus.PENDING:
next_match.status = MatchStatus.SCHEDULED
db.add(next_match)
clear_from_next(match.winner_next_match, winner_id)
if match.loser_next_match and loser_id:
clear_from_next(match.loser_next_match, loser_id)
db.commit()
+25 -67
View File
@@ -7,7 +7,7 @@ from sqlalchemy import Enum as SqlEnum
from sqlalchemy import ForeignKey, Integer, String
from sqlalchemy.orm import Mapped, mapped_column, relationship
from .constants import BracketType, MatchSourceType, MatchStatus, TournamentTypes
from .constants import MatchStatus, TournamentTypes, BracketTypes
from .database import Base
@@ -28,12 +28,6 @@ class Tournament(Base):
"Court", back_populates="tournament", cascade="all, delete-orphan"
)
# The Structure (Skeleton)
nodes: Mapped[list["BracketNode"]] = relationship(
"BracketNode", back_populates="tournament", cascade="all, delete-orphan"
)
# The Events (Real Games)
matches: Mapped[list["Match"]] = relationship(
"Match", back_populates="tournament", cascade="all, delete-orphan"
)
@@ -65,86 +59,50 @@ class Court(Base):
tournament: Mapped["Tournament"] = relationship(back_populates="courts")
class BracketNode(Base):
__tablename__ = "bracket_nodes"
id: Mapped[str] = mapped_column(String, primary_key=True)
tournament_id: Mapped[str] = mapped_column(ForeignKey("tournaments.id"))
bracket_type: Mapped[BracketType] = mapped_column(SqlEnum(BracketType))
round_number: Mapped[int] = mapped_column(Integer)
display_number: Mapped[int] = mapped_column(Integer)
# --- Planning / Scheduling ---
planned_court_id: Mapped[Optional[int]] = mapped_column(
ForeignKey("courts.id"), nullable=True
)
planned_start_time: Mapped[Optional[datetime]] = mapped_column(
DateTime, nullable=True
)
# --- Flow Logic ---
source_p1_node_id: Mapped[Optional[str]] = mapped_column(String, nullable=True)
source_p2_node_id: Mapped[Optional[str]] = mapped_column(String, nullable=True)
source_p1_type: Mapped[Optional[MatchSourceType]] = mapped_column(
SqlEnum(MatchSourceType), nullable=True
)
source_p2_type: Mapped[Optional[MatchSourceType]] = mapped_column(
SqlEnum(MatchSourceType), nullable=True
)
# --- Next Step ---
winner_next_node_id: Mapped[Optional[str]] = mapped_column(String, nullable=True)
loser_next_node_id: Mapped[Optional[str]] = mapped_column(String, nullable=True)
# --- Current State ---
p1_team_id: Mapped[Optional[int]] = mapped_column(
ForeignKey("teams.id"), nullable=True
)
p2_team_id: Mapped[Optional[int]] = mapped_column(
ForeignKey("teams.id"), nullable=True
)
# Link to actual match (only exists if active)
match: Mapped[Optional["Match"]] = relationship(
"Match", back_populates="node", uselist=False
)
tournament: Mapped["Tournament"] = relationship(back_populates="nodes")
court: Mapped[Optional["Court"]] = relationship()
# Helper to get team names quickly
p1_team: Mapped["Team"] = relationship("Team", foreign_keys=[p1_team_id])
p2_team: Mapped["Team"] = relationship("Team", foreign_keys=[p2_team_id])
class Match(Base):
__tablename__ = "matches"
id: Mapped[str] = mapped_column(String, primary_key=True)
tournament_id: Mapped[str] = mapped_column(ForeignKey("tournaments.id"))
node_id: Mapped[str] = mapped_column(ForeignKey("bracket_nodes.id"), unique=True)
match_number: Mapped[int] = mapped_column(Integer)
round_number: Mapped[int] = mapped_column(Integer, default=1)
bracket_type: Mapped[BracketTypes] = mapped_column(SqlEnum(BracketTypes))
court_id: Mapped[Optional[int]] = mapped_column(
ForeignKey("courts.id"), nullable=True
)
timestamp: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
start_time: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
status: Mapped[MatchStatus] = mapped_column(
SqlEnum(MatchStatus), default=MatchStatus.PENDING
SqlEnum(MatchStatus), default=MatchStatus.SCHEDULED
)
p1_team_id: Mapped[int] = mapped_column(ForeignKey("teams.id"))
p2_team_id: Mapped[int] = mapped_column(ForeignKey("teams.id"))
p1_team_id: Mapped[Optional[int]] = mapped_column(
ForeignKey("teams.id"), nullable=True
)
p2_team_id: Mapped[Optional[int]] = mapped_column(
ForeignKey("teams.id"), nullable=True
)
sets: Mapped[list[dict]] = mapped_column(JSON, default=list)
winner_team_id: Mapped[Optional[int]] = mapped_column(
ForeignKey("teams.id"), nullable=True
)
winner_next_match_id: Mapped[Optional[str]] = mapped_column(
ForeignKey("matches.id"), nullable=True
)
loser_next_match_id: Mapped[Optional[str]] = mapped_column(
ForeignKey("matches.id"), nullable=True
)
node: Mapped["BracketNode"] = relationship(back_populates="match")
tournament: Mapped["Tournament"] = relationship(back_populates="matches")
court: Mapped[Optional["Court"]] = relationship()
p1_team: Mapped["Team"] = relationship("Team", foreign_keys=[p1_team_id])
p2_team: Mapped["Team"] = relationship("Team", foreign_keys=[p2_team_id])
winner_next_match: Mapped["Match"] = relationship(
"Match", remote_side=[id], foreign_keys=[winner_next_match_id]
)
loser_next_match: Mapped["Match"] = relationship(
"Match", remote_side=[id], foreign_keys=[loser_next_match_id]
)
+1 -1
View File
@@ -3,4 +3,4 @@ from fastapi import APIRouter
router = APIRouter(prefix="/tournaments", tags=["Tournaments"])
from . import tournaments, teams, courts, matches, report, bracket
from . import tournaments, teams, courts, matches, report
-15
View File
@@ -1,15 +0,0 @@
# 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
+12 -9
View File
@@ -1,5 +1,5 @@
# backend/app/routes/tournaments/report.py
from typing import List, Optional
from typing import Optional
from fastapi import Depends, HTTPException, Query
from sqlalchemy.orm import Session
@@ -45,7 +45,9 @@ async def report_score(
_apply_score(match, report.sets)
flag_modified(match, "sets")
db.commit()
logic.advance_flow(db, t)
if match.winner_team_id:
logic.advance_winner(db, match, match.winner_team_id)
await send_ws_update(id)
return SUCCESS
@@ -59,11 +61,6 @@ async def edit_score(
db: Session = Depends(get_db),
user: Optional[str] = Depends(get_optional_user),
):
"""
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:
raise HTTPException(404, "Tournament not found")
@@ -74,12 +71,16 @@ async def edit_score(
if not match:
raise HTTPException(404, "Match not found")
logic.undo_advancement(db, match)
if report.sets:
_apply_score(match, report.sets)
flag_modified(match, "sets")
db.commit()
logic.advance_flow(db, t)
if match.winner_team_id:
logic.advance_winner(db, match, match.winner_team_id)
await send_ws_update(id)
return SUCCESS
@@ -103,6 +104,8 @@ async def clear_score(
if not match:
raise HTTPException(404, "Match not found")
logic.undo_advancement(db, match)
match.winner_team_id = None
match.status = MatchStatus.PENDING
match.sets = []
@@ -114,7 +117,7 @@ async def clear_score(
return SUCCESS
def _apply_score(match: models.Match, sets: List[schemas.SetScore]):
def _apply_score(match: models.Match, sets: list[schemas.SetScore]):
"""
Calculates winner based on sets and updates the match object.
Does NOT commit to DB.
+1 -1
View File
@@ -6,7 +6,7 @@ from ..core.websocket_manager import manager
router = APIRouter(prefix="/ws", tags=["Websocket"])
@router.websocket("/")
@router.websocket("")
async def websocket_endpoint(websocket: WebSocket):
await manager.connect(websocket)
try:
+14 -26
View File
@@ -3,7 +3,7 @@ from datetime import datetime
from pydantic import BaseModel, ConfigDict
from .constants import BracketType, MatchSourceType, MatchStatus, TournamentTypes
from .constants import MatchStatus, TournamentTypes, BracketTypes
class TeamSchema(BaseModel):
@@ -38,39 +38,26 @@ class ScoreReport(BaseModel):
class MatchOut(BaseModel):
id: str
node_id: str
match_number: int
round_number: int
bracket_type: BracketTypes
status: MatchStatus
timestamp: datetime | None = None
court: CourtSchema | None = None
p1_team: TeamSchema | None = None
p2_team: TeamSchema | None = None
start_time: datetime | None = None
court_id: int | None = None
p1_team_id: int | None = None
p2_team_id: int | None = None
winner_team_id: int | None = None
winner_next_match_id: str | None = None
loser_next_match_id: str | None = None
sets: list[SetScore] = []
model_config = ConfigDict(from_attributes=True)
class BracketNodeOut(BaseModel):
id: str
display_number: int
bracket_type: BracketType
round_number: int
planned_start_time: datetime | None = None
planned_court_id: int | None = None
source_p1_node_id: str | None = None
source_p2_node_id: str | None = None
source_p1_type: MatchSourceType | None = None
source_p2_type: MatchSourceType | None = None
winner_next_node_id: str | None = None
loser_next_node_id: str | None = None
p1_team: TeamSchema | None = None
p2_team: TeamSchema | None = None
match: MatchOut | None = None
model_config = ConfigDict(from_attributes=True)
class TournamentCreate(BaseModel):
name: str
code: str
@@ -106,6 +93,7 @@ class TournamentDetail(BaseModel):
type: TournamentTypes
teams: list[TeamSchema]
courts: list[CourtSchema]
matches: list[MatchOut]
model_config = ConfigDict(from_attributes=True)