diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..12b2331 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +scores.json diff --git a/main.py b/main.py index 1a81349..ddce83c 100644 --- a/main.py +++ b/main.py @@ -29,6 +29,7 @@ def get_score() -> str: scoreDTOs.sort(reverse = True, key = lambda score: score.score) return json.dumps(scoreDTOs) + @app.route('/score', methods = ['POST']) @cross_origin() def add_score() -> str: @@ -50,18 +51,26 @@ def add_score() -> str: if allready_saved_score is None: scores.append(score) + writeScores() return json.dumps({'status': 'success', 'msg': 'created score'}) if allready_saved_score.ip == score.ip: allready_saved_score.score = score.score + writeScores() return json.dumps({'status': 'updated score'}) else: return json.dumps({'status': 'error', 'msg': 'score has been achieved on different machine'}) + def json_body(): return request.json +def writeScores(): + f = open('scores.json', 'w') + f.write(json.dumps(scores)) + + def find_score_by(username: str) -> Score | None: for score in scores: if score.username == username: @@ -70,6 +79,9 @@ def find_score_by(username: str) -> Score | None: return None def main(): + f = open('scores.json', 'r') + fileObject: list[Score] = json.loads(f.read()) + scores.append(*fileObject) app.run(debug = True) if __name__ == "__main__":