summaryrefslogtreecommitdiff
path: root/entities/ship.py
blob: 922f961a73043ebb326badf9eea6351e449e68cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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()