From 254a5eda7d17ff28d4604d95dba6f102120189b8 Mon Sep 17 00:00:00 2001 From: Botond Hende Date: Sat, 31 Aug 2024 01:06:58 +0200 Subject: ship data initialization on startup --- apirouters/agents.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'apirouters/agents.py') diff --git a/apirouters/agents.py b/apirouters/agents.py index 89f736f..4ac2167 100644 --- a/apirouters/agents.py +++ b/apirouters/agents.py @@ -1,15 +1,17 @@ from enum import Enum -from typing import Annotated +from typing import Annotated, Dict, List import openapi_client from fastapi import APIRouter, Depends, Response from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm +from ..modules import tasks +from ..modules.ships import Ship from ..modules.database import cursor, conn router = APIRouter() oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") -agents = {} +agents: Dict[str, 'Agent'] = {} class Agent: @@ -22,6 +24,7 @@ class Agent: self.systems_api = openapi_client.SystemsApi(self.api_client) self.agent_symbol = agent_symbol + self.ships: Dict[str, Ship] = {} @router.post("/token") @@ -65,6 +68,32 @@ def load_agents_from_database() -> None: cursor.execute("DELETE from agents WHERE callsign = ?", (invalid,)) conn.commit() + for agent in agents.values(): + try: + response = agent.fleet_api.get_my_ships() + for ship_data in response.data: + agent.ships[ship_data.symbol] = Ship(ship_data.symbol) + + for ship in agent.ships.values(): + ship.load_task() + except: + for ship in agent.ships.values(): + ship.set_task(tasks.ERROR) + + cursor.execute("SELECT symbol FROM ships WHERE symbol LIKE ?", (f"{agent.agent_symbol}-%",)) + ship_names = agent.ships.keys() + for row in cursor.fetchall(): + if row[0] in ship_names: + continue + + missing_ship = Ship(row[0]) + missing_ship.set_task(tasks.MIA) + agent.ships[row[0]] = missing_ship + + + + + @router.post("/{callsign}/init", status_code=201) async def init_agent(callsign: str, token: Annotated[str, Depends(oauth2_scheme)], response: Response): -- cgit v1.2.3-70-g09d2