diff --git a/settings.toml b/settings.toml new file mode 100644 index 0000000..80e013e --- /dev/null +++ b/settings.toml @@ -0,0 +1,21 @@ +[keybinds] +rotate_colockwise = "up arrow" +hard_drop = "space" +hold = "shift+c" +rotate_counter_colockwise = "ctrl+z" +pause = "esc+F1" +move_left = "left arrow" +move_right = "right arrow" +move_down = "down arrow" + +[numpad] +hold = "numpad 0" +hard_drop = "numpad 5" +move_left = "numpad 4" +move_right = "numpad 6" +soft_drop = "numpad 2" +rotate_colockwise_1 = "numpad 1" +rotate_colockwise_2 = "numpad 5" +rotate_colockwise_3 = "numpad 9" +rotate_counter_colockwise_1 = "numpad 3" +rotate_counter_colockwise_2 = "numpad 7" diff --git a/src/utils/__init__.py b/src/utils/__init__.py index 1817a6c..ab9befe 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -3,6 +3,7 @@ from .enum import Direction, GameMode, Rotation from .figure import Figure, FigureConfig from .log import log from .path import BASE_PATH +from .settings import read_settings, save_settings from .tuples import BestMove, Size from .weights import Weights @@ -18,4 +19,6 @@ __all__ = [ "GameMode", "Weights", "BestMove", + "read_settings", + "save_settings", ] diff --git a/src/utils/config.py b/src/utils/config.py index 6158f69..5e7c6a7 100644 --- a/src/utils/config.py +++ b/src/utils/config.py @@ -1,6 +1,6 @@ from pathlib import Path -from attr import define, field +from attr import define from pygame import Vector2 as Vec2 from .colors import TokyoNightNight diff --git a/src/utils/settings.py b/src/utils/settings.py new file mode 100644 index 0000000..920cd12 --- /dev/null +++ b/src/utils/settings.py @@ -0,0 +1,33 @@ +from pathlib import Path +from typing import Optional + +import toml + +from .config import CONFIG, Config +from .log import log + + +def save_settings(settings: Config, file_path: Path) -> None: + with open(file_path, "w") as file: + toml.dump(settings, file) + + +def read_settings(file_path: Path) -> Optional[dict[str, str]]: + """ + Read and parse a TOML file and return the content as a dictionary. + + Args: + file_path: The path to the TOML file. + + Returns: + dict: The parsed content of the TOML file. + """ + try: + with open(file_path, "r") as file: + return toml.load(file) + except FileNotFoundError: + log.error(f"Error: The file '{file_path}' does not exist.") + return None + except toml.TomlDecodeError as e: + log.error(f"rror decoding TOML file: {e}") + return None