135 lines
2.9 KiB
Python
135 lines
2.9 KiB
Python
from pydantic import BaseModel
|
|
from enum import Enum
|
|
|
|
import requests
|
|
|
|
|
|
class BaseError(Exception):
|
|
...
|
|
|
|
|
|
class PositionError(BaseError):
|
|
...
|
|
|
|
|
|
class PlayerInactiveError(BaseError):
|
|
...
|
|
|
|
|
|
class PlayerNotFoundError(BaseError):
|
|
...
|
|
|
|
|
|
class Direction(str, Enum):
|
|
LEFT = "left"
|
|
RIGHT = "right"
|
|
UP = "up"
|
|
DOWN = "down"
|
|
|
|
|
|
class Board(BaseModel):
|
|
width: int
|
|
height: int
|
|
|
|
|
|
class Position(BaseModel):
|
|
x: int
|
|
y: int
|
|
|
|
|
|
class Destination(BaseModel):
|
|
position: Position
|
|
|
|
|
|
class Player(BaseModel):
|
|
id: str
|
|
name: str
|
|
active: bool
|
|
position: Position
|
|
move_count: int
|
|
move_attempt_count: int
|
|
|
|
|
|
class PingResponse(BaseModel):
|
|
message: str
|
|
|
|
|
|
class StartGameResponse(BaseModel):
|
|
board: Board
|
|
destination: Destination
|
|
player: Player
|
|
|
|
|
|
class PlayerInfoResponse(BaseModel):
|
|
player: Player
|
|
|
|
|
|
class GameInfoResponse(BaseModel):
|
|
board: Board
|
|
destination: Destination
|
|
|
|
|
|
class FairHopper:
|
|
def __init__(self, host: str) -> None:
|
|
self.host = host
|
|
|
|
def format_url(self, path: str) -> str:
|
|
return f"{self.host}{path}"
|
|
|
|
def ping(self) -> PingResponse:
|
|
r = requests.get(self.format_url("/ping"))
|
|
r.raise_for_status()
|
|
return PingResponse(**r.json())
|
|
|
|
def start_game(self, player_name: str) -> StartGameResponse:
|
|
payload = {
|
|
"player_name": player_name,
|
|
}
|
|
r = requests.post(self.format_url("/game"), json=payload)
|
|
r.raise_for_status()
|
|
return StartGameResponse(**r.json())
|
|
|
|
def get_game_info(self) -> GameInfoResponse:
|
|
r = requests.get(self.format_url(f"/game"))
|
|
r.raise_for_status()
|
|
return GameInfoResponse(**r.json())
|
|
|
|
def get_player_info(self, id: str) -> PlayerInfoResponse:
|
|
r = requests.get(self.format_url(f"/player/{id}"))
|
|
|
|
if r.status_code == 403:
|
|
raise PlayerInactiveError()
|
|
elif r.status_code == 404:
|
|
raise PlayerNotFoundError()
|
|
else:
|
|
r.raise_for_status()
|
|
|
|
return PlayerInfoResponse(**r.json())
|
|
|
|
def move_left(self, id: str) -> PlayerInfoResponse:
|
|
return self.move(id, Direction.LEFT)
|
|
|
|
def move_right(self, id: str) -> PlayerInfoResponse:
|
|
return self.move(id, Direction.RIGHT)
|
|
|
|
def move_up(self, id: str) -> PlayerInfoResponse:
|
|
return self.move(id, Direction.UP)
|
|
|
|
def move_down(self, id: str) -> PlayerInfoResponse:
|
|
return self.move(id, Direction.DOWN)
|
|
|
|
def move(self, id: str, direction: Direction) -> PlayerInfoResponse:
|
|
path = f"/player/{id}/move/{direction}"
|
|
r = requests.post(self.format_url(path))
|
|
|
|
if r.status_code == 403:
|
|
raise PlayerInactiveError()
|
|
elif r.status_code == 404:
|
|
raise PlayerNotFoundError()
|
|
elif r.status_code == 409:
|
|
raise PositionError()
|
|
else:
|
|
r.raise_for_status()
|
|
|
|
return PlayerInfoResponse(**r.json())
|