uuid -> id

This commit is contained in:
Eden Kirin
2023-03-31 17:16:00 +02:00
parent 28a981980f
commit d45aca6c30
6 changed files with 103 additions and 21 deletions

View File

@ -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

View File

@ -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:

View File

@ -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

View File

@ -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