Complete main functionallity of backend and frontend.
This commit is contained in:
+34
-22
@@ -8,7 +8,6 @@ from sqlalchemy.orm import Session
|
||||
from . import models
|
||||
from .constants import MatchStatus, TournamentTypes
|
||||
from .core.brackets import BracketGenerator
|
||||
from .core import structures
|
||||
|
||||
|
||||
def generate_bracket(db: Session, t: models.Tournament):
|
||||
@@ -21,18 +20,26 @@ def generate_bracket(db: Session, t: models.Tournament):
|
||||
abstract_matches = gen.generate(len(teams), double_elimination=is_double)
|
||||
id_map = {m.id: str(uuid4()) for m in abstract_matches}
|
||||
|
||||
def resolve_target(match_node: structures.Match | None):
|
||||
curr = match_node
|
||||
while curr and curr.is_bye:
|
||||
def resolve_target(match_node, is_winner_path):
|
||||
if is_winner_path:
|
||||
curr = match_node.next_win
|
||||
slot = getattr(match_node, "next_win_slot", None)
|
||||
else:
|
||||
curr = match_node.next_loss
|
||||
slot = getattr(match_node, "next_loss_slot", None)
|
||||
|
||||
while curr and getattr(curr, "is_bye", False):
|
||||
slot = getattr(curr, "next_win_slot", None)
|
||||
curr = curr.next_win
|
||||
return curr
|
||||
|
||||
return curr, slot
|
||||
|
||||
db_matches = []
|
||||
friendly_counter = 1
|
||||
|
||||
for m in abstract_matches:
|
||||
real_win = resolve_target(m.next_win)
|
||||
real_loss = resolve_target(m.next_loss)
|
||||
real_win, win_slot = resolve_target(m, True)
|
||||
real_loss, loss_slot = resolve_target(m, False)
|
||||
|
||||
initial_status = MatchStatus.SCHEDULED
|
||||
p1_id = None
|
||||
@@ -59,7 +66,9 @@ def generate_bracket(db: Session, t: models.Tournament):
|
||||
p1_team_id=p1_id,
|
||||
p2_team_id=p2_id,
|
||||
winner_next_match_id=id_map[real_win.id] if real_win else None,
|
||||
winner_next_match_slot=win_slot if real_win else None,
|
||||
loser_next_match_id=id_map[real_loss.id] if real_loss else None,
|
||||
loser_next_match_slot=loss_slot if real_loss else None,
|
||||
)
|
||||
db_matches.append(new_match)
|
||||
friendly_counter += 1
|
||||
@@ -172,12 +181,14 @@ def advance_winner(db: Session, match: models.Match, winner_id: int):
|
||||
|
||||
loser_id = match.p1_team_id if match.p1_team_id != winner_id else match.p2_team_id
|
||||
|
||||
def update_next_match(next_match: models.Match, team_id: int):
|
||||
if not next_match:
|
||||
def update_next_match(
|
||||
next_match: models.Match, team_id: int, target_slot: int | None
|
||||
):
|
||||
if not next_match or target_slot is None:
|
||||
return
|
||||
if not next_match.p1_team_id:
|
||||
if target_slot == 0:
|
||||
next_match.p1_team_id = team_id
|
||||
elif not next_match.p2_team_id:
|
||||
elif target_slot == 1:
|
||||
next_match.p2_team_id = team_id
|
||||
|
||||
if (
|
||||
@@ -189,9 +200,9 @@ def advance_winner(db: Session, match: models.Match, winner_id: int):
|
||||
|
||||
db.add(next_match)
|
||||
|
||||
update_next_match(match.winner_next_match, winner_id)
|
||||
update_next_match(match.winner_next_match, winner_id, match.winner_next_match_slot)
|
||||
if match.loser_next_match and loser_id:
|
||||
update_next_match(match.loser_next_match, loser_id)
|
||||
update_next_match(match.loser_next_match, loser_id, match.loser_next_match_slot)
|
||||
|
||||
db.commit()
|
||||
|
||||
@@ -203,8 +214,10 @@ def undo_advancement(db: Session, match: models.Match):
|
||||
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: models.Match, team_id: int):
|
||||
if not next_match:
|
||||
def clear_from_next(
|
||||
next_match: models.Match, team_id: int, target_slot: int | None
|
||||
):
|
||||
if not next_match or target_slot is None:
|
||||
return
|
||||
|
||||
if next_match.winner_team_id:
|
||||
@@ -212,17 +225,16 @@ def undo_advancement(db: Session, match: models.Match):
|
||||
next_match.winner_team_id = None
|
||||
next_match.sets = []
|
||||
|
||||
if next_match.p1_team_id == team_id:
|
||||
if target_slot == 0:
|
||||
next_match.p1_team_id = None
|
||||
elif next_match.p2_team_id == team_id:
|
||||
elif target_slot == 1:
|
||||
next_match.p2_team_id = None
|
||||
if next_match.p1_team_id and next_match.p2_team_id:
|
||||
next_match.status = MatchStatus.PENDING
|
||||
else:
|
||||
|
||||
if next_match.status == MatchStatus.PENDING:
|
||||
next_match.status = MatchStatus.SCHEDULED
|
||||
|
||||
db.add(next_match)
|
||||
|
||||
clear_from_next(match.winner_next_match, winner_id)
|
||||
clear_from_next(match.winner_next_match, winner_id, match.winner_next_match_slot)
|
||||
if match.loser_next_match and loser_id:
|
||||
clear_from_next(match.loser_next_match, loser_id)
|
||||
clear_from_next(match.loser_next_match, loser_id, match.loser_next_match_slot)
|
||||
|
||||
Reference in New Issue
Block a user