Send player info with product purchase data
This commit is contained in:
@ -6,7 +6,6 @@ from typing import Optional
|
||||
from hopper.countdown_timer import CountdownTimer
|
||||
from hopper.enums import Direction, GameState, PlayerMoveResult, PlayerState
|
||||
from hopper.errors import Collision, GameLockForMovement, PositionOutOfBounds
|
||||
from hopper.interfaces import SendGameDumpInterface
|
||||
from hopper.models.board import (
|
||||
BOARD_DUMP_CHARS,
|
||||
BoardLayout,
|
||||
@ -19,13 +18,12 @@ from hopper.models.board import (
|
||||
)
|
||||
from hopper.models.player import Player, PlayerList, Position
|
||||
from hopper.watchdog import InactivityWatchdog
|
||||
from hopper.ws_server import WSServer
|
||||
from settings import settings
|
||||
|
||||
|
||||
class GameEngine:
|
||||
def __init__(
|
||||
self, board: GameBoard, ws_server: Optional[SendGameDumpInterface] = None
|
||||
) -> None:
|
||||
def __init__(self, board: GameBoard, ws_server: WSServer = None) -> None:
|
||||
self.board = board
|
||||
self.ws_server = ws_server
|
||||
self.players = PlayerList()
|
||||
@ -82,7 +80,12 @@ class GameEngine:
|
||||
if self.ws_server:
|
||||
await self.ws_server.send_game_dump()
|
||||
|
||||
#!!!!!!!!!!!!!!!
|
||||
await self._player_on_destination(player)
|
||||
#!!!!!!!!!!!!!!!
|
||||
|
||||
await asyncio.sleep(settings.game.MOVE_DELAY)
|
||||
|
||||
return player
|
||||
|
||||
def _create_player_start_position(self) -> Position:
|
||||
@ -171,23 +174,38 @@ class GameEngine:
|
||||
await self.ws_server.send_game_dump()
|
||||
self.__debug_print_board()
|
||||
|
||||
await self.ws_server.send_product_purchase_message(products=settings.products)
|
||||
await self.ws_server.send_product_purchase_start_message(
|
||||
player=player, products=settings.products
|
||||
)
|
||||
|
||||
logging.info(
|
||||
f"Starting purchase countdown timer for {settings.purchase_timeout} seconds"
|
||||
)
|
||||
|
||||
def on_purchase_timer_tick(time_left) -> None:
|
||||
asyncio.run(
|
||||
self.ws_server.send_product_purchase_time_left_message(
|
||||
player=player, time_left=time_left
|
||||
)
|
||||
)
|
||||
|
||||
def on_purchase_timer_done() -> None:
|
||||
logging.info("Ding ding! Purchase countdown timer timeout")
|
||||
self._purchase_countdown_timer = None
|
||||
asyncio.run(
|
||||
self.ws_server.send_product_purchase_done_message(
|
||||
player=player, product=None
|
||||
)
|
||||
)
|
||||
self.game_state = GameState.RUNNING
|
||||
|
||||
logging.info(f"Starting purchase countdown timer for {settings.purchase_timeout} seconds")
|
||||
self._purchase_countdown_timer = CountdownTimer(
|
||||
seconds=settings.purchase_timeout,
|
||||
callback=self._on_purchase_timeout,
|
||||
timer_tick_callback=on_purchase_timer_tick,
|
||||
timer_done_callback=on_purchase_timer_done,
|
||||
)
|
||||
self._purchase_countdown_timer.start()
|
||||
|
||||
def _on_purchase_timeout(self) -> None:
|
||||
logging.info("Ding ding! Purchase countdown timer timeout")
|
||||
self._purchase_countdown_timer = None
|
||||
|
||||
asyncio.run(self.ws_server.send_product_purchase_done_message(product=None))
|
||||
|
||||
self.game_state = GameState.RUNNING
|
||||
|
||||
def get_board_layout(self) -> BoardLayout:
|
||||
return BoardLayout(board=self.board, players=self.players)
|
||||
|
||||
@ -198,7 +216,7 @@ class GameEngineFactory:
|
||||
board_width: int,
|
||||
board_height: int,
|
||||
obstacle_count: int = 0,
|
||||
ws_server: Optional[SendGameDumpInterface] = None,
|
||||
ws_server: WSServer = None,
|
||||
) -> GameEngine:
|
||||
board = GameBoard(
|
||||
width=board_width,
|
||||
@ -224,7 +242,7 @@ class GameEngineFactory:
|
||||
|
||||
@staticmethod
|
||||
def create_default(
|
||||
ws_server: Optional[SendGameDumpInterface] = None,
|
||||
ws_server: WSServer = None,
|
||||
) -> GameEngine:
|
||||
return GameEngineFactory.create(
|
||||
board_width=settings.board.WIDTH,
|
||||
|
||||
Reference in New Issue
Block a user