151 lines
5.4 KiB
Python
151 lines
5.4 KiB
Python
# backend/app/models.py
|
|
from datetime import datetime
|
|
from typing import Optional
|
|
|
|
from sqlalchemy import JSON, DateTime
|
|
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 .database import Base
|
|
|
|
|
|
class Tournament(Base):
|
|
__tablename__ = "tournaments"
|
|
|
|
id: Mapped[str] = mapped_column(String(8), primary_key=True, index=True)
|
|
name: Mapped[str] = mapped_column(String, nullable=False)
|
|
code: Mapped[str] = mapped_column(String, nullable=True)
|
|
timestamp: Mapped[datetime] = mapped_column(DateTime, nullable=False)
|
|
duration: Mapped[int] = mapped_column(Integer, default=30)
|
|
type: Mapped[TournamentTypes] = mapped_column(SqlEnum(TournamentTypes))
|
|
|
|
teams: Mapped[list["Team"]] = relationship(
|
|
"Team", back_populates="tournament", cascade="all, delete-orphan"
|
|
)
|
|
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"
|
|
)
|
|
|
|
@property
|
|
def team_count(self) -> int:
|
|
return len(self.teams)
|
|
|
|
@property
|
|
def court_count(self) -> int:
|
|
return len(self.courts)
|
|
|
|
|
|
class Team(Base):
|
|
__tablename__ = "teams"
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
|
name: Mapped[str] = mapped_column(String, nullable=False)
|
|
tournament_id: Mapped[str] = mapped_column(ForeignKey("tournaments.id"))
|
|
|
|
tournament: Mapped["Tournament"] = relationship(back_populates="teams")
|
|
|
|
|
|
class Court(Base):
|
|
__tablename__ = "courts"
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
|
name: Mapped[str] = mapped_column(String, nullable=False)
|
|
tournament_id: Mapped[str] = mapped_column(ForeignKey("tournaments.id"))
|
|
|
|
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)
|
|
|
|
court_id: Mapped[Optional[int]] = mapped_column(
|
|
ForeignKey("courts.id"), nullable=True
|
|
)
|
|
start_time: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
|
|
|
status: Mapped[MatchStatus] = mapped_column(
|
|
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)
|
|
winner_team_id: Mapped[Optional[int]] = mapped_column(
|
|
ForeignKey("teams.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])
|