file write work in progress

This commit is contained in:
=
2024-02-08 09:43:30 +01:00
parent 835b8c5821
commit d9c10434cd
2 changed files with 13 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
scores.json

12
main.py
View File

@@ -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__":