diff --git a/API/Apps/Game/consumers.py b/API/Apps/Game/consumers.py index 83c50d01..62a5f9fe 100644 --- a/API/Apps/Game/consumers.py +++ b/API/Apps/Game/consumers.py @@ -16,7 +16,7 @@ from Apps.Profile.models import Profile from Apps.Game.matchmaking import match - +from django.core.serializers.json import DjangoJSONEncoder class MatchMakingConsumer(WebsocketConsumer): def connect(self): self.accept() @@ -176,7 +176,7 @@ async def send_initial_state(self): 'state_type': 'initial_state', 'details': data, 'game': GameConsumer.game_states[self.game_id] - })) + }, cls=DjangoJSONEncoder)) async def send_score_state(self, event): await self.send(text_data=json.dumps({ @@ -189,6 +189,7 @@ async def send_finish_state(self, event): 'state_type': 'finish_state', 'game': event['game'], 'winner': event['winner'], + 'tournament_id': str(event['tournament_id']) })) await self.close() @@ -296,12 +297,14 @@ async def update(self): }) if GameConsumer.game_states[self.game_id]['player_two']['score'] >= winner_ball_count: self.finish_game(self.player2['nickname']) + game = await self.get_game() await self.channel_layer.group_send( self.game_group_name, { 'type': 'send_finish_state', 'game': GameConsumer.game_states[self.game_id], - 'winner': self.player2 + 'winner': self.player2, + 'tournament_id': str(game['tournament']) } ) self.stop_event.set() @@ -325,13 +328,15 @@ async def update(self): 'dy': random.choice([-5, 5]) }) if GameConsumer.game_states[self.game_id]['player_one']['score'] >= winner_ball_count: + game = await self.get_game() self.finish_game(self.player1['nickname']) await self.channel_layer.group_send( self.game_group_name, { 'type': 'send_finish_state', 'game': GameConsumer.game_states[self.game_id], - 'winner': self.player1 + 'winner': self.player1, + 'tournament_id': str(game['tournament']) } ) self.stop_event.set() diff --git a/API/Apps/Tournament/admin.py b/API/Apps/Tournament/admin.py index 22c77877..ca36fddc 100644 --- a/API/Apps/Tournament/admin.py +++ b/API/Apps/Tournament/admin.py @@ -1,7 +1,8 @@ from django.contrib import admin -from Apps.Tournament.models import Tournament +from Apps.Tournament.models import Tournament, Round # Register your models here. -admin.site.register(Tournament) \ No newline at end of file +admin.site.register(Tournament) +admin.site.register(Round) \ No newline at end of file diff --git a/API/Apps/Tournament/consumers.py b/API/Apps/Tournament/consumers.py index c3716579..8e138945 100644 --- a/API/Apps/Tournament/consumers.py +++ b/API/Apps/Tournament/consumers.py @@ -41,7 +41,6 @@ def check_params(self): return True def connect(self): - self.accept() query_string = self.scope['query_string'].decode() params = urllib.parse.parse_qs(query_string) @@ -53,7 +52,9 @@ def connect(self): self.channel_name ) if self.check_params(): + self.send_error("invalid_params") self.close(code=1000) + return instance = Profile.objects.get(nickname=self.nickname) serializer = TournamentProfileSerializer(instance) cache_key = f"user_{self.tournament_id}" @@ -72,15 +73,17 @@ def connect(self): "tournament_name": tournament.name, "players": data } + self.accept() self.send_to_group(tournament_info, "tournament_info") def receive(self, text_data): data = json.loads(text_data) + print("data",data) if data['send_type'] == 'checkMatch': self.checkMatch(self.nickname, self.tournament_id) elif data['send_type'] == 'start': self.check_start_conditions() - # self.StartTournament(data) + self.StartTournament(data) def check_start_conditions(self): player = get_player_from_cache(f"user_{self.tournament_id}", self.nickname) @@ -91,10 +94,6 @@ def check_start_conditions(self): if len(players) < 3: self.send_error("invalid_tournament") return - for player in players: - if not player['is_ready']: - self.send_error("players_not_ready") - return def send_error(self, error_type): if error_type == "invalid_profile": @@ -106,6 +105,8 @@ def send_error(self, error_type): self.send(text_data=json.dumps({"error": "tournament_started", "message": "Tournament Already Started"})) elif error_type == "players_not_ready": self.send(text_data=json.dumps({"error": "players_not_ready", "message": "All players must be ready"})) + elif error_type == "invalid_params": + self.send(text_data=json.dumps({"error": "invalid_params", "message": "Something went wrong"})) def disconnect(self, close_code): async_to_sync(self.channel_layer.group_discard)( @@ -127,11 +128,9 @@ def disconnect(self, close_code): tournament.delete() def StartTournament(self, data): + print(get_players_from_cache(f"user_{self.tournament_id}")) tournament = Tournament.objects.get(id=self.tournament_id) participants = tournament.current_participants.all() - if tournament.rounds.exists(): - self.send_error("tournament_started") - return if tournament.current_participants.count() > 2: round_number = 1 round_obj = Round.objects.create(round_number=round_number) @@ -153,6 +152,7 @@ def StartTournament(self, data): return game = Game.objects.create(player1=profile1, player2=profile2, tournament_id=self.tournament_id) game_id = str(game.id) + game.tournament_id = self.tournament_id player1_nick = str(profile1.nickname) player2_nick = str(profile2.nickname) game_info = { @@ -168,12 +168,9 @@ def StartTournament(self, data): print(e) def checkMatch(self, profile_id1, tournament_id): + try: - profile_id = int(profile_id1) - except ValueError: - print("Tournament ID metin olarak beklenen türde değil.") - try: - tournament = Tournament.objects.get(pk=tournament_id) + tournament = Tournament.objects.get(pk=self.tournament_id) last_round = tournament.rounds.order_by('-round_number').first() except Tournament.DoesNotExist: print("Turnuva Yok") @@ -190,8 +187,7 @@ def checkMatch(self, profile_id1, tournament_id): player_participated = False for game in last_round.matches.all(): - if game.player1.id == profile_id or game.player2.id == profile_id: - print(game.player1.id, game.player2.id) + if game.player1.nickname == self.nickname or game.player2.nickname == self.nickname: player_participated = True if game.winner is None: game.winner = game.player2 @@ -221,5 +217,4 @@ def checkMatch(self, profile_id1, tournament_id): new_round.participants.remove(winners[i]) new_round.participants.remove(winners[i + 1]) new_round.save() - print("Yeni Turnuva Oluşturuldu") return diff --git a/API/static/scripts/game.js b/API/static/scripts/game.js index c32c423c..039a24b9 100644 --- a/API/static/scripts/game.js +++ b/API/static/scripts/game.js @@ -127,11 +127,12 @@ function setPlayerData(state) } function handleInitialState(state) { + setCurrentPoints(state) setPlayerData(state); draw(state.game,"red","blue"); } -function printWinner(winner,socket){ +function printWinner(data,winner,socket){ let winnerHTML = `