summaryrefslogtreecommitdiff
path: root/src/sliceitoff/hiscores
diff options
context:
space:
mode:
authorViljami Ilola <+@hix.fi>2024-03-30 13:04:39 +0200
committerViljami Ilola <+@hix.fi>2024-03-30 13:04:39 +0200
commitacfac65f17a274d6d7f73cb44ed9476032241134 (patch)
tree0ef93c0d81171a6be751e3edd8d4a029f1095fcc /src/sliceitoff/hiscores
parent607c00bc57fb68572754404cb5da0e7f7a5618e5 (diff)
lint, refactor, doc, repeat
Diffstat (limited to 'src/sliceitoff/hiscores')
-rw-r--r--src/sliceitoff/hiscores/__init__.py1
-rw-r--r--src/sliceitoff/hiscores/hiscores.py23
2 files changed, 15 insertions, 9 deletions
diff --git a/src/sliceitoff/hiscores/__init__.py b/src/sliceitoff/hiscores/__init__.py
index f3d7811..fdad3e5 100644
--- a/src/sliceitoff/hiscores/__init__.py
+++ b/src/sliceitoff/hiscores/__init__.py
@@ -1 +1,2 @@
+""" highscores - High score storing and listing """
from .hiscores import HiScores
diff --git a/src/sliceitoff/hiscores/hiscores.py b/src/sliceitoff/hiscores/hiscores.py
index 2725931..1b3b87f 100644
--- a/src/sliceitoff/hiscores/hiscores.py
+++ b/src/sliceitoff/hiscores/hiscores.py
@@ -1,9 +1,12 @@
+""" hiscores.hiscores - high socres: loading, saving, converting to string"""
import os
MAX_HIGHSCORES = 20
class HiScores:
+ """ Keeps track of high scores """
def __init__(self):
+ """ On creation load high scores from config file """
self.table=[]
self.config_filename = os.path.join(
os.getenv('HOME'),
@@ -12,7 +15,7 @@ class HiScores:
if not os.path.isfile(self.config_filename):
self.table=[(0,"") for _ in range(MAX_HIGHSCORES)]
return
- with open(self.config_filename, "r") as config_file:
+ with open(self.config_filename, "r", encoding="utf-8") as config_file:
for line in config_file:
option, value = line.split('=')
if option == 'hiscore':
@@ -20,30 +23,32 @@ class HiScores:
self.add(int(score.strip()),name.strip())
if len(self.table)<MAX_HIGHSCORES:
self.table+=[(0,"") for _ in range(MAX_HIGHSCORES-len(self.table))]
-
+
def add(self, score, initials):
+ """ Add new high score and reranks top """
self.table.append( (score, initials) )
self.table.sort(reverse=True)
self.table = self.table[:MAX_HIGHSCORES]
-
+
def high_enough(self, score):
+ """ 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 os.path.isfile(self.config_filename):
- with open(self.config_filename, "r") as config_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') as config_file:
+ 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 __str__(self):
text = (
" "