This commit is contained in:
Auric Vente 2024-07-20 05:03:10 -06:00
parent 896b39eb4a
commit 42791b1957
2 changed files with 31 additions and 8 deletions

View File

@ -52,6 +52,9 @@ class Ant:
Utils.print(f"Name is {self.get_name()}") Utils.print(f"Name is {self.get_name()}")
Utils.print(f"It hatched {self.get_age()}") Utils.print(f"It hatched {self.get_age()}")
def get_score(self) -> int:
return self.triumph - self.hits
class Ants: class Ants:
ants: ClassVar[list[Ant]] = [] ants: ClassVar[list[Ant]] = []
@ -94,7 +97,7 @@ class Ants:
def terminate() -> None: def terminate() -> None:
from .game import Game from .game import Game
if not len(Ants.ants): if Ants.empty():
return return
ant = Ants.get_random_ant() ant = Ants.get_random_ant()
@ -130,8 +133,23 @@ class Ants:
Storage.save_ants(Ants.ants) Storage.save_ants(Ants.ants)
@staticmethod @staticmethod
def get_lazy() -> Ant: def empty() -> bool:
return min(Ants.ants, key=lambda ant: ant.updated) return len(Ants.ants) == 0
@staticmethod
def get_lazy() -> Ant | None:
if Ants.empty():
return None
now = Utils.now()
mins = 10 * 60
# Filter ants where updated is older than 10 minutes
ants = [a for a in Ants.ants if (now - a.updated) > mins]
if not len(ants):
ants = Ants.ants
return random.choice(ants)
@staticmethod @staticmethod
def set_status(ant: Ant, status: str, method: str) -> None: def set_status(ant: Ant, status: str, method: str) -> None:
@ -173,7 +191,7 @@ class Ants:
@staticmethod @staticmethod
def get_top_ant() -> tuple[Ant, int] | None: def get_top_ant() -> tuple[Ant, int] | None:
if not len(Ants.ants): if Ants.empty():
return None return None
top = None top = None
@ -181,7 +199,10 @@ class Ants:
# This could be a one-liner but I might expand the algorithm later # This could be a one-liner but I might expand the algorithm later
for ant in Ants.ants: for ant in Ants.ants:
score = max(0, ant.triumph - ant.hits) score = ant.get_score()
if score <= 0:
continue
if (not top) or (score > top_score): if (not top) or (score > top_score):
top = ant top = ant

View File

@ -158,12 +158,14 @@ class Game:
@staticmethod @staticmethod
def get_status() -> None: def get_status() -> None:
num_ants = len(Ants.ants) if Ants.empty():
if not num_ants:
return return
ant = Ants.get_lazy() ant = Ants.get_lazy()
if not ant:
return
num = random.randint(1, 10) num = random.randint(1, 10)
s = RandomSentence() s = RandomSentence()
status = "" status = ""