Skip to content

Commit 8e43f5d

Browse files
author
Seungwon An
authored
Merge pull request #35 from MomHelpMe/dev/tournament-game
[DEV] 수 많은 일
2 parents dd459ba + d886c32 commit 8e43f5d

32 files changed

+2141
-238
lines changed

backend/game/consumers.py

Lines changed: 71 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,26 @@
1010

1111
SCREEN_HEIGHT = 750
1212
SCREEN_WIDTH = 1250
13+
MAX_SCORE = 150
1314

1415

1516
class GameState:
1617
def __init__(self):
1718
print("initializing game state!")
1819
self.map = GameMap()
19-
self.left_bar = Bar(
20-
0,
21-
Bar.X_GAP,
22-
SCREEN_HEIGHT // 2 - Bar.HEIGHT // 2,
23-
0
24-
)
20+
self.left_bar = Bar(0, Bar.X_GAP, SCREEN_HEIGHT // 2 - Bar.HEIGHT // 2, 0)
2521
self.right_bar = Bar(
2622
1,
2723
SCREEN_WIDTH - Bar.WIDTH - Bar.X_GAP,
2824
SCREEN_HEIGHT // 2 - Bar.HEIGHT // 2,
29-
SCREEN_WIDTH - Bar.WIDTH
25+
SCREEN_WIDTH - Bar.WIDTH,
3026
)
3127
self.left_ball = Ball(0, SCREEN_HEIGHT, SCREEN_WIDTH, self.left_bar)
3228
self.right_ball = Ball(1, SCREEN_HEIGHT, SCREEN_WIDTH, self.right_bar)
29+
self.score = [0, 0]
30+
self.player = ["player1", "player2"]
31+
self.penalty_time = [0, 0]
32+
3333

3434
class GameConsumer(AsyncWebsocketConsumer):
3535
game_tasks = {}
@@ -50,7 +50,7 @@ async def receive(self, text_data):
5050
text_data_json = json.loads(text_data)
5151
action = text_data_json["action"]
5252

53-
print("text_data", text_data_json)
53+
# print("text_data", text_data_json)
5454
if action == "authenticate":
5555
token = text_data_json.get("token")
5656
if not token or not self.authenticate(token):
@@ -75,7 +75,6 @@ async def receive(self, text_data):
7575

7676
# Start ball movement if not already running
7777
if self.room_group_name not in GameConsumer.game_tasks:
78-
print("starting game loop!")
7978
GameConsumer.game_tasks[self.room_group_name] = asyncio.create_task(
8079
self.game_loop()
8180
)
@@ -113,9 +112,13 @@ def authenticate(self, token):
113112
# Decode JWT token
114113
decoded = jwt.decode(token, settings.SECRET_KEY, algorithms=["HS256"])
115114
uid = decoded.get("id")
116-
print(f"uid: {uid}", f"room_name: {self.room_name}", str(uid) == str(self.room_name))
115+
print(
116+
f"uid: {uid}",
117+
f"room_name: {self.room_name}",
118+
str(uid) == str(self.room_name),
119+
)
117120
# Check if uid matches the room_name
118-
if str(uid) == str(self.room_name):
121+
if str(uid) == str(self.room_name):
119122
return True
120123
else:
121124
return False
@@ -129,7 +132,9 @@ def authenticate(self, token):
129132
async def disconnect(self, close_code):
130133
# Leave room group
131134
if self.authenticated:
132-
await self.channel_layer.group_discard(self.room_group_name, self.channel_name)
135+
await self.channel_layer.group_discard(
136+
self.room_group_name, self.channel_name
137+
)
133138
# Decrease the client count for this room
134139
if self.room_group_name in GameConsumer.client_counts:
135140
GameConsumer.client_counts[self.room_group_name] -= 1
@@ -141,7 +146,6 @@ async def disconnect(self, close_code):
141146
else:
142147
GameConsumer.client_counts[self.room_group_name] = 0
143148

144-
145149
async def send_initialize_game(self):
146150
state = GameConsumer.game_states[self.room_group_name]
147151
await self.send(
@@ -163,6 +167,8 @@ async def send_initialize_game(self):
163167
"right_ball_y": state.right_ball.y,
164168
"ball_radius": Ball.RADIUS,
165169
"map": state.map.map,
170+
"player": state.player,
171+
"max_score": MAX_SCORE,
166172
}
167173
)
168174
)
@@ -175,26 +181,65 @@ async def game_loop(self):
175181
state.left_bar.update()
176182
state.right_bar.update()
177183

178-
state.left_ball.move(state.left_bar)
184+
state.penalty_time[0] = state.left_ball.move(state.left_bar)
179185
state.left_ball.check_bar_collision(state.left_bar)
180186
state.left_ball.check_bar_collision(state.right_bar)
181-
state.left_ball.check_collision(state.map)
187+
state.left_ball.check_collision(state.map, state.score)
182188

183-
state.right_ball.move(state.right_bar)
189+
state.penalty_time[1] = state.right_ball.move(state.right_bar)
184190
state.right_ball.check_bar_collision(state.left_bar)
185191
state.right_ball.check_bar_collision(state.right_bar)
186-
state.right_ball.check_collision(state.map)
192+
state.right_ball.check_collision(state.map, state.score)
187193

188-
if count % 3 == 0:
194+
if count % 5 == 0:
189195
await self.send_game_state()
190-
await asyncio.sleep(0.005)
196+
await asyncio.sleep(0.00390625)
191197
await state.map.init()
198+
if state.score[0] >= MAX_SCORE:
199+
await asyncio.sleep(0.1)
200+
await self.send_game_result(0)
201+
await asyncio.sleep(0.1)
202+
await self.close()
203+
break
204+
elif state.score[1] >= MAX_SCORE:
205+
await asyncio.sleep(0.1)
206+
await self.send_game_result(1)
207+
await asyncio.sleep(0.1)
208+
await self.close()
209+
break
192210
else:
193-
await asyncio.sleep(0.005)
211+
await asyncio.sleep(0.00390625)
194212
count += 1
195213
except asyncio.CancelledError:
196214
# Handle the game loop cancellation
197215
print("Game loop cancelled for", self.room_group_name)
216+
await self.close()
217+
218+
async def send_game_result(self, winner):
219+
state = GameConsumer.game_states[self.room_group_name]
220+
await self.channel_layer.group_send(
221+
self.room_group_name,
222+
{
223+
"type": "game_result_message",
224+
"score": state.score,
225+
"winner": winner,
226+
},
227+
)
228+
229+
async def game_result_message(self, event):
230+
score = event["score"]
231+
winner = event["winner"]
232+
233+
# Send the game result to the WebSocket
234+
await self.send(
235+
text_data=json.dumps(
236+
{
237+
"type": "game_result",
238+
"score": score,
239+
"winner": winner,
240+
}
241+
)
242+
)
198243

199244
async def send_game_state(self):
200245
state = GameConsumer.game_states[self.room_group_name]
@@ -211,6 +256,8 @@ async def send_game_state(self):
211256
"right_ball_x": state.right_ball.x,
212257
"right_ball_y": state.right_ball.y,
213258
"map_diff": state.map.diff,
259+
"score": state.score,
260+
"penalty_time": state.penalty_time,
214261
},
215262
)
216263

@@ -223,6 +270,8 @@ async def update_game_state_message(self, event):
223270
left_ball_y = event["left_ball_y"]
224271
right_ball_x = event["right_ball_x"]
225272
right_ball_y = event["right_ball_y"]
273+
score = event["score"]
274+
penalty_time = event["penalty_time"]
226275

227276
# Send the updated game state to the WebSocket
228277
await self.send(
@@ -238,6 +287,8 @@ async def update_game_state_message(self, event):
238287
"right_ball_x": int(right_ball_x),
239288
"right_ball_y": int(right_ball_y),
240289
"map_diff": GameConsumer.game_states[self.room_group_name].map.diff,
290+
"score": score,
291+
"penalty_time": penalty_time,
241292
}
242293
)
243294
)

0 commit comments

Comments
 (0)