summaryrefslogtreecommitdiff
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
parent54cf5ab6dde77dff1afe177b33a054925569a0cf (diff)
separate settings from hiscore
-rw-r--r--src/sliceitoff/game/game.py12
-rw-r--r--src/sliceitoff/hiscores/__init__.py2
-rw-r--r--src/sliceitoff/hiscores/hiscores.py56
-rw-r--r--src/sliceitoff/settings/__init__.py2
-rw-r--r--src/sliceitoff/settings/settings.py82
-rw-r--r--src/sliceitoff/settings/static.py26
6 files changed, 138 insertions, 42 deletions
diff --git a/src/sliceitoff/game/game.py b/src/sliceitoff/game/game.py
index 2ef377f..f85a544 100644
--- a/src/sliceitoff/game/game.py
+++ b/src/sliceitoff/game/game.py
@@ -6,13 +6,14 @@ from pathlib import Path
import pygame
+from sliceitoff.settings import settings
from sliceitoff.display import Display
from sliceitoff.stats import Stats
from sliceitoff.screens import (
hiscores_screen,
instructions1_screen,
instructions2_screen)
-from sliceitoff.hiscores import HiScores
+from sliceitoff.hiscores import hi_scores
from sliceitoff.text import fonts
from sliceitoff.sfx import sfx
@@ -32,7 +33,6 @@ class Game:
self.clock = pygame.time.Clock()
self.display = Display()
self.stats = None
- self.hiscores = HiScores()
pygame.mouse.set_visible(False)
def __del__(self):
@@ -49,7 +49,7 @@ class Game:
def show_highscores(self):
""" displays highscores and waits a key """
sfx.music("pimpelipompeli")
- his = Show(hiscores_screen(str(self.hiscores)))
+ his = Show(hiscores_screen(str(hi_scores)))
while his.active:
his.update(dt = self.clock.tick())
self.display.update( [his] )
@@ -94,6 +94,8 @@ class Game:
while True:
match self.mainmenu():
case MenuItems.QUIT:
+ hi_scores.save()
+ settings.save()
break
case MenuItems.HISCORES:
self.show_highscores()
@@ -103,6 +105,6 @@ class Game:
self.settings()
case MenuItems.NEWGAME:
self.newgame()
- if self.hiscores.high_enough(self.stats.score):
- self.hiscores.add( self.stats.score, self.initials())
+ if hi_scores.high_enough(self.stats.score):
+ hi_scores.add( self.stats.score, self.initials())
self.show_highscores()
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()
diff --git a/src/sliceitoff/settings/__init__.py b/src/sliceitoff/settings/__init__.py
new file mode 100644
index 0000000..99fd1b6
--- /dev/null
+++ b/src/sliceitoff/settings/__init__.py
@@ -0,0 +1,2 @@
+""" settings - Settings handler """
+from .settings import settings
diff --git a/src/sliceitoff/settings/settings.py b/src/sliceitoff/settings/settings.py
new file mode 100644
index 0000000..cf588b7
--- /dev/null
+++ b/src/sliceitoff/settings/settings.py
@@ -0,0 +1,82 @@
+""" settings.settings - handles settings reading, updating and writing """
+import os
+from pathlib import Path
+from .static import DEFAULT_SETTINGS
+
+class Settings:
+ def __init__(self, filename = None):
+ self.settings=[]
+ 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.settings=DEFAULT_SETTINGS[:]
+ return
+ with open(self.config_filename, "r", encoding="utf-8") as config_file:
+ for line in config_file:
+ entry = self.validate_line(line)
+ if not entry:
+ continue
+ self.settings.append(entry)
+
+ def validate_line(self, line):
+ if not line or line[0] == '#':
+ return None
+ data = line.split('=')
+ if len(data) != 2:
+ return None
+ return data[0], data[1]
+
+ def get_values(self, option):
+ return [x[1] for x in self.settings if x[0]==option]
+
+ def put_values(self, option, values):
+ for x in values:
+ self.put_value(option, x)
+
+ def remove_values(self, option):
+ for x in self.settings[:]:
+ if x[0]==option:
+ self.settings.remove(x)
+
+ def replace_values(self, option, values):
+ self.remove_values(option)
+ self.put_values(option, values)
+
+ def get_value(self, option):
+ v = self.get_values(option)
+ if not v:
+ return None
+ return v
+
+ def get_value_or_default(self, option):
+ v = self.get_values(option)
+ if v:
+ return v
+ return [x[1] for x in DEFAULT_SETTINGS if x[0]==option][1]
+
+ def put_value(self, option, value):
+ self.settings.append((option, value))
+
+ def replace_value(self, option, value):
+ self.remove_values(option)
+ self.put_value(option, value)
+
+ def save(self):
+ with open(self.config_filename, 'w', encoding="utf-8") as config_file:
+ for option, value in self.settings:
+ config_file.write(f"{option}={value}\n")
+
+# Initialize only one time
+try:
+ # pylint: disable = used-before-assignment
+ # This is intented behaviour
+ settings
+except NameError:
+ settings = Settings()
diff --git a/src/sliceitoff/settings/static.py b/src/sliceitoff/settings/static.py
new file mode 100644
index 0000000..8c8935b
--- /dev/null
+++ b/src/sliceitoff/settings/static.py
@@ -0,0 +1,26 @@
+""" settings.static - default settings etc static data """
+
+DEFAULT_SETTINGS = [
+ ("hiscore", "281238!HIL"),
+ ("hiscore", "190355!VIL"),
+ ("hiscore", "164710!KSU"),
+ ("hiscore", "100000!---"),
+ ("hiscore", "90000!---"),
+ ("hiscore", "80000!-S-"),
+ ("hiscore", "70000!-L-"),
+ ("hiscore", "60000!-I-"),
+ ("hiscore", "50000!-C-"),
+ ("hiscore", "40000!-E-"),
+ ("hiscore", "30000!---"),
+ ("hiscore", "25000!-I-"),
+ ("hiscore", "20000!-T-"),
+ ("hiscore", "15000!---"),
+ ("hiscore", "10000!-O-"),
+ ("hiscore", "8000!-F-"),
+ ("hiscore", "6000!-F-"),
+ ("hiscore", "4000!---"),
+ ("hiscore", "2000!---"),
+ ("hiscore", "0!---"),
+ ("sfx_volume", "10"),
+ ("music_volume", "10")
+]