From dcd5cec3c15172c66846ae3d09a192b980e297fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Mon, 25 Mar 2024 14:37:30 +0900 Subject: [PATCH 01/17] feat: create GameList object #504 --- backend/games/consumers/GameConsumer.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/backend/games/consumers/GameConsumer.py b/backend/games/consumers/GameConsumer.py index 20a69518..e4ceac79 100644 --- a/backend/games/consumers/GameConsumer.py +++ b/backend/games/consumers/GameConsumer.py @@ -20,6 +20,9 @@ class GameConsumer(AsyncWebsocketConsumer): + class GameList: + pass + def __init__(self, *args, **kwargs): super().__init__(args, kwargs) self.user = None @@ -95,6 +98,7 @@ async def _process_valid_user_connect(self): self.match3_group_name = game_group_name await self.channel_layer.group_add(self.game_group_name, self.channel_name) + await self._create_game_object(self.game_group_name) await self._assignment_match() if self.manager: @@ -208,6 +212,15 @@ def _validate_user(self, user): else: raise Exception("게임 방에 속한 유저가 아닙니다.") + async def _create_game_object(self, game_group_name): + setattr(self.GameList, game_group_name, None) + if self.game.id == 0: + setattr(getattr(self.GameList, game_group_name), 'match1', None) + else: + setattr(getattr(self.GameList, game_group_name), 'match1', None) + setattr(getattr(self.GameList, game_group_name), 'match2', None) + setattr(getattr(self.GameList, game_group_name), 'match3', None) + async def _assignment_match(self): if self.game.mode == 0: # 1e1 self.my_match = 1 From 5e4688b7c32cd7b829c708e255380f3de0f2a396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Mon, 25 Mar 2024 14:37:48 +0900 Subject: [PATCH 02/17] refactor: code refactoring --- backend/games/consumers/GameConsumer.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/backend/games/consumers/GameConsumer.py b/backend/games/consumers/GameConsumer.py index e4ceac79..bb04c79d 100644 --- a/backend/games/consumers/GameConsumer.py +++ b/backend/games/consumers/GameConsumer.py @@ -91,11 +91,7 @@ async def _process_valid_user_connect(self): await self._validate_user(self.user.nickname) logger.info(f"[인게임] {self.user.nickname} - {self.game_id}번 방 연결 - {self.manager}") - game_group_name = f"ingame_{self.game_id}" - self.game_group_name = game_group_name - self.match1_group_name = f"match1_{self.game_id}" - self.match2_group_name = f"match2_{self.game_id}" - self.match3_group_name = game_group_name + await self._set_group_name() await self.channel_layer.group_add(self.game_group_name, self.channel_name) await self._create_game_object(self.game_group_name) @@ -212,6 +208,13 @@ def _validate_user(self, user): else: raise Exception("게임 방에 속한 유저가 아닙니다.") + async def _set_group_name(self): + game_group_name = f"ingame_{self.game_id}" + self.game_group_name = game_group_name + self.match1_group_name = f"match1_{self.game_id}" + self.match2_group_name = f"match2_{self.game_id}" + self.match3_group_name = game_group_name + async def _create_game_object(self, game_group_name): setattr(self.GameList, game_group_name, None) if self.game.id == 0: From fc3bc75089e90c32f74b5a09ec3c5e8748e8fcf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Sun, 31 Mar 2024 18:57:35 +0900 Subject: [PATCH 03/17] =?UTF-8?q?fix:=20=EA=B5=AC=EA=B8=80=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=9D=B8=20=EB=B2=84=EA=B7=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/login/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/login/views.py b/backend/login/views.py index 1c1f613b..d7e2a059 100644 --- a/backend/login/views.py +++ b/backend/login/views.py @@ -149,7 +149,7 @@ def get_email(self, access_token): class GoogleCallbackView(OAuthCallbackView): def get_email_auth_uri(self): - return 'https://localhost:443/auth' + return 'https://localhost:443/register' client_id = GOOGLE_CLIENT_ID client_secret = GOOGLE_CLIENT_SECRET token_api = "https://oauth2.googleapis.com/token" From cd5e5c9cce5aa4b6fe8f9e43e69d27bcfe159f01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Sun, 31 Mar 2024 19:06:11 +0900 Subject: [PATCH 04/17] =?UTF-8?q?fix:=20=EA=B0=9D=EC=B2=B4=20=EB=B3=80?= =?UTF-8?q?=EC=88=98=20=EC=83=9D=EC=84=B1=20=EC=88=98=EC=A0=95=20#504?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/games/consumers/GameConsumer.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/backend/games/consumers/GameConsumer.py b/backend/games/consumers/GameConsumer.py index bb04c79d..8cc0e64e 100644 --- a/backend/games/consumers/GameConsumer.py +++ b/backend/games/consumers/GameConsumer.py @@ -216,13 +216,12 @@ async def _set_group_name(self): self.match3_group_name = game_group_name async def _create_game_object(self, game_group_name): - setattr(self.GameList, game_group_name, None) - if self.game.id == 0: - setattr(getattr(self.GameList, game_group_name), 'match1', None) + if self.game.mode == 0: + setattr(self.GameList, f'{game_group_name}_match1', None) else: - setattr(getattr(self.GameList, game_group_name), 'match1', None) - setattr(getattr(self.GameList, game_group_name), 'match2', None) - setattr(getattr(self.GameList, game_group_name), 'match3', None) + setattr(self.GameList, f'{game_group_name}_match1', None) + setattr(self.GameList, f'{game_group_name}_match2', None) + setattr(self.GameList, f'{game_group_name}_match3', None) async def _assignment_match(self): if self.game.mode == 0: # 1e1 From f2ff7047c773d2d6ae4621995eafcbaf86091ae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Sun, 31 Mar 2024 19:57:28 +0900 Subject: [PATCH 05/17] fix: edit function name --- backend/games/consumers/GameConsumer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/games/consumers/GameConsumer.py b/backend/games/consumers/GameConsumer.py index 8cc0e64e..bc085342 100644 --- a/backend/games/consumers/GameConsumer.py +++ b/backend/games/consumers/GameConsumer.py @@ -94,7 +94,7 @@ async def _process_valid_user_connect(self): await self._set_group_name() await self.channel_layer.group_add(self.game_group_name, self.channel_name) - await self._create_game_object(self.game_group_name) + await self._create_match_object(self.game_group_name) await self._assignment_match() if self.manager: @@ -215,7 +215,7 @@ async def _set_group_name(self): self.match2_group_name = f"match2_{self.game_id}" self.match3_group_name = game_group_name - async def _create_game_object(self, game_group_name): + async def _create_match_object(self, game_group_name): if self.game.mode == 0: setattr(self.GameList, f'{game_group_name}_match1', None) else: From 947296c62ab825d83f4acb4904e37a23fc1215d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Sun, 31 Mar 2024 19:57:56 +0900 Subject: [PATCH 06/17] fix: delete unused var --- backend/games/consumers/GameConsumer.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/backend/games/consumers/GameConsumer.py b/backend/games/consumers/GameConsumer.py index bc085342..308f898b 100644 --- a/backend/games/consumers/GameConsumer.py +++ b/backend/games/consumers/GameConsumer.py @@ -39,11 +39,6 @@ def __init__(self, *args, **kwargs): self.match2_group_name = None self.match3_group_name = None - self.ping_pong_map = None - self.match1 = None - self.match2 = None - self.match3 = None - async def connect(self): if await self._is_invalid_user(): await self._reject_invalid_user() From 17ffce9a749042888514de3525d6e0244b07e92b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Sun, 31 Mar 2024 19:58:23 +0900 Subject: [PATCH 07/17] chore: delete comment --- backend/games/consumers/GameConsumer.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/backend/games/consumers/GameConsumer.py b/backend/games/consumers/GameConsumer.py index 308f898b..6261d7e3 100644 --- a/backend/games/consumers/GameConsumer.py +++ b/backend/games/consumers/GameConsumer.py @@ -434,10 +434,6 @@ async def _send_end_message(self, match: Result): 'data': data } ) - # await self.game_end({ - # 'type': type_, - # 'data': data - # }) async def _send_start_message(self, match, group_name): data = { @@ -471,10 +467,6 @@ async def _send_start_message(self, match, group_name): 'data': data } ) - # await self.game_start({ - # 'type': 'game_start', - # 'data': data - # }) async def _send_in_game_message(self, match, group_name): data = { From b7fc8915ad245b785d2f8060f4721379f72db5cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Sun, 31 Mar 2024 19:59:06 +0900 Subject: [PATCH 08/17] feat: add delete object func #504 --- backend/games/consumers/GameConsumer.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/backend/games/consumers/GameConsumer.py b/backend/games/consumers/GameConsumer.py index 6261d7e3..08abd617 100644 --- a/backend/games/consumers/GameConsumer.py +++ b/backend/games/consumers/GameConsumer.py @@ -218,6 +218,9 @@ async def _create_match_object(self, game_group_name): setattr(self.GameList, f'{game_group_name}_match2', None) setattr(self.GameList, f'{game_group_name}_match3', None) + async def _delete_match_object(self, game_group_name: str, my_match: int): + delattr(self.GameList, f'{game_group_name}_match{my_match}') + async def _assignment_match(self): if self.game.mode == 0: # 1e1 self.my_match = 1 From 5d679335d720e9a1f93eead841ee8b87b4eee1f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Sun, 31 Mar 2024 20:00:02 +0900 Subject: [PATCH 09/17] refactor: game code refactoring #504 --- backend/games/consumers/GameConsumer.py | 49 ++++++++++--------------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/backend/games/consumers/GameConsumer.py b/backend/games/consumers/GameConsumer.py index 08abd617..e0708c38 100644 --- a/backend/games/consumers/GameConsumer.py +++ b/backend/games/consumers/GameConsumer.py @@ -284,30 +284,21 @@ async def _print_start_log(self, mode): async def _process_game_start(self, message_data): if self.player1: - if self.my_match == 1: - self.match1 = await self._game_ready(message_data, self.match1_group_name) - if self.match1 is not None: - self.match1 = await self._play_game(self.match1, self.match1_group_name) - if self.channel_layer.groups[self.match1_group_name].__len__() == 2: - await self._save_match_data(self.my_match, self.match1, True) - else: - await self._save_match_data(self.my_match, self.match1, False) - if self.game.mode == 0: - await self._save_game_status(3) - await self._update_winner_data(self.game.mode) - else: - await self._save_match3_matching_in_database(await self._get_match12_winner(1)) - await self._send_end_message(self.game.match1) - elif self.my_match == 2: - self.match2 = await self._game_ready(message_data, self.match2_group_name) - if self.match2 is not None: - self.match2 = await self._play_game(self.match2, self.match2_group_name) - if self.channel_layer.groups[self.match2_group_name].__len__() == 2: - await self._save_match_data(self.my_match, self.match2, True) - else: - await self._save_match_data(self.my_match, self.match2, False) - await self._save_match3_matching_in_database(await self._get_match12_winner(2)) - await self._send_end_message(self.game.match2) + match_group_name = f'match{self.my_match}_{self.game_id}' + match = await self._game_ready(message_data, match_group_name) + if match is not None: + await self._play_game(match, match_group_name) + if self.channel_layer.groups[match_group_name].__len__() == 2: + await self._save_match_data(self.my_match, match, True) + else: + await self._save_match_data(self.my_match, match, False) + if self.game.mode == 0: + await self._save_game_status(3) + await self._update_winner_data(self.game.mode) + else: + await self._save_match3_matching_in_database(await self._get_match12_winner(self.my_match)) + await self._send_end_message(self.my_match) + await self._delete_match_object(self.game_group_name, self.my_match) async def _process_match3_game_start(self, message_data): self.my_match = 3 @@ -315,13 +306,13 @@ async def _process_match3_game_start(self, message_data): await self._save_game_object_by_id() await self._set_player1() if self.player1: - self.match3 = await self._game_ready(message_data, self.match3_group_name) - if self.match3 is not None: - self.match3 = await self._play_game(self.match3, self.match3_group_name) - await self._save_match_data(self.my_match, self.match3, True) + match = await self._game_ready(message_data, self.match3_group_name) + if match is not None: + await self._play_game(match, self.match3_group_name) + await self._save_match_data(self.my_match, match, True) await self._save_game_status(3) await self._update_winner_data(self.game.mode) - await self._send_end_message(self.game.match3) + await self._send_end_message(self.my_match) async def _game_ready(self, message_data, group_name): await self._init_game(message_data, self.my_match) From e41288108a1126abca70743732853a468df04b99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Sun, 31 Mar 2024 20:01:21 +0900 Subject: [PATCH 10/17] refactor: get object function refactoring #504 --- backend/games/consumers/GameConsumer.py | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/backend/games/consumers/GameConsumer.py b/backend/games/consumers/GameConsumer.py index e0708c38..bd5d59fb 100644 --- a/backend/games/consumers/GameConsumer.py +++ b/backend/games/consumers/GameConsumer.py @@ -107,7 +107,7 @@ async def _process_valid_user_disconnect(self): if self.game.mode != 0 and await self._is_loser() and await self._is_match_finished(self.my_match): await self.channel_layer.group_discard(self.game_group_name, self.channel_name) if await self._is_game_finished(self.game.mode) is False: - match = await self._get_my_match_PingPongGame_object(self.my_match) + match = await self._get_my_match_PingPongGame_object(self.game_group_name, self.my_match) group_name = await self._get_my_match_group_name(self.my_match) await self._dodge(self.my_match, match, self.player1, group_name) @@ -318,7 +318,7 @@ async def _game_ready(self, message_data, group_name): await self._init_game(message_data, self.my_match) if await self._waiting_join(group_name, 'match'): return None - match = await self._get_my_match_PingPongGame_object(self.my_match) + match = await self._get_my_match_PingPongGame_object(self.game_group_name, self.my_match) await self._send_start_message(match, group_name) await asyncio.sleep(2) match.started_at = datetime.now() @@ -351,7 +351,7 @@ async def _process_keyboard_input(self, message_data): await self._send_data(await self._get_my_match_group_name(self.my_match), 'down') else: if message_data == 'up': - match = await self._get_my_match_PingPongGame_object(self.my_match) + match = await self._get_my_match_PingPongGame_object(self.game_group_name, self.my_match) if match.left_side_player.bar.y >= 0: p1_lock.acquire() match.left_side_player.bar.y -= GAME_SETTINGS_DICT['bar']['speed'] @@ -359,7 +359,7 @@ async def _process_keyboard_input(self, message_data): match.left_side_player.bar.y = 0 p1_lock.release() elif message_data == 'down': - match = await self._get_my_match_PingPongGame_object(self.my_match) + match = await self._get_my_match_PingPongGame_object(self.game_group_name, self.my_match) if match.left_side_player.bar.y + GAME_SETTINGS_DICT['bar']['height'] <= match.ping_pong_map.height: p2_lock.acquire() match.left_side_player.bar.y += GAME_SETTINGS_DICT['bar']['speed'] @@ -519,7 +519,7 @@ async def game_end(self, event): async def up(self, event): if event['sender_nickname'] != self.user.nickname: - match = await self._get_my_match_PingPongGame_object(self.my_match) + match = await self._get_my_match_PingPongGame_object(self.game_group_name, self.my_match) if match.right_side_player.bar.y >= 0: p2_lock.acquire() match.right_side_player.bar.y -= GAME_SETTINGS_DICT['bar']['speed'] @@ -529,7 +529,7 @@ async def up(self, event): async def down(self, event): if event['sender_nickname'] != self.user.nickname: - match = await self._get_my_match_PingPongGame_object(self.my_match) + match = await self._get_my_match_PingPongGame_object(self.game_group_name, self.my_match) if match.right_side_player.bar.y + GAME_SETTINGS_DICT['bar']['height'] <= match.ping_pong_map.height: p2_lock.acquire() match.right_side_player.bar.y += GAME_SETTINGS_DICT['bar']['speed'] @@ -549,15 +549,8 @@ async def player2_disconnect(self, event): await self._save_winner(self.my_match) match.finished = True - async def _get_my_match_PingPongGame_object(self, my_match): - match = None - if my_match == 1: - match = self.match1 - elif my_match == 2: - match = self.match2 - elif my_match == 3: - match = self.match3 - return match + async def _get_my_match_PingPongGame_object(self, game_group_name, my_match): + return getattr(self.GameList, f'{game_group_name}_match{my_match}') async def _get_my_match_group_name(self, my_match): group_name = None From 41c3492168043c45d79007c34c32b40e78cae84d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Sun, 31 Mar 2024 20:02:09 +0900 Subject: [PATCH 11/17] refactor: refactoring game code #504 --- backend/games/consumers/GameConsumer.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/backend/games/consumers/GameConsumer.py b/backend/games/consumers/GameConsumer.py index bd5d59fb..88b038b6 100644 --- a/backend/games/consumers/GameConsumer.py +++ b/backend/games/consumers/GameConsumer.py @@ -329,7 +329,6 @@ async def _play_game(self, match, group_name): await self._check_game(match) await self._send_in_game_message(match, group_name) await asyncio.sleep(GAME_SETTINGS_DICT['play']['frame']) - return match async def _send_final_match_table(self): serializer_data = await self._get_serializer_data(True) @@ -394,14 +393,9 @@ def _init_game(self, message_data, match): map_width = message_data['map_width'] map_height = message_data['map_height'] - self.ping_pong_map = PingPongMap(map_width, map_height) - - if match == 1: - self.match1 = PingPongGame(self.ping_pong_map, self.game.match1.player1, self.game.match1.player2) - elif match == 2: - self.match2 = PingPongGame(self.ping_pong_map, self.game.match2.player1, self.game.match2.player2) - elif match == 3: - self.match3 = PingPongGame(self.ping_pong_map, self.game.match3.player1, self.game.match3.player2) + setattr(self.GameList, + f'{self.game_group_name}_match{str(match)}', + PingPongGame(PingPongMap(map_width, map_height), self.game.match1.player1, self.game.match1.player2)) @database_sync_to_async def _save_match3_matching_in_database(self, winner: User): @@ -412,7 +406,9 @@ def _save_match3_matching_in_database(self, winner: User): match.player2 = winner match.save() - async def _send_end_message(self, match: Result): + async def _send_end_message(self, my_match: int): + match = getattr(self.game, f'match{my_match}') + type_ = 'game_end' data = { 'game_id': self.game_id, From 7be86c04a553313b4e978804a2a7e45b7c5f2b3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Sun, 31 Mar 2024 20:23:18 +0900 Subject: [PATCH 12/17] refactor: keyboard refactoring #504 --- backend/games/consumers/GameConsumer.py | 61 ++++++++----------------- 1 file changed, 20 insertions(+), 41 deletions(-) diff --git a/backend/games/consumers/GameConsumer.py b/backend/games/consumers/GameConsumer.py index 88b038b6..bae381b6 100644 --- a/backend/games/consumers/GameConsumer.py +++ b/backend/games/consumers/GameConsumer.py @@ -343,28 +343,27 @@ def _set_player1(self): self.player1 = True async def _process_keyboard_input(self, message_data): - if self.player1 is False: - if message_data == 'up': - await self._send_data(await self._get_my_match_group_name(self.my_match), 'up') - elif message_data == 'down': - await self._send_data(await self._get_my_match_group_name(self.my_match), 'down') + position = None + if self.player1: + position = 'left' else: - if message_data == 'up': - match = await self._get_my_match_PingPongGame_object(self.game_group_name, self.my_match) - if match.left_side_player.bar.y >= 0: - p1_lock.acquire() - match.left_side_player.bar.y -= GAME_SETTINGS_DICT['bar']['speed'] - if match.left_side_player.bar.y < 0: - match.left_side_player.bar.y = 0 - p1_lock.release() - elif message_data == 'down': - match = await self._get_my_match_PingPongGame_object(self.game_group_name, self.my_match) - if match.left_side_player.bar.y + GAME_SETTINGS_DICT['bar']['height'] <= match.ping_pong_map.height: - p2_lock.acquire() - match.left_side_player.bar.y += GAME_SETTINGS_DICT['bar']['speed'] - if match.left_side_player.bar.y > match.ping_pong_map.height: - match.left_side_player.bar.y = match.ping_pong_map.height - GAME_SETTINGS_DICT['bar']['height'] - p2_lock.release() + position = 'right' + await self._move_bar(message_data, position) + + async def _move_bar(self, key, position): + match = await self._get_my_match_PingPongGame_object(self.game_group_name, self.my_match) + player = getattr(match, f'{position}_side_player') + + if key == 'up': + if player.bar.y >= 0: + player.bar.y -= GAME_SETTINGS_DICT['bar']['speed'] + if player.bar.y < 0: + player.bar.y = 0 + elif key == 'down': + if player.bar.y + GAME_SETTINGS_DICT['bar']['height'] <= match.ping_pong_map.height: + player.bar.y += GAME_SETTINGS_DICT['bar']['speed'] + if player.bar.y > match.ping_pong_map.height: + player.bar.y = match.ping_pong_map.height - GAME_SETTINGS_DICT['bar']['height'] async def _send_data(self, group_name, type_): await self.channel_layer.group_send( @@ -513,26 +512,6 @@ async def in_game(self, event): async def game_end(self, event): await self.send(text_data=json.dumps(event)) - async def up(self, event): - if event['sender_nickname'] != self.user.nickname: - match = await self._get_my_match_PingPongGame_object(self.game_group_name, self.my_match) - if match.right_side_player.bar.y >= 0: - p2_lock.acquire() - match.right_side_player.bar.y -= GAME_SETTINGS_DICT['bar']['speed'] - if match.right_side_player.bar.y < 0: - match.right_side_player.bar.y = 0 - p2_lock.release() - - async def down(self, event): - if event['sender_nickname'] != self.user.nickname: - match = await self._get_my_match_PingPongGame_object(self.game_group_name, self.my_match) - if match.right_side_player.bar.y + GAME_SETTINGS_DICT['bar']['height'] <= match.ping_pong_map.height: - p2_lock.acquire() - match.right_side_player.bar.y += GAME_SETTINGS_DICT['bar']['speed'] - if match.right_side_player.bar.y > match.ping_pong_map.height: - match.right_side_player.bar.y = match.ping_pong_map.height - GAME_SETTINGS_DICT['bar']['height'] - p2_lock.release() - async def player2_disconnect(self, event): if self.player1: match_attributes = { From 6de674876e8189d51b0676e9cd034083dcc1cc82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Sun, 31 Mar 2024 20:24:25 +0900 Subject: [PATCH 13/17] fix: use score choices --- backend/games/consumers/GameConsumer.py | 2 +- backend/src/choices.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/backend/games/consumers/GameConsumer.py b/backend/games/consumers/GameConsumer.py index bae381b6..f457dce5 100644 --- a/backend/games/consumers/GameConsumer.py +++ b/backend/games/consumers/GameConsumer.py @@ -383,7 +383,7 @@ async def _check_game(self, match): if (whether_score_a_goal := match.ball.is_goal_in(match.ping_pong_map)) != [False, False]: match.update_score(whether_score_a_goal) match.ball.reset(match.ping_pong_map) - if match.left_side_player.score + match.right_side_player.score == 5: + if match.left_side_player.score + match.right_side_player.score == GAME_SETTINGS_DICT['play']['score']: match.finished = True match.ball.move() diff --git a/backend/src/choices.py b/backend/src/choices.py index db1ce4e5..1f90d4b5 100644 --- a/backend/src/choices.py +++ b/backend/src/choices.py @@ -59,7 +59,8 @@ } }, 'play': { - 'frame': 1 / 24 + 'frame': 1 / 24, + 'score': 5, } } From d8776a7b408776a010a3b9e58a71b4f59fa5ada1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Mon, 1 Apr 2024 01:45:02 +0900 Subject: [PATCH 14/17] refactor: disconnect handling #504 --- backend/games/consumers/GameConsumer.py | 64 +++++++++++-------------- 1 file changed, 27 insertions(+), 37 deletions(-) diff --git a/backend/games/consumers/GameConsumer.py b/backend/games/consumers/GameConsumer.py index f457dce5..965fb208 100644 --- a/backend/games/consumers/GameConsumer.py +++ b/backend/games/consumers/GameConsumer.py @@ -104,12 +104,24 @@ async def _process_valid_user_connect(self): async def _process_valid_user_disconnect(self): await self._save_game_object_by_id() + try: + await self._delete_match_object(self.game_group_name, 1) + except AttributeError: + pass + try: + await self._delete_match_object(self.game_group_name, 2) + except AttributeError: + pass + try: + await self._delete_match_object(self.game_group_name, 3) + except AttributeError: + pass if self.game.mode != 0 and await self._is_loser() and await self._is_match_finished(self.my_match): await self.channel_layer.group_discard(self.game_group_name, self.channel_name) if await self._is_game_finished(self.game.mode) is False: match = await self._get_my_match_PingPongGame_object(self.game_group_name, self.my_match) group_name = await self._get_my_match_group_name(self.my_match) - await self._dodge(self.my_match, match, self.player1, group_name) + await self._dodge(self.my_match, match, group_name) @database_sync_to_async def _is_loser(self): @@ -122,9 +134,16 @@ def _is_loser(self): except Exception as e: return False - async def _dodge(self, my_match, result: PingPongGame, player1: bool, match_group_name): - if player1: - if result is None: + async def _dodge(self, my_match, result: PingPongGame, match_group_name): + if result is None: + await self.channel_layer.group_send( + match_group_name, + { + 'type': 'close.connection', + 'data': 'dodge' + }) + else: + if result.started_at is None: await self.channel_layer.group_send( match_group_name, { @@ -132,27 +151,10 @@ async def _dodge(self, my_match, result: PingPongGame, player1: bool, match_grou 'data': 'dodge' }) else: - if result.started_at is None: - await self.channel_layer.group_send( - match_group_name, - { - 'type': 'close.connection', - 'data': 'dodge' - }) - else: - self._save_match_data(my_match, result, False) - if my_match != 3: - await self.channel_layer.group_discard(self.game_group_name, self.channel_name) - await self.channel_layer.group_discard(match_group_name, self.channel_name) - else: - await self.channel_layer.group_send( - match_group_name, - { - 'type': 'player2_disconnect' - }) - if my_match != 3: - await self.channel_layer.group_discard(self.game_group_name, self.channel_name) - await self.channel_layer.group_discard(match_group_name, self.channel_name) + self._save_match_data(my_match, result, False) + if my_match != 3: + await self.channel_layer.group_discard(self.game_group_name, self.channel_name) + await self.channel_layer.group_discard(match_group_name, self.channel_name) @database_sync_to_async def _is_match_finished(self, my_match): @@ -512,18 +514,6 @@ async def in_game(self, event): async def game_end(self, event): await self.send(text_data=json.dumps(event)) - async def player2_disconnect(self, event): - if self.player1: - match_attributes = { - 1: self.match1, - 2: self.match2, - 3: self.match3 - } - match = match_attributes.get(self.my_match) - if match is not None: - await self._save_winner(self.my_match) - match.finished = True - async def _get_my_match_PingPongGame_object(self, game_group_name, my_match): return getattr(self.GameList, f'{game_group_name}_match{my_match}') From 4bcfa8ec5692f414cd5ac566c9b6b59ae3e3c33d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Mon, 1 Apr 2024 01:45:44 +0900 Subject: [PATCH 15/17] chore: remove comment --- backend/games/consumers/GameConsumer.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/backend/games/consumers/GameConsumer.py b/backend/games/consumers/GameConsumer.py index 965fb208..578696ef 100644 --- a/backend/games/consumers/GameConsumer.py +++ b/backend/games/consumers/GameConsumer.py @@ -483,10 +483,6 @@ async def _send_in_game_message(self, match, group_name): 'data': data } ) - # await self.in_game({ - # 'type': 'in_game', - # 'data': data - # }) async def close_connection(self, event): await self.send(text_data=json.dumps(event)) From 892d874bb5f1403614b71acbf40a95fae9dad6a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Mon, 1 Apr 2024 01:46:00 +0900 Subject: [PATCH 16/17] feat: delete object --- backend/games/consumers/GameConsumer.py | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/games/consumers/GameConsumer.py b/backend/games/consumers/GameConsumer.py index 578696ef..58acbaea 100644 --- a/backend/games/consumers/GameConsumer.py +++ b/backend/games/consumers/GameConsumer.py @@ -315,6 +315,7 @@ async def _process_match3_game_start(self, message_data): await self._save_game_status(3) await self._update_winner_data(self.game.mode) await self._send_end_message(self.my_match) + await self._delete_match_object(self.game_group_name, 3) async def _game_ready(self, message_data, group_name): await self._init_game(message_data, self.my_match) From d3313a380db7a7548e0d6ed0fa43a3e9ed755338 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Mon, 1 Apr 2024 01:46:18 +0900 Subject: [PATCH 17/17] refactor: code refactoring #504 --- backend/games/consumers/GameConsumer.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/backend/games/consumers/GameConsumer.py b/backend/games/consumers/GameConsumer.py index 58acbaea..414ec132 100644 --- a/backend/games/consumers/GameConsumer.py +++ b/backend/games/consumers/GameConsumer.py @@ -515,14 +515,12 @@ async def _get_my_match_PingPongGame_object(self, game_group_name, my_match): return getattr(self.GameList, f'{game_group_name}_match{my_match}') async def _get_my_match_group_name(self, my_match): - group_name = None - if my_match == 1: - group_name = self.match1_group_name - elif my_match == 2: - group_name = self.match2_group_name - elif my_match == 3: - group_name = self.match3_group_name - return group_name + group_name_options = { + 1: self.match1_group_name, + 2: self.match2_group_name, + 3: self.match3_group_name, + } + return group_name_options.get(my_match) @database_sync_to_async def _get_serializer_data(self, final):