tournament: use tournamentGameModel instead gameModel

This commit is contained in:
starnakin 2024-04-25 16:11:27 +02:00
parent c4407adffb
commit ff230bccf6
3 changed files with 35 additions and 18 deletions

View File

@ -6,12 +6,13 @@ from django.contrib.auth.models import User
from django.db.models import QuerySet
from django.utils.translation import gettext as _
from games.models import GameModel
from games.serializers import GameSerializer
from profiles.models import ProfileModel
from profiles.serializers import ProfileSerializer
from .models import TournamentModel
from .models import TournamentGameModel
from .serializers import TournamentGameSerializer
import json
class TournamentMember:
@ -33,7 +34,7 @@ class TournamentMember:
self.send("error", data_to_send)
def send_goto(self, game: GameModel):
def send_goto(self, game: TournamentGameModel):
self.send("go_to", {"game_id": game.pk})
def _receive_participating(self, data: dict) -> None:
@ -84,7 +85,7 @@ class TournamentRoom:
self._room_manager: TournamentRoomManager = room_manager
self._member_list: set[TournamentMember] = set()
self._model: TournamentModel = tournament
self._game_in_progress_list: set[GameModel] = set()
self._game_in_progress_list: set[TournamentGameModel] = set()
self._current_round = 0
def join(self, socket: TournamentWebConsumer) -> TournamentMember:
@ -94,17 +95,20 @@ class TournamentRoom:
return member
def set_game_as_finished(self, game: GameModel):
def set_game_as_finished(self, game: TournamentGameModel):
self._game_in_progress_list.remove(game)
data: dict = GameSerializer(game).data
data.update({"round": self._current_round})
self.broadcast("game_update", data)
self.broadcast("game_update", TournamentGameSerializer(game).data)
if len(self._game_in_progress_list) == 0:
self._round_finished()
def set_game_as_started(self, game: TournamentGameModel):
self._game_in_progress_list.add(game)
self.broadcast("game_update", TournamentGameSerializer(game).data)
def _finish(self, winner: User):
self._model.finish(winner)
@ -112,7 +116,7 @@ class TournamentRoom:
def _round_finished(self):
if self._current_round == self._model.round:
last_game: GameModel = self._model.get_games_by_round(self._current_round)[0]
last_game: TournamentGameSerializer = self._model.get_games_by_round(self._current_round)[0]
self._finish(last_game.winner)
return
@ -124,9 +128,9 @@ class TournamentRoom:
participant_list: set[User] = self._model.get_participants_by_round(self._current_round)
self._game_in_progress_list = self._model.create_round(participant_list, self._current_round)
game_list = self._model.create_round(participant_list, self._current_round)
for game in self._game_in_progress_list:
for game in game_list:
for player in game.get_players():
participant: TournamentMember = self.get_participant_by_profile(player)
participant.send_goto(game)

View File

@ -1,4 +1,5 @@
from __future__ import annotations
from typing import Any
from games.models import GameModel
@ -96,10 +97,20 @@ class TournamentGameModel(GameModel):
round = models.IntegerField()
pos = models.IntegerField()
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
from .consumers import tournament_manager
self.room = tournament_manager.get(self.tournament)
def start(self):
super().start()
self.room.set_game_as_started(self)
def finish(self, winner_id):
super().finish(winner_id)
from .consumers import tournament_manager
room = tournament_manager.get(self.tournament)
room.set_game_as_finished(self)
self.room.set_game_as_finished(self)

View File

@ -45,10 +45,12 @@ class TournamentGameSerializer(serializers.ModelSerializer):
start_timestamp = serializers.ReadOnlyField()
stop_timestamp = serializers.ReadOnlyField()
gamemode = serializers.ReadOnlyField()
round = serializers.ReadOnlyField()
pos = serializers.ReadOnlyField()
class Meta:
model = TournamentGameModel
fields = ["id", "winner_id", "state", "started", "finished", "players", "start_timestamp", "stop_timestamp", "game_type"]
fields = ["id", "winner_id", "state", "started", "finished", "players", "start_timestamp", "stop_timestamp", "game_type", "round", "pos"]
def get_state(self, instance: TournamentGameModel):
if (instance.finished):