Added seperate bracket/node logic
This commit is contained in:
+78
-49
@@ -7,13 +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 (
|
||||
TournamentTypes,
|
||||
MatchSourceType,
|
||||
MatchStatus,
|
||||
WinnerSide,
|
||||
BracketType,
|
||||
)
|
||||
from .constants import BracketType, MatchSourceType, MatchStatus, TournamentTypes
|
||||
from .database import Base
|
||||
|
||||
|
||||
@@ -33,11 +27,17 @@ class Tournament(Base):
|
||||
courts: Mapped[list["Court"]] = relationship(
|
||||
"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"
|
||||
)
|
||||
|
||||
# --- ADD THESE PROPERTIES ---
|
||||
@property
|
||||
def team_count(self) -> int:
|
||||
return len(self.teams)
|
||||
@@ -65,57 +65,86 @@ 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"), primary_key=True
|
||||
tournament_id: Mapped[str] = mapped_column(ForeignKey("tournaments.id"))
|
||||
|
||||
node_id: Mapped[str] = mapped_column(ForeignKey("bracket_nodes.id"), unique=True)
|
||||
|
||||
court_id: Mapped[Optional[int]] = mapped_column(
|
||||
ForeignKey("courts.id"), nullable=True
|
||||
)
|
||||
|
||||
# --- Structural Info ---
|
||||
bracket: Mapped[BracketType] = mapped_column(
|
||||
SqlEnum(BracketType, native_enum=False)
|
||||
)
|
||||
round: Mapped[int] = mapped_column(Integer)
|
||||
number: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
|
||||
|
||||
# --- Scheduling ---
|
||||
timestamp: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||||
court_name: Mapped[Optional[str]] = mapped_column(String, nullable=True)
|
||||
|
||||
# --- Player Info ---
|
||||
p1_name: Mapped[Optional[str]] = mapped_column(String, nullable=True)
|
||||
p2_name: Mapped[Optional[str]] = mapped_column(String, nullable=True)
|
||||
|
||||
winner: Mapped[Optional[str]] = mapped_column(String, nullable=True)
|
||||
start_time: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||||
|
||||
status: Mapped[MatchStatus] = mapped_column(
|
||||
SqlEnum(MatchStatus, native_enum=False), default=MatchStatus.PENDING
|
||||
SqlEnum(MatchStatus), default=MatchStatus.PENDING
|
||||
)
|
||||
|
||||
p1_team_id: Mapped[int] = mapped_column(ForeignKey("teams.id"))
|
||||
p2_team_id: Mapped[int] = mapped_column(ForeignKey("teams.id"))
|
||||
|
||||
sets: Mapped[list[dict]] = mapped_column(JSON, default=list)
|
||||
|
||||
previous_match_p1_id: Mapped[Optional[str]] = mapped_column(String, nullable=True)
|
||||
previous_match_p2_id: Mapped[Optional[str]] = mapped_column(String, nullable=True)
|
||||
|
||||
source_p1_type: Mapped[Optional[MatchSourceType]] = mapped_column(
|
||||
SqlEnum(MatchSourceType, native_enum=False), nullable=True
|
||||
)
|
||||
source_p2_type: Mapped[Optional[MatchSourceType]] = mapped_column(
|
||||
SqlEnum(MatchSourceType, native_enum=False), nullable=True
|
||||
winner_team_id: Mapped[Optional[int]] = mapped_column(
|
||||
ForeignKey("teams.id"), nullable=True
|
||||
)
|
||||
|
||||
winner_next_match_id: Mapped[Optional[str]] = mapped_column(String, nullable=True)
|
||||
loser_next_match_id: Mapped[Optional[str]] = mapped_column(String, nullable=True)
|
||||
|
||||
node: Mapped["BracketNode"] = relationship(back_populates="match")
|
||||
tournament: Mapped["Tournament"] = relationship(back_populates="matches")
|
||||
court: Mapped[Optional["Court"]] = relationship()
|
||||
|
||||
@property
|
||||
def winner_side(self) -> WinnerSide:
|
||||
if not self.winner:
|
||||
return WinnerSide.NONE
|
||||
if self.winner == self.p1_name:
|
||||
return WinnerSide.P1
|
||||
if self.winner == self.p2_name:
|
||||
return WinnerSide.P2
|
||||
return WinnerSide.NONE
|
||||
p1_team: Mapped["Team"] = relationship("Team", foreign_keys=[p1_team_id])
|
||||
p2_team: Mapped["Team"] = relationship("Team", foreign_keys=[p2_team_id])
|
||||
|
||||
Reference in New Issue
Block a user