Skip to content

Commit

Permalink
[功能] 新增 action message
Browse files Browse the repository at this point in the history
  • Loading branch information
yockwang committed Apr 27, 2024
1 parent 80474c3 commit 86c1e96
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
6 changes: 6 additions & 0 deletions backend/domain/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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 All @@ -27,6 +28,7 @@ def __init__(self, game_id: str, players: List[dict], active: bool = True):
self.turn: Optional[int] = None # 玩家從開始施法到結束稱為回合
self.current_player: Optional[int] = None # 目前可進行施法動作玩家index
self.spells: Dict[str, Spell] = self.load_spells() # 初始化所有可能的魔法石
self.action_message: str = ""

def load_spells(self) -> Dict[str, Spell]:
"""載入遊戲中可能存在的魔法石"""
Expand Down Expand Up @@ -119,6 +121,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,
}

if self.current_player is not None:
Expand Down Expand Up @@ -154,6 +157,9 @@ def from_dict(cls, data: dict) -> "Game":
active=data.get("active", True),
)

if "action_message" in data:
game.action_message = data["action_message"]

if "current_player" in data:
game.current_player = data["current_player"]

Expand Down
2 changes: 2 additions & 0 deletions backend/service/game_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def create_game(self, player_ids: list[str]) -> Game:
game = Game(game_id=None, players=player_ids)
game_id = self.game_repository.create_game(game)
game.game_id = game_id # 從資料取得的game_id
game.action_message = "開始遊戲"
self.game_repository.update_game(game)
return game

Expand All @@ -28,6 +29,7 @@ def player_join_game(self, game_id: str, player_id: str) -> bool:
if player.joined:
return False
player.joined = True
game.action_message = player.player_id + " 加入遊戲"
self.game_repository.update_game(game)
player_joined_stat = True

Expand Down

0 comments on commit 86c1e96

Please sign in to comment.