Skip to content

Commit

Permalink
LNX-201 Add a system for adding agents
Browse files Browse the repository at this point in the history
  • Loading branch information
Krystian Jandy committed Oct 15, 2023
1 parent 3d51584 commit 2334a51
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
41 changes: 30 additions & 11 deletions src/routers/scene_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import Dict, Union, List

import opensimplex
from fastapi import APIRouter
from fastapi import APIRouter, HTTPException
from lynx.common.actions.create_object import CreateObject
from lynx.common.actions.remove_object import RemoveObject
from lynx.common.object import Object
Expand All @@ -26,7 +26,6 @@
terminate_process_for_object,
log_and_return_message,
set_agent_dead,
add_agent_to_player
)
from src.utils.tick_handler import create_tick

Expand Down Expand Up @@ -57,11 +56,25 @@ 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)
player = state.scene.get_player(object.owner)
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")

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()
raise HTTPException(
status_code=400,
detail={
"message": "Cannot buy agent, not enough resources",
"cost": cost
}
)

logger.debug(f"Object has been successfully deserialized, starting to add object to scene")
state.scene.add_to_pending_actions(CreateObject(r.serialized_object).serialize())
logger.debug(f"Object has been successfully added to pending actions. Starting to add agent to player")
await add_agent_to_player(state.scene, object)
logger.debug(f"Agent has been successfully added to player")
logger.debug(f"Object has been successfully added to pending actions.")
if object.tick != "":
logger.debug(f"Object has tick {object.tick}")
background_tasks.add_task(spawn_process_for_new_agent, object)
Expand Down Expand Up @@ -146,20 +159,26 @@ async def get_ranking():
@router.get("/player-agents/{player_id}")
async def get_agents_from_player_list(player_id: str):
logger.debug(f"Starting to get agents 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 []
return {}

logger.debug(f"Agents for player {player_id} have been successfully fetched")
return player.get_agents()

slot_cost = player.calculate_slot_cost()
agent_max = player.max_agents
number_of_agents = player.alive_agents_count()
return {
"agents": player.get_agents(),
"slot_cost": slot_cost,
"agent_max": agent_max,
"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}")

player = state.scene.get_player(player_id)
if player is None:
return log_and_return_message(f"Player with id: {player_id} does not exist")
Expand Down Expand Up @@ -197,7 +216,7 @@ async def get_agent_from_list(player_id: str, agent_id: int):
@router.get("/player-resources/{player_id}")
async def get_player_resources(player_id: str):
logger.debug(f"Starting to get resources 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")
Expand Down
18 changes: 0 additions & 18 deletions src/utils/helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,24 +136,6 @@ async def set_agent_dead(player_id: str, object_id: int):
player.agents[object_id].time_death = datetime.now().isoformat()
logger.debug(f"Agent {object_id} has been successfully set dead")


async def add_agent_to_player(scene: Scene, object: Object):
player = scene.get_player(object.owner)
if player is None:
logger.warning(f"Player with id: {object.owner} does not exist")
return

agent = Agent(
id=object.id,
type=object.get_type(),
time_creation=datetime.now().isoformat(),
time_death=None,
is_alive=True,
tick=object.tick,
)
player.add_agent(agent)


async def spawn_process_for_new_agent(object: Object):
logger.debug(f"Starting to spawn process for new agent {object.id}")
await wait_for_next_tick()
Expand Down

0 comments on commit 2334a51

Please sign in to comment.