@@ -54,9 +54,9 @@ async def initialize(self, user1, user2):
5454 self .left_ball = Ball (0 , SCREEN_HEIGHT , SCREEN_WIDTH , self .left_bar )
5555 self .right_ball = Ball (1 , SCREEN_HEIGHT , SCREEN_WIDTH , self .right_bar )
5656 self .score = [0 , 0 ]
57- print ("user1:" , user1 , "user2:" , user2 )
57+ # print("user1:", user1, "user2:", user2)
5858 user1_nickname , user2_nickname = await get_user_nicknames (user1 , user2 )
59- print ("user1_nickname:" , user1_nickname , "user2_nickname:" , user2_nickname )
59+ # print("user1_nickname:", user1_nickname, "user2_nickname:", user2_nickname)
6060 self .player = [user1_nickname , user2_nickname ]
6161 self .penalty_time = [0 , 0 ]
6262 print ("initializing game state finished successfully!" )
@@ -75,8 +75,12 @@ async def connect(self):
7575 self .authenticated = False
7676 self .start_time = timezone .now ()
7777
78+ if self .room_group_name not in MatchingGameConsumer .game_states :
79+ return
80+
7881 await self .accept ()
7982
83+
8084 async def receive (self , text_data ):
8185 text_data_json = json .loads (text_data )
8286 action = text_data_json ["action" ]
@@ -85,10 +89,9 @@ async def receive(self, text_data):
8589 token = text_data_json .get ("token" )
8690 if not token or not self .authenticate (token ):
8791 print ("authentication failed" )
88- await self .close (code = 4001 )
92+ # await self.close(code=4001)
8993 return
9094 self .authenticated = True
91- self .my_uid = 0
9295
9396 # Join room group after successful authentication
9497 await self .channel_layer .group_add (self .room_group_name , self .channel_name )
@@ -108,8 +111,6 @@ async def receive(self, text_data):
108111 MatchingGameConsumer .game_tasks [self .room_group_name ] = (
109112 asyncio .create_task (self .game_loop ())
110113 )
111- elif not self .authenticated :
112- await self .close (code = 4001 )
113114 else :
114115 bar = text_data_json .get ("bar" )
115116 state = MatchingGameConsumer .game_states [self .room_group_name ]
@@ -158,22 +159,40 @@ def authenticate(self, token):
158159 return False
159160
160161 async def disconnect (self , close_code ):
161- # Leave room group
162+ # 인증된 소켓만 처리
162163 if self .authenticated :
163- await self .channel_layer . group_discard (
164- self .room_group_name , self .channel_name
165- )
166- # Decrease the client count for this room
164+ print ( "disconnected: " , self .user_index )
165+ await self .channel_layer . group_discard ( self . room_group_name , self .channel_name )
166+
167+ # 현재 방의 접속자 수를 1 줄임
167168 if self .room_group_name in MatchingGameConsumer .client_counts :
168169 MatchingGameConsumer .client_counts [self .room_group_name ] -= 1
169- if MatchingGameConsumer .client_counts [self .room_group_name ] <= 0 :
170- MatchingGameConsumer .game_tasks [self .room_group_name ].cancel ()
171- del MatchingGameConsumer .game_tasks [self .room_group_name ]
172- del MatchingGameConsumer .game_states [self .room_group_name ]
173- del MatchingGameConsumer .client_counts [self .room_group_name ]
170+
171+ # 만약 이제 방에 1명만 남았다면 -> 남은 사람이 승자
172+ if MatchingGameConsumer .client_counts [self .room_group_name ] == 1 :
173+ # 누가 남았는지 판별 (내가 0이라면 1이 승자, 내가 1이라면 0이 승자)
174+ winner_index = 1 - self .user_index
175+ state = MatchingGameConsumer .game_states [self .room_group_name ]
176+
177+ # send_game_result를 통해 DB에 게임결과 저장 및
178+ # 나머지(승자)에게 게임 종료 메시지 전송
179+ await self .send_game_result (winner_index )
180+ await asyncio .sleep (0.1 )
181+ await self .close ()
182+
183+ # 아무도 안 남았다면 방을 완전히 정리
184+ elif MatchingGameConsumer .client_counts [self .room_group_name ] <= 0 :
185+ if self .room_group_name in MatchingGameConsumer .game_tasks :
186+ MatchingGameConsumer .game_tasks [self .room_group_name ].cancel ()
187+ del MatchingGameConsumer .game_tasks [self .room_group_name ]
188+ if self .room_group_name in MatchingGameConsumer .game_states :
189+ del MatchingGameConsumer .game_states [self .room_group_name ]
190+ if self .room_group_name in MatchingGameConsumer .client_counts :
191+ del MatchingGameConsumer .client_counts [self .room_group_name ]
174192 else :
175193 MatchingGameConsumer .client_counts [self .room_group_name ] = 0
176194
195+
177196 async def send_initialize_game (self ):
178197 state = MatchingGameConsumer .game_states [self .room_group_name ]
179198 await self .send (
@@ -248,6 +267,7 @@ async def game_loop(self):
248267 async def send_game_result (self , winner ):
249268 state = MatchingGameConsumer .game_states [self .room_group_name ]
250269
270+ print ("Game result for" , self .room_group_name , ":" , state .score , "Winner:" , winner )
251271 await self .save_game_result (state , winner )
252272 await self .channel_layer .group_send (
253273 self .room_group_name ,
@@ -270,6 +290,10 @@ def save_game_result(self, state, winner):
270290 user2_obj = None
271291
272292 score1 , score2 = state .score
293+ if winner == 0 :
294+ score1 = MAX_SCORE
295+ else :
296+ score2 = MAX_SCORE
273297
274298 game = Game .objects .create (
275299 game_type = "PvP" ,
@@ -300,16 +324,21 @@ async def game_result_message(self, event):
300324 score = event ["score" ]
301325 winner = event ["winner" ]
302326
303- # Send the game result to the WebSocket
304- await self .send (
305- text_data = json .dumps (
306- {
307- "type" : "game_result" ,
308- "score" : score ,
309- "winner" : winner ,
310- }
327+ if not self .scope ["client" ]:
328+ return
329+ try :
330+ await self .send (
331+ text_data = json .dumps (
332+ {
333+ "type" : "game_result" ,
334+ "score" : score ,
335+ "winner" : winner ,
336+ }
337+ )
311338 )
312- )
339+ except Exception as e :
340+ print ("Failed to send game_result_message:" , e , "for" , self .user_index )
341+
313342
314343 async def send_game_state (self ):
315344 state = MatchingGameConsumer .game_states [self .room_group_name ]
0 commit comments