forked from linbug/planigale
/
planigale_flask.py
83 lines (58 loc) · 2.06 KB
/
planigale_flask.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
from flask import Flask, render_template, request, redirect, session, url_for
from planigale import Question, Species, Planigale, PlanigaleGame, PlanigaleConsole
import os
app = Flask(__name__)
app.secret_key = os.urandom(24)
def get_new_session():
global curr_session
curr_session += 1
def get_session_id_game():
if 'id' not in session:
session['id'] = get_new_session()
# forward to question for new game?
id = session['id']
if id not in games:
games[id] = PlanigaleGame(data,total_questions = 5)
# forward to question for new game?
game = games[id]
return id, game
def new_game():
pass
@app.route('/', methods=['GET'])
@app.route('/index', methods=['GET'])
def index():
id, game = get_session_id_game()
return render_template('index.html')
@app.route('/question', methods=['GET'])
def question():
id, game = get_session_id_game()
return render_template('question.html',
question_num = game.question_num,
question = game.curr_question )
@app.route('/answer', methods=['POST'])
def answer():
id, game = get_session_id_game()
guess_species = game.curr_question.species[int(request.form["choice"])]
game.score_question(game.curr_question, guess_species)
validation_dict = {True: "Correct!", False: "Incorrect!"}
return render_template('answer.html',
question_num = game.question_num,
question = game.curr_question,
validation = validation_dict[game.curr_question.correct])
@app.route('/next', methods=['POST'])
def next():
id, game = get_session_id_game()
if game.next_question():
return redirect(url_for('question'))
else:
return redirect(url_for('summary'))
@app.route('/summary', methods=['GET'])
def summary():
id, game = get_session_id_game()
return render_template('summary.html',
game = game)
if __name__ == '__main__':
curr_session = 0
data = Planigale.load_species()
games = {}
app.run(debug = True)