-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
99 lines (80 loc) · 2.67 KB
/
server.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from audioop import avg
import flask
import json
from flask import jsonify
from flask import request
from flask import send_from_directory
from flask import render_template
from system import getData
from database import testDrit
splitword = 'burger'
app = flask.Flask(__name__)
# app.config["DEBUG"] = True
@app.route('/', methods=['GET'])
def get():
return render_template('form.html')
@app.route('/answers/<name>', methods=['GET', 'POST'])
def answers(name):
if request.method == 'GET':
data = open(f'answers/{name}.json', 'rb').read()
dict = json.loads(data)
all_headers = list(dict.keys())
all_rows = list(zip(*dict.values()))
return render_template('personScore.html', rows = all_rows, headers = all_headers, name = name)
elif request.method == 'POST':
text = request.form[f'burger_field_{name}']
if not splitword in text:
raise Exception(f"gimme {splitword}")
else:
stringArray = text.split(splitword)
data = json.loads(getData(stringArray))
content = json.loads(open(f'answers/{name}.json', 'rb').read())
for game, score in data.items():
content[game].append(score)
with open(f'answers/{name}.json', 'w', encoding='utf-8') as f:
json.dump(content, f, ensure_ascii=False, indent=4)
return f"Data for {name} updated!"
@app.route('/answers', methods=['GET'])
def stats():
players = []
joonData = open('answers/joon.json', 'rb').read()
joonData = json.loads(joonData)
players.append(["joon", joonData])
klitoData = open('answers/klito.json', 'rb').read()
klitoData = json.loads(klitoData)
players.append(["klito", klitoData])
peteData = open('answers/pete.json', 'rb').read()
peteData = json.loads(peteData)
players.append(["pete", peteData])
playerDict = {
"joon":0,
"klito":0,
"pete":0
}
for player in players:
dict = {
"worldle": 0,
"countryle": 0,
"globle": 0,
"tradle": 0,
"flagle": 0
}
for item in player[1].items():
avgNum = 0
for e in item[1]:
avgNum = avgNum + e
if (len(item[1])):
avgNum = avgNum / len(item[1])
dict[item[0]] = avgNum
playerDict[player[0]] = dict
return f'''
<h1>Average Scores</h1>
<p>Joon: {playerDict["joon"]}</p>
<br>
<p>Klito: {playerDict["klito"]}</p>
<br>
<p>Pete: {playerDict["pete"]}</p>
<br>
<p>{testDrit()}</p>
<a href="/"><button>← Tilbake</button></a>
'''