From d45aca6c30175512b8bffd238c444b0de595b8f1 Mon Sep 17 00:00:00 2001 From: Eden Kirin Date: Fri, 31 Mar 2023 17:16:00 +0200 Subject: [PATCH] uuid -> id --- hopper/api/dto.py | 6 ++--- hopper/api/views.py | 22 ++++++++--------- hopper/models/player.py | 6 ++--- hopper/models/product.py | 2 +- sdk/demo.py | 51 ++++++++++++++++++++++++++++++++++++++++ settings_template.py | 37 ++++++++++++++++++++++++++--- 6 files changed, 103 insertions(+), 21 deletions(-) create mode 100644 sdk/demo.py diff --git a/hopper/api/dto.py b/hopper/api/dto.py index 9cc9c08..5654929 100644 --- a/hopper/api/dto.py +++ b/hopper/api/dto.py @@ -27,7 +27,7 @@ class PositionDto(BaseModel): class PlayerDto(BaseModel): - uuid: str + id: str name: str active: bool position: PositionDto @@ -42,7 +42,7 @@ class DestinationDto(BaseModel): class ProductDto(BaseModel): name: str - uuid: str + id: str description: Optional[str] = None class StartGameRequestDto(BaseModel): @@ -75,4 +75,4 @@ class GetProductsResponse(BaseModel): class PurchaseProductDto(BaseModel): - product_uuid: str + product_id: str diff --git a/hopper/api/views.py b/hopper/api/views.py index 9aec4f7..f1aba16 100644 --- a/hopper/api/views.py +++ b/hopper/api/views.py @@ -24,8 +24,8 @@ from settings import settings router = APIRouter() -def get_player(uuid: str, engine: GameEngine = Depends(get_game_engine)) -> Player: - player = engine.players.find(uuid) +def get_player(id: str, engine: GameEngine = Depends(get_game_engine)) -> Player: + player = engine.players.find(id) if player is None: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Player not found" @@ -76,7 +76,7 @@ async def start_game( @router.get( - "/player/{uuid}", + "/player/{id}", response_model=PlayerInfoResponseDto, responses={ status.HTTP_403_FORBIDDEN: { @@ -85,7 +85,7 @@ async def start_game( }, status.HTTP_404_NOT_FOUND: { "model": ErrorResponseDto, - "description": " Player with uuid not found, probably kicked out", + "description": " Player with id not found, probably kicked out", }, }, ) @@ -96,7 +96,7 @@ async def get_player_info( @router.post( - "/player/{uuid}/move/{direction}", + "/player/{id}/move/{direction}", response_model=MovePlayerResponseDto, status_code=status.HTTP_201_CREATED, responses={ @@ -110,7 +110,7 @@ async def get_player_info( }, status.HTTP_404_NOT_FOUND: { "model": ErrorResponseDto, - "description": " Player with uuid not found, probably kicked out", + "description": " Player with id not found, probably kicked out", }, status.HTTP_409_CONFLICT: { "model": ErrorResponseDto, @@ -157,24 +157,24 @@ async def get_products() -> GetProductsResponse: ) -@router.get("/products/{uuid}", response_model=ProductDto) -async def get_product(uuid: str) -> ProductDto: +@router.get("/products/{id}", response_model=ProductDto) +async def get_product(id: str) -> ProductDto: for product in settings.products: - if product.uuid == uuid: + if product.id == id: return ProductDto.from_orm(product) raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Product not found" ) -@router.post("/player/{uuid}/product/purchase") +@router.post("/player/{id}/product/purchase") async def purchase_product( body: PurchaseProductDto, engine: GameEngine = Depends(get_game_engine), player: Player = Depends(get_player), ): for product in settings.products: - if product.uuid == body.product_uuid: + if product.id == body.product_id: try: await engine.purchase_product(player=player, product=product) except PurchaseForbiddenForPlayer: diff --git a/hopper/models/player.py b/hopper/models/player.py index 5d5010d..3c511af 100644 --- a/hopper/models/player.py +++ b/hopper/models/player.py @@ -15,7 +15,7 @@ class Position: @dataclass class Player: name: str - uuid: str = field(default_factory=lambda: str(uuid.uuid4())) + id: str = field(default_factory=lambda: str(uuid.uuid4())) position: Position = field(default_factory=lambda: Position(0, 0)) move_count: int = 0 move_attempt_count: int = 0 @@ -31,8 +31,8 @@ class Player: class PlayerList(list[Player]): - def find(self, uuid: str) -> Optional[Player]: + def find(self, id: str) -> Optional[Player]: for player in self: - if player.uuid == uuid: + if player.id == id: return player return None diff --git a/hopper/models/product.py b/hopper/models/product.py index 3939a4d..3581efa 100644 --- a/hopper/models/product.py +++ b/hopper/models/product.py @@ -6,5 +6,5 @@ from typing import Optional @dataclass class Product: name: str - uuid: str = field(default_factory=lambda: str(uuid.uuid4())) + id: str = field(default_factory=lambda: str(uuid.uuid4())) description: Optional[str] = None diff --git a/sdk/demo.py b/sdk/demo.py new file mode 100644 index 0000000..6e9c839 --- /dev/null +++ b/sdk/demo.py @@ -0,0 +1,51 @@ +import random +from fh_sdk import Direction, FairHopper, Position +import math + +HOST = "http://localhost" +PORT = 8010 + + +def calc_angle(position1: Position, position2: Position) -> float: + x1, y1 = position1.x, position1.y + x2, y2 = position2.x, position2.y + return math.atan2(y2 - y1, x2 - x1) * (180 / math.pi) + + +fh = FairHopper(host=HOST, port=PORT) +res = fh.ping() + +game = fh.start_game(player_name=f"Mirko {random.randint(0, 9999)}") +print(game.player.position) +quit() + +res = fh.get_game_info() +print(">>>>>", res) + +res = fh.get_player_info("XX") +print(">>>>>", res) + +position = game.player.position +dest_position = game.destination.position + +# p1 = PositionDto(x=0, y=20) +# p2 = PositionDto(x=10, y=10) +# angle = calc_angle(p1, p2) +# print(angle) +# quit() + +for _ in range(10): + angle = calc_angle(position, dest_position) + 180 + if 0 <= angle < 90: + direction = Direction.RIGHT + elif 90 <= angle <= 180: + direction = Direction.DOWN + elif 180 <= angle <= 270: + direction = Direction.RIGHT + else: + direction = Direction.UP + + print(position, dest_position, int(angle), direction) + + move_response = fh.move(game.player.id, direction) + position = move_response.player.position diff --git a/settings_template.py b/settings_template.py index e1ad9c9..03b0f2a 100644 --- a/settings_template.py +++ b/settings_template.py @@ -2,18 +2,49 @@ import logging from hopper.models.config import ( BoardSettings, + DebugSettings, GameSettings, InactivityWatchdogSettings, Settings, WSServerSettings, ) +from hopper.models.player import Player, Position +from hopper.models.product import Product settings = Settings( game=GameSettings(), - board=BoardSettings(), + board=BoardSettings( + WIDTH=20, + HEIGHT=20, + OBSTACLE_COUNT=10, + ), inacivity_watchdog=InactivityWatchdogSettings(), + purchase_timeout=5, log_level=logging.INFO, + products=[ + Product(name="CocaCola", id="cocacola-id"), + Product(name="Pepsi", id="pepsi-id"), + Product(name="Fanta", id="fanta-id"), + Product(name="Snickers", id="snickers-id"), + Product(name="Mars", id="mars-id"), + Product(name="Burek", id="burek-id"), + ], ws_server=WSServerSettings(), - purchase_timeout=10, - debug=None, + debug=DebugSettings( + PRINT_BOARD=True, + PLAYERS=[ + Player( + name="Pero", + id="test-player-pero", + position=Position(x=9, y=10), + can_be_deactivated=False, + ), + Player( + name="Mirko", + id="test-player-mirko", + position=Position(x=10, y=5), + can_be_deactivated=False, + ), + ], + ), )