Send player info with product purchase data
This commit is contained in:
@ -5,21 +5,24 @@ from typing import Callable, Optional
|
||||
|
||||
class CountdownTimer(Thread):
|
||||
def __init__(
|
||||
self, seconds: int, callback: Optional[Callable[[], None]] = None
|
||||
self, seconds: int, timer_tick_callback: Optional[Callable[[int], None]] = None, timer_done_callback: Optional[Callable[[], None]] = None
|
||||
) -> None:
|
||||
self.seconds = seconds
|
||||
self.stop_event = Event()
|
||||
self.callback = callback
|
||||
self.timer_tick_callback = timer_tick_callback
|
||||
self.timer_done_callback = timer_done_callback
|
||||
super().__init__(daemon=True)
|
||||
|
||||
def run(self) -> None:
|
||||
cnt = self.seconds
|
||||
while cnt and not self.stop_event.is_set():
|
||||
cnt -= 1
|
||||
time_left = self.seconds
|
||||
while time_left and not self.stop_event.is_set():
|
||||
time.sleep(1)
|
||||
time_left -= 1
|
||||
if self.timer_tick_callback:
|
||||
self.timer_tick_callback(time_left)
|
||||
|
||||
if cnt == 0 and self.callback:
|
||||
self.callback()
|
||||
if time_left == 0 and self.timer_done_callback:
|
||||
self.timer_done_callback()
|
||||
|
||||
def stop(self) -> None:
|
||||
self.stop_event.set()
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -1,16 +0,0 @@
|
||||
from typing import Iterable, Optional, Protocol
|
||||
|
||||
from hopper.models.product import Product
|
||||
|
||||
|
||||
class SendGameDumpInterface(Protocol):
|
||||
async def send_game_dump(self) -> None:
|
||||
...
|
||||
|
||||
async def send_product_purchase_message(self, products: Iterable[Product]) -> None:
|
||||
...
|
||||
|
||||
async def send_product_purchase_done_message(
|
||||
self, product: Optional[Product] = None
|
||||
) -> None:
|
||||
...
|
||||
@ -26,22 +26,26 @@ class ProductDto(BaseModel):
|
||||
description: Optional[str] = None
|
||||
|
||||
|
||||
class GameDumpPlayerDto(PlayerDto):
|
||||
...
|
||||
|
||||
|
||||
class GameDumpDto(BaseModel):
|
||||
board: BoardDto
|
||||
destination: DestinationDto
|
||||
players: list[GameDumpPlayerDto]
|
||||
players: list[PlayerDto]
|
||||
layers: list[LayerDto]
|
||||
|
||||
|
||||
class ProductPurchaseStartDto(BaseModel):
|
||||
player: PlayerDto
|
||||
products: list[ProductDto]
|
||||
timeout: int
|
||||
|
||||
|
||||
class ProductPurchaseTimerDto(BaseModel):
|
||||
time_left: int
|
||||
player: PlayerDto
|
||||
|
||||
|
||||
class ProductPurchaseDoneDto(BaseModel):
|
||||
player: PlayerDto
|
||||
product: Optional[ProductDto] = None
|
||||
|
||||
|
||||
@ -64,11 +68,16 @@ class WSGameDumpMessage(WSMessage):
|
||||
data: GameDumpDto
|
||||
|
||||
|
||||
class WSProductPurchaseStart(WSMessage):
|
||||
class WSProductPurchaseStartMessage(WSMessage):
|
||||
message: str = "product_purchase_start"
|
||||
data: ProductPurchaseStartDto
|
||||
|
||||
|
||||
class WSProductPurchaseDone(WSMessage):
|
||||
class WSProductPurchaseTimerTickMessage(WSMessage):
|
||||
message: str = "product_purchase_timer_tick"
|
||||
data: ProductPurchaseTimerDto
|
||||
|
||||
|
||||
class WSProductPurchaseDoneMessage(WSMessage):
|
||||
message: str = "product_purchase_done"
|
||||
data: ProductPurchaseDoneDto
|
||||
|
||||
@ -3,16 +3,15 @@ import datetime
|
||||
import logging
|
||||
import time
|
||||
from threading import Thread
|
||||
from typing import Optional
|
||||
|
||||
from hopper.interfaces import SendGameDumpInterface
|
||||
from hopper.models.player import PlayerList
|
||||
from hopper.ws_server import WSServer
|
||||
from settings import settings
|
||||
|
||||
|
||||
class InactivityWatchdog(Thread):
|
||||
def __init__(
|
||||
self, players: PlayerList, ws_server: Optional[SendGameDumpInterface] = None
|
||||
self, players: PlayerList, ws_server: WSServer = None
|
||||
) -> None:
|
||||
self.players = players
|
||||
self.ws_server = ws_server
|
||||
@ -61,8 +60,6 @@ class InactivityWatchdog(Thread):
|
||||
self.send_game_dump()
|
||||
|
||||
def send_game_dump(self):
|
||||
if not self.ws_server:
|
||||
return
|
||||
logging.info("Sending WS game dump")
|
||||
asyncio.run(self.ws_server.send_game_dump())
|
||||
|
||||
|
||||
@ -7,16 +7,20 @@ import websockets
|
||||
from websockets import WebSocketServerProtocol
|
||||
from websockets.exceptions import ConnectionClosedOK
|
||||
|
||||
from hopper.models.player import Player
|
||||
from hopper.models.product import Product
|
||||
from hopper.models.ws_dto import (
|
||||
GameDumpDto,
|
||||
ProductPurchaseDoneDto,
|
||||
ProductPurchaseStartDto,
|
||||
ProductPurchaseTimerDto,
|
||||
WSGameDumpMessage,
|
||||
WSMessage,
|
||||
WSProductPurchaseDone,
|
||||
WSProductPurchaseStart,
|
||||
WSProductPurchaseDoneMessage,
|
||||
WSProductPurchaseStartMessage,
|
||||
WSProductPurchaseTimerTickMessage,
|
||||
)
|
||||
from settings import settings
|
||||
|
||||
|
||||
class WSServer(Thread):
|
||||
@ -85,16 +89,35 @@ class WSServer(Thread):
|
||||
message = self._create_game_dump_message()
|
||||
await self.send_message_to_clients(message)
|
||||
|
||||
async def send_product_purchase_message(self, products: Iterable[Product]) -> None:
|
||||
message = WSProductPurchaseStart(
|
||||
data=ProductPurchaseStartDto(products=products)
|
||||
async def send_product_purchase_start_message(
|
||||
self, player: Player, products: Iterable[Product]
|
||||
) -> None:
|
||||
message = WSProductPurchaseStartMessage(
|
||||
data=ProductPurchaseStartDto(
|
||||
player=player,
|
||||
products=products,
|
||||
timeout=settings.purchase_timeout,
|
||||
)
|
||||
)
|
||||
await self.send_message_to_clients(message)
|
||||
|
||||
async def send_product_purchase_time_left_message(
|
||||
self, player: Player, time_left: int
|
||||
) -> None:
|
||||
message = WSProductPurchaseTimerTickMessage(
|
||||
data=ProductPurchaseTimerDto(
|
||||
player=player,
|
||||
time_left=time_left,
|
||||
)
|
||||
)
|
||||
await self.send_message_to_clients(message)
|
||||
|
||||
async def send_product_purchase_done_message(
|
||||
self, product: Optional[Product] = None
|
||||
self, player: Player, product: Optional[Product] = None
|
||||
) -> None:
|
||||
message = WSProductPurchaseDone(data=ProductPurchaseDoneDto(product=product))
|
||||
message = WSProductPurchaseDoneMessage(
|
||||
data=ProductPurchaseDoneDto(player=player, product=product),
|
||||
)
|
||||
await self.send_message_to_clients(message)
|
||||
|
||||
async def run_async(self) -> None:
|
||||
|
||||
Reference in New Issue
Block a user