summaryrefslogtreecommitdiff
path: root/entities
diff options
context:
space:
mode:
authorBotond Hende <nettingman@gmail.com>2024-09-02 00:21:13 +0200
committerBotond Hende <nettingman@gmail.com>2024-09-02 00:21:13 +0200
commit1638f40eccc4a1321ee1bf19e3756157b6c965e1 (patch)
tree99209de68ebcd4f42cafcdf14e563d7916d69e00 /entities
parent3a390ff218903b8665a99f94d7f3a65357b6e96d (diff)
refactored code, use get_ship dependency for ship related calls
Diffstat (limited to 'entities')
-rw-r--r--entities/__init__.py0
-rw-r--r--entities/agent.py17
-rw-r--r--entities/ship.py41
-rw-r--r--entities/task_types.py21
4 files changed, 79 insertions, 0 deletions
diff --git a/entities/__init__.py b/entities/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/entities/__init__.py
diff --git a/entities/agent.py b/entities/agent.py
new file mode 100644
index 0000000..ba99dac
--- /dev/null
+++ b/entities/agent.py
@@ -0,0 +1,17 @@
+import openapi_client
+from typing import Dict
+
+from .ship import Ship
+
+
+class Agent:
+ def __init__(self, agent_symbol, token):
+ config = openapi_client.Configuration(access_token=token)
+ self.api_client = openapi_client.ApiClient(config)
+
+ self.agents_api = openapi_client.AgentsApi(self.api_client)
+ self.fleet_api = openapi_client.FleetApi(self.api_client)
+ self.systems_api = openapi_client.SystemsApi(self.api_client)
+
+ self.agent_symbol = agent_symbol
+ self.ships: Dict[str, Ship] = {}
diff --git a/entities/ship.py b/entities/ship.py
new file mode 100644
index 0000000..922f961
--- /dev/null
+++ b/entities/ship.py
@@ -0,0 +1,41 @@
+from typing import Dict
+
+from . import task_types
+from ..modules.database import cursor, conn
+
+
+class Ship:
+ def __init__(self, symbol: str):
+ self.symbol = symbol
+ self.callsign = "-".split(symbol)[0]
+
+ self.task = None
+ self.name = None
+
+ def get_data(self) -> Dict:
+ return {"name": self.name, "symbol": self.symbol, "task": self.task}
+
+ def load_task(self):
+ cursor.execute("SELECT name, task, params FROM ships WHERE symbol = ?", (self.symbol,))
+ row = cursor.fetchone()
+ if row is None:
+ cursor.execute("INSERT INTO ships (symbol, task, params, name) VALUES (?, ?, ?, ?)",
+ (self.symbol, task_types.IDLE, None, self.symbol))
+ conn.commit()
+
+ self.task = task_types.IDLE
+ self.task = self.symbol
+ else:
+ self.name = row[0]
+ self.task = row[1]
+
+ def set_task(self, task):
+ self.task = task
+ if task != task_types.ERROR:
+ cursor.execute("UPDATE ships SET task = ? WHERE symbol = ?", (task, self.symbol))
+ conn.commit()
+
+ def rename(self, name):
+ self.name = name
+ cursor.execute("UPDATE ships SET name = ? WHERE symbol = ?", (name, self.symbol))
+ conn.commit()
diff --git a/entities/task_types.py b/entities/task_types.py
new file mode 100644
index 0000000..d23ce55
--- /dev/null
+++ b/entities/task_types.py
@@ -0,0 +1,21 @@
+from typing import Annotated
+from pydantic.functional_validators import AfterValidator
+
+IDLE = 'IDLE'
+MINING = 'MINING'
+
+MIA = 'MIA'
+ERROR = 'ERROR'
+
+task_types = [
+ IDLE,
+ MINING,
+]
+
+
+def is_task_type(task: str):
+ assert task in task_types, f"'{task}' is not a valid task type."
+ return task
+
+
+task_type = Annotated[str, AfterValidator(is_task_type)]