Merge branch 'settings' into 'master'
Paramètres See merge request ynerant/dungeon-battle!5
This commit is contained in:
commit
58c77fa5bf
3
.gitignore
vendored
3
.gitignore
vendored
@ -8,3 +8,6 @@ venv/
|
||||
.pytest_cache/
|
||||
|
||||
__pycache__
|
||||
|
||||
# Don't commit settings
|
||||
settings.json
|
||||
|
@ -1,5 +1,6 @@
|
||||
from .interfaces import Map
|
||||
from .mapdisplay import MapDisplay
|
||||
from .settings import Settings
|
||||
from .term_manager import TermManager
|
||||
|
||||
|
||||
@ -8,6 +9,9 @@ class Game:
|
||||
|
||||
def init(self) -> None:
|
||||
Game.INSTANCE = self
|
||||
self.settings = Settings()
|
||||
self.settings.load_settings()
|
||||
self.settings.write_settings()
|
||||
with TermManager() as term_manager:
|
||||
self._start_game(term_manager.screen)
|
||||
|
||||
|
83
dungeonbattle/settings.py
Normal file
83
dungeonbattle/settings.py
Normal file
@ -0,0 +1,83 @@
|
||||
import json
|
||||
import os
|
||||
from typing import Any, Generator
|
||||
|
||||
|
||||
class Settings:
|
||||
"""
|
||||
This class stores the settings of the game.
|
||||
Settings can be get by using for example settings.TEXTURE_PACK directly.
|
||||
The comment can be get by using settings.get_comment('TEXTURE_PACK').
|
||||
We can define the setting by simply use settings.TEXTURE_PACK = 'new_key'
|
||||
"""
|
||||
def __init__(self):
|
||||
self.KEY_UP_PRIMARY = ['z', 'Touche principale pour aller vers le haut']
|
||||
self.KEY_UP_SECONDARY = ['KEY_UP', 'Touche secondaire pour aller vers le haut']
|
||||
self.KEY_DOWN_PRIMARY = ['s', 'Touche principale pour aller vers le bas']
|
||||
self.KEY_DOWN_SECONDARY = ['KEY_DOWN', 'Touche secondaire pour aller vers le bas']
|
||||
self.KEY_LEFT_PRIMARY = ['q', 'Touche principale pour aller vers la gauche']
|
||||
self.KEY_LEFT_SECONDARY = ['KEY_LEFT', 'Touche secondaire pour aller vers la gauche']
|
||||
self.KEY_RIGHT_PRIMARY = ['d', 'Touche principale pour aller vers la droite']
|
||||
self.KEY_RIGHT_SECONDARY = ['KEY_RIGHT', 'Touche secondaire pour aller vers la droite']
|
||||
self.TEXTURE_PACK = ['ASCII', 'Pack de textures utilisé']
|
||||
|
||||
def __getattribute__(self, item: str) -> Any:
|
||||
superattribute = super().__getattribute__(item)
|
||||
if item.isupper() and item in self.settings_keys:
|
||||
return superattribute[0]
|
||||
return superattribute
|
||||
|
||||
def __setattr__(self, name: str, value: Any) -> None:
|
||||
if name in self.settings_keys:
|
||||
object.__getattribute__(self, name)[0] = value
|
||||
return
|
||||
return super().__setattr__(name, value)
|
||||
|
||||
def get_comment(self, item: str) -> str:
|
||||
"""
|
||||
Retrieve the comment of a setting.
|
||||
"""
|
||||
if item in self.settings_keys:
|
||||
return object.__getattribute__(self, item)[1]
|
||||
for key in self.settings_keys:
|
||||
if getattr(self, key) == item:
|
||||
return object.__getattribute__(self, key)[1]
|
||||
|
||||
@property
|
||||
def settings_keys(self) -> Generator[str, Any, None]:
|
||||
"""
|
||||
Get the list of all parameters.
|
||||
"""
|
||||
return (key for key in self.__dict__)
|
||||
|
||||
def loads_from_string(self, json_str: str) -> None:
|
||||
"""
|
||||
Dump settings
|
||||
"""
|
||||
d = json.loads(json_str)
|
||||
for key in d:
|
||||
setattr(self, key, d[key])
|
||||
|
||||
def dumps_to_string(self) -> str:
|
||||
"""
|
||||
Dump settings
|
||||
"""
|
||||
d = dict()
|
||||
for key in self.settings_keys:
|
||||
d[key] = getattr(self, key)
|
||||
return json.dumps(d, indent=4)
|
||||
|
||||
def load_settings(self) -> None:
|
||||
"""
|
||||
Loads the settings from a file
|
||||
"""
|
||||
if os.path.isfile("settings.json"):
|
||||
with open("settings.json", "r") as f:
|
||||
self.loads_from_string(f.read())
|
||||
|
||||
def write_settings(self) -> None:
|
||||
"""
|
||||
Dumps the settings into a file
|
||||
"""
|
||||
with open("settings.json", "w") as f:
|
||||
f.write(self.dumps_to_string())
|
30
dungeonbattle/settings_test.py
Normal file
30
dungeonbattle/settings_test.py
Normal file
@ -0,0 +1,30 @@
|
||||
import unittest
|
||||
|
||||
from dungeonbattle.settings import Settings
|
||||
|
||||
|
||||
class TestSettings(unittest.TestCase):
|
||||
def test_settings(self) -> None:
|
||||
"""
|
||||
Ensure that settings are well loaded.
|
||||
"""
|
||||
settings = Settings()
|
||||
self.assertEqual(settings.KEY_UP_PRIMARY, 'z')
|
||||
self.assertEqual(settings.KEY_DOWN_PRIMARY, 's')
|
||||
self.assertEqual(settings.KEY_LEFT_PRIMARY, 'q')
|
||||
self.assertEqual(settings.KEY_RIGHT_PRIMARY, 'd')
|
||||
self.assertEqual(settings.KEY_UP_SECONDARY, 'KEY_UP')
|
||||
self.assertEqual(settings.KEY_DOWN_SECONDARY, 'KEY_DOWN')
|
||||
self.assertEqual(settings.KEY_LEFT_SECONDARY, 'KEY_LEFT')
|
||||
self.assertEqual(settings.KEY_RIGHT_SECONDARY, 'KEY_RIGHT')
|
||||
self.assertEqual(settings.TEXTURE_PACK, 'ASCII')
|
||||
self.assertEqual(settings.get_comment(settings.TEXTURE_PACK), settings.get_comment('TEXTURE_PACK'))
|
||||
self.assertEqual(settings.get_comment(settings.TEXTURE_PACK), 'Pack de textures utilisé')
|
||||
|
||||
settings.TEXTURE_PACK = 'UNICODE'
|
||||
self.assertEqual(settings.TEXTURE_PACK, 'UNICODE')
|
||||
|
||||
settings.write_settings()
|
||||
settings.load_settings()
|
||||
|
||||
self.assertEqual(settings.TEXTURE_PACK, 'UNICODE')
|
Loading…
Reference in New Issue
Block a user