From 6a64709d17018423e38e21b775e4911d84199a89 Mon Sep 17 00:00:00 2001 From: Eden Kirin Date: Fri, 21 Apr 2023 23:39:20 +0200 Subject: [PATCH] Product methods for python SDK --- python/fh_sdk.py | 53 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/python/fh_sdk.py b/python/fh_sdk.py index 51e8eab..6ab7c01 100644 --- a/python/fh_sdk.py +++ b/python/fh_sdk.py @@ -1,3 +1,4 @@ +from typing import Optional from pydantic import BaseModel from enum import Enum @@ -27,6 +28,13 @@ class Direction(str, Enum): DOWN = "down" +class PlayerState(str, Enum): + CREATED = "CREATED" + MOVING = "MOVING" + ON_DESTINATION = "ON_DESTINATION" + INACTIVE = "INACTIVE" + + class Board(BaseModel): width: int height: int @@ -48,6 +56,13 @@ class Player(BaseModel): position: Position move_count: int move_attempt_count: int + state: PlayerState + + +class Product(BaseModel): + name: str + id: str + description: Optional[str] = None class PingResponse(BaseModel): @@ -69,6 +84,10 @@ class GameInfoResponse(BaseModel): destination: Destination +class ProductListResponse(BaseModel): + products: list[Product] + + class FairHopper: def __init__(self, host: str) -> None: self.host = host @@ -106,20 +125,20 @@ class FairHopper: return PlayerInfoResponse(**r.json()) - def move_left(self, id: str) -> PlayerInfoResponse: - return self.move(id, Direction.LEFT) + def move_left(self, player_id: str) -> PlayerInfoResponse: + return self.move(player_id, Direction.LEFT) - def move_right(self, id: str) -> PlayerInfoResponse: - return self.move(id, Direction.RIGHT) + def move_right(self, player_id: str) -> PlayerInfoResponse: + return self.move(player_id, Direction.RIGHT) - def move_up(self, id: str) -> PlayerInfoResponse: - return self.move(id, Direction.UP) + def move_up(self, player_id: str) -> PlayerInfoResponse: + return self.move(player_id, Direction.UP) - def move_down(self, id: str) -> PlayerInfoResponse: - return self.move(id, Direction.DOWN) + def move_down(self, player_id: str) -> PlayerInfoResponse: + return self.move(player_id, Direction.DOWN) - def move(self, id: str, direction: Direction) -> PlayerInfoResponse: - path = f"/player/{id}/move/{direction}" + def move(self, player_id: str, direction: Direction) -> PlayerInfoResponse: + path = f"/player/{player_id}/move/{direction}" r = requests.post(self.format_url(path)) if r.status_code == 403: @@ -132,3 +151,17 @@ class FairHopper: r.raise_for_status() 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())