cromulant/ants.py

73 lines
1.7 KiB
Python
Raw Normal View History

2024-07-18 06:51:11 +00:00
from __future__ import annotations
2024-07-18 07:20:36 +00:00
from typing import ClassVar
2024-07-18 06:51:11 +00:00
2024-07-18 07:20:36 +00:00
from utils import Utils
2024-07-18 06:51:11 +00:00
from database import Database
2024-07-18 07:20:36 +00:00
2024-07-18 06:51:11 +00:00
class Ant:
def __init__(self) -> None:
2024-07-18 07:20:36 +00:00
now = Utils.now()
2024-07-18 06:51:11 +00:00
self.id: int
self.created = now
self.updated = now
self.name = ""
self.status = ""
self.hits = 0
self.triumph = 0
2024-07-18 07:20:36 +00:00
def get_name(self) -> str:
2024-07-18 06:51:11 +00:00
return self.name or "Nameless"
2024-07-18 07:20:36 +00:00
def get_age(self) -> str:
now = Utils.now()
return Utils.time_ago(self.created, now)
def describe(self) -> None:
Utils.print(f"Name is {self.get_name()}")
Utils.print(f"It hatched {self.get_age()}")
2024-07-18 06:51:11 +00:00
class Ants:
2024-07-18 07:20:36 +00:00
ants: ClassVar[list[Ant]] = []
2024-07-18 06:51:11 +00:00
@staticmethod
def get_all() -> None:
2024-07-18 07:20:36 +00:00
Database.cursor.execute(
"SELECT id, created, updated, name, status, hits, triumph FROM ants"
)
2024-07-18 06:51:11 +00:00
rows = Database.cursor.fetchall()
for row in rows:
ant = Ant()
ant.id = row[0]
ant.created = row[1]
ant.updated = row[2]
ant.name = row[3]
ant.status = row[4]
ant.hits = row[5]
ant.triumph = row[6]
2024-07-18 07:20:36 +00:00
Ants.ants.append(ant)
2024-07-18 06:51:11 +00:00
@staticmethod
def hatch() -> None:
2024-07-18 07:20:36 +00:00
now = Utils.now()
2024-07-18 06:51:11 +00:00
Database.cursor.execute(
"INSERT INTO ants (created, updated) VALUES (?, ?)",
(now, now),
)
Database.connection.commit()
Database.cursor.execute("SELECT last_insert_rowid()")
row = Database.cursor.fetchone()
ant = Ant()
ant.id = row[0]
2024-07-18 07:20:36 +00:00
Utils.print(f"Ant hatched: {ant.id}")
@staticmethod
def terminate() -> None:
pass