diff options
Diffstat (limited to 'src/sliceitoff/hiscores')
-rw-r--r-- | src/sliceitoff/hiscores/__init__.py | 2 | ||||
-rw-r--r-- | src/sliceitoff/hiscores/hiscores.py | 56 |
2 files changed, 21 insertions, 37 deletions
diff --git a/src/sliceitoff/hiscores/__init__.py b/src/sliceitoff/hiscores/__init__.py index fdad3e5..75ce884 100644 --- a/src/sliceitoff/hiscores/__init__.py +++ b/src/sliceitoff/hiscores/__init__.py @@ -1,2 +1,2 @@ """ highscores - High score storing and listing """ -from .hiscores import HiScores +from .hiscores import hi_scores diff --git a/src/sliceitoff/hiscores/hiscores.py b/src/sliceitoff/hiscores/hiscores.py index bcf41a0..d963605 100644 --- a/src/sliceitoff/hiscores/hiscores.py +++ b/src/sliceitoff/hiscores/hiscores.py @@ -1,32 +1,16 @@ """ hiscores.hiscores - high socres: loading, saving, converting to string """ -import os -from pathlib import Path -from .static import INITIAL_HIGHSCORES, MAX_HIGHSCORES +from sliceitoff.settings import settings +from .static import MAX_HIGHSCORES class HiScores: """ Keeps track of high scores """ - def __init__(self, filename = None): + def __init__(self): """ On creation load high scores from config file """ self.table=[] - if filename: - self.config_filename = filename - else: - if os.name == 'nt': - self.config_filename = (Path.home().resolve() - .joinpath('sliceitoff.cfg')) - else: - self.config_filename = (Path.home().resolve() - .joinpath('.config').joinpath('sliceitoffrc')) - if not self.config_filename.is_file(): - self.table=INITIAL_HIGHSCORES[:] - return - with open(self.config_filename, "r", encoding="utf-8") as config_file: - for line in config_file: - option, *value = line.split('=') - if option == 'hiscore' and value: - score, name = value[0].split('!') - self.add(int(score.strip()),name.strip()) + for value in settings.get_values("hiscore"): + score, name = value.split('!') + self.add(int(score.strip()),name.strip()) if len(self.table)<MAX_HIGHSCORES: self.table+=[(0,"") for _ in range(MAX_HIGHSCORES-len(self.table))] @@ -40,20 +24,11 @@ class HiScores: """ Score is enough to make high scores """ return self.table[-1][0] < score - def __del__(self): - """ On object deletion save current high scores to config file """ - oldlines=[] - if self.config_filename.is_file(): - with (open(self.config_filename, "r", encoding="utf-8") - as config_file): - for line in config_file: - option, *_ = line.split('=') - if option != 'hiscore': - oldlines.append(line) - with open(self.config_filename, 'w', encoding="utf-8") as config_file: - config_file.writelines(oldlines) - for score, name in self.table: - config_file.write(f"hiscore={score}!{name}\n") + def save(self): + """ Save current high scores """ + settings.replace_values( + "hiscore", + [f"{score}!{name}" for score, name in self.table]) def __str__(self): text = ( @@ -68,3 +43,12 @@ class HiScores: f"\xed{self.table[i+half][1]:<3s} " f"\xef{self.table[i+half][0]:07}\n") return text + + +# Initialize only one time +try: + # pylint: disable = used-before-assignment + # This is intented behaviour + hi_scores +except NameError: + hi_scores = HiScores() |