64 lines
2.1 KiB
Python
64 lines
2.1 KiB
Python
# backend/app/core/utils.py
|
|
from .brackets import Match
|
|
|
|
|
|
class MermaidLive:
|
|
|
|
@staticmethod
|
|
def _get_visual_target(match: Match, is_win=True) -> Match | None:
|
|
current = match.next_win if is_win else match.next_loss
|
|
while current and current.is_bye:
|
|
current = current.next_win
|
|
return current
|
|
|
|
@classmethod
|
|
def export(cls, matches: list[Match]) -> None:
|
|
print("\n--- COPY TO MERMAID.LIVE ---")
|
|
print("graph LR")
|
|
print(" classDef wb stroke:#01579b,stroke-width:2px;")
|
|
print(" classDef lb stroke:#b71c1c,stroke-width:2px,stroke-dasharray: 5 5;")
|
|
print(" classDef final stroke:#e65100,stroke-width:4px;")
|
|
|
|
wb_nodes = [
|
|
m
|
|
for m in matches
|
|
if "WB" in m.name or "Semifinal" in m.name or "Winners Final" in m.name
|
|
]
|
|
|
|
lb_nodes = [m for m in matches if "LB" in m.name or "Losers Final" in m.name]
|
|
final_nodes = [
|
|
m for m in matches if "Grand Final" in m.name or "3rd Place" in m.name
|
|
]
|
|
|
|
print(" subgraph Winners Bracket")
|
|
for m in wb_nodes:
|
|
cls._print_node(m, "wb")
|
|
print(" end")
|
|
|
|
if lb_nodes:
|
|
print(" subgraph Losers Bracket")
|
|
for m in lb_nodes:
|
|
cls._print_node(m, "lb")
|
|
print(" end")
|
|
|
|
print(" subgraph Championship / 3rd Place")
|
|
for m in final_nodes:
|
|
cls._print_node(m, "final")
|
|
print(" end")
|
|
|
|
for m in matches:
|
|
target_win = cls._get_visual_target(m, is_win=True)
|
|
if target_win:
|
|
print(f" M{m.id} --> M{target_win.id}")
|
|
|
|
target_loss = cls._get_visual_target(m, is_win=False)
|
|
if target_loss:
|
|
print(f" M{m.id} -.-> M{target_loss.id}")
|
|
|
|
@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]})"
|
|
print(f' M{m.id}["{label}"]:::{style}')
|