forked from rSimulate/Cosmosium
/
app.py
512 lines (434 loc) · 17.3 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#!/usr/bin/env python
#=====================================#
# COSMOSIUM #
#=====================================#
# SpaceAppChallenge 2014 #
# #
# Daniel Andersen #
# Tom Riecken #
# Tylar Murray #
# Britton Broderick #
# Max Howeth #
# David Gundry #
# #
#=====================================#
__author__ = 'rsimulate'
#=====================================#
# Library Imports #
#=====================================#
# Primary Components
import os
import sqlite3 as lite
import sys
import json
import string
import random
import pymongo # import Connection
from py.lib.bottle.bottle import static_file, template, request, Bottle, response, redirect, abort
# OAuth components
import rauth
import config
# websockets:
from geventwebsocket import WebSocketError
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.getUser import getProfile, 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
from py.getAsteroid import asterankAPI
#=====================================#
# GLOBALS #
#=====================================#
app = Bottle()
CHUNKS = chunks() # static chunks or strings for the site
DOMAIN = 'localhost:7099' # 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)
# initial write of JSON files (to clear out old ones):
GAMES.games[0].OOIs.write2JSON(Settings('default').asteroidDB, Settings('default').ownersDB)
#=====================================#
# 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 js_static(filename):
return static_file(filename, root='./fonts/')
@app.route('/img/<filename:path>')
def js_static(filename):
return static_file(filename, root='./img/')
@app.route('/db/<filename:path>')
def js_static(filename):
return static_file(filename, root='./db/')
#=====================================#
# Routing Helper Functions #
#=====================================#
def getLoggedInUser(request):
'''
returns user object if logged in, else returns None
'''
if request.get_cookie("cosmosium_login"):
userLoginToken = request.get_cookie("cosmosium_login")
try:
return USERS.getUserByToken(userLoginToken)
except (KeyError, ReferenceError) as E: # user token not found or user has been garbage-collected
return None
else: return None
#=====================================#
# Custom Error Handles #
#=====================================#
@app.error(404)
def error404(error):
_user = getLoggedInUser(request)
if _user != None:
return template('tpl/pages/404',
chunks=CHUNKS,
user=_user,
config=Settings(MASTER_CONFIG, showBG=False),
pageTitle="LOST IN SPACE")
else:
redirect('/userLogin')
@app.error(500)
def error500(error):
print '500 error getting ', request.url, ':', response.body
return "oops! something 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>."
#=====================================#
# game content #
#=====================================#
@app.route('/content')
def makeContentHTML():
name=request.query.name # content page name
subDir = request.query.section=request.query.section # specific section of page (like type of research page)
# check for user login token in cookies
if request.get_cookie("cosmosium_login"):
userLoginToken = request.get_cookie("cosmosium_login")
try:
_user = USERS.getUserByToken(userLoginToken)
except (KeyError, ReferenceError) as E: # user token not found or user has been garbage-collected
redirect('/userLogin')
treeimg=''
if name == 'research': # then get research subDir info
if subDir=='spaceIndustry':
treeimg="img/space_industry_tech_tree_images.svg";
elif subDir=='humanHabitation':
treeimg="img/space_industry_tech_tree.svg";
elif subDir=='roboticsAndAI':
treeimg="img/space_industry_tech_tree_images.svg";
else:
return template('tpl/content/404') # error404('404')
fileName='tpl/content/'+name
if os.path.isfile(fileName+'.tpl'): #if file exists, use it
return template(fileName,
tree_src=treeimg, #used only for research pages
chunks=CHUNKS,
user=_user,
oois=GAMES.games[0].OOIs,
config=Settings(MASTER_CONFIG),
pageTitle=name)
else: # else show content under construction
print 'unknown content request: '+fileName
return template('tpl/content/under_construction')
else:
redirect('/userLogin')
#=====================================#
# Splash Page #
#=====================================#
@app.route("/")
def makeSplash():
return template('tpl/pages/splash', gameList=GAMES, demoIDs=demoIDs)
#=====================================#
# main gameplay page #
#=====================================#
@app.route("/play")
def makeGamePage():
# check for user login token in cookies
if request.get_cookie("cosmosium_login"):
userLoginToken = request.get_cookie("cosmosium_login")
try:
_user = USERS.getUserByToken(userLoginToken)
except (KeyError, ReferenceError) as E: # user token not found or user has been garbage-collected
return userLogin('user token not found')
if _user.game == None:
GAMES.joinGame(_user)
return template('tpl/pages/play',
chunks=CHUNKS,
user=_user,
oois=GAMES.games[0].OOIs,
config=Settings(MASTER_CONFIG),
pageTitle="Cosmosium Asteriod Ventures!")
else: return userLogin('user login cookie not found')
#=====================================#
# js #
#=====================================#
@app.route("/resourceUpdate.js")
def makeResourceUpdater():
# check for user login token in cookies
_user = getLoggedInUser(request)
if _user != None:
return template('tpl/js/resourceUpdate', user=_user)
else:
redirect('/userLogin')
@app.route("/webSocketSetup.js")
def makeSocketSetup():
return template('tpl/js/webSocketSetup', client_id='0', DOMAIN=DOMAIN)
# NOTE: this next approach is better than using the real file... but not working currently.
#@app.route("/js/game_frame_nav.js")
#def makeFrameNav():
# content_files = [fname.split('.')[0] for fname in os.listdir('tpl/content/')] # the files w/o links cause issue here...
# linked_content_files = ['dash','systemView','missionControl','launchpad','observatories','timeline','neos','mainBelt','kuiperBelt','spaceIndustry','humanHabitation','roboticsAndAI','launchSys','resMarket','fuelNet','spaceTourism','outreach','gov','org']
# return template('tpl/js/game_frame_nav',
# contentFiles=linked_content_files)
#=====================================#
# 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()
mesDict = eval(str(message))
try:
gameID = mesDict['gID']
userID = mesDict['uID']
cmd = mesDict['cmd']
data = mesDict['dat']
except KeyError:
print 'malformed message!'
except TypeError as e:
if e.message == "'NoneType' object has no attribute '__getitem__'":
# it's likely that pesky onclose message I can't fix... ignore for now
print 'connection closed'
else:
raise
# TODO: call message parser sort of like:
#game_manager.parseMessage(message,wsock)
# NOTE: message parser should probably be an attribute of the game
webSocketParser.parse(cmd, data, USERS.getUserByToken(userID), wsock, GAMES.games[0].OOIs)
print "received :",cmd,'from',userID
except WebSocketError:
print 'websocketerror encountered'
break
#=====================================#
# Asteroid Views Routing #
#=====================================#
@app.route('/searchtest')
def searchTest():
return template('tpl/searchView',asteroidDB="db/test_asteroids.js")
@app.route('/searchNEOs')
def searchNEOs():
return template('tpl/searchView',asteroidDB="db/NEOs.js")
@app.route('/searchMainBelt')
def searchMains():
return template('tpl/searchView',asteroidDB="db/MainBelt.js")
@app.route('/searchKuiperBelt')
def searchKuipers():
return template('tpl/searchView',asteroidDB="db/KuiperBelt.js")
@app.route('/systemView')
def systemView():
_user = getLoggedInUser(request)
if _user != None:
return template('tpl/systemView',
user=_user,
chunks=CHUNKS,
config=Settings('default',showFrame=False,showResources=False,showBG=False,controlBG=True),
pageTitle="system View"
)
else:
redirect('/userLogin')
@app.route('/viewTest')
def systemView():
return template('tpl/systemView',
user=User(),
chunks=CHUNKS,
config=Settings('test',showFrame=False,showResources=False,showBG=False,controlBG=True),
pageTitle="ViewTest"
)
@app.route('/getAsteroids')
def getOOIs():
GAMES.games[0].OOIs.write2JSON(Settings('default').asteroidDB, Settings('default').ownersDB)
return Settings(MASTER_CONFIG).asteroidDB
@app.route('/asteroidReq')
def processReq():
q = request.query.query
lim = request.query.limit
print 'q=',q,' l=',lim
return template('type: {{datatype}} (response {{res}})', datatype="asterank", res=asterankAPI(q,lim))
#=====================================#
# SQLite for Basic UI Data #
#=====================================#
# SQLite test
@app.route('/data')
def database():
con = lite.connect('test.db')
with con:
cur = con.cursor()
cur.execute('SELECT SQLITE_VERSION()')
data = cur.fetchone()
return "SQLite version: %s" % data
#=====================================#
# MONGO DB for State Storage #
#=====================================#
### Testing Mongo with this Example:
### https://github.com/mongolab/mongodb-driver-examples/blob/master/py/pymongo_simple_example.py
### DEFAULT Mongo Path & Port
### MongoDB starting : pid=1160 port=27017 dbpath=/data/db/
### Change with arguments found here:
### http://docs.mongodb.org/manual/tutorial/manage-mongodb-processes/
SEED_DATA = [
{
'technology': 'Hydrazine',
'tech_type': 'Propulsion',
'parts_enabled':'Hydrazine Engine',
'research_level': 1,
'sci_pts_cost': 10,
},
{
'technology': 'Ionized Propulsion',
'tech_type': 'Propulsion',
'parts_enabled':'Xenon Thruster',
'research_level': 2,
'sci_pts_cost': 20,
},
]
### Standard URI format: mongodb://[dbuser:dbpassword@]host:port/dbname
# db.addUser("spacecaptain", "5P@C3")
MainDB = 'localhost:27020/MainDB'
MONGODB_URI = 'mongodb://carl:sagan@' + MainDB
#connection = Connection()
###############################################################################
# main ?
###############################################################################
def main(args):
client = pymongo.MongoClient(MONGODB_URI)
db = client.get_default_database()
# First we'll add a few songs. Nothing is required to create the songs
# collection; it is created automatically when we insert.
techtree = db['TechTree']
# Note that the insert method can take either an array or a single dict.
techtree.insert(SEED_DATA)
query = {'tech_type': 'Propulsion'}
techtree.update(query, {'$set': {'parts_enabled': 'Rocket Engine'}})
# Running a query
techlist = techtree.find({'sci_pts_cost': {'$gte': 10}}).sort('research_level', 1)
for tech in techlist:
print ('You can research %s in the %s category, at research level %s for %d science points.' %
(tech['technology'], tech['tech_type'], tech['research_level'], tech['sci_pts_cost']))
### If you wanted to drop this table
#db.drop_collection('TechTree')
### Only close the connection when your app is terminating
client.close()
#=====================================#
# 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 userLogin(specialMessage=''):
return template('tpl/pages/userLogin', demoIDs=demoIDs, message=specialMessage)
@app.post('/loggin')
def setLoginCookie():
uid = request.forms.get('userid')
pw = request.forms.get('password')
rem = request.forms.get('remember_me')
_user = USERS.getUserByName(uid) # TODO: replace this with db lookup
if _user: # if user has existing login
if uid in demoIDs or False: # TODO: replace this false with password check
loginToken = uid+"loginToken"+''.join(random.choice(string.ascii_letters + string.digits) for _ in range(5))
userObj = getProfile(uid)
try:
USERS.addUser(userObj,loginToken)
except ValueError as e:
print e.message
response.set_cookie("cosmosium_login",loginToken,max_age=60*60*5)
redirect('/play')
else:
return userLogin('user not found')
# I'm not sure how to use this... =( ~Tylar
@app.route('/login<:re:/?>')
def login():
params = dict(
scope='email profile',
response_type='code',
redirect_uri=redirect_uri
)
url = google.get_authorize_url(**params)
app.redirect(url)
# Successful login
@app.route('/success<:re:/?>')
def login_success():
code = app.request.params.get('code')
session = google.get_auth_session(
data=dict(
code=code,
redirect_uri=redirect_uri,
grant_type='authorization_code'
),
decoder=json.loads
)
json_path = 'https://www.googleapis.com/oauth2/v1/userinfo'
session_json = session.get(json_path).json()
return 'Welcome {name}!'.format(**session_json)
# Unfortunately, there seem to be no reference for complete list of keys,
# but here are the complete json keys returned by the scope: email profile:
# * email
# * family_name
# * gender
# * given_name
# * id
# * link
# * locale
# * name
# * picture
#app.run(
# server=config.SERVER,
# port=config.PORT,
# host=config.HOST
#)
#=====================================#
# WEB SERVER START #
#=====================================#
if __name__ == "__main__":
try:
# app.catchall = True # Now most exceptions are re-raised within bottle.
port = int(os.environ.get("PORT", 7099))
#run(host='0.0.0.0', port=port)
server = WSGIServer(("0.0.0.0", port), app,
handler_class=WebSocketHandler)
print 'starting server on '+str(port)
server.serve_forever()
print 'code after this point never executes.'
main(sys.argv[1:]) # Invokes Mongo
finally:
print 'shutting down...'
# do all your destructing here, the server is going down.