diff --git a/cromulant/ants.py b/cromulant/ants.py index cc779d6..937fcc8 100644 --- a/cromulant/ants.py +++ b/cromulant/ants.py @@ -99,7 +99,11 @@ class Ants: if Ants.empty(): return - ant = Ants.get_random_ant() + ant = Ants.random_ant() + + if not ant: + return + Ants.ants.remove(ant) Ants.save() @@ -120,7 +124,10 @@ class Ants: Window.confirm("Terminate all ants?", action) @staticmethod - def get_random_ant() -> Ant: + def random_ant() -> Ant | None: + if Ants.empty(): + return None + return random.choice(Ants.ants) @staticmethod @@ -136,10 +143,19 @@ class Ants: return len(Ants.ants) == 0 @staticmethod - def get_lazy() -> Ant | None: + def get_next() -> Ant | None: if Ants.empty(): return None + if len(Ants.ants) == 1: + return Ants.ants[0] + + ignore = [] + current = Ants.get_current() + + if current: + ignore.append(current) + now = Utils.now() mins = 10 * 60 # Filter ants where updated is older than 10 minutes @@ -148,8 +164,16 @@ class Ants: if not len(ants): ants = Ants.ants + ants = [a for a in ants if a not in ignore] return random.choice(ants) + @staticmethod + def get_current() -> Ant | None: + if Ants.empty(): + return None + + return max(Ants.ants, key=lambda ant: ant.updated) + @staticmethod def set_status(ant: Ant, status: str, method: str) -> None: from .game import Game diff --git a/cromulant/game.py b/cromulant/game.py index 94f494a..7583155 100644 --- a/cromulant/game.py +++ b/cromulant/game.py @@ -161,7 +161,7 @@ class Game: if Ants.empty(): return - ant = Ants.get_lazy() + ant = Ants.get_next() if not ant: return