Skip to content

Commit f865ec5

Browse files
author
teds-lin
committed
[修改]Refactor player_join_game to return detailed response and update tests for error handling
1 parent e224a24 commit f865ec5

File tree

4 files changed

+59
-21
lines changed

4 files changed

+59
-21
lines changed

backend/app/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,14 @@ def player_join_game(player_id):
9191
if not room_id:
9292
return jsonify({"error": "Game room ID required"}), 400
9393

94-
if GameService.player_join_game(room_id, player_id):
94+
result = GameService.player_join_game(room_id, player_id)
95+
if result["success"]:
9596
return (
9697
jsonify({"message": "Player joined the game", "gameRoomID": room_id}),
9798
200,
9899
)
99100
else:
100-
return jsonify({"message": "Unable to join game"}), 400
101+
return jsonify({"error": result["message"]}), result["status_code"]
101102

102103

103104
@app.route("/stone", methods=["PATCH"])

backend/service/game_service.py

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,47 @@ def create_game(
2323
self.game_repository.update_game(game)
2424
return game
2525

26-
def player_join_game(self, game_id: str, player_id: str) -> bool:
26+
def player_join_game(self, game_id: str, player_id: str) -> dict:
2727
game = self.game_repository.get_game_by_id(game_id)
28-
if not (game and game.is_active()):
29-
# 從資料庫確認game_id存在,並且遊戲進行中
30-
return False
31-
player_joined_stat = False
32-
if game:
33-
player = next((p for p in game.players if p.player_id == player_id), None)
34-
if player:
35-
if player.joined:
36-
return False
37-
player.joined = True
38-
game.action_message = player.player_id + " 加入遊戲"
39-
game.event_name = "game_joined"
40-
self.game_repository.update_game(game)
41-
player_joined_stat = True
28+
29+
# 遊戲不存在的情況
30+
if not game:
31+
return {
32+
"success": False,
33+
"message": "Game room does not exist",
34+
"status_code": 404,
35+
}
36+
37+
# 遊戲不在進行中的情況
38+
if not game.is_active():
39+
return {
40+
"success": False,
41+
"message": "Game is not active",
42+
"status_code": 403,
43+
}
44+
45+
# 檢查玩家是否存在於遊戲中
46+
player = next((p for p in game.players if p.player_id == player_id), None)
47+
if not player:
48+
return {
49+
"success": False,
50+
"message": "Player not found in this game",
51+
"status_code": 404,
52+
}
53+
54+
# 檢查玩家是否已經加入遊戲
55+
if player.joined:
56+
return {
57+
"success": False,
58+
"message": "Player already joined this game",
59+
"status_code": 409, # Conflict
60+
}
61+
62+
# 成功加入遊戲
63+
player.joined = True
64+
game.action_message = player.player_id + " 加入遊戲"
65+
game.event_name = "game_joined"
66+
self.game_repository.update_game(game)
4267

4368
all_joined = all(p.joined for p in game.players)
4469

@@ -47,8 +72,17 @@ def player_join_game(self, game_id: str, player_id: str) -> bool:
4772
game.action_message = "回合開始"
4873
game.event_name = "turn_started"
4974
self.game_repository.update_game(game)
50-
51-
return player_joined_stat
75+
return {
76+
"success": True,
77+
"message": "All player joined the game successfully",
78+
"status_code": 200,
79+
}
80+
else:
81+
return {
82+
"success": True,
83+
"message": "Player joined the game successfully",
84+
"status_code": 200,
85+
}
5286

5387
def start_game(self, game: Game) -> Game:
5488
game = game.init_game_state(game)

backend/tests/e2e/test_invalid_gameRoomID.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
def test_join_game_invalid_gameRoomID(client):
22
game_id_data = {"gameRoomID": "invalid gameRoomID"}
33
resp = client.put("/player/p1/join", json=game_id_data)
4-
assert resp.status_code == 400
4+
assert resp.status_code == 404
55

66

77
def test_stone_invalid_gameRoomID(client):

backend/tests/integration/test_game_service.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,10 @@ def test_player_rejoined(game_service, five_player_game):
330330
game.round = 2
331331
game_service.game_repository.update_game(game)
332332

333-
assert not game_service.player_join_game(game_id, "Yock")
333+
result = game_service.player_join_game(game_id, "Yock")
334+
assert result["success"] is False
335+
assert result["status_code"] == 409 # 檢查是否因為玩家已加入而失敗
336+
334337
game2 = game_service.game_repository.get_game_by_id(game_id)
335338
assert game2.round == 2
336339

0 commit comments

Comments
 (0)