def get_json_tally(req, raw=False): tally = _read_tally(req) for ent in tally["scoreboard"]: prident = profile.fetch_pridfile_as_dict(ent["prid"]) if prident == None: prident = profile.NULL_PROFILE ent["shortname"] = prident["shortname"] ent["longname"] = prident["longname"] if "_general" in modulemap.gamemap[req["gamename"]]: if "dispunit" in modulemap.gamemap[req["gamename"]]["_general"]: ent["dispunit"] = modulemap.gamemap[req["gamename"]]["_general"]["dispunit"] del ent["prid"] if raw: req["_bindata"] = tally else: req["_bindata"] = json.dumps(tally) req["_binlen"] = len(req["_bindata"]) return
def optional_prepare_template ( req ): return # forget it for now logging.info ( "dkong does attempt to modify template prior to sending to client..." ) # fetch tally tally = singlescore_handler._read_tally ( req ) if tally [ 'scoreboard' ][ 0 ][ 'prid' ] != '_default_': # theres a real score here pridfile = profile.fetch_pridfile_as_dict ( tally [ 'scoreboard' ][ 0 ][ 'prid' ] ) if not pridfile: return if not pridfile [ 'shortname' ].isdigit(): return # imprint top initials req [ '_bindata' ][ 7 + 8 ] = encode_char ( pridfile [ 'shortname' ][ 0 ] ) req [ '_bindata' ][ 7 + 9 ] = encode_char ( pridfile [ 'shortname' ][ 1 ] ) req [ '_bindata' ][ 7 + 10 ] = encode_char ( pridfile [ 'shortname' ][ 2 ] )
def do_GET( self ): #logging.debug ( "do_GET vars: %s" % ( vars ( self ) ) ) logging.debug ( "GET against path '%s'" % ( self.path ) ) # parse out ?key=value sillyness if '?' in self.path: try: self.path, crap = self.path.split ( '&', 2 ) except: pass self.path, query = self.path.split ( '?', 2 ) self._query = dict() key, value = query.split ( '=', 2 ) self._query [ key ] = value # do we have a JSONP request? jsonp_pre = '' jsonp_post = '' try: if 'jsonp' in self._query: jsonp_pre = self._query [ 'jsonp' ] + '(' jsonp_post = ');' except: pass req = dict() try: paths = self.path.split ( "/", 6 ) try: basepage, basepage_ver = paths [ 1 ].split ( '_', 2 ) req [ 'ver' ] = basepage_ver except: logging.debug ( "Missing _ver: vars: %s" % ( vars ( self ) ) ) basepage = paths [ 1 ] req [ 'basepage' ] = basepage req [ 'gamename' ] = paths [ 2 ] except: try: basepage, basepage_ver = paths [ 1 ].split ( '_', 2 ) req [ 'ver' ] = basepage_ver except: logging.debug ( "Missing _ver: vars: %s" % ( vars ( self ) ) ) basepage = paths [ 1 ] req [ 'basepage' ] = basepage logging.debug ( "request looks like GET %s" % ( req ) ) if not self.is_valid_request ( req ): self.send_response ( 406 ) # not acceptible return if req [ 'basepage' ] == 'banner': d = dict() f = open ( "runtime/banner/banner.txt", 'r' ) text = f.read() f.close() d [ 'banner' ] = text bindata = json.dumps ( d ) self.send_response ( 200 ) # okay; the following is the right header sequence self.send_header ( 'Content-type', 'application/json; charset=utf-8' ) self.send_header ( 'Content-length', len ( bindata ) ) self.end_headers() self.wfile.write ( bindata ) elif req [ 'basepage' ] == 'ohai': d = dict() d [ 'status' ] = 'OK' bindata = json.dumps ( d ) self.send_response ( 200 ) # okay; the following is the right header sequence self.send_header ( 'Content-type', 'application/json; charset=utf-8' ) self.send_header ( 'Content-length', len ( bindata ) ) self.end_headers() self.wfile.write ( bindata ) elif req [ 'basepage' ] == 'links': now = datetime.datetime.now() lastmonth = dates.subtract_one_month ( now ) html = '' html += '<br/>\n' html += 'Recent score submissions: <a href="%s">here</a></td>\n' % ( config.get ( 'WhereAmI', 'displayhost' ) + 'activity_1/' ) html += '<br/>\n' html += '<br/>\n' html += '<table>\n' html += ' <tr>\n' html += ' <td style="padding:0 15px 0 15px;"><b>Game</b></td>\n' html += ' <td style="padding:0 15px 0 15px;"><b>Last Month</b></td>\n' html += ' <td style="padding:0 15px 0 15px;"><b>Current Month So Far</b></td>\n' html += ' <td style="padding:0 15px 0 15px;"><b>All Time</b></td>\n' html += ' </tr>\n' for k,mm in modulemap.gamemap.iteritems(): if mm [ 'status' ] in ( 'available', 'active' ): html += ' <tr>\n' html += ' <td style="padding:0 15px 0 15px;">%s</td>\n' % ( mm [ 'longname' ] ) html += ' <td style="padding:0 15px 0 15px;"><a href="%s">here</a></td>\n' % ( config.get ( 'WhereAmI', 'displayhost' ) + 'scoreboard_1/' + mm [ 'gamename' ] + '/' + str(lastmonth.year) + str('%02d'%lastmonth.month) + '/' ) html += ' <td style="padding:0 15px 0 15px;"><a href="%s">here</a></td>\n' % ( config.get ( 'WhereAmI', 'displayhost' ) + 'scoreboard_1/' + mm [ 'gamename' ] + '/' ) html += ' <td style="padding:0 15px 0 15px;"><a href="%s">here</a></td>\n' % ( config.get ( 'WhereAmI', 'displayhost' ) + 'scoreboard_1/' + mm [ 'gamename' ] + '/ALL' ) html += ' </tr>\n' self.send_response ( 200 ) # okay; the following is the right header sequence self.send_header ( 'Content-type', 'text/html; charset=utf-8' ) self.send_header ( 'Content-length', len ( html ) ) self.end_headers() self.wfile.write ( html ) elif req [ 'basepage' ] == 'curgamelist': gl = list() for k,mm in modulemap.gamemap.iteritems(): if mm [ 'status' ] in ( 'available', 'active' ): req = dict() req [ 'gamename' ] = mm [ 'gamename' ] req [ 'longname' ] = mm [ 'longname' ] req [ 'status' ] = mm [ 'status' ] req [ 'genre' ] = mm [ 'genre' ] req [ 'field' ] = mm [ 'field' ] req [ '_last_tally_update_e' ] = mm [ '_last_tally_update_e' ] if '_general' in modulemap.gamemap [ req [ 'gamename' ] ]: if 'dispunit' in modulemap.gamemap [ req [ 'gamename' ] ][ '_general' ]: req [ 'dispunit' ] = modulemap.gamemap [ req [ 'gamename' ] ] [ '_general' ][ 'dispunit' ] t = mm [ 'handler' ]._read_tally ( req ) try: req [ '_top_score' ] = t [ 'hi' ] pridfile = profile.fetch_pridfile_as_dict ( t [ 'prid' ] ) req [ '_top_shortname' ] = pridfile [ 'shortname' ] req [ '_top_longname' ] = pridfile [ 'longname' ] except: req [ '_top_score' ] = 0 req [ '_top_shortname' ] = '' req [ '_top_longname' ] = '' gl.append ( req ) d = dict() d [ 'gamelist' ] = gl bindata = jsonp_pre + json.dumps ( d ) + jsonp_post self.send_response ( 200 ) # okay; the following is the right header sequence self.send_header ( 'Access-Control-Allow-Origin', '*' ) # milkshake: http://enable-cors.org/ self.send_header ( 'Content-type', 'application/json; charset=utf-8' ) self.send_header ( 'Content-length', len ( bindata ) ) self.end_headers() self.wfile.write ( bindata ) elif req [ 'basepage' ] == 'json': if len ( paths ) >= 4 and paths [ 3 ]: if paths [ 3 ].isdigit(): req [ '_backdate' ] = paths [ 3 ] logging.info ( "Request is backdated; now looks like %s" % ( req ) ) else: req [ '_backdate' ] = 'ALLTIM' logging.info ( "Request is for all-time; now looks like %s" % ( req ) ) if req [ 'gamename' ] != 'ALL' and not self.is_valid_game ( req ): self.send_response ( 406 ) # not acceptible return self.send_response ( 200 ) # okay; the following is the right header sequence self.send_header ( 'Access-Control-Allow-Origin', '*' ) # milkshake: http://enable-cors.org/ self.send_header ( 'Content-type', 'application/json; charset=utf-8' ) self.end_headers() if req [ 'gamename' ] == 'ALL': # iterate them all aggregate = dict() for aname in modulemap.gamemap: req [ 'gamename' ] = aname modulemap.gamemap [ req [ 'gamename' ] ][ 'handler' ].get_json_tally ( req, raw=True ) aggregate [ aname ] = req [ '_bindata' ] req [ '_bindata' ] = json.dumps ( aggregate ) req [ '_binlen' ] = len ( req [ '_bindata' ] ) else: modulemap.gamemap [ req [ 'gamename' ] ][ 'handler' ].get_json_tally ( req ) self.wfile.write ( jsonp_pre + req [ '_bindata' ] + jsonp_post ) elif req [ 'basepage' ] == 'execinfo': # /execinfo/GAMENAME/PLATFORM if len ( paths ) >= 4 and paths [ 3 ]: if paths [ 3 ] == 'pandora': logging.debug ( "Request is for pandora execinfo; looks like %s" % ( req ) ) else: logging.error ( "Request is for unknown platform execinfo; looks like %s" % ( req ) ) self.send_response ( 406 ) # not acceptible return if not self.is_valid_game ( req ): logging.error ( "Request is for a bad gamename %s" % ( req ) ) self.send_response ( 406 ) # not acceptible return # TODO: this should be abstracted into a platform handler, or source handler, or something.. try: req [ '_bindata' ] = json.dumps ( modulemap.gamemap [ req [ 'gamename' ] ][ '_general' ][ 'execinfo' ][ paths [ 3 ] ] ) except: req [ '_bindata' ] = json.dumps ( { "status": "ERROR in server configuration" } ) self.send_response ( 200 ) self.send_header ( 'Content-type', 'text/html' ) self.send_header ( 'Content-length', len ( req [ '_bindata' ] ) ) self.end_headers() self.wfile.write ( req [ '_bindata' ] ) elif req [ 'basepage' ] == 'scoreboard': if len ( paths ) >= 4 and paths [ 3 ]: if paths [ 3 ].isdigit(): req [ '_backdate' ] = paths [ 3 ] logging.info ( "Request is backdated; now looks like %s" % ( req ) ) else: req [ '_backdate' ] = 'ALLTIM' logging.info ( "Request is for all-time; now looks like %s" % ( req ) ) if not self.is_valid_game ( req ): logging.error ( "Request is for a bad gamename %s" % ( req ) ) self.send_response ( 406 ) # not acceptible return if req [ 'gamename' ] in modulemap.gamemap: modulemap.gamemap [ req [ 'gamename' ] ][ 'handler' ].get_html_tally ( req ) self.send_response ( 200 ) self.send_header ( 'Content-type', 'text/html' ) self.send_header ( 'Content-length', len ( req [ '_bindata' ] ) ) self.end_headers() self.wfile.write ( req [ '_bindata' ] ) #self.send_response ( 200 ) # okay else: self.send_response ( 406 ) # not acceptible logging.error ( "No module found for game %s" % ( gamename ) ) return elif req [ 'basepage' ] == 'activity': activity_log.get_log_html ( req ) self.send_response ( 200 ) self.send_header ( 'Content-type', 'text/html' ) self.send_header ( 'Content-length', len ( req [ '_bindata' ] ) ) self.end_headers() self.wfile.write ( req [ '_bindata' ] ) elif req [ 'basepage' ] == 'activityjson': logging.info ( "Fetching activity log" ) j = activity_log.get_log_json ( req ) data = jsonp_pre + j + jsonp_post req [ '_bindata' ] = data self.send_response ( 200 ) self.send_header ( 'Content-type', 'text/html' ) self.send_header ( 'Content-length', len ( req [ '_bindata' ] ) ) self.end_headers() self.wfile.write ( req [ '_bindata' ] ) elif req [ 'basepage' ] == 'hi': if not self.is_valid_game ( req ): self.send_response ( 406 ) # not acceptible return if req [ 'gamename' ] in modulemap.gamemap: modulemap.gamemap [ req [ 'gamename' ] ][ 'handler' ].get_hi ( req ) self.send_response ( 200 ) self.send_header ( 'Content-length', req [ '_binlen' ] ) self.end_headers() self.wfile.write ( req [ '_bindata' ] ) else: self.send_response ( 406 ) # not acceptible logging.error ( "No module found for game %s" % ( gamename ) ) return else: self.send_response ( 406 ) # not acceptible return
def get_html_tally(req): tally = _read_tally(req) if "_backdate" in req: if req["_backdate"].isdigit(): timeframe = "Specific Month: " + req["_backdate"] else: timeframe = "All Time" else: timeframe = "Current Month" html = "" html += "<h2>" + req["gamename"] + "</h2>\n" html += "<h3>" + timeframe + "</h3>\n" html += "<table>\n" html += "<tr>\n" html += ' <td style="padding:0 15px 0 15px;"><b>Rank</b></td>\n' html += ' <td style="padding:0 15px 0 15px;"><b>Initial</b></td>\n' html += ' <td style="padding:0 15px 0 15px;"><b>Name</b></td>\n' html += ' <td style="padding:0 15px 0 15px;"><b>Score</b></td>\n' html += ' <td style="padding:0 15px 0 15px;"><b>When</b></td>\n' html += "</tr>\n" i = 1 pridcache = dict() lastprident = None lastrun = 0 # for an RLE-like run count for ent in tally["scoreboard"]: prident = None if ent["prid"]: try: prident = pridcache[ent["prid"]] except: prident = profile.fetch_pridfile_as_dict(ent["prid"]) pridcache[ent["prid"]] = prident if prident == None: prident = profile.NULL_PROFILE tlocal = time.localtime(ent["time"]) tdisplay = time.strftime("%d-%b-%Y", tlocal) # units unit = "" if "_general" in modulemap.gamemap[req["gamename"]]: if "dispunit" in modulemap.gamemap[req["gamename"]]["_general"]: unit = " " + str(modulemap.gamemap[req["gamename"]]["_general"]["dispunit"]) showrow = 1 # 0 no, 1 yes, 2 ellipses if False: # True -> force to full length display lastprident = None # if uncommented, forces full display .. no ellipses hidden entries if lastprident == prident: showrow = 0 lastrun += 1 else: # if not first row, and the RLE is significant .. show an ellipses if lastprident != None and lastrun > 0: showrow = 2 else: showrow = 1 # last and current are not the same, so RLE is back to zero lastrun = 0 if showrow == 0: pass # suppress else: if showrow == 2: # so our last row is not same as this row, and last guy was not also the first # row.. so show "..." html += "<tr>\n" html += ' <td style="padding:0 15px 0 15px;">' + "" + "</td>\n" html += ' <td style="padding:0 15px 0 15px;">' + "" + "</td>\n" html += ' <td style="padding:0 15px 0 15px;">' + "..." + "</td>\n" html += ' <td style="padding:0 15px 0 15px;"></td>\n' html += ' <td style="padding:0 15px 0 15px;"></td>\n' html += "</tr>\n" # showrow == 1, or showrow == 2 .. show this line html += "<tr>\n" html += ' <td style="padding:0 15px 0 15px;">' + str(i) + "</td>\n" html += ' <td style="padding:0 15px 0 15px;">' + prident["shortname"] + "</td>\n" html += ' <td style="padding:0 15px 0 15px;">' + prident["longname"] + "</td>\n" if ent["score"] > 0: html += ' <td style="padding:0 15px 0 15px;">' + str(ent["score"]) + unit + "</td>\n" else: html += ' <td style="padding:0 15px 0 15px;">-</td>\n' if ent["time"] > 0: html += ' <td style="padding:0 15px 0 15px;">' + tdisplay + "</td>\n" else: html += ' <td style="padding:0 15px 0 15px;"></td>\n' html += "</tr>\n" lastprident = prident i += 1 html += "</table>\n" html += "<p>%d unique profiles in the leaderboard</p>\n" % (len(pridcache)) req["_bindata"] = html req["_binlen"] = len(req["_bindata"]) return
def get_log_html ( req ): tally = _readlog ( req ) html = '' html += "<h2>Recent Activity</h2>\n" html += "<table>\n" html += '<tr>\n' html += ' <td style="padding:0 15px 0 15px;"><b>Initial</b></td>\n' html += ' <td style="padding:0 15px 0 15px;"><b>Name</b></td>\n' html += ' <td style="padding:0 15px 0 15px;"><b>Game</b></td>\n' html += ' <td style="padding:0 15px 0 15px;"><b>Score</b></td>\n' html += ' <td style="padding:0 15px 0 15px;"><b>Board Rank</b></td>\n' html += ' <td style="padding:0 15px 0 15px;"><b>When</b></td>\n' html += ' <td style="padding:0 15px 0 15px;"><b>Scoreboard</b></td>\n' html += '</tr>\n' i = 1 pridcache = dict() for ent in tally: if ent [ 'gamename' ] == '': break # end of table prident = None if ent [ 'prid' ]: try: prident = pridcache [ ent [ 'prid' ] ] except: prident = profile.fetch_pridfile_as_dict ( ent [ 'prid' ] ) pridcache [ ent [ 'prid' ] ] = prident if prident == None: prident = profile.NULL_PROFILE tlocal = time.localtime ( ent [ 'time' ] ) tdisplay = time.strftime ( '%d-%b-%Y', tlocal ) showrow = 1 # 0 no, 1 yes, 2 ellipses if showrow == 0: pass # suppress else: if showrow == 2: # so our last row is not same as this row, and last guy was not also the first # row.. so show "..." html += '<tr>\n' html += ' <td style="padding:0 15px 0 15px;">' + "" + "</td>\n" html += ' <td style="padding:0 15px 0 15px;">' + "" + "</td>\n" html += ' <td style="padding:0 15px 0 15px;">' + "..." + "</td>\n" html += ' <td style="padding:0 15px 0 15px;"></td>\n' html += ' <td style="padding:0 15px 0 15px;"></td>\n' html += ' <td style="padding:0 15px 0 15px;"></td>\n' html += '</tr>\n' # showrow == 1, or showrow == 2 .. show this line html += '<tr>\n' html += ' <td style="padding:0 15px 0 15px;">' + prident [ 'shortname' ] + "</td>\n" html += ' <td style="padding:0 15px 0 15px;">' + prident [ 'longname' ] + "</td>\n" try: html += ' <td style="padding:0 15px 0 15px;">' + modulemap.gamemap [ ent [ 'gamename' ] ][ 'longname' ] + "</td>\n" except: html += ' <td style="padding:0 15px 0 15px;">' + '(missing configuration for ' + ent [ 'gamename' ] + ')' + "</td>\n" if ent [ 'score' ] > 0: html += ' <td style="padding:0 15px 0 15px;">' + str ( ent [ 'score' ] ) + "</td>\n" else: html += ' <td style="padding:0 15px 0 15px;">-</td>\n' html += ' <td style="padding:0 15px 0 15px;">' + str(ent [ 'rank' ] + 1) + "</td>\n" if ent [ 'time' ] > 0: html += ' <td style="padding:0 15px 0 15px;">' + tdisplay + "</td>\n" else: html += ' <td style="padding:0 15px 0 15px;"></td>\n' try: if ent [ '_target' ] == 'MONTHLY': html += ' <td style="padding:0 15px 0 15px;">Monthly</td>\n' elif ent [ '_target' ] == 'ALLTIME': html += ' <td style="padding:0 15px 0 15px;">All Time</td>\n' except: html += ' <td style="padding:0 15px 0 15px;"></td>\n' html += '</tr>\n' i += 1 if i > 50: break html += "</table>\n" html += "<p>%d unique profiles in the log</p>\n" % ( len ( pridcache ) ) req [ '_bindata' ] = html req [ '_binlen' ] = len ( req [ '_bindata' ] ) return
def get_log_json ( req ): tally = _readlog ( req ) retlist = list() i = 1 pridcache = dict() for ent in tally: newent = dict() if ent [ 'gamename' ] == '': break # end of table prident = None if ent [ 'prid' ]: try: prident = pridcache [ ent [ 'prid' ] ] except: prident = profile.fetch_pridfile_as_dict ( ent [ 'prid' ] ) pridcache [ ent [ 'prid' ] ] = prident if prident == None: prident = profile.NULL_PROFILE showrow = 1 # 0 no, 1 yes, 2 ellipses if showrow == 0: pass # suppress else: # showrow == 1, or showrow == 2 .. show this line newent [ 'shortname' ] = prident [ 'shortname' ] newent [ 'longname' ] = prident [ 'longname' ] try: newent [ 'gamename' ] = modulemap.gamemap [ ent [ 'gamename' ] ][ 'longname' ] except: newent [ 'gamename' ] = '(missing)' if ent [ 'score' ] > 0: newent [ 'score' ] = ent [ 'score' ] else: newent [ 'score' ] = 0 newent [ 'rank' ] = ent [ 'rank' ] + 1 if ent [ 'time' ] > 0: newent [ 'time' ] = ent [ 'time' ] else: newent [ 'time' ] = '' try: newent [ '_target' ] = ent [ '_target' ] except: pass if '_general' in modulemap.gamemap [ ent [ 'gamename' ] ]: if 'dispunit' in modulemap.gamemap [ ent [ 'gamename' ] ][ '_general' ]: newent [ 'dispunit' ] = modulemap.gamemap [ ent [ 'gamename' ] ] [ '_general' ][ 'dispunit' ] retlist.append ( newent ) i += 1 if i > 50: break bindata = json.dumps ( retlist ) return bindata