Product methods for python SDK
This commit is contained in:
@ -1,3 +1,4 @@
|
|||||||
|
from typing import Optional
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
@ -27,6 +28,13 @@ class Direction(str, Enum):
|
|||||||
DOWN = "down"
|
DOWN = "down"
|
||||||
|
|
||||||
|
|
||||||
|
class PlayerState(str, Enum):
|
||||||
|
CREATED = "CREATED"
|
||||||
|
MOVING = "MOVING"
|
||||||
|
ON_DESTINATION = "ON_DESTINATION"
|
||||||
|
INACTIVE = "INACTIVE"
|
||||||
|
|
||||||
|
|
||||||
class Board(BaseModel):
|
class Board(BaseModel):
|
||||||
width: int
|
width: int
|
||||||
height: int
|
height: int
|
||||||
@ -48,6 +56,13 @@ class Player(BaseModel):
|
|||||||
position: Position
|
position: Position
|
||||||
move_count: int
|
move_count: int
|
||||||
move_attempt_count: int
|
move_attempt_count: int
|
||||||
|
state: PlayerState
|
||||||
|
|
||||||
|
|
||||||
|
class Product(BaseModel):
|
||||||
|
name: str
|
||||||
|
id: str
|
||||||
|
description: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
class PingResponse(BaseModel):
|
class PingResponse(BaseModel):
|
||||||
@ -69,6 +84,10 @@ class GameInfoResponse(BaseModel):
|
|||||||
destination: Destination
|
destination: Destination
|
||||||
|
|
||||||
|
|
||||||
|
class ProductListResponse(BaseModel):
|
||||||
|
products: list[Product]
|
||||||
|
|
||||||
|
|
||||||
class FairHopper:
|
class FairHopper:
|
||||||
def __init__(self, host: str) -> None:
|
def __init__(self, host: str) -> None:
|
||||||
self.host = host
|
self.host = host
|
||||||
@ -106,20 +125,20 @@ class FairHopper:
|
|||||||
|
|
||||||
return PlayerInfoResponse(**r.json())
|
return PlayerInfoResponse(**r.json())
|
||||||
|
|
||||||
def move_left(self, id: str) -> PlayerInfoResponse:
|
def move_left(self, player_id: str) -> PlayerInfoResponse:
|
||||||
return self.move(id, Direction.LEFT)
|
return self.move(player_id, Direction.LEFT)
|
||||||
|
|
||||||
def move_right(self, id: str) -> PlayerInfoResponse:
|
def move_right(self, player_id: str) -> PlayerInfoResponse:
|
||||||
return self.move(id, Direction.RIGHT)
|
return self.move(player_id, Direction.RIGHT)
|
||||||
|
|
||||||
def move_up(self, id: str) -> PlayerInfoResponse:
|
def move_up(self, player_id: str) -> PlayerInfoResponse:
|
||||||
return self.move(id, Direction.UP)
|
return self.move(player_id, Direction.UP)
|
||||||
|
|
||||||
def move_down(self, id: str) -> PlayerInfoResponse:
|
def move_down(self, player_id: str) -> PlayerInfoResponse:
|
||||||
return self.move(id, Direction.DOWN)
|
return self.move(player_id, Direction.DOWN)
|
||||||
|
|
||||||
def move(self, id: str, direction: Direction) -> PlayerInfoResponse:
|
def move(self, player_id: str, direction: Direction) -> PlayerInfoResponse:
|
||||||
path = f"/player/{id}/move/{direction}"
|
path = f"/player/{player_id}/move/{direction}"
|
||||||
r = requests.post(self.format_url(path))
|
r = requests.post(self.format_url(path))
|
||||||
|
|
||||||
if r.status_code == 403:
|
if r.status_code == 403:
|
||||||
@ -132,3 +151,17 @@ class FairHopper:
|
|||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
|
|
||||||
return PlayerInfoResponse(**r.json())
|
return PlayerInfoResponse(**r.json())
|
||||||
|
|
||||||
|
def get_products(self) -> list[Product]:
|
||||||
|
r = requests.get(self.format_url("/products"))
|
||||||
|
response_data = ProductListResponse(**r.json())
|
||||||
|
return response_data.products
|
||||||
|
|
||||||
|
def purchase_product(self, player_id: str, product_id: str) -> Product:
|
||||||
|
url = self.format_url(f"/player/{player_id}/product/purchase")
|
||||||
|
payload = {
|
||||||
|
"product_id": product_id,
|
||||||
|
}
|
||||||
|
r = requests.post(url, json=payload)
|
||||||
|
r.raise_for_status()
|
||||||
|
return Product(**r.json())
|
||||||
|
|||||||
Reference in New Issue
Block a user