-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
284 lines (211 loc) · 8.98 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
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
from flask import Flask, render_template, redirect, flash, session, request, jsonify
from flask_debugtoolbar import DebugToolbarExtension
from psycopg2 import IntegrityError
from models import db, connect_db, User, Word, Users_Words
# from secret import KEY
from forms import NewUserForm, UserLoginForm, WordForm
from sqlalchemy.exc import IntegrityError
import requests, json, os, re
app = Flask(__name__)
uri = os.environ.get('DATABASE_URL', 'postgresql:///wordgame')
if uri.startswith("postgres://"):
uri = uri.replace("postgres://", "postgresql://", 1)
app.config['SQLALCHEMY_DATABASE_URI'] = uri
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = False
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'mochi')
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
debug = DebugToolbarExtension(app)
connect_db(app)
KEY = os.environ.get('SECRET_KEY', 'mochi')
BASE_URL = "https://wordsapiv1.p.rapidapi.com/words/"
HEADERS = {
"X-RapidAPI-Host": "wordsapiv1.p.rapidapi.com",
"X-RapidAPI-Key": KEY
}
@app.route('/')
def home_page():
"""Displays landing page with option to log in or play without logging in."""
return render_template('home.html')
@app.route('/signup', methods=["GET", "POST"])
def user_signup():
"""Allows a new user to sign up for a free account when a unique username and valid password is submitted."""
form = NewUserForm()
if form.validate_on_submit():
try:
username = form.username.data
password = form.password.data
user = User.signup(username, password)
db.session.add(user)
db.session.commit()
session["user_id"] = user.id
flash(f"You have created an account, {username}!", "success")
return redirect('/game/info')
except IntegrityError:
flash("Sorry, that username is already taken. Please try another one.", "warning")
form.username.data = ""
form.password.data = ""
return render_template('signup.html', form=form)
@app.route('/login', methods=["GET", "POST"])
def user_login():
"""Allows user who has already signed up to log in with valid username and password"""
form = UserLoginForm()
if form.validate_on_submit():
username = form.username.data
password = form.password.data
user = User.authenticate(username, password)
if user:
session["user_id"] = user.id
flash(f"You are logged in, {username}!", "success")
return redirect('/game/info')
else:
flash("Username or password not valid. Please try again.", "danger")
form.username.data = ""
form.password.data = ""
return render_template('login.html', form=form)
@app.route('/logout')
def user_logout():
"""Allows user to log out."""
flash("You've signed out!", "warning")
session.pop("user_id")
return redirect('/')
@app.route('/game/info')
def game_info():
"""Displays instructions/rules for game and start button."""
return render_template('game-info.html')
@app.route('/game/play')
def game_play():
"""Initiates game by getting a random word from Words API with the given parameters"""
querystring = {"lettersmin":"6","lettersMax":"10","syllablesMin":"2","syllablesMax":"5","frequencymin":"2.00","frequencymax":"5.00","hasDetails":"definitions", "hasDetails":"synonyms", "random":"true"}
response = requests.request("GET", BASE_URL, headers=HEADERS, params=querystring)
data = response.json()
print(data)
if " " in data['word'] or "-" in data['word']:
return redirect('/game/play')
try:
mystery_word = Word(
word = data['word'],
pos = data['results'][0]['partOfSpeech'],
syllable_count = data['syllables']['count'],
definition = data['results'][0]['definition'],
synonyms = data['results'][0]['synonyms']
)
db.session.add(mystery_word)
db.session.commit()
synonyms = data['results'][0]['synonyms']
session['word'] = mystery_word.word
session['word_id'] = mystery_word.id
except IntegrityError:
return redirect('/game/play')
return render_template('/game-play.html', word=mystery_word, synonyms=synonyms)
@app.route('/game/check-guess', methods=["GET","POST"])
def check_guess():
"""Checks if user has guessed the word and provide feedback accordingly"""
guess = request.args['guess']
return jsonify(guess)
@app.route('/game/get-score', methods=["POST"])
def get_score():
"""Get final score when game ends"""
output = request.get_json()
result = json.loads(output)
score = result['score']
session['score'] = score
return result
@app.route('/game/finish')
def show_stats():
"""Displays feedback and game stats when user guesses word correctly or time runs out."""
word = session['word']
score = session['score']
if "user_id" in session:
user_id = session['user_id']
user = User.query.get(user_id)
word_id = session['word_id']
users_words = Users_Words(user=user_id, word=word_id)
db.session.add(users_words)
db.session.commit()
else:
user = "anonymous"
update_points()
update_rankings()
leaders = User.query.order_by(User.total_points.desc()).limit(5)
return render_template('game-finish.html', score=score, word=word, leaders=leaders, user=user)
def update_points():
"""Adds current round score to logged-in user's running points total"""
if "user_id" in session:
id = session['user_id']
u = User.query.get(id)
u.total_points = u.total_points + session['score']
db.session.commit()
else:
return redirect('/game/finish')
def update_rankings():
"""Ranks players in descending order according to greatest cumulative points earned."""
rankings = User.query.order_by(User.total_points.desc()).all()
for user in rankings:
user.rank = rankings.index(user)+1
db.session.commit()
@app.route('/word-info/<word>')
def show_word_info(word):
"""Provides details about the user's selected word"""
word = Word.query.filter_by(word=word).first()
syns = word.synonyms
syns2 = syns.replace('{', '')
syns3 = syns2.replace('}', '')
synonyms = syns3.split(',')
return render_template('word-info.html', word=word, synonyms=synonyms)
@app.route('/word-lookup', methods=["GET", "POST"])
def lookup_word():
"""Allows user to type in a word and get definition and get information about the word"""
form = WordForm()
word = form.word.data
existing_word = Word.query.filter(Word.word==word).first()
if existing_word:
word = existing_word
return redirect (f'/word-info/{word.word}')
if request.method == "POST":
response = requests.request("GET", f"https://wordsapiv1.p.rapidapi.com/words/{word}", headers=HEADERS)
data = response.json()
try:
word = Word(
word=data['word'],
definition=data['results'][0]['definition'],
pos=data['results'][0]['partOfSpeech'],
synonyms=data['results'][0]['synonyms']
)
db.session.add(word)
db.session.commit()
if "user_id" in session:
user_id = session['user_id']
users_words = Users_Words(user=user_id, word=word.id)
db.session.add(users_words)
db.session.commit()
return redirect (f'/word-info/{word.word}')
except KeyError:
flash("Sorry, we don't have all the information for this word. Please try another word.", "warning")
return render_template('word-form.html', form=form, word=word)
@app.route('/word-info/<word>/delete', methods=["POST"])
def delete_word(word):
"""Allows logged-in user to delete selected word from their words list"""
word = Word.query.filter_by(word=word).first()
if "user_id" in session:
db.session.delete(word)
db.session.commit()
flash("You have deleted a word from your list.", "warning")
return redirect('/game/play')
else:
flash("You are not authorized to delete words.", "danger")
return redirect('/')
@app.route('/user-words')
def show_user_words():
"""Displays list of words from user's games and allows user to view more info or delete words"""
user_words_list = []
if "user_id" not in session:
flash("Sorry, you must have an account to see your words.", "danger")
return redirect('/')
else:
user_id = session['user_id']
users_words = Users_Words.query.filter_by(user=user_id).all()
for word in users_words:
user_word = Word.query.get(word.word)
user_words_list.append(user_word)
return render_template('user-words.html', list=user_words_list)