-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.py
362 lines (297 loc) · 11.4 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env python
# =====================================#
# COSMOSIUM #
#=====================================#
# SpaceAppChallenge 2014 #
# #
# Tom Riecken #
# Tylar Murray #
# Brian Erikson #
# Martin Kronberg #
# Daniel Andersen #
# Max Howeth #
# David Gundry #
# #
#=====================================#
__author__ = 'rsimulate'
#=====================================#
# Library Imports #
#=====================================#
# std lib components:
import os
import string
import random
# bottle:
from bottle import static_file, template, request, Bottle, response, redirect, abort
# OAuth components:
import rauth
# web sockets:
import geventwebsocket
from gevent.pywsgi import WSGIServer
from geventwebsocket.handler import WebSocketHandler
import py.webSocketParser as webSocketParser
# Template Components
from py.page_maker.chunks import chunks # global chunks
from py.page_maker.Settings import Settings
# ui handlers:
from py.query_parsers.DemoUsers import getDemoProfile, demoIDs
# game logic:
from py.game_logic.user.User import User
from py.game_logic.GameList import GameList
from py.game_logic.UserList import UserList
# server setup settings:
import config
#=====================================#
# GLOBALS #
#=====================================#
app = Bottle()
CHUNKS = chunks() # static chunks or strings for the site
DOMAIN = config.DOMAIN # domain name
GAMES = GameList() # list of ongoing games on server
GAMES.unpickle() # restores any games that were saved last time server shut down
USERS = UserList() # list of users on the server TODO: replace use of this w/ real db.
MASTER_CONFIG = 'default' # config keyword for non-test pages. (see Config.py for more info)
loginTokens = []
#=====================================#
# Static Routing #
#=====================================#
@app.route('/css/<filename:path>')
def css_static(filename):
return static_file(filename, root='./css/')
@app.route('/js/<filename:path>')
def js_static(filename):
return static_file(filename, root='./js/')
@app.route('/fonts/<filename:path>')
def font_static(filename):
return static_file(filename, root='./fonts/')
@app.route('/img/<filename:path>')
def img_static(filename):
return static_file(filename, root='./img/')
@app.route('/db/<filename:path>')
def db_static(filename):
return static_file(filename, root='./db/')
@app.route('/models/<filename:path>')
def po_static(filename):
return static_file(filename, root='./models/')
#=====================================#
# dynamic js files #
#=====================================#
@app.route("/tpl/js/<filename>")
def get_js_template(filename):
# check for user login token in cookies
_user = get_user(request)
if _user is not None:
# try:
return template('tpl/js/' + filename, user=_user, DOMAIN=DOMAIN)
# except:
# print 'error getting template "'+str(filename)+'"!'
# return 404
else:
redirect('/userLogin')
#=====================================#
# Routing Helper Functions #
#=====================================#
def get_user(req):
"""
:param user_name: user name passed via query string (used only for test cases to circumnavigate login cookies)
:returns: user object if logged in, else redirects to login page
"""
if req.get_cookie("cosmosium_login"):
user_login_token = req.get_cookie("cosmosium_login")
try:
return USERS.getUserByToken(user_login_token)
except (KeyError, ReferenceError): # user token not found or user has been garbage-collected
pass
# if user was not found
if req.query.user == 'admin': # check for test users
user = USERS.getUserByName("admin_test_user")
set_login_cookie(user, "admin_test_user", None, None)
return user
# if not a normal user and not a test user
else:
redirect('/userLogin')
#=====================================#
# Custom Error Handles #
#=====================================#
@app.error(404)
def error404(error):
print error
return template('tpl/pages/404',
chunks=CHUNKS,
user=get_user(request),
config=Settings(MASTER_CONFIG, showBG=False),
pageTitle="LOST IN SPACE")
@app.error(500)
def error500(error):
print error
print '500 error getting ', request.url, ':', response.body
return "Oops! Something must've broke. We've logged the error. \
If you want to help us sort it out, please visit \
<a href='https://github.com/rSimulate/Cosmosium/issues'>our issue tracker on github</a>."
#=====================================#
# Splash Page #
#=====================================#
@app.route("/")
def make_splash():
return template('tpl/pages/splash', gameList=GAMES, demoIDs=demoIDs)
#=====================================#
# main gameplay page #
#=====================================#
@app.route("/play")
@app.route("/play/")
def make_game_page():
_user = get_user(request)
_user.disconnected = False
if _user.game is None:
GAMES.joinGame(_user)
return template('tpl/pages/play',
chunks=CHUNKS,
user=_user,
oois=GAMES.games[0].OOIs,
config=Settings(MASTER_CONFIG),
pageTitle="Asteroid Ventures!")
#=====================================#
# web sockets #
#=====================================#
@app.route('/tests')
def display_test_page():
_user = get_user(request)
_user.disconnected = False
if _user.game is None:
GAMES.joinGame(_user)
return template('tests/test_list_site',
chunks=CHUNKS,
user=_user,
oois=GAMES.games[0].OOIs,
config=Settings(MASTER_CONFIG),
pageTitle="Asteroid Ventures!")
#=====================================#
# web sockets #
#=====================================#
@app.route('/websocket')
def handle_websocket():
wsock = request.environ.get('wsgi.websocket')
if not wsock:
abort(400, 'Expected WebSocket request.')
while True:
try:
message = wsock.receive()
message_dict = eval(str(message))
try:
game_id = message_dict['gID']
user_id = message_dict['uID']
cmd = message_dict['cmd']
data = message_dict['dat']
except KeyError:
print 'malformed message!'
except TypeError as e:
if e.message == "'NoneType' object has no attribute '__getitem__'":
if user_id is not None:
USERS.getUserByToken(user_id).signOut()
else:
raise
user = USERS.getUserByToken( user_id)
print "received :", cmd, 'from', user_id
webSocketParser.parse(cmd, data, user, wsock, USERS, GAMES.games[0].OOIs)
except geventwebsocket.WebSocketError:
print 'client disconnected'
break
#=====================================#
# OAUTH SECTION #
#=====================================#
# RAUTH Calls to config.py
oauth2 = rauth.OAuth2Service
google = oauth2(
client_id=config.GOOGLE_CLIENT_ID,
client_secret=config.GOOGLE_CLIENT_SECRET,
name='google',
authorize_url='https://accounts.google.com/o/oauth2/auth',
access_token_url='https://accounts.google.com/o/oauth2/token',
base_url='https://accounts.google.com/o/oauth2/auth',
)
redirect_uri = '{uri}:{port}/success'.format(
uri=config.GOOGLE_BASE_URI,
port=config.PORT
)
# Login Routing
@app.route('/userLogin')
def user_login(special_message=''):
return template('tpl/pages/userLogin', demoIDs=demoIDs, message=special_message)
@app.post('/loggin') # Path currently used solely for demoIDs
def submit_log_in():
uid = request.forms.get('userid')
pw = request.forms.get('password')
rem = request.forms.get('remember_me')
_user = USERS.getUserByName(uid)
set_login_cookie(_user, uid, pw, rem)
redirect('/play')
def set_login_cookie(_user, uid, pw, rem):
if _user: # if user has existing login (in python memory)
if uid in demoIDs or False: # TODO: replace this false with password check
login_token = uid + ''.join(
random.choice(string.ascii_letters + string.digits) for _ in range(5))
user_obj = getDemoProfile(uid)
try:
USERS.addUser(user_obj, login_token)
except ValueError as e:
print e.message
response.set_cookie("cosmosium_login", login_token, max_age=60 * 60 * 5)
# redirect('/play')
elif False: # if user is in database
# TODO: load user into USERS (python memory)
pass
else:
return user_login('user not found')
@app.post('/signup')
def setLoginCookie(): # Depreciated for now
uid = request.forms.get('userid')
pw = request.forms.get('password')
rpw = request.forms.get('repeat_password')
org = request.forms.get('org')
quote = request.forms.get('quote')
# Successful login
@app.post('/success')
def login_success():
token = request.forms.get('token')
session = rauth.OAuth2Session(config.GOOGLE_CLIENT_ID, config.GOOGLE_CLIENT_SECRET, token)
json = session.get('https://www.googleapis.com/oauth2/v1/userinfo').json()
if json is None:
return
def convert(inputt):
if isinstance(inputt, dict):
return {convert(key): convert(value) for key, value in inputt.iteritems()}
elif isinstance(inputt, list):
return [convert(element) for element in inputt]
elif isinstance(inputt, unicode):
return inputt.encode('utf-8')
else:
return input
json = convert(json)
name = json['name'].replace(" ", "_")
user = User(name, json['picture'], "NASA", "For the Benefit of All")
gameToken = user.name
gameToken = gameToken.replace(" ", "_")
USERS.addUser(user, gameToken)
response.set_cookie("cosmosium_login", gameToken, max_age=60 * 60 * 5)
loginTokens.append({'name': user.name, 'social_token': token, 'game_token': gameToken})
# now that we're logged in, send the user where they were trying to go, else to main page
target = request.query.target or '/play'
redirect(target)
@app.route('/signout')
def signout():
token = request.get_cookie('cosmosium_login')
user = USERS.getUserByToken(token)
user.signOut()
#=====================================#
# WEB SERVER START #
#=====================================#
if __name__ == "__main__":
try:
port = int(os.environ.get("PORT", config.PORT))
server = WSGIServer(("0.0.0.0", port), app,
handler_class=WebSocketHandler)
print 'starting server on ' + str(port)
server.serve_forever()
finally:
print 'shutting down...'
# do all your destructing here, the server is going down.