-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
158 lines (101 loc) · 4.67 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from flask import Flask, redirect, url_for, render_template, request, session
import game
import readbucketdata
import createData
import appconfig
import json
app = Flask(__name__)
app.secret_key = 'hello'
# Home route:
@app.route("/", methods = ["GET", "POST"])
def home():
session.permanent = True
word_info = readbucketdata.readbucketdata("letters.csv")
old_word_info = readbucketdata.readbucketdata("oldletters.csv")
oldMaxPoints, oldBestWords = old_word_info["maxPoints"][0], eval(old_word_info["bestWords"][0])
letters, letterPts, maxPoints, bestWords, lid = eval(word_info["letters"][0]), eval(word_info["letterPts"][0]), int(word_info["maxPoints"][0]), eval(word_info["bestWords"][0]), int(word_info["lid"][0])
# Turns bestWords into dictionary so it can be turned into JSON
bestWordsDict = {}
for item in bestWords:
bestWordsDict[item[0]] = item[1]
oldBestWordsDict = {}
for item in oldBestWords:
oldBestWordsDict[item[0]] = item[1]
cutoff = bestWords[0][1]
for i in range(5):
currPts = bestWords[i][1]
if currPts < cutoff:
cutoff = currPts
topBest = [wordPair for wordPair in bestWords if wordPair[1] > cutoff]
numAdd = 5 - len(topBest)
topBestDict = {}
for item in topBest:
topBestDict[item[0]] = item[1]
topBest = json.dumps(topBestDict)
bestWords = json.dumps(bestWordsDict)
possWords = json.dumps(eval(word_info["possWords"][0]))
oldBestWords = json.dumps(oldBestWordsDict)
oldBestWordsOnly = list(oldBestWordsDict.keys())
return render_template("game.html", letters = letters, letterPts = letterPts, maxPoints = maxPoints, bestWords = bestWords, lid = lid, possWords = possWords, oldMaxPoints = oldMaxPoints, oldBestWords = oldBestWords, oldBestWordsOnly = oldBestWordsOnly, topBest = topBest, numAdd = numAdd)
# Beta version of the app for testing new features
@app.route("/test", methods = ["GET", "POST"])
def test():
session.permanent = True
word_info = readbucketdata.readbucketdata("letters.csv")
old_word_info = readbucketdata.readbucketdata("oldletters.csv")
oldMaxPoints, oldBestWords = old_word_info["maxPoints"][0], eval(old_word_info["bestWords"][0])
letters, letterPts, maxPoints, bestWords, lid = eval(word_info["letters"][0]), eval(word_info["letterPts"][0]), int(word_info["maxPoints"][0]), eval(word_info["bestWords"][0]), int(word_info["lid"][0])
# Turns bestWords into dictionary so it can be turned into JSON
bestWordsDict = {}
for item in bestWords:
bestWordsDict[item[0]] = item[1]
oldBestWordsDict = {}
for item in oldBestWords:
oldBestWordsDict[item[0]] = item[1]
cutoff = bestWords[0][1]
for i in range(5):
currPts = bestWords[i][1]
if currPts < cutoff:
cutoff = currPts
topBest = [wordPair for wordPair in bestWords if wordPair[1] > cutoff]
numAdd = 5 - len(topBest)
topBestDict = {}
for item in topBest:
topBestDict[item[0]] = item[1]
topBest = json.dumps(topBestDict)
bestWords = json.dumps(bestWordsDict)
possWords = json.dumps(eval(word_info["possWords"][0]))
oldBestWords = json.dumps(oldBestWordsDict)
oldBestWordsOnly = list(oldBestWordsDict.keys())
return render_template("test.html", letters = letters, letterPts = letterPts, maxPoints = maxPoints, bestWords = bestWords, lid = lid, possWords = possWords, oldMaxPoints = oldMaxPoints, oldBestWords = oldBestWords, oldBestWordsOnly = oldBestWordsOnly, topBest = topBest, numAdd = numAdd)
# Login page for admin to see the current game info
@app.route("/login", methods = ["GET", "POST"])
def login():
lid = int(readbucketdata.readbucketdata("letters.csv")["lid"][0])
if request.method == "POST":
if (request.form["usr"] == appconfig.admin_name) and (request.form["pwd"] == appconfig.admin_pwd):
session["usr"] = appconfig.admin_name
print("Logged in as " + appconfig.admin_name)
return redirect(url_for('home'))
else:
return render_template("invalidLogin.html", lid = lid)
return render_template("login.html", lid = lid)
# For users to provide feedback about the game
@app.route("/feedback", methods = ["GET", "POST"])
def feedback():
lid = int(readbucketdata.readbucketdata("letters.csv")["lid"][0])
return render_template("feedback.html", lid = lid)
# Admin page for getting quick access to game data
@app.route("/data", methods = ["GET", "POST"])
def data():
lid = int(readbucketdata.readbucketdata("letters.csv")["lid"][0])
bestWords = eval(readbucketdata.readbucketdata("letters.csv")["bestWords"][0])
if "usr" not in session:
return redirect(url_for('home'))
else:
if request.method == "POST":
createData.main()
return redirect(url_for("home"))
return render_template("data.html", lid = lid, bestWords = bestWords)
if __name__ == '__main__':
app.run(debug = True)