Skip to content

Commit

Permalink
feat: add endpoitn for buy slot
Browse files Browse the repository at this point in the history
  • Loading branch information
Krystian Jandy committed Oct 16, 2023
1 parent 2334a51 commit 15daf8f
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/routers/scene_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from fastapi import APIRouter, HTTPException
from lynx.common.actions.create_object import CreateObject
from lynx.common.actions.remove_object import RemoveObject
from lynx.common.actions.update_resources import UpdateResources
from lynx.common.object import Object
from lynx.common.scene import Scene
from lynx.common.vector import Vector
Expand Down Expand Up @@ -56,18 +57,24 @@ async def get(tick_number: int, player: str = "") -> Dict[str, Union[int, str, L
async def add(r: AddObjectDTO, background_tasks: BackgroundTasks):
logger.debug(f"Starting to add object to scene, starting to deserialize object")
object = Object.deserialize(r.serialized_object)
inventory = {
"Wood": 10,
"Stone": 10,
}
state.scene.add_to_pending_actions(UpdateResources(object.owner, inventory).serialize())
player = state.scene.get_player(object.owner)
player.player_resources = inventory
if player is None:
logger.warning(f"Player with id: {object.owner} does not exist")
raise HTTPException(status_code=400, detail=f"Player with id: {object.owner} does not exist")
raise HTTPException(status_code=400, detail={"message": f"Player with id: {object.owner} does not exist"})

if player.add_agent_from_object(object) is False:
logger.warning(f"Player with id: {object.owner} cannot buy agent")
cost = player.calculate_agent_cost()
cost = player.calculate_slot_cost()
raise HTTPException(
status_code=400,
detail={
"message": "Cannot buy agent, not enough resources",
"message": "Cannot create agent, you should buy more slots",
"cost": cost
}
)
Expand All @@ -82,6 +89,24 @@ async def add(r: AddObjectDTO, background_tasks: BackgroundTasks):
return {"serialized_object": object.serialize()}


@router.post("/buy-slot/{player_id}")
async def buy_slot(player_id: str):
logger.debug(f"Starting to buy slot for player {player_id}")
player = state.scene.get_player(player_id)
if player is None:
logger.warning(f"Player with id: {player_id} does not exist")
return {"message": f"Player with id: {player_id} does not exist"}

if player.can_purchase_slot() is False:
logger.warning(f"Player with id: {player_id} cannot buy slot")
return {"message": "Cannot buy slot, you should collect more resources"}

logger.debug(f"Player has enough resources, starting to buy slot")
player.purchase_slot(state.scene)
logger.debug(f"Slot has been successfully bought")
return {"message": "Slot has been successfully bought"}


@router.delete("/delete_object/{player_id}/{object_id}")
async def delete_object(player_id: str, object_id: int):
logger.debug(f"Starting to delete object {object_id} from scene")
Expand Down Expand Up @@ -175,6 +200,7 @@ async def get_agents_from_player_list(player_id: str):
"number_of_agents": number_of_agents,
}


@router.delete("/player-agents/{player_id}/{agent_id}")
async def delete_agent_from_list(player_id: str, agent_id: int):
logger.debug(f"Starting to delete agent {agent_id} for player {player_id}")
Expand Down

0 comments on commit 15daf8f

Please sign in to comment.