From e66fb136c8fd10fe8bafb273c938a3d332d997ba Mon Sep 17 00:00:00 2001 From: Botond Hende Date: Sun, 1 Sep 2024 00:27:54 +0200 Subject: setting tasks for ships --- apirouters/agents.py | 6 +++--- apirouters/tasks.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 apirouters/tasks.py (limited to 'apirouters') diff --git a/apirouters/agents.py b/apirouters/agents.py index 55825e6..a1e015b 100644 --- a/apirouters/agents.py +++ b/apirouters/agents.py @@ -1,13 +1,13 @@ from enum import Enum -from typing import Annotated, Dict, List +from typing import Annotated, Dict import openapi_client from fastapi import APIRouter, Depends, Response, HTTPException from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm -from ..modules import tasks -from ..modules.ships import Ship +from ..modules import task_type from ..modules.database import cursor, conn +from ..modules.ships import Ship router = APIRouter() oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") diff --git a/apirouters/tasks.py b/apirouters/tasks.py new file mode 100644 index 0000000..806de91 --- /dev/null +++ b/apirouters/tasks.py @@ -0,0 +1,42 @@ +from typing import Annotated +from fastapi import APIRouter, Depends + +from ..modules.database import cursor, conn +from ..modules.ships import Ship +from ..modules import task_type +from .agents import auth_agent, Agent + +router = APIRouter() + + +@router.get("/tasks") +async def get_tasks(agent: Annotated[Agent, Depends(auth_agent)]): + ships = [] + for ship in agent.ships.values(): + ship.load_task() + ships.append({"symbol": ship.symbol, "task": ship.task}) + + return {"ships": ships} + + +@router.get("/task/{ship_symbol}") +async def get_tasks(ship_symbol: str, agent: Annotated[Agent, Depends(auth_agent)]): + for current_ship in agent.ships.values(): + if current_ship.symbol == ship_symbol: + current_ship.load_task() + return {"symbol": current_ship.symbol, "task": current_ship.task} + + return {"error": "Unknown ship symbol."} + + +@router.post("/task/{ship_symbol}/set/{task_type}") +async def get_tasks(ship_symbol: str, task: str, agent: Annotated[Agent, Depends(auth_agent)]): + if task not in task_type.task_types: + return {"error": "Invalid task."} + + for current_ship in agent.ships.values(): + if current_ship.symbol == ship_symbol: + current_ship.set_task(task) + return {"symbol": current_ship.symbol, "task": current_ship.task} + + return {"error": "Unknown ship symbol."} -- cgit v1.2.3-70-g09d2