From 1638f40eccc4a1321ee1bf19e3756157b6c965e1 Mon Sep 17 00:00:00 2001 From: Botond Hende Date: Mon, 2 Sep 2024 00:21:13 +0200 Subject: refactored code, use get_ship dependency for ship related calls --- entities/ship.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 entities/ship.py (limited to 'entities/ship.py') 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() -- cgit v1.2.3-70-g09d2