Player state

This commit is contained in:
Eden Kirin
2023-03-30 13:09:23 +02:00
parent 8a48d61dc9
commit ecffdc5d1e
11 changed files with 84 additions and 25 deletions

View File

@ -3,7 +3,8 @@ import logging
import random
from typing import Optional
from hopper.enums import Direction, PlayerMoveResult, GameState
from hopper.countdown_timer import CountdownTimer
from hopper.enums import Direction, PlayerMoveResult, GameState, PlayerState
from hopper.errors import Collision, PositionOutOfBounds, GameLockForMovement
from hopper.interfaces import SendGameDumpInterface
from hopper.models.board import (
@ -29,8 +30,8 @@ class GameEngine:
self.ws_server = ws_server
self.players = PlayerList()
self._inacivity_watchdog = None
self.__debug_print_board()
self.game_state = GameState.RUNNING
self._purchase_countdown_timer: Optional[CountdownTimer] = None
self.reset_game()
def dump_board(self) -> list[list[str]]:
dump = self.board.dump()
@ -63,11 +64,16 @@ class GameEngine:
)
self._inacivity_watchdog.start()
async def start_game(self, player_name: str) -> Player:
async def reset_game(self) -> None:
self.__debug_print_board()
self.game_state = GameState.RUNNING
async def start_game_for_player(self, player_name: str) -> Player:
self._start_inactivity_watchdog()
player = Player(
name=player_name,
position=self._create_player_start_position(),
state=PlayerState.CREATED,
)
self.players.append(player)
@ -115,17 +121,17 @@ class GameEngine:
player.reset_timeout()
if self.game_state == GameState.LOCK_FOR_MOVEMENT:
raise GameLockForMovement("Player reached destination. Can't move now.")
raise GameLockForMovement("Player reached destination. Can't move anymore.")
# player will not be able to move once they reach the destination
if player.reached_destination:
self.game_state = GameState.LOCK_FOR_MOVEMENT
if player.state == PlayerState.ON_DESTINATION:
return PlayerMoveResult.DESTINATION_REACHED
logging.info(f"Player {player} move to {direction}")
new_position = self._move_position(player.position, direction)
player.move_attempt_count += 1
player.state = PlayerState.MOVING
if not self._position_in_board_bounds(new_position):
raise PositionOutOfBounds()
@ -137,7 +143,7 @@ class GameEngine:
player.move_count += 1
if self._is_player_on_destination(player):
player.reached_destination = True
player.state = PlayerState.ON_DESTINATION
logging.info(f"Player {player} reached destination!")
if self.ws_server:
@ -145,7 +151,8 @@ class GameEngine:
self.__debug_print_board()
if player.reached_destination:
if player.state == PlayerState.ON_DESTINATION:
self.game_state = GameState.LOCK_FOR_MOVEMENT
return PlayerMoveResult.DESTINATION_REACHED
await asyncio.sleep(settings.game.MOVE_DELAY)
@ -163,6 +170,10 @@ class GameEngine:
def _colided_with_obstacle(self, position: Position) -> bool:
return self.board.get_object_at_position(position) is not None
def _player_on_destination(self, player: Player) -> None:
self.game_state = GameState.LOCK_FOR_MOVEMENT
self._purchase_countdown_timer = CountdownTimer()
def get_board_layout(self) -> BoardLayout:
return BoardLayout(board=self.board, players=self.players)