-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
67 lines (59 loc) · 2.21 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from flask import Flask, request
import json
app = Flask(__name__)
# Route to get the data for the leaderboard
@app.route("/")
def leaderboard():
# No authentication necessary - this could be a public website
arr = []
# Try and except block to catch any errors that may occur when reading the file
try:
# Open the file and read the contents
with open('scores.json', 'r') as f:
arr = json.loads(f.read())
# Convert the dictionary to a list of dictionaries with the name included
temp_arr = []
for i in arr:
temp_arr.append({
'name': i,
'score': arr[i]['score'],
'turns': arr[i]['turns'],
'coins': arr[i]['coins'],
'arrows': arr[i]['arrows'],
'wumpus': arr[i]['wumpus']
})
# Sort the array in descending order by score
temp_arr = sorted(temp_arr, key= lambda x: x['score'], reverse=True)
# set the return array to this new array
arr = temp_arr
except:
print('Error reading scores.json')
return arr
# method to add scores to the leaderboard
@app.route('/addscore', methods=['POST'])
def addscore():
# Not anyone can add scores - only the app
# this is a rough way to do it - but this is a proof of concept
if(request.form['password'] == 'resin'):
# Try and except block to catch any errors that may occur when reading the file
scores = {}
try:
with open('scores.json', 'r') as f:
scores = json.load(f)
except FileNotFoundError:
pass
# Overwriting the score if the name already exists
# This makes it so each user can only set one high score
scores[request.form['name']] = {
'score': request.form['score'],
'turns': request.form['turns'],
'coins': request.form['coins'],
'arrows': request.form['arrows'],
'wumpus': request.form['wumpus']
}
# Save the scores to a file
with open('scores.json', 'w') as f:
json.dump(scores, f)
return 'Score added'
if __name__ == '__main__':
app.run()