Compare commits
3 Commits
28a981980f
...
c30529c087
| Author | SHA1 | Date | |
|---|---|---|---|
| c30529c087 | |||
| 80c7c80451 | |||
| d45aca6c30 |
22
README.md
22
README.md
@ -220,7 +220,7 @@ Response body:
|
||||
}
|
||||
},
|
||||
"player": {
|
||||
"uuid": "75bba7cd-a4c1-4b50-b0b5-6382c2822a25",
|
||||
"id": "75bba7cd-a4c1-4b50-b0b5-6382c2822a25",
|
||||
"name": "Pero",
|
||||
"position": {
|
||||
"x": 0,
|
||||
@ -234,17 +234,17 @@ Response body:
|
||||
|
||||
### Player Move
|
||||
|
||||
- POST `/player/{uuid}/move/left`
|
||||
- POST `/player/{uuid}/move/right`
|
||||
- POST `/player/{uuid}/move/up`
|
||||
- POST `/player/{uuid}/move/down`
|
||||
- POST `/player/{id}/move/left`
|
||||
- POST `/player/{id}/move/right`
|
||||
- POST `/player/{id}/move/up`
|
||||
- POST `/player/{id}/move/down`
|
||||
|
||||
Request body: None
|
||||
|
||||
Response code:
|
||||
- 200 OK: Destination reached
|
||||
- 201 Created: Player moved successfully
|
||||
- 403 Forbidden: Player uuid not valid, probably timeout
|
||||
- 403 Forbidden: Player id not valid, probably timeout
|
||||
- 409 Conflict: Invalid move, obstacle or position out of board
|
||||
- 422 Unprocessable Content: Validation error
|
||||
|
||||
@ -252,7 +252,7 @@ Response body:
|
||||
```json
|
||||
{
|
||||
"player": {
|
||||
"uuid": "string",
|
||||
"id": "string",
|
||||
"name": "Pero",
|
||||
"position": {
|
||||
"x": 50,
|
||||
@ -266,7 +266,7 @@ Response body:
|
||||
|
||||
### Get Player Info
|
||||
|
||||
GET `/player/{{uuid}}`
|
||||
GET `/player/{{id}}`
|
||||
|
||||
Request body: None
|
||||
|
||||
@ -274,7 +274,7 @@ Response body:
|
||||
```json
|
||||
{
|
||||
"player": {
|
||||
"uuid": "string",
|
||||
"id": "string",
|
||||
"name": "Pero",
|
||||
"position": {
|
||||
"x": 50,
|
||||
@ -334,7 +334,7 @@ Data:
|
||||
},
|
||||
"players": [
|
||||
{
|
||||
"uuid": "test-player-id",
|
||||
"id": "test-player-id",
|
||||
"name": "Pero",
|
||||
"active": true,
|
||||
"position": {
|
||||
@ -345,7 +345,7 @@ Data:
|
||||
"move_attempt_count": 3
|
||||
},
|
||||
{
|
||||
"uuid": "95962b49-0003-4bf2-b205-71f2590f2318",
|
||||
"id": "95962b49-0003-4bf2-b205-71f2590f2318",
|
||||
"name": "Mirko",
|
||||
"active": true,
|
||||
"position": {
|
||||
|
||||
@ -39,7 +39,7 @@ POST http://localhost:8010/player/test-player-pero/product/purchase
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"product_uuid": "cocacola-id"
|
||||
"product_id": "cocacola-id"
|
||||
}
|
||||
###
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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:
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
51
sdk/demo.py
Normal file
51
sdk/demo.py
Normal file
@ -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
|
||||
@ -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,
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user