Product purchase
This commit is contained in:
@ -6,16 +6,20 @@ from hopper.api.dto import (
|
||||
DestinationDto,
|
||||
ErrorResponseDto,
|
||||
GameInfoDto,
|
||||
GetProductsResponse,
|
||||
MovePlayerResponseDto,
|
||||
PingResponse,
|
||||
PlayerInfoResponseDto,
|
||||
ProductDto,
|
||||
PurchaseProductDto,
|
||||
StartGameRequestDto,
|
||||
StartGameResponseDto,
|
||||
)
|
||||
from hopper.engine import GameEngine
|
||||
from hopper.enums import Direction, PlayerMoveResult
|
||||
from hopper.errors import Collision, GameLockForMovement, PositionOutOfBounds
|
||||
from hopper.errors import Collision, GameLockForMovement, PositionOutOfBounds, PurchaseForbiddenForPlayer
|
||||
from hopper.models.player import Player
|
||||
from settings import settings
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@ -144,3 +148,42 @@ async def move_player(
|
||||
response.status_code = status.HTTP_200_OK
|
||||
|
||||
return MovePlayerResponseDto(player=player)
|
||||
|
||||
|
||||
@router.get("/products", response_model=GetProductsResponse)
|
||||
async def get_products() -> GetProductsResponse:
|
||||
return GetProductsResponse(
|
||||
products=settings.products,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/products/{uuid}", response_model=ProductDto)
|
||||
async def get_product(uuid: str) -> ProductDto:
|
||||
for product in settings.products:
|
||||
if product.uuid == uuid:
|
||||
return ProductDto.from_orm(product)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Product not found"
|
||||
)
|
||||
|
||||
|
||||
@router.post("/player/{uuid}/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:
|
||||
try:
|
||||
await engine.purchase_product(player=player, product=product)
|
||||
except PurchaseForbiddenForPlayer:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Purchase forbidden for this player",
|
||||
)
|
||||
break
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Product not found"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user