From 54842bac29217abf7bff1e1ffb519fe9e6e3cbc2 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Wed, 11 Dec 2019 21:45:52 -0500 Subject: [PATCH] Scoreboard mostly working. It doesn't read the existing scores correctly yet though. --- libstormgames.py | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/libstormgames.py b/libstormgames.py index bfd5dfc..bfd7463 100755 --- a/libstormgames.py +++ b/libstormgames.py @@ -38,6 +38,7 @@ class scoreboard(): 'Handles scores and top 10' def __init__(self, startingScore = 0): + read_config() try: localConfig.add_section("scoreboard") except: @@ -46,16 +47,20 @@ class scoreboard(): self.oldScores = [] for i in range(1, 11): try: - self.oldScores.insert(i - 1, read_config("scoreboard", i)) + self.oldScores.insert(i - 1, localConfig.get("scoreboard", i)) except: pass - #self.oldScores.insert(i - 1, 0) + self.oldScores.insert(i - 1, 0) for i in range(1, 11): if self.oldScores[i - 1] == None: self.oldScores[i - 1] = 0 def __del__(self): self.Update_Scores() + try: + write_config() + except: + pass def Decrease_Score(self, points = 1): self.score -= points @@ -70,36 +75,35 @@ class scoreboard(): self.score += points def Update_Scores(self): - newScores = self.oldScores.copy() - for i in newScores: - if self.score > newScores[newScores.index(i)]: - self.oldScores[newScores.index(newScores[i])] = i - localConfig.set("scoreboard", str(newScores.index(newScores[i])), str(i)) - try: - write_config() - except: - pass + # Update the scores + for i, j in enumerate(self.oldScores): + if self.score > j: + self.oldScores[i] = self.score + break + # Update the scoreboard section of the games config file. + for i, j in enumerate(self.oldScores): + localConfig.set("scoreboard", str(i + 1), str(j)) def write_config(writeGlobal = False): if writeGlobal == False: - with open(gamePath + "config.ini", 'w') as configfile: + with open(gamePath + "/config.ini", 'w') as configfile: localConfig.write(configfile) else: - with open(globalPath + "config.ini", 'w') as configfile: + with open(globalPath + "/config.ini", 'w') as configfile: globalConfig.write(configfile) -def read_config(section, value, readGlobal = False): +def read_config(readGlobal = False): if readGlobal == False: try: - with open(gamePath + "config.ini", 'r') as configfile: - return localConfig.read(section, value) + with open(gamePath + "/config.ini", 'r') as configfile: + localConfig.read(configfile) except: pass else: try: - with open(globalPath + "config.ini", 'r') as configfile: - return globalConfig.read(section, value) + with open(globalPath + "/config.ini", 'r') as configfile: + globalConfig.read(configfile) except: pass