From 3c8e0a9b9a3721adae17750f0308eefcaf5fa71a Mon Sep 17 00:00:00 2001 From: Kevin Baumgartner Date: Fri, 8 May 2026 16:04:12 +0200 Subject: [PATCH 1/4] feat(highscore): create basic structure for highscore --- src/highscore.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/highscore.py diff --git a/src/highscore.py b/src/highscore.py new file mode 100644 index 0000000..14f6564 --- /dev/null +++ b/src/highscore.py @@ -0,0 +1,17 @@ +import json + +class Highscore: + def __init__(self): + self.path = "./highscore.json" + + def load_scores(self): + pass + + def save_scores(self, scores): + pass + + def add_score(self, player_name, score_ects, time_survived, won): + pass + + def get_top_scores(self, number_of_top_scores): + pass \ No newline at end of file From 7846bfa04209cf2b9dd6b9a4f03fa8cad40b4caa Mon Sep 17 00:00:00 2001 From: Kevin Baumgartner Date: Fri, 8 May 2026 16:18:25 +0200 Subject: [PATCH 2/4] feat(highscore): add methods for highscore handling --- src/highscore.py | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/highscore.py b/src/highscore.py index 14f6564..16fae15 100644 --- a/src/highscore.py +++ b/src/highscore.py @@ -2,16 +2,46 @@ import json class Highscore: def __init__(self): + # fix path to json file with highscores self.path = "./highscore.json" + # check if file already exists, otherwise create it + try: + with open(self.path, "r") as f: + pass + except FileNotFoundError: + self.save_scores([]) + def load_scores(self): - pass + with open(self.path, "r") as f: + return json.load(f) def save_scores(self, scores): - pass + with open(self.path, "w") as file: + json.dump(scores, file, indent=4) def add_score(self, player_name, score_ects, time_survived, won): - pass + # load existing scores from file + scores = self.load_scores() - def get_top_scores(self, number_of_top_scores): - pass \ No newline at end of file + # assign values from new score and append to existing scores + new_score = { + "name": player_name, + "ects": score_ects, + "time": time_survived, + "won": won + } + scores.append(new_score) + + # sort by ects descending + scores.sort(key=lambda score: score["ects"], reverse=True) + + # keep only top 50 elements + scores = scores[:50] + + self.save_scores(scores) + + def get_top_scores(self): + scores = self.load_scores() + scores.sort(key=lambda score: score["ects"], reverse=True) + return scores[:5] \ No newline at end of file From 1553630e1c7d694ed80bc6018e45a47dc66617c6 Mon Sep 17 00:00:00 2001 From: Kevin Baumgartner Date: Fri, 8 May 2026 16:36:07 +0200 Subject: [PATCH 3/4] feat(highscore): add method to get formatted top 5 highscores --- src/highscore.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/highscore.py b/src/highscore.py index 16fae15..6fb57ed 100644 --- a/src/highscore.py +++ b/src/highscore.py @@ -44,4 +44,22 @@ class Highscore: def get_top_scores(self): scores = self.load_scores() scores.sort(key=lambda score: score["ects"], reverse=True) - return scores[:5] \ No newline at end of file + return scores[:5] + + def get_top_scores_formatted(self): + top_scores = self.get_top_scores() + formatted_scores = [] + + for index, score in enumerate(top_scores): + result = "PASSED" if score["won"] else "FAILED" + + line = ( + f"{index + 1}. {score['name']} | " + f"ECTS: {score['ects']} | " + f"Time: {score['time']} | " + f"Result: {result}" + ) + + formatted_scores.append(line) + + return formatted_scores \ No newline at end of file From b466ee3d7ce53b0c940a137efb0e0fe5fa19a942 Mon Sep 17 00:00:00 2001 From: Kevin Baumgartner Date: Fri, 8 May 2026 16:55:58 +0200 Subject: [PATCH 4/4] feat(highscore): add comments and json file --- src/highscore.json | 38 ++++++++++++++++++++++++++++++++++++++ src/highscore.py | 27 +++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 src/highscore.json diff --git a/src/highscore.json b/src/highscore.json new file mode 100644 index 0000000..7ef8cca --- /dev/null +++ b/src/highscore.json @@ -0,0 +1,38 @@ +[ + { + "name": "Niklas", + "ects": 55, + "time": "02:00", + "won": true + }, + { + "name": "Anna", + "ects": 48, + "time": "01:35", + "won": true + }, + { + "name": "Kevin", + "ects": 42, + "time": "01:58", + "won": true + }, + { + "name": "Lena", + "ects": 35, + "time": "01:44", + "won": false + }, + { + "name": "Lukas", + "ects": 20, + "time": "00:58", + "won": false + }, + { + "name": "Max", + "ects": 12, + "time": "00:30", + "won": false + } +] \ No newline at end of file diff --git a/src/highscore.py b/src/highscore.py index 6fb57ed..71f443b 100644 --- a/src/highscore.py +++ b/src/highscore.py @@ -13,14 +13,33 @@ class Highscore: self.save_scores([]) def load_scores(self): + """ + Will load the highscore json file. + :return: All scores form json file. + """ with open(self.path, "r") as f: return json.load(f) def save_scores(self, scores): + """ + Write score data into json file. File will be overwritten. + :param scores: Scores to store in json highscore file. + :return: None. + """ + # save scores to file with open(self.path, "w") as file: json.dump(scores, file, indent=4) def add_score(self, player_name, score_ects, time_survived, won): + """ + Add a new score to the list of existing scores from highscore file. The + number of entries is limited to 50. + :param player_name: Name of the player. + :param score_ects: Scored ECTS in game. + :param time_survived: Time survived in game. + :param won: Game won/lose. + :return: None. + """ # load existing scores from file scores = self.load_scores() @@ -42,11 +61,19 @@ class Highscore: self.save_scores(scores) def get_top_scores(self): + """ + Get top 5 scores from highscore file. + :return: Return top 5 scores as list. + """ scores = self.load_scores() scores.sort(key=lambda score: score["ects"], reverse=True) return scores[:5] def get_top_scores_formatted(self): + """ + Get top 5 scores from highscore file in nice format. + :return: Return top 5 scores as formatted string in list. + """ top_scores = self.get_top_scores() formatted_scores = []