summaryrefslogtreecommitdiff
path: root/src/sliceitoff/hiscores/hiscores.py
diff options
context:
space:
mode:
authorViljami Ilola <+@hix.fi>2024-04-27 00:40:24 +0300
committerViljami Ilola <+@hix.fi>2024-04-27 00:40:24 +0300
commita4794437d561a01c8bea12d59502e4cc72acfbea (patch)
treea27f26062bf766f0b90b0965c179be0f29022240 /src/sliceitoff/hiscores/hiscores.py
parent54cf5ab6dde77dff1afe177b33a054925569a0cf (diff)
separate settings from hiscore
Diffstat (limited to 'src/sliceitoff/hiscores/hiscores.py')
-rw-r--r--src/sliceitoff/hiscores/hiscores.py56
1 files changed, 20 insertions, 36 deletions
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()