Demo SDK
This commit is contained in:
@ -28,7 +28,7 @@ def get_player(uuid: str, engine: GameEngine = Depends(get_game_engine)) -> Play
|
||||
)
|
||||
if not player.active:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Player kicked out due to inactivity",
|
||||
)
|
||||
return player
|
||||
@ -73,6 +73,16 @@ async def start_game(
|
||||
"/player/{uuid}",
|
||||
response_model=PlayerInfoResponseDto,
|
||||
status_code=status.HTTP_201_CREATED,
|
||||
responses={
|
||||
status.HTTP_403_FORBIDDEN: {
|
||||
"model": ErrorResponseDto,
|
||||
"description": " Player inactive",
|
||||
},
|
||||
status.HTTP_404_NOT_FOUND: {
|
||||
"model": ErrorResponseDto,
|
||||
"description": " Player with uuid not found, probably kicked out",
|
||||
},
|
||||
},
|
||||
)
|
||||
async def get_player_info(
|
||||
player: Player = Depends(get_player),
|
||||
@ -91,7 +101,11 @@ async def get_player_info(
|
||||
},
|
||||
status.HTTP_403_FORBIDDEN: {
|
||||
"model": ErrorResponseDto,
|
||||
"description": " Player uuid not valid, probably due to inactivity",
|
||||
"description": " Player inactive",
|
||||
},
|
||||
status.HTTP_404_NOT_FOUND: {
|
||||
"model": ErrorResponseDto,
|
||||
"description": " Player with uuid not found, probably kicked out",
|
||||
},
|
||||
status.HTTP_409_CONFLICT: {
|
||||
"model": ErrorResponseDto,
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import random
|
||||
from typing import Optional
|
||||
|
||||
from hopper.enums import Direction, PlayerMoveResult
|
||||
@ -32,9 +33,12 @@ class GameEngine:
|
||||
dump = self.board.dump()
|
||||
|
||||
for player in self.players:
|
||||
dump[player.position.y][player.position.x] = BOARD_DUMP_CHARS[
|
||||
ObjectType.PLAYER
|
||||
]
|
||||
if player.position.y < len(dump) and player.position.x < len(
|
||||
dump[player.position.y]
|
||||
):
|
||||
dump[player.position.y][player.position.x] = BOARD_DUMP_CHARS[
|
||||
ObjectType.PLAYER
|
||||
]
|
||||
|
||||
return dump
|
||||
|
||||
@ -57,7 +61,7 @@ class GameEngine:
|
||||
self._start_inactivity_watchdog()
|
||||
player = Player(
|
||||
name=player_name,
|
||||
position=Position(0, 0),
|
||||
position=self._create_player_start_position(),
|
||||
)
|
||||
self.players.append(player)
|
||||
|
||||
@ -67,8 +71,24 @@ class GameEngine:
|
||||
if self.ws_server:
|
||||
await self.ws_server.send_game_state()
|
||||
|
||||
await asyncio.sleep(settings.game.MOVE_DELAY)
|
||||
return player
|
||||
|
||||
def _create_player_start_position(self) -> Position:
|
||||
"""Create random position somewhere on the board border"""
|
||||
border_len = (self.board.width + self.board.height) * 2
|
||||
rnd_position = random.randint(0, border_len - 1)
|
||||
|
||||
if rnd_position < self.board.width * 2:
|
||||
x = rnd_position % self.board.width
|
||||
y = 0 if rnd_position < self.board.width else self.board.height - 1
|
||||
else:
|
||||
rnd_position -= 2 * self.board.width
|
||||
x = 0 if rnd_position < self.board.height else self.board.width - 1
|
||||
y = rnd_position % self.board.height
|
||||
|
||||
return Position(x=x, y=y)
|
||||
|
||||
def _move_position(self, position: Position, direction: Direction) -> Position:
|
||||
new_position = Position(position.x, position.y)
|
||||
if direction == Direction.LEFT:
|
||||
@ -148,7 +168,7 @@ class GameEngineFactory:
|
||||
board = GameBoard(
|
||||
width=board_width,
|
||||
height=board_height,
|
||||
destination=Destination(Position(board_height // 2, board_height // 2)),
|
||||
destination=Destination(Position(board_width // 2, board_height // 2)),
|
||||
)
|
||||
obstacle_layer = Layer(name="obstacles")
|
||||
for _ in range(obstacle_count):
|
||||
@ -184,7 +204,10 @@ class GameEngineFactory:
|
||||
player = Player(
|
||||
name="Pero",
|
||||
uuid="test-player-id",
|
||||
position=Position(2, 2),
|
||||
position=Position(
|
||||
settings.debug.TEST_PLAYER_START_X,
|
||||
settings.debug.TEST_PLAYER_START_Y,
|
||||
),
|
||||
can_be_deactivated=False,
|
||||
)
|
||||
players.append(player)
|
||||
|
||||
@ -4,7 +4,7 @@ from typing import Optional
|
||||
|
||||
@dataclass
|
||||
class GameSettings:
|
||||
MOVE_DELAY: int = 0.5 # seconds
|
||||
MOVE_DELAY: float = 0.5 # seconds
|
||||
|
||||
@dataclass
|
||||
class BoardSettings:
|
||||
@ -30,6 +30,8 @@ class WSServerSettings:
|
||||
class DebugSettings:
|
||||
PRINT_BOARD: bool = False
|
||||
CREATE_TEST_PLAYER: bool = False
|
||||
TEST_PLAYER_START_X: int = 0
|
||||
TEST_PLAYER_START_Y: int = 0
|
||||
|
||||
|
||||
@dataclass
|
||||
|
||||
Reference in New Issue
Block a user