-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
256 lines (220 loc) · 6.38 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import os
from flask import Flask, render_template, request, redirect, url_for, session, send_from_directory,flash
import play_game
import read_data
import make_json
import mongo
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = "custom"
ALLOWED_EXTENSIONS = set(['txt'])
app = Flask(__name__, static_url_path='/static')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.debug = True
app.secret_key = "Nothing"
FACT = 'The German government decided to ally with the Austro-Hungarian Empire.'
OG = 'The German government decided to ally with the Austro-Hungarian Empire.'
F = ""
O = ""
CURR = None
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/custom/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
@app.route('/upload', methods=['POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect('/custom')
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
flash('No selected file')
return redirect('/custom')
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
make_json.make(filename.split('.')[0], custom=True)
return redirect('custom-game')
@app.route("/")
def HomePage():
if "email" in session:
check = True
else:
check = False
return render_template('Index.html', check=check)
@app.route("/login")
def login():
if "email" in session:
return redirect(url_for("custom"))
return render_template('Login.html')
# @app.route("/Dashboard")
# def Dashboard():
# return "dashboard"
@app.route('/login_action', methods=['POST'])
def login_action():
email = request.form['email']
password = request.form['password']
check, role = mongo.Login(email, password)
if check:
print("User exists")
session['email'] = email
if role == "Teacher":
session['game'] = "True"
print("game editing")
print("session started")
print(role)
return "Success"
else:
print("User Does not exists")
return {"error": "User does not exists"}
@app.route('/sign_action', methods=['POST'])
def sign_action():
name = request.form['name']
email = request.form['email']
password = request.form['password']
Role = request.form['Role']
print(Role)
if mongo.Register(email, name, password,Role):
print("User Account Created")
return "User Account Created"
else:
print("Error Occured")
return "Error Occured"
@app.route("/play-ww1")
def play_ww1():
return render_template('W1.html')
@app.route("/out")
def out():
session.clear()
return render_template('Index.html',check=False)
@app.route("/play-ww2")
def play_ww2():
return render_template('W2.html')
@app.route("/custom")
def custom():
if "game" in session:
print("if entered")
return render_template('custom.html')
if "email" in session : return render_template("custom.html",check=True)
return render_template("Index.html",check=False)
@app.route("/custom-game")
def cust_game():
lst = os.listdir('custom_data')
st = set()
for i in lst:
t = i.split('_')[2:]
st.add('_'.join(t))
print(st)
return render_template('custom_game.html', val=list(st))
@app.route("/act",methods=["POST"])
def act():
global CURR
CURR = None
global FACT
tp = read_data.get_data('ww1_f')
gd = tp['game_data']
cd = tp['content_data']
number = int(request.form['number'])
data = play_game.play(number, 'ww1_f')
# print(data)
if number == 6 :
FACT = OG
if "options" not in data:
FACT = OG
print(data)
return data
ops = data['options']
tdct = {}
counter = ['y','n']
for i in ops:
tdct[counter[0]] = {
'val': cd[i],
'next': ops[i]['next'],
'more':cd[ops[i]['more']]
}
del counter[0]
res = {
'question':cd[data['question']],
'fact':FACT,
'chap':cd[data['chapter']],
'options':tdct
}
FACT = cd[data['fact']]
# print(res)
# ans = {"question":"Second"}
return res
@app.route('/play_custom/<name>')
def my_view_func(name):
global CURR
CURR = name
tp = read_data.get_data(name, custom=True)
gd = tp['game_data']
cd = tp['content_data']
res = play_game.play(1, name, custom=True)
ops = res['options']
tdct = {}
counter = ['y','n']
for i in ops:
tdct[counter[0]] = {
'val': cd[i],
'next': ops[i]['next'],
'more':cd[ops[i]['more']]
}
del counter[0]
global F,O
F,O = [cd[res['fact']]]*2
res = {
'question':cd[res['question']],
'fact':None,
'chap':cd[res['chapter']],
'options':tdct
}
print(res)
# ans = {"question":"Second"}
res['name'] = name
return render_template('play_custom.html', val=res)
@app.route("/cust",methods=["POST"])
def cust():
global F
tp = read_data.get_data(CURR, custom=True)
gd = tp['game_data']
cd = tp['content_data']
number = int(request.form['number'])
data = play_game.play(number, CURR, custom=True)
print(data)
if number == 6 :
F = O
if "options" not in data:
F = O
return data
ops = data['options']
tdct = {}
counter = ['y','n']
for i in ops:
tdct[counter[0]] = {
'val': cd[i],
'next': ops[i]['next'],
'more':cd[ops[i]['more']]
}
del counter[0]
res = {
'question':cd[data['question']],
'fact':F,
'chap':cd[data['chapter']],
'options':tdct
}
F = cd[data['fact']]
print(res)
# ans = {"question":"Second"}
return res
@app.errorhandler(404)
def error404(error):
return render_template('404.html'), 404
if __name__ == "__main__":
app.run()