-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
340 lines (284 loc) · 10.7 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
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
#TeenTitanics - Cathy Cai, Ahnaf Kazi, Ricky Lin, Matthew Ming
import json
import os
import urllib
from flask import Flask, render_template, session, redirect, request, url_for, flash
from util import db, auth
app = Flask(__name__)
app.secret_key = os.urandom(32)
#obtains keys to use from key database
with open("data/key.json") as f:
data = json.loads(f.read())
key=data["key"]
@app.route("/")
def index():
'''This function redirects the user to their profile page if they are logged in.
If they aren't, it will render the login page
where the users will be prompted to enter their username and password.
When users enter and submit the information,
they will be redirected to the authentication page.'''
#if the user is logged in redirect them to their profile page
if 'user' in session:
return redirect('/home')
#if not, load the login page
return render_template("index.html")
@app.route("/logout")
def logout():
if 'user' in session:
session.pop('user')
flash('Sucessfully Logged Out')
return redirect('/')
@app.route("/register")
def register():
'''This function redirects the user to their profile page if they are logged in.
If they aren't, it will render the account creator page
where they will be prompted to enter their desired username and password
as well as confirm their password.
Once the user sumbits their information, they will be redirected to their profile page.
Users can also click the login link which brings them back to the login page.'''
#redirects if not logged in
if 'user' in session:
return redirect('/home')
return render_template("register.html")
@app.route("/auth", methods = ['POST','GET'])
def authenticate():
'''This function redirects users who got to the the authenticate page without entering a form to the login page.
If they did enter a form, it will check if the username and password are in the database.
If they are, it will redirect them to their home page and flash a message indicating a successful login.
If they aren't, it will redirect them to the login page and flash a message indicating the issue.
Whether the username or the password was incorrect.
If they were both incorrect, the message will only indicate an issue with the username.'''
loginStatus = ''
#if the user got here without entering a form, redirect them to the index
if request.method == 'GET' or not('user' in request.form.keys()):
return redirect('/')
#checks the user's login info or account creation
if "pass2" in request.form.keys():
loginStatus = auth.register(request.form['user'],request.form['pass1'],request.form['pass2'])
else: loginStatus = auth.login(request.form["user"],request.form["pass"])
#if the user successsfully logs in or creates an acount, redirect them to their profile page
if loginStatus in ["Account creation successful","Login Successful"]:
session['user'] = request.form['user']
return redirect('/home')
else:
flash(loginStatus)
#Redirects to previous page or root if there is none
return redirect(request.referrer or '/')
@app.route('/home')
def home():
return render_template("home.html")
@app.route('/leaders', methods=['POST', 'GET'])
def leaders():
try:
user = session['user']
except:
return redirect('/')
try:
winner = request.form["win"]
print ("winner retrieved")
print (winner)
if winner == "1":
user_info = db.findInfo('userInfo', user, 'username', fetchOne = True)
money = user_info[7]
wins = user_info[8]
db.modify('userInfo', 'money', money + 300, 'username', user)
print ('mod')
db.modify('userInfo', 'wins', wins + 1, 'username', user)
leaders = db.leaderboard()
return render_template("leaders.html", leaders=leaders)
return render_template("leaders.html", leaders=leaders)
except:
leaders = db.leaderboard()
return render_template("leaders.html", leaders=leaders)
@app.route('/market')
def market():
try:
user = session['user']
except:
return redirect('/')
user_info = db.findInfo('userInfo', user, 'username', fetchOne = True)
money = user_info[7]
slots = user_info[3] * 100
if (slots == 900):
slots = "NOTHING YOU HAVE EVERYTHING ALREADY"
# stuff = user_info[???]
return render_template("market.html",
username = user,
money = money,
price = slots
)
@app.route('/start')
def start():
try:
user = session['user']
except:
return redirect('/')
user_info = db.findInfo('userInfo', user, 'username', fetchOne = True)
slots = user_info[3]
print(slots)
pokemon_images = []
print ('heres the info')
poke_info = db.findAll('pokeInfo')[:slots]
print(poke_info)
for poke in poke_info:
pokemon_images.append(poke[1] + '.png')
print(pokemon_images)
return render_template("start.html",
pokemons = pokemon_images
)
@app.route('/game', methods=['POST', 'GET'])
def game():
try:
user = session['user']
except:
return redirect('/')
chosenpoke = request.form['chosenpoke']
chosenpoke = chosenpoke[:-4]
ip = "https://ipapi.co/json/"
response = urllib.request.urlopen(ip)
obj = json.loads(response.read())
lat = str(obj['latitude'])
lon = str(obj['longitude'])
weather = "https://api.darksky.net/forecast/" + key + "/" + lat + "," + lon
response = urllib.request.urlopen(weather)
obj = json.loads(response.read())
condition = obj['currently']['icon']
effect = ''
#print(conditon)
if ('cloudy' in condition.lower()):
effect = 'cloudy'
elif ("clear" in condition.lower()):
effect = 'clear'
elif ('rain' in condition.lower()):
effect = 'rain'
else:
effect = 'none'
return render_template("game.html",
pokemon = chosenpoke,
effect = effect)
@app.route('/newpoke', methods=['POST', 'GET'])
def newpoke():
try:
user = session['user']
except:
return redirect('/')
user_info = db.findInfo('userInfo', user, 'username', fetchOne = True)
money = user_info[7]
slots = user_info[3]
price = slots * 100
# assuming 3 slots initiated
if (slots == 9):
price = "NOTHING YOU HAVE EVERYTHING ALREADY"
flash("You have everything already!")
return render_template("market.html",
username=user,
money=money,
price=price
)
elif money >= price:
poke_info = db.findAll('pokeInfo')
# new_poke = poke_info[slots]
new_poke = poke_info[0]
slots += 1
db.modify('userInfo', 'slots', slots, 'username', user)
db.modify('userInfo', 'money', money - price, 'username', user)
return redirect('/start')
else:
if (slots == 9):
price = "NOTHING YOU HAVE EVERYTHING ALREADY"
flash ("You don't have enough money! Earn money by winning a new game.")
return render_template("market.html",
username=user,
money=money,
price=price
)
@app.route('/newhealth', methods=['POST', 'GET'])
def newhealth():
try:
user = session['user']
except:
return redirect('/')
user_info = db.findInfo('userInfo', user, 'username', fetchOne = True)
money = user_info[7]
health = user_info[4]
slots = user_info[3] * 100
if (slots == 900):
slots = "NOTHING YOU HAVE EVERYTHING ALREADY"
if money >= 100:
health += 1
db.modify('userInfo', 'healthUpgrade', health, 'username', user)
db.modify('userInfo', 'money', money - 100, 'username', user)
money = money - 100
return render_template("market.html",
username=user,
money=money,
price=slots
)
else:
flash ("You don't have enough money! Earn money by winning a new game.")
return render_template("market.html",
username=user,
money=money,
price=slots
)
@app.route('/newattack', methods=['POST', 'GET'])
def newattack():
try:
user = session['user']
except:
return redirect('/')
user_info = db.findInfo('userInfo', user, 'username', fetchOne = True)
money = user_info[7]
attack = user_info[5]
slots = user_info[3] * 100
if (slots == 900):
slots = "NOTHING YOU HAVE EVERYTHING ALREADY"
if money >= 100:
attack += 1
db.modify('userInfo', 'attackUpgrade', attack, 'username', user)
db.modify('userInfo', 'money', money - 100, 'username', user)
money = money - 100
return render_template("market.html",
username=user,
money=money,
price=slots
)
else:
flash ("You don't have enough money! Earn money by winning a new game.")
return render_template("market.html",
username=user,
money=money,
price=slots
)
@app.route('/newspeed', methods=['POST', 'GET'])
def newspeed():
try:
user = session['user']
except:
return redirect('/')
user_info = db.findInfo('userInfo', user, 'username', fetchOne = True)
money = user_info[7]
speed = user_info[6]
slots = user_info[3] * 100
if (slots == 900):
slots = "NOTHING YOU HAVE EVERYTHING ALREADY"
if money >= 100:
speed += 1
db.modify('userInfo', 'speedUpgrade', speed, 'username', user)
db.modify('userInfo', 'money', money - 100, 'username', user)
money = money - 100
return render_template("market.html",
username=user,
money=money,
price=slots
)
else:
flash ("You don't have enough money! Earn money by winning a new game.")
return render_template("market.html",
username=user,
money=money,
price=slots
)
if __name__ == "__main__":
app.debug = True
app.run()