-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
92 lines (68 loc) · 2.37 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
import os
from functools import reduce
from flask import Flask, session, render_template, request, redirect, url_for
from helpers import check_word, get_board, get_from_session_or_init
app = Flask(__name__)
app.secret_key = os.environ['SECRET_KEY']
@app.route('/', methods=['GET'])
def board():
"""Render template for user to play, handle submitted words."""
if 'board' in session:
board = session['board']
else:
board = get_board()
session['board'] = board
words = get_from_session_or_init(session, 'words', [])
score = reduce(lambda x, y: x + len(y), words, 0)
return render_template(
'app.html',
board=board,
words=words,
current_word=get_from_session_or_init(session, 'current_word', ''),
is_valid=get_from_session_or_init(session, 'is_valid', False),
message=get_from_session_or_init(session, 'message', ''),
score=score,
end=get_from_session_or_init(session, 'end', ''),
)
@app.route('/check', methods=['POST'])
def check():
"""
Given a word, check if:
- (1) it can be formed on the board,
- (2) it can be found in the dictionary
If both conditions are satisfied, return True.
"""
if 'board' in session:
board = session['board']
else:
board = get_board()
session['board'] = board
# check that word is valid
word = request.form['word'].upper()
words = get_from_session_or_init(session, 'words', [])
checked_word_obj = check_word(word, words, board)
session['current_word'] = word
session['is_valid'] = checked_word_obj['is_valid']
session['message'] = checked_word_obj['message']
if checked_word_obj['is_valid']:
session['words'].append(word)
# set the end time if first post of game
end = get_from_session_or_init(session, 'end', '')
if not end:
session['end'] = request.form['end']
session.modified = True
return redirect(url_for('board'))
@app.route('/clear', methods=['GET'])
def clear():
"""
Resets the session e.g. found words
"""
session.pop('board', None)
session.pop('current_word', None)
session.pop('words', None)
session.pop('end', None)
session.modified = True
return redirect(url_for('board'))
@app.route('/instructions', methods=['GET'])
def instructions():
return render_template('instructions.html')