-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
679 lines (571 loc) · 30.5 KB
/
run.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
import os
import copy
import json
import random
import datetime
from flask import Flask, render_template, request, redirect, url_for, session, flash
from functools import wraps #decorators for requires login
from flask_mail import Mail, Message
app = Flask(__name__)
# Use the following to run LOCALLY will need the import
# app.config.from_pyfile('config.cfg')
# Use the following to run from HEROKU - remove the import
app.config["MAIL_SERVER"] = os.getenv('MAIL_SERVER')
app.config["MAIL_USERNAME"] = os.getenv('MAIL_USERNAME')
app.config["MAIL_PASSWORD"] = os.getenv('MAIL_PASSWORD')
app.config["MAIL_PORT"] = os.getenv('MAIL_PORT')
app.config["MAIL_USE_SSL"] = os.getenv('MAIL_USE_SSL')
app.config["MAIL_DEFAULT_SENDER"] = os.getenv('MAIL_DEFAULT_SENDER')
app.secret_key = "Not a secure key" # Needed for sessions to work properly
mail = Mail(app)
loggedUsers = {}
# login_required decorator -- from a tutorial and adapted
def login_required(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return f(*args, **kwargs)
else:
flash('You need to log in first')
return redirect(url_for("index"))
return wrap
def read_from_file(file_name):
store=""
file = "data/" + file_name
with open(file, "r") as readdata:
store = readdata.read()
return store
# Generate list of all riddles available
allRiddles = json.loads(read_from_file("riddles.json"))
class User(object):
''' This is the prototype of a logged user. There will be an instance
for each logged user. '''
logged_users = 0
def __init__(self, username, is_logged, total_points, games_played, route="index"):
self.username = username
self.is_logged = is_logged
self.total_user_points = total_points
self.games_played = games_played
self.number_of_games = 0
self.points_best_game = 0
self.date_best_game = "01/01/2000"
self.game_on = False # Is there a current game active True/False
self.game = ""
self.points_this_game = 0 # Should this belong to User or Game
self.session = 1
self.current_route = route
User.logged_users += 1 # Keep track of how many users are logged in
def addLoggedUser(self):
''' Add a logged in user to the dictionary of logged in users. '''
global loggedUsers
loggedUsers[self.username] = self
if self.username:
addLoggedUser(self)
class Game(object):
''' This is the prototype of a game. When an instance is created it will be owned
by a logged in user. If the user logs out or the game finishes the game instance
will be destroyed. '''
def __init__(self):
self.riddles_sequence = [] # List of riddles [[id, url, ans], [..], ...]
self.riddle_counter = -1 # Are these doing the same thing?
self.current_riddle = [] # The current riddle [id, url, ans]
self.attempt = 1
self.points = 10 # Points gained for this attempt if WIN
self.most_recent_answer = ""
self.wrong_answers = []
def generate_riddle_sequence(self):
global allRiddles
id_list = []
for x in range(0, 10): #To select 10 random riddles
repeat = True
while repeat:
choose_game = random.choice(allRiddles)
if choose_game['id'] not in id_list:
repeat = False
id_list.append(choose_game['id'])
item = []
item.append(choose_game['id'])
item.append(choose_game['source'])
item.append(choose_game['answer'])
self.riddles_sequence.append(item)
def select_current_riddle(self):
self.riddle_counter += 1
self.current_riddle = self.riddles_sequence[self.riddle_counter]
generate_riddle_sequence(self)
select_current_riddle(self)
defaultUser = User("default", False, 0, [], "index")
def createUser(name):
tempUser = User(name, True, 0, [])
vars()[name] = copy.deepcopy(tempUser)
loggedUsers[name] = copy.deepcopy(tempUser)
return vars()[name]
def fill_best_individual_games():
store = json.loads(read_from_file("hof_individual.json"))["best_individual_games"]
return store
def fill_best_all_games():
store = json.loads(read_from_file("hof_all_games.json"))["best_all_games"]
return store
def store_game_info(username):
thisUser = loggedUsers[username]
thisUser.number_of_games += 1
thisUser.total_user_points += thisUser.points_this_game
today = datetime.datetime.now().strftime("%d/%m/%Y")
info = (today, thisUser.points_this_game)
extract_games_played = thisUser.games_played
thisUser.games_played.insert(0, info)
if thisUser.points_this_game > thisUser.points_best_game:
thisUser.points_best_game = thisUser.points_this_game
thisUser.date_best_game = today # Today's date
allusers = json.loads(read_from_file("users.json"))
allusers[username] = {"username": thisUser.username,
"games_played": thisUser.games_played,
"date_best_game": thisUser.date_best_game,
"number_of_games": thisUser.number_of_games,
"points_best_game": thisUser.points_best_game,
"total_user_points": thisUser.total_user_points}
with open("data/users.json", "w") as outfile:
json.dump(allusers, outfile, sort_keys=True, indent=4)
# Update hof_individual.json
insert_hof_info=[]
insert_hof_info.append(today)
insert_hof_info.append(username)
insert_hof_info.append(thisUser.points_this_game)
insert_in_hof_individual(insert_hof_info)
# Update hof_all_games.json
insert_all_games=[]
insert_all_games.append(username)
insert_all_games.append(thisUser.total_user_points)
insert_all_games.append(thisUser.number_of_games)
insert_in_hof_all_games(insert_all_games)
thisUser.points_this_game = 0
return
def insert_in_hof_individual(data):
# global best_individual_games
best_individual_games = fill_best_individual_games()
# Get list of points from json - already sorted
sorted_points = [] #Reverse order
for item in best_individual_games:
sorted_points.insert(0, item[3])
# Add new points - only if it is more than at least the smallest number
if data[2] > min(sorted_points):
sorted_points.insert(0, data[2])
sorted_points.sort()
# Reduce length of list to 10 so that I will have the best 10
# when I insert the new data
while len(sorted_points) > 10:
del sorted_points[0]
# Build new list of sorted data
insert_done = False
new_points_list =[]
counter = len(best_individual_games)-1
pointer = 0
for item in sorted_points:
if item == data[2] and insert_done == False: # New item
new_points_list.insert(0, (counter + 1, data[0], data[1], data[2]))
insert_done = True
else:
if insert_done:
new_points_list.insert(0, best_individual_games[counter-1])
else:
new_points_list.insert(0, [best_individual_games[counter-1][0] + 1, best_individual_games[counter-1][1],best_individual_games[counter-1][2], best_individual_games[counter-1][3]])
counter -= 1
pointer += 1
else:
new_points_list = best_individual_games
# Prepare dictionary to write as jason
to_write = {}
to_write['best_individual_games'] = new_points_list
# Store to file
with open('data/hof_individual.json', 'w') as outfile:
json.dump(to_write, outfile, sort_keys=True, indent=4)
return
def insert_in_hof_all_games(data):
# global best_all_games
best_all_games = fill_best_all_games()
# Get list of points from json - already sorted
sorted_points = [] #Reverse order
for item in best_all_games:
sorted_points.insert(0, item[2])
# Add new points - only if it is more than at least the smallest number
if data[1] > min(sorted_points):
sorted_points.insert(0, data[1])
sorted_points.sort()
# Reduce length of list to 10 so that I will have the best 10
# when I insert the new data
while len(sorted_points) > 10:
del sorted_points[0]
# Build new list of sorted data
insert_done = False
new_points_list =[]
counter = len(best_all_games)-1
pointer = 0
for item in sorted_points:
if item == data[1] and insert_done == False: # New item
new_points_list.insert(0, (counter + 1, data[0], data[1], data[2]))
insert_done = True
else:
if insert_done:
new_points_list.insert(0, best_all_games[counter-1])
else:
new_points_list.insert(0, [best_all_games[counter-1][0] + 1, best_all_games[counter-1][1],best_all_games[counter-1][2],best_all_games[counter-1][3]])
counter -= 1
pointer += 1
else:
new_points_list = best_all_games
# Prepare dictionary to write as jason
to_write = {}
to_write['best_all_games'] = new_points_list
# Store to file
with open('data/hof_all_games.json', 'w') as outfile:
json.dump(to_write, outfile, sort_keys=True, indent=4)
return
@app.route('/<currentUser>', methods=['GET','POST'])
@app.route('/', methods=['GET','POST'])
def index(currentUser=defaultUser.username):
try:
if request.method == 'POST':
if 'register' in request.form:
return redirect(url_for('register'))
except Exception as e:
return "<h1> Error: " + str(e) + "</h1>"
allusers = json.loads(read_from_file("users.json"))
message = ""
try:
if session['user']:
currentUser = session['user']
except:
pass
thisUser=loggedUsers[currentUser]
thisUser.current_route = "index"
return render_template("index.html", thisUser=thisUser, message=message)
@app.route('/login', methods=['GET','POST'])
def login():
thisUser=""
try:
if request.method == 'POST':
username = request.form['username']
allusers = json.loads(read_from_file("users.json"))
if username == "":
allusers = ""
message = "Please enter your username to log in."
return render_template('index.html', thisUser=defaultUser, username="", message=message)
elif username in allusers.keys(): #User already registered
print("User {} is registered".format(username)) #Debug
# Is user already logged in?
if username in loggedUsers.keys():
message = username + " is already logged in. Do you want to proceed? If you proceed the previous session will be destroyed."
return render_template("proceed_login.html", thisUser=defaultUser, username=username, message=message)
else: # User is not logged in yet
x = createUser(username)
loggedUsers[username].total_user_points = allusers[username]['total_user_points']
loggedUsers[username].games_played = allusers[username]['games_played']
loggedUsers[username].points_best_game = allusers[username]['points_best_game']
loggedUsers[username].number_of_games = allusers[username]['number_of_games']
loggedUsers[username].date_best_game = allusers[username]['date_best_game']
loggedUsers[username].current_route = "user"
loggedUsers[username].is_logged = True
session['logged_in'] = True
session['user'] = username
session['session'] = 1
print(session)
return render_template("user.html", thisUser=loggedUsers[username], username="", message="")
else:
return redirect(url_for('register'))
except Exception as e:
return "<h1> Error: " + str(e) + "</h1>"
# return "What has happened? login route is present." #Should never reach here
return redirect(url_for("server_error")) #Should never reach here unless user tries to break the game.
@app.route('/proceed_login/<username>', methods=['GET', 'POST'])
def proceed_login(username):
loggedUsers[username].session+=1
session['session'] = loggedUsers[username].session
print(session)
loggedUsers[username].current_route = "user"
return render_template("user.html", thisUser=loggedUsers[username])
@app.route('/register', methods=['GET','POST'])
def register():
thisUser = copy.deepcopy(defaultUser)
thisUser.current_route = "register"
button_control = {} # Control which buttons show up.
button_control["username"] = ""
button_control["register"] = ""
button_control["check_active"] = ""
button_control["register_active"] = "btn-deactivated btn-hide"
try:
if request.method == 'POST':
username = request.form['username']
allusers = json.loads(read_from_file("users.json"))
button_control["username"] = username
if 'check' in request.form:
if username == "":
username_feedback = "Please type in a username and check its availability."
feedback_type = "error"
elif username in allusers:
username_feedback = "Username already exist. Please try another one."
feedback_type = "error"
else:
button_control["register"] = "register"
button_control["register_active"] = ""
button_control["check_active"] = "btn-deactivated btn-hide"
username_feedback = "Username available. Please click the register button."
feedback_type = "success"
return render_template("register.html",button_control=button_control, username_feedback=username_feedback, feedback_type=feedback_type, thisUser=thisUser)
if 'register' in request.form:
username = request.form['username']
if username == "":
username_feedback = "Please type in a username and check its availability."
feedback_type = "normal"
return render_template("register.html",button_control=button_control, username_feedback=username_feedback, feedback_type=feedback_type, thisUser=thisUser)
elif username in allusers:
username_feedback = "Username already exist. Please try another one."
feedback_type = "error"
return render_template("register.html",button_control=button_control, username_feedback=username_feedback, feedback_type=feedback_type, thisUser=thisUser)
else:
allusers[username] = {"username": username, "games_played": [], "date_best_game": "", "number_of_games": 0, "points_best_game": 0, "total_user_points": 0}
with open("data/users.json", "w") as outfile:
json.dump(allusers, outfile, sort_keys=True, indent=4)
x = createUser(username)
loggedUsers[username].total_user_points = allusers[username]['total_user_points']
loggedUsers[username].games_played = allusers[username]['games_played']
loggedUsers[username].points_best_game = allusers[username]['points_best_game']
loggedUsers[username].number_of_games = allusers[username]['number_of_games']
loggedUsers[username].date_best_game = allusers[username]['date_best_game']
loggedUsers[username].current_route = "user"
loggedUsers[username].is_logged = True
session['logged_in'] = True
session['user'] = username
session['session'] = 1
print(session)
return render_template("user.html", thisUser=loggedUsers[username], username="", message="")
except Exception as e:
return "<h1> Error: " + str(e) + "</h1>"
username_feedback = "Enter a valid username."
feedback_type = "normal"
return render_template("register.html", button_control=button_control, username_feedback=username_feedback, feedback_type=feedback_type, thisUser=thisUser)
@app.route('/logout/<currentUser>/<sessionNo>', methods=['GET', 'POST'])
@login_required
def logout(currentUser, sessionNo):
if request.method == 'POST': #RESET
try:
thisUser = loggedUsers[currentUser]
except Exception as e:
message = "This session has expired. {} has been logged out from somewhere else.".format(e)
return render_template("index.html", thisUser=defaultUser, message=message)
try:
if int(sessionNo) == thisUser.session:
# Prepare to delete instances
# IF GAME OBJECT EXIST DELETE, OTHERWISE DO NOTHING
if thisUser.game == "":
pass
else:
thisUser.game = "" # del thisUser.game
del thisUser
del loggedUsers[currentUser]
session.pop('logged_in', None)
session.pop('user', None)
session.pop('session', None)
print("This is session: {}".format(session))
return redirect(url_for("index"))
else:
message = "This session has been disabled. You were logged out from somewhere else."
return render_template("index.html", thisUser=defaultUser, message=message)
except Exception as e:
message="Something wrong happened. Please login again."
return render_template("index.html", thisUser=defaultUser, message=message)
@app.route('/user/<currentUser>', methods=['GET', 'POST'])
@app.route('/user', methods=['GET', 'POST'])
@login_required
def user(currentUser=defaultUser.username):
thisUser=loggedUsers[currentUser]
thisUser.current_route = "user"
return render_template("user.html", thisUser=thisUser)
@app.route('/halloffame/<currentUser>')
@app.route('/halloffame')
def halloffame(currentUser=defaultUser.username):
thisUser=loggedUsers[currentUser]
thisUser.current_route = "halloffame"
best_individual_games = fill_best_individual_games()
best_all_games = fill_best_all_games()
return render_template("halloffame.html", thisUser=thisUser, best_individual_games=best_individual_games, best_all_games=best_all_games)
@app.route('/about/<currentUser>')
@app.route('/about')
def about(currentUser=defaultUser.username):
thisUser=loggedUsers[currentUser]
thisUser.current_route = "about"
return render_template("about.html", thisUser=thisUser)
@app.route('/contact/<currentUser>', methods=['GET', 'POST'])
@app.route('/contact', methods=['GET', 'POST'])
def contact(currentUser=defaultUser.username):
thisUser=loggedUsers[currentUser]
thisUser.current_route = "contact"
if request.method == "GET":
return render_template("contact.html", thisUser=thisUser)
try:
name = request.form['name']
email = request.form['email']
subject = request.form['subject']
message = request.form['message']
msg = Message(subject, sender=email, recipients=[app.config['MAIL_DEFAULT_SENDER']])
msg.body = "{} sent the following message from the Riddle-Me this game website: \n__________________________________________\n\n{}".format(name, message)
mail.send(msg)
return render_template("message_sent.html", name=name, email=email, subject=subject, message=message, thisUser=thisUser)
except Exception as e:
return render_template("message_error.html", email=email, thisUser=thisUser)
'''
ERROR EXAMPLE:
smtplib.SMTPRecipientsRefused: {'[email protected]': (550, b'Verification failed for <-----@hotmail>\nThe mail server could not deliver mail to -----------@---------. The account or domain may not exist, they may be blacklisted, or missing the proper dns entries.\nSender verify failed')}'''
@app.route('/game/<currentUser>', methods=['GET', 'POST'])
@app.route('/game', methods=['GET', 'POST'])
@login_required
def game(currentUser=defaultUser.username):
thisUser=loggedUsers[currentUser]
thisUser.current_route = "game" # I will need this to control the menu
if thisUser.game_on == False:
thisUser.game_on = True # Game On
thisUser.game = Game() # CREATE A GAME
try:
if request.method == 'POST':
if 'play' in request.form:
thisUser.game.points = 10
thisUser.game.attempt = 1
elif 'answer_btn' in request.form:
if thisUser.game.attempt == 1: # Check Answer
answer = request.form['answer_text']
# Clean the answer - If there are multiple spaces or other white characters in between the words
temp =[] # To hold Clean answer
temp = answer.split()
thisUser.game.most_recent_answer=""
for item in temp:
thisUser.game.most_recent_answer += item + " "
thisUser.game.most_recent_answer = thisUser.game.most_recent_answer.strip() # Strip trailing spaces - there shouldn't be any left but just in case.
if thisUser.game.most_recent_answer.lower() == thisUser.game.current_riddle[2].lower(): # if match - answer correct
thisUser.points_this_game += 10
thisUser.game.wrong_answers = []
thisUser.game.attemp = 1 # First attempt of
thisUser.game.riddle_counter += 1 # Next Riddle
if thisUser.game.riddle_counter > len(thisUser.game.riddles_sequence)-1: # Call next riddle
return redirect(url_for('game_over', currentUser=thisUser.username)) # GAME OVER
else: # Trigger next riddle
thisUser.game.current_riddle = thisUser.game.riddles_sequence[thisUser.game.riddle_counter]
else: # Otherwise answer is wrong
if len(answer) == 0: #if no answer is given
thisUser.game.wrong_answers.append("-")
else:
thisUser.game.wrong_answers.append(answer)
thisUser.game.attempt = 2 # This is your next attempt
thisUser.game.points = 6 # Set correct number of points
elif thisUser.game.attempt == 2:
# Get all words and concatenate them
answer = ""
index = ""
local_answer = thisUser.game.current_riddle[2].split()
for ndx, each_word in enumerate(local_answer):
index = 'answer_text' + str(ndx+1)
answer += (request.form[index].strip() + " ") #Strip any typed white spaces
temp =[] # Clean answer
temp = answer.split()
thisUser.game.most_recent_answer=""
for item in temp:
thisUser.game.most_recent_answer += item + " "
thisUser.game.most_recent_answer = thisUser.game.most_recent_answer.strip() # Strip trailing spaces
if thisUser.game.most_recent_answer.lower() == thisUser.game.current_riddle[2].lower(): # Answer correct
thisUser.points_this_game += 6 # Gain points
thisUser.game.attempt = 1 # Reset attempt
thisUser.game.points = 10
thisUser.game.wrong_answers = []
thisUser.game.riddle_counter += 1 # Next Riddle
if thisUser.game.riddle_counter > len(thisUser.game.riddles_sequence)-1: # Call next riddle
return redirect(url_for('game_over', currentUser=thisUser.username))
else:
thisUser.game.current_riddle = thisUser.game.riddles_sequence[thisUser.game.riddle_counter]
else: # Otherwise answer is wrong
if len(answer) == 0: #if no answer is given
thisUser.game.wrong_answers.append("-")
else:
thisUser.game.wrong_answers.append(answer)
thisUser.game.attempt = 3 # This is your next attempt
thisUser.game.points = 2 # Set correct number of points
elif thisUser.game.attempt == 3:
print("This is attempt 3 to check the answer.")
# Get all words and concatenate them
answer = ""
index = ""
local_answer = thisUser.game.current_riddle[2].split()
for ndx, each_word in enumerate(local_answer):
index = 'answer_text' + str(ndx+1)
answer += (request.form[index].strip() + " ") #Strip any typed white spaces
temp =[] # Clean answer
temp = answer.split()
thisUser.game.most_recent_answer=""
for item in temp:
thisUser.game.most_recent_answer += item + " "
thisUser.game.most_recent_answer = thisUser.game.most_recent_answer.strip() # Strip trailing spaces
if thisUser.game.most_recent_answer.lower() == thisUser.game.current_riddle[2].lower(): # Answer correct
thisUser.points_this_game += 2 # Gain points
thisUser.game.attempt = 1 # Reset attempt
thisUser.game.points = 10
thisUser.game.wrong_answers = [] # Reset wrong answers
thisUser.game.riddle_counter += 1
if thisUser.game.riddle_counter > len(thisUser.game.riddles_sequence)-1: # Call next riddle
return redirect(url_for('game_over', currentUser=thisUser.username))
else: # Trigger next riddle
thisUser.game.current_riddle = thisUser.game.riddles_sequence[thisUser.game.riddle_counter]
else: # Otherwise answer is wrong
thisUser.game.attempt = 1 # This is your next attempt
thisUser.game.points = 10 # Set correct number of points
thisUser.game.wrong_answers = [] # Reset wrong answers
thisUser.game.riddle_counter += 1
if thisUser.game.riddle_counter > len(thisUser.game.riddles_sequence)-1:
return redirect(url_for('game_over', currentUser=thisUser.username))
else: # Call next riddle
thisUser.game.current_riddle = thisUser.game.riddles_sequence[thisUser.game.riddle_counter]
elif 'pass_btn' in request.form:
if thisUser.game.attempt == 1:
thisUser.game.wrong_answers = []
thisUser.game.wrong_answers = ["-"]
thisUser.game.attempt = 2
thisUser.game.points = 6
elif thisUser.game.attempt == 2:
thisUser.game.points = 2
thisUser.game.wrong_answers.append("-")
thisUser.game.attempt = 3
elif thisUser.game.attempt == 3:
if thisUser.game.riddle_counter > len(thisUser.game.riddles_sequence)-1:
return redirect(url_for('game_over', currentUser=thisUser.username))
thisUser.game.points = 10
thisUser.game.attempt = 1
thisUser.game.riddle_counter += 1
thisUser.game.wrong_answers = []
if thisUser.game.riddle_counter > len(thisUser.game.riddles_sequence)-1: # Call next riddle
return redirect(url_for('game_over', currentUser=thisUser.username))
else:
thisUser.game.current_riddle = thisUser.game.riddles_sequence[thisUser.game.riddle_counter]
except Exception as e:
message = "This session has expired. {} has been logged out from somewhere else.".format(e)
return render_template("index.html", thisUser=defaultUser, message=message)
return render_template("game.html", thisUser=thisUser)
@app.route('/game_over/<currentUser>', methods=['GET', 'POST'])
@app.route('/game_over', methods=['GET', 'POST'])
@login_required
def game_over(currentUser=defaultUser.username):
thisUser=loggedUsers[currentUser]
thisUser.current_route = "user" # I will need this to control the menu
thisUser.game_on = False
flash(thisUser.points_this_game)
store_game_info(currentUser) # Updates Hall of fame too
return render_template("user.html", thisUser=thisUser)
@app.errorhandler(404)
def page_not_found(error):
return render_template('error404.html'), 404
@app.errorhandler(500)
def server_error(e):
send = "<h1>Internal Server Error</h1><p>The server is acting up. I am not sure if it had breakfast this morning.<br>Please try again later.</p><p>{}</p>".format(e)
return render_template('error500.html'), 500
@app.route('/error500_recover', methods=['POST'])
def error500_recover():
name = ""
thisUser = ""
name = request.form['username']
return "Extracted username: {}".format(name)
if __name__ == '__main__':
app.run(host=os.getenv('IP'), port=int(os.getenv('PORT')), debug=False)