From 896b39eb4ac4be4ac62423e7708843b78623ca2a Mon Sep 17 00:00:00 2001 From: Auric Vente Date: Sat, 20 Jul 2024 04:48:04 -0600 Subject: [PATCH] Mods --- cromulant/config.py | 9 +++++++++ cromulant/game.py | 17 ++++++++++++----- cromulant/main.py | 2 ++ cromulant/settings.py | 26 ++++++++++++++++++++++++++ cromulant/storage.py | 10 ++++++++++ 5 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 cromulant/settings.py diff --git a/cromulant/config.py b/cromulant/config.py index 9c55e37..5354053 100644 --- a/cromulant/config.py +++ b/cromulant/config.py @@ -51,6 +51,7 @@ class Config: input_text_color: str = "rgb(18, 18, 18)" input_border_color: str = "rgb(140, 140, 140)" input_caret_color: str = "rgb(18, 18, 18)" + settings_json: Path @staticmethod def prepare() -> None: @@ -61,6 +62,14 @@ class Config: Config.ants_json.parent.mkdir(parents=True, exist_ok=True) Config.ants_json.write_text("[]") + Config.settings_json = ( + Path(appdirs.user_config_dir()) / "cromulant" / "settings.json" + ) + + if not Config.settings_json.exists(): + Config.settings_json.parent.mkdir(parents=True, exist_ok=True) + Config.settings_json.write_text("{}") + Config.icon_path = Config.here / "img" / "icon_4.jpg" Config.status_image_path = Config.here / "img" / "icon_5.jpg" Config.hatched_image_path = Config.here / "img" / "icon_7.jpg" diff --git a/cromulant/game.py b/cromulant/game.py index 16c6d7f..13e07f4 100644 --- a/cromulant/game.py +++ b/cromulant/game.py @@ -18,6 +18,7 @@ from .utils import Utils from .ants import Ant from .ants import Ants from .window import Window +from .settings import Settings class Game: @@ -204,14 +205,14 @@ class Game: @staticmethod def start_loop() -> None: - speed = Window.speed.currentText() + speed = Settings.speed - if speed == "Fast": + if speed == "fast": delay = Config.loop_delay_fast - elif speed == "Normal": - delay = Config.loop_delay_normal - else: + elif speed == "slow": delay = Config.loop_delay_slow + else: + delay = Config.loop_delay_normal Game.timer = QTimer() Game.timer.timeout.connect(Game.get_status) @@ -219,6 +220,12 @@ class Game: @staticmethod def update_speed() -> None: + speed = Window.speed.currentText().lower() + + if speed == Settings.speed: + return + + Settings.set_speed(speed) Game.timer.stop() Game.start_loop() diff --git a/cromulant/main.py b/cromulant/main.py index 2b38b45..686cab7 100644 --- a/cromulant/main.py +++ b/cromulant/main.py @@ -5,6 +5,7 @@ from .utils import Utils from .ants import Ants from .window import Window from .game import Game +from .settings import Settings def main() -> None: @@ -12,6 +13,7 @@ def main() -> None: Utils.prepare() Ants.prepare() Window.prepare() + Settings.prepare() Game.prepare() Game.start_loop() Window.start() diff --git a/cromulant/settings.py b/cromulant/settings.py new file mode 100644 index 0000000..728ea87 --- /dev/null +++ b/cromulant/settings.py @@ -0,0 +1,26 @@ +from .window import Window +from .storage import Storage + + +class Settings: + speed: str + + @staticmethod + def prepare() -> None: + settings = Storage.get_settings() + Settings.speed = settings.get("speed", "normal") + speed = Settings.speed.capitalize() + Window.speed.setCurrentText(speed) + + @staticmethod + def save() -> None: + settings = { + "speed": Settings.speed, + } + + Storage.save_settings(settings) + + @staticmethod + def set_speed(speed: str) -> None: + Settings.speed = speed + Settings.save() diff --git a/cromulant/storage.py b/cromulant/storage.py index d3fc064..16debeb 100644 --- a/cromulant/storage.py +++ b/cromulant/storage.py @@ -26,3 +26,13 @@ class Storage: def get_names() -> Any: with Config.names_json.open() as file: return json.load(file) + + @staticmethod + def get_settings() -> Any: + with Config.settings_json.open() as file: + return json.load(file) + + @staticmethod + def save_settings(settings: dict[str, Any]) -> None: + with Config.settings_json.open("w") as file: + json.dump(settings, file)