This commit is contained in:
Auric Vente 2024-07-21 07:26:40 -06:00
parent aeb81739ba
commit ac9c7d5dba
2 changed files with 89 additions and 76 deletions

View File

@ -17,7 +17,7 @@ class Ant:
self.updated = now
self.name = ""
self.status = ""
self.method = ""
self.method = "hatched"
self.triumph = 0
self.hits = 0
@ -62,6 +62,25 @@ class Ant:
tooltip += f"\nTriumph: {self.triumph} | Hits: {self.hits}"
return tooltip
def get_status(self) -> str:
if (not self.status) and (not self.method):
return "No update yet"
status = self.status
if self.method == "triumph":
total = f"({self.triumph} total)"
status = f"{Config.triumph_icon} {Config.triumph_message} {total}"
elif self.method == "hit":
total = f"({self.hits} total)"
status = f"{Config.hit_icon} {Config.hit_message} {total}"
elif self.method == "thinking":
status = f"Thinking about {status}"
elif self.method == "travel":
status = f"Traveling to {status}"
return status
class Ants:
ants: ClassVar[list[Ant]] = []
@ -83,7 +102,7 @@ class Ants:
ant.name = Ants.random_name()
Ants.ants.append(ant)
Ants.announce_hatch(ant)
Game.add_update(ant)
Ants.save()
Game.update_info()
@ -136,7 +155,7 @@ class Ants:
ant.method = method
ant.updated = Utils.now()
Game.add_status(ant)
Game.add_update(ant)
Game.update_info()
Ants.save()
@ -177,9 +196,8 @@ class Ants:
return top, top_score
@staticmethod
def merge() -> None:
if len(Ants.ants) < 2:
return
def merge(ant_1: Ant | None = None) -> None:
from .game import Game
def split(ant: Ant) -> list[str]:
return re.split(r"[ -]", ant.name)
@ -195,6 +213,7 @@ class Ants:
return [Utils.capitalize(word) for word in words]
if not ant_1:
ant_1 = Ants.random_ant()
if not ant_1:
@ -226,11 +245,8 @@ class Ants:
if not name:
return
Ants.announce_terminate(ant_1)
Ants.announce_terminate(ant_2)
Ants.ants.remove(ant_1)
Ants.ants.remove(ant_2)
Ants.set_terminated(ant_1)
Ants.set_terminated(ant_2)
now = Utils.now()
@ -242,27 +258,25 @@ class Ants:
ant.hits = ant_1.hits + ant_2.hits
Ants.ants.append(ant)
Ants.announce_hatch(ant)
Game.add_update(ant)
Ants.hatch()
@staticmethod
def announce_hatch(ant: Ant) -> None:
from .game import Game
image_path = Config.hatched_image_path
Game.add_message(
"Hatched", f"{ant.name} is born", image_path, tooltip=ant.tooltip()
)
@staticmethod
def announce_terminate(ant: Ant) -> None:
from .game import Game
image_path = Config.terminated_image_path
Game.add_message(
"Terminated", f"{ant.name} is gone", image_path, tooltip=ant.tooltip()
)
@staticmethod
def clear() -> None:
Ants.ants = []
@staticmethod
def terminate(ant: Ant) -> None:
if ant.method == "terminated":
return
Ants.set_terminated(ant)
Ants.hatch()
@staticmethod
def set_terminated(ant: Ant) -> None:
from .game import Game
ant.method = "terminated"
Game.add_update(ant)
Ants.ants.remove(ant)

View File

@ -1,14 +1,14 @@
from __future__ import annotations
import random
from pathlib import Path
from PySide6.QtCore import Qt # type: ignore
from PySide6.QtWidgets import QHBoxLayout # type: ignore
from PySide6.QtWidgets import QVBoxLayout
from PySide6.QtWidgets import QLabel
from PySide6.QtWidgets import QWidget
from PySide6.QtGui import QPixmap # type: ignore
from PySide6.QtGui import QMouseEvent # type: ignore
from PySide6.QtGui import QPixmap
from PySide6.QtCore import QTimer
from .config import Config
@ -29,47 +29,12 @@ class Game:
Game.update_info()
@staticmethod
def add_status(ant: Ant) -> None:
container = QHBoxLayout()
status = ant.status
color = None
if (not ant.status) and (not ant.method):
status = "No update yet"
if ant.method == "triumph":
total = f"({ant.triumph} total)"
status = f"{Config.triumph_icon} {Config.triumph_message} {total}"
color = Config.triumph_color
elif ant.method == "hit":
total = f"({ant.hits} total)"
status = f"{Config.hit_icon} {Config.hit_message} {total}"
color = Config.hit_color
elif ant.method == "thinking":
status = f"Thinking about {status}"
elif ant.method == "travel":
status = f"Traveling to {status}"
tooltip = ant.tooltip()
image_label = Game.get_image(Config.status_image_path, color, tooltip=tooltip)
right_container = Game.make_right_container(ant.name, status)
container.addWidget(image_label)
container.addSpacing(Config.space_1)
container.addWidget(right_container)
Game.add_container(container)
@staticmethod
def add_message(
title: str,
message: str,
image_path: Path,
color: tuple[int, int, int] | None = None,
tooltip: str = "",
def add_update(
ant: Ant,
) -> None:
container = QHBoxLayout()
image_label = Game.get_image(image_path, color, tooltip=tooltip)
right_container = Game.make_right_container(title, message)
image_label = Game.get_image(ant)
right_container = Game.make_right_container(ant)
container.addWidget(image_label)
container.addSpacing(Config.space_1)
@ -97,7 +62,17 @@ class Game:
Filter.check()
@staticmethod
def make_right_container(title: str, message: str) -> QWidget:
def make_right_container(ant: Ant) -> QWidget:
if ant.method == "hatched":
title = "Hatched"
message = f"{ant.name} is born"
elif ant.method == "terminated":
title = "Terminated"
message = f"{ant.name} is gone"
else:
title = ant.name
message = ant.get_status()
root = QWidget()
root.setObjectName("view_right")
container = QVBoxLayout()
@ -124,8 +99,23 @@ class Game:
@staticmethod
def get_image(
path: Path, color: tuple[int, int, int] | None = None, tooltip: str = ""
ant: Ant,
) -> QLabel:
if ant.method == "hatched":
path = Config.hatched_image_path
elif ant.method == "terminated":
path = Config.terminated_image_path
else:
path = Config.status_image_path
if ant.method == "triumph":
color = Config.triumph_color
elif ant.method == "hit":
color = Config.hit_color
else:
color = None
tooltip = ant.tooltip()
image_label = QLabel()
image_label.setObjectName("view_image")
pixmap = QPixmap(str(path))
@ -154,7 +144,7 @@ class Game:
if tooltip:
image_label.setToolTip(tooltip)
image_label.mousePressEvent = lambda event: Game.toggle_song()
image_label.mousePressEvent = lambda event: Game.image_action(event, ant)
return image_label
@staticmethod
@ -207,7 +197,7 @@ class Game:
ants = sorted(Ants.ants, key=lambda ant: ant.updated)
for ant in ants:
Game.add_status(ant)
Game.add_update(ant)
@staticmethod
def start_loop() -> None:
@ -289,3 +279,12 @@ class Game:
@staticmethod
def update_size() -> None:
pass
@staticmethod
def image_action(event: QMouseEvent, ant: Ant) -> None:
if event.button() == Qt.LeftButton:
Ants.terminate(ant)
elif event.button() == Qt.MiddleButton:
Ants.merge(ant)
else:
Game.toggle_song()