Enable Team Tournaments, add Tournament Trees, implement temporary tree persistance #66
@ -1,5 +1,9 @@
|
||||
import json
|
||||
from pprint import pprint
|
||||
from typing import Optional
|
||||
|
||||
from from_root import from_root
|
||||
|
||||
from src.ezgg_lan_manager.services.DatabaseService import DatabaseService
|
||||
from src.ezgg_lan_manager.services.UserService import UserService
|
||||
from src.ezgg_lan_manager.types.Participant import Participant
|
||||
@ -90,6 +94,7 @@ class TournamentService:
|
||||
tournament = await self.get_tournament_by_id(tournament_id)
|
||||
if tournament:
|
||||
tournament.start()
|
||||
await self._generate_initial_json_file(tournament)
|
||||
await self._db_service.change_tournament_status(tournament_id, tournament.status)
|
||||
self._cache_dirty = True
|
||||
|
||||
@ -99,3 +104,37 @@ class TournamentService:
|
||||
tournament.cancel()
|
||||
await self._db_service.change_tournament_status(tournament_id, tournament.status)
|
||||
self._cache_dirty = True
|
||||
|
||||
async def _generate_initial_json_file(self, tournament: Tournament) -> None:
|
||||
"""
|
||||
Generates the initial JSON file for the tournament. Won't generate a new one if one already exists.
|
||||
ToDo: Remove this method when final tournament system is completed.
|
||||
"""
|
||||
p = tournament.participants
|
||||
pairs = [
|
||||
(p[i], p[i + 1]) if i + 1 < len(p) else (p[i], None)
|
||||
for i in range(0, len(p), 2)
|
||||
]
|
||||
data = {
|
||||
"rounds": [
|
||||
[
|
||||
{
|
||||
"opponent_1_id": pair[0].id if pair[0] is not None else None,
|
||||
"opponent_2_id": pair[1].id if pair[1] is not None else None,
|
||||
"winner": None
|
||||
} for pair in pairs
|
||||
]
|
||||
]
|
||||
}
|
||||
|
||||
# Resolve byes
|
||||
for match in data["rounds"][0]:
|
||||
if match["opponent_2_id"] is None:
|
||||
match["winner"] = match["opponent_1_id"]
|
||||
|
||||
file_name = tournament.name.replace(" ", "_") + ".json"
|
||||
try:
|
||||
with open(from_root("tournament_data", file_name), "x") as f:
|
||||
json.dump(data, f, indent=4)
|
||||
except FileExistsError:
|
||||
pass
|
||||
|
||||
Loading…
Reference in New Issue
Block a user