-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
201 lines (148 loc) · 5.56 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
from flask import (
Flask, render_template, request, jsonify, session,
redirect, url_for
)
from config import *
from game_db_helper import (
db_create_user, db_create_game, db_update_game_creator,
db_get_game_users, db_start_game, db_get_game_info,
db_add_game_word, db_get_game_words, db_get_game_info_from_code,
db_get_completed_game_sentences,
db_get_completed_game_sentences_from_code
)
from helper_funcs import is_valid_word
app = Flask(__name__)
app.secret_key = SECRET_KEY
@app.route('/')
def home():
return render_template('home.html')
@app.route('/create', methods=['GET', 'POST'])
def create_game():
if request.method == 'GET':
return render_template('create_game.html')
else:
user_name = request.form.get('creator_user_name', 'No Name')
game_name = request.form.get('game_name', 'No Name')
number_of_sentences = int(request.form.get('number_of_sentences', 1))
words_shown = int(request.form.get('words_shown', 3))
if number_of_sentences < 1 or number_of_sentences > 3:
number_of_sentences = 1
if words_shown < 1 or words_shown > 3:
words_shown = 3
new_game_id = db_create_game(game_name, number_of_sentences, words_shown)
session['game_id'] = new_game_id
new_user_id = db_create_user(user_name, new_game_id)
session['user_id'] = new_user_id
db_update_game_creator(new_game_id, new_user_id)
return jsonify({'status': 'success'})
@app.route('/join', methods=['GET', 'POST'])
def join_game():
if request.method == "GET":
return render_template('game/join.html')
else:
user_name = request.form.get('user_name', 'No Name')
game_code = request.form.get('game_code', None)
game_info_ = db_get_game_info_from_code(game_code)
new_user_id = db_create_user(user_name, game_info_['game_id'])
if new_user_id != 0:
session['game_id'] = game_info_['game_id']
session['user_id'] = new_user_id
return jsonify({'status': 'success'})
else:
return jsonify({'status': 'failure'})
@app.route('/join/<string:code>')
def join_game_w_code(code):
game_info_ = db_get_game_info_from_code(code)
game_name = game_info_['game_name']
is_started = True if game_info_['game_started'] else False
return render_template(
'game/join.html',
code=code,
name=game_name,
is_started=is_started
)
@app.route('/game/start', methods=['GET', 'POST'])
def game_start():
game_id = session.get('game_id', None)
user_id = session.get('user_id', None)
game_info_ = db_get_game_info(user_id, game_id)
is_creator = game_info_['is_creator']
game_code = game_info_['game_code']
if request.method == 'GET':
return render_template(
'game/start.html', is_creator=is_creator, game_code=game_code)
else:
if is_creator:
db_start_game(game_id)
return jsonify({'status': 'success'})
return jsonify({'status': 'failure'})
@app.route('/game/on')
def game_on():
user_id = session.get('user_id', None)
game_id = session.get('game_id', None)
game_info_ = db_get_game_info(user_id, game_id)
return render_template('game/on.html', game_name=game_info_['game_name'])
@app.route('/game/end')
def game_end():
user_id = session.get('user_id', None)
game_id = session.get('game_id', None)
word_info = db_get_completed_game_sentences(game_id)
game_info_ = db_get_game_info(user_id, game_id)
return render_template(
'game/end.html',
word_info=word_info,
game_name=game_info_['game_name'],
game_code=game_info_['game_code']
)
@app.route('/game/end/<string:code>')
def game_end_w_code(code):
word_info = db_get_completed_game_sentences_from_code(code)
game_info_ = db_get_game_info_from_code(code)
return render_template(
'game/end.html',
word_info=word_info,
game_name=game_info_['game_name'],
game_code=game_info_['game_code']
)
@app.route('/game/word_list')
def game_word_list():
game_id = session.get('game_id', None)
return jsonify({'status': 'success', 'words': db_get_game_words(game_id)})
@app.route('/game/add_word', methods=['POST'])
def game_add_word():
game_id = session.get('game_id', None)
user_id = session.get('user_id', None)
new_word = request.form.get('next_word', None)
if new_word is not None:
new_word = new_word.strip()
else:
return jsonify({'status': 'failure'})
if not is_valid_word(new_word):
return jsonify({'status': 'failure'})
game_info_ = db_get_game_info(user_id, game_id)
if (
game_info_['is_current_user'] and
not game_info_['game_finished'] and
game_info_['game_started']
):
db_add_game_word(new_word, game_id, user_id)
return jsonify({'status': 'success'})
return jsonify({'status': 'failure'})
@app.route('/game/info')
def game_info():
game_id = session.get('game_id', None)
user_id = session.get('user_id', None)
game_info_ = db_get_game_info(user_id, game_id)
return jsonify(game_info_)
@app.route('/game/users')
def game_users():
users = db_get_game_users(session.get('game_id', None))
return render_template('game/users.html', users=users)
@app.route('/clear')
def clear():
session_keys = [x for x in session.keys()]
for key in session_keys:
del session[key]
return redirect(url_for('home'))
if __name__ == '__main__':
app.run(debug=DEBUG, host='0.0.0.0')