Skip to content

Commit

Permalink
[refactor] code reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
teds-lin committed May 11, 2024
1 parent 3d12748 commit ce8eb56
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 17 deletions.
3 changes: 1 addition & 2 deletions backend/domain/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class Game:
game_id: str
players: List[Player]
active: bool = True


def __init__(self, game_id: str, players: List[dict], active: bool = True):
self.game_id = game_id
Expand Down Expand Up @@ -121,7 +120,7 @@ def to_dict(self) -> dict:
"game_id": self.game_id,
"players": [player.to_dict() for player in self.players],
"active": self.active,
"action_message":self.action_message,
"action_message": self.action_message,
}

if self.current_player is not None:
Expand Down
4 changes: 3 additions & 1 deletion backend/repository/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def __init__(self, config: Config):
username: Union[str, None] = self.config.MONGODB_SETTINGS.get("username")
password: Union[str, None] = self.config.MONGODB_SETTINGS.get("password")

uri: str = f"mongodb://{self.config.MONGODB_SETTINGS['host']}:{self.config.MONGODB_SETTINGS['port']}/?retryWrites=true&w=majority"
uri: str = (
f"mongodb://{self.config.MONGODB_SETTINGS['host']}:{self.config.MONGODB_SETTINGS['port']}/?retryWrites=true&w=majority"
)

if username and password:
uri = f"mongodb+srv://{username}:{password}@{self.config.MONGODB_SETTINGS['host']}/"
Expand Down
4 changes: 3 additions & 1 deletion backend/repository/redis_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ def __init__(self, config: Config):
self.db: int = self.config.REDIS_DB
self.REDIS_PROTOCOL: str = self.config.REDIS_PROTOCOL
if self.config.REDIS_PASSWORD:
self.redis_uri: str = f"{self.REDIS_PROTOCOL}://default:{self.config.REDIS_PASSWORD}@{self.host}:{self.port}/{self.db}"
self.redis_uri: str = (
f"{self.REDIS_PROTOCOL}://default:{self.config.REDIS_PASSWORD}@{self.host}:{self.port}/{self.db}"
)
else:
self.redis_uri: str = (
f"{self.REDIS_PROTOCOL}://{self.host}:{self.port}/{self.db}"
Expand Down
17 changes: 12 additions & 5 deletions backend/service/game_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def player_join_game(self, game_id: str, player_id: str) -> bool:
game = self.start_game(game)
game.action_message = "回合開始"
self.game_repository.update_game(game)

return player_joined_stat

def start_game(self, game: Game) -> Game:
Expand Down Expand Up @@ -82,9 +82,16 @@ def cast_spell(
hp_damge = -1
if spell_name == "Magic 1":
# 喊得是火龍,玩家擲骰扣HP
hp_damge = roll_dice() * -1
hp_damge = roll_dice() * -1
player.update_HP(hp_damge)
game.action_message = player.player_id + " 施放 " +spell_name + " 失敗自損 " + str(abs(hp_damge)) +" 滴血"
game.action_message = (
player.player_id
+ " 施放 "
+ spell_name
+ " 失敗自損 "
+ str(abs(hp_damge))
+ " 滴血"
)
self.game_repository.update_game(game)

if player.get_HP() == 0:
Expand Down Expand Up @@ -121,7 +128,7 @@ def cast_spell(
# 將手牌放置於階梯
game.ladder.append(spell_name)
# 儲存目前遊戲狀態
game.action_message = player.player_id + " 施法 " + spell_name +" 成功 "
game.action_message = player.player_id + " 施法 " + spell_name + " 成功 "
self.game_repository.update_game(game)

for p in game.players:
Expand Down Expand Up @@ -237,7 +244,7 @@ def convert_object_id(data):
return data

game = self.game_repository.get_game_by_id(game_id)
if game:
if game:
# [待討論]新增一個玩家是否存在的判斷?
query_result = game.real_game_can_see(player_id).to_dict()
return convert_object_id(query_result)
Expand Down
10 changes: 5 additions & 5 deletions backend/tests/e2e/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,22 @@ def test_roomID_status(client):
assert resp.status_code == 200

# 測試gameRoomID不存在 與 player_id 正確
params = "player_id=p1"
resp = client.get(f"/player/status?{params}")
params = "player_id=p1"
resp = client.get(f"/player/status?{params}")
assert resp.status_code == 400
resp_json = resp.json
assert resp_json["message"] == "gameRoomID not found"

# 測試gameRoomID錯誤的 與 player_id 正確
params = "player_id=p1&gameRoomID=" + "wrong_gameroomID"
resp = client.get(f"/player/status?{params}")
resp = client.get(f"/player/status?{params}")
assert resp.status_code == 400
resp_json = resp.json
assert resp_json["message"] == "gameRoomID does not exist"

# 測試gameRoomID正確 與 player_id 不存在
params = "gameRoomID=" + game_id
resp = client.get(f"/player/status?{params}")
resp = client.get(f"/player/status?{params}")
assert resp.status_code == 400
resp_json = resp.json
assert resp_json["message"] == "player_id not found"
assert resp_json["message"] == "player_id not found"
8 changes: 5 additions & 3 deletions backend/tests/integration/test_game_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ def test_player_status(game_service, five_player_game):
assert game_service_result["players"][2]["player_id"] == "Tux"
assert game_service_result["players"][2]["HP"] == 6


# 測試玩家重複加入,不會開新的一局
def test_player_rejoined(game_service, five_player_game):
game_id = five_player_game.game_id
Expand All @@ -329,11 +330,12 @@ def test_player_rejoined(game_service, five_player_game):
game.round = 2
game_service.game_repository.update_game(game)

assert game_service.player_join_game(game_id, "Yock") == False
assert not game_service.player_join_game(game_id, "Yock")
game2 = game_service.game_repository.get_game_by_id(game_id)
assert game2.round == 2

#取得遊戲狀態若無此房號則回傳訊息

# 取得遊戲狀態若無此房號則回傳訊息
def test_roomID_status(game_service, five_player_game):
game_id = five_player_game.game_id
for player in five_player_game.players:
Expand All @@ -342,7 +344,7 @@ def test_roomID_status(game_service, five_player_game):
query_player_id = five_player_game.players[2].player_id
assert query_player_id == "Tux"

#輸入正確的player_id、錯誤的game_roomID
# 輸入正確的player_id、錯誤的game_roomID
room_id = None
game_roomID = game_service.player_status(room_id, query_player_id)
assert game_roomID is None

0 comments on commit ce8eb56

Please sign in to comment.