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 --- modules/ships.py | 27 +++++++++++++++++++++++++++ modules/tasks.py | 5 +++++ 2 files changed, 32 insertions(+) create mode 100644 modules/ships.py create mode 100644 modules/tasks.py (limited to 'modules') diff --git a/modules/ships.py b/modules/ships.py new file mode 100644 index 0000000..0a296ca --- /dev/null +++ b/modules/ships.py @@ -0,0 +1,27 @@ +from . import tasks +from .database import cursor, conn + + +class Ship: + def __init__(self, symbol: str): + self.symbol = symbol + self.callsign = "-".split(symbol)[0] + + self.task = None + + def load_task(self): + cursor.execute("SELECT task, params FROM ships WHERE symbol = ?", (self.symbol,)) + row = cursor.fetchone() + if row is None: + cursor.execute("INSERT INTO ships (symbol, task, params) VALUES (?, ?, ?)", (self.symbol, tasks.IDLE, None)) + conn.commit() + + self.task = tasks.IDLE + else: + self.task = row[0] + + def set_task(self, task): + self.task = task + if task != tasks.ERROR: + cursor.execute("UPDATE ships SET task = ? WHERE symbol = ?", (task, self.symbol)) + conn.commit() diff --git a/modules/tasks.py b/modules/tasks.py new file mode 100644 index 0000000..8479134 --- /dev/null +++ b/modules/tasks.py @@ -0,0 +1,5 @@ +IDLE = 'IDLE' +MINING = 'MINING' + +MIA = 'MIA' +ERROR = 'ERROR' -- cgit v1.2.3-70-g09d2