return "<Player(%s, %s)>" % (self.player_id, self.nick.encode('utf-8'))
def to_dict(self):
- return {'player_id':self.player_id, 'name':self.nick.encode('utf-8'),
+ return {'player_id':self.player_id, 'nick':self.nick,
'joined':self.create_dt.strftime('%Y-%m-%dT%H:%M:%SZ'),
'active_ind':self.active_ind, 'location':self.location,
- 'stripped_nick':self.stripped_nick.encode('utf-8'),
- 'nick_html_colors':self.nick_html_colors().encode('utf-8')}
+ 'stripped_nick':self.stripped_nick}
def epoch(self):
return timegm(self.create_dt.timetuple())
return "<Server(%s, %s)>" % (self.server_id, self.name.encode('utf-8'))
def to_dict(self):
- return {'server_id':self.server_id, 'name':self.name.encode('utf-8'),
+ return {'server_id':self.server_id, 'name':self.name,
'ip_addr':self.ip_addr, 'location':self.location}
def fuzzy_date(self):
return "<Map(%s, %s, %s)>" % (self.map_id, self.name, self.version)
def to_dict(self):
- return {'map_id':self.map_id, 'name':self.name, 'version':self.version,
- 'pk3_name':self.pk3_name, 'curl_url':self.curl_url}
+ return {'map_id':self.map_id, 'name':self.name, 'version':self.version,}
def fuzzy_date(self):
return pretty_date(self.create_dt)
return "<Game(%s, %s, %s, %s)>" % (self.game_id, self.start_dt, self.game_type_cd, self.server_id)
def to_dict(self):
- return {'game_id':self.game_id, 'start':self.start_dt.strftime('%Y-%m-%dT%H:%M:%SZ'), 'game_type_cd':self.game_type_cd, 'server_id':self.server_id}
+ return {'game_id':self.game_id, 'start':self.start_dt.strftime('%Y-%m-%dT%H:%M:%SZ'),
+ 'game_type_cd':self.game_type_cd, 'server_id':self.server_id}
def fuzzy_date(self):
return pretty_date(self.start_dt)
from cgi import escape as html_escape
from datetime import datetime, timedelta
from decimal import Decimal
+from collections import namedtuple
# Map of special chars to ascii from Darkplace's console.c.
_qfont_table = [
def datetime_seconds(td):
return float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
-def namedtuple_to_dict(tup):
- result = {}
- for key,value in tup._asdict().items():
- result[key] = value
- return result
-
-def fix_json_types(data):
+def to_json(data):
+ if not type(data) == dict:
+ # assume it's a named tuple
+ data = data._asdict()
result = {}
for key,value in data.items():
- if type(value) == Decimal:
+ if value == None:
+ result[key] = None
+ elif type(value) in [bool,int,long,float,complex,str]:
+ result[key] = value
+ elif type(value) == unicode:
+ result[key] = value.encode('utf-8')
+ elif type(value) == Decimal:
result[key] = float(value)
elif type(value) == datetime:
result[key] = value.strftime('%Y-%m-%dT%H:%M:%SZ')
elif type(value) == timedelta:
result[key] = datetime_seconds(value)
else:
- result[key] = value
+ print key,value
+ result[key] = to_json(value.to_dict())
return result
from sqlalchemy import desc, distinct
from webhelpers.paginate import Page, PageURL
from xonstat.models import *
-from xonstat.util import page_url, namedtuple_to_dict, fix_json_types
+from xonstat.util import page_url, to_json
log = logging.getLogger(__name__)
games_played = {}
for game in player_info['games_played']:
- games_played[game.game_type_cd] = namedtuple_to_dict(game)
+ games_played[game.game_type_cd] = to_json(game)
overall_stats = {}
for gt,stats in player_info['overall_stats'].items():
- overall_stats[gt] = fix_json_types(namedtuple_to_dict(stats))
+ overall_stats[gt] = to_json(stats)
elos = {}
for gt,elo in player_info['elos'].items():
- elos[gt] = fix_json_types(elo.to_dict())
+ elos[gt] = to_json(elo.to_dict())
ranks = {}
for gt,rank in player_info['ranks'].items():
- ranks[gt] = namedtuple_to_dict(rank)
+ ranks[gt] = to_json(rank)
fav_maps = {}
- for gt,stats in player_info['fav_maps'].items():
- fav_maps[gt] = namedtuple_to_dict(stats)
+ for gt,mapinfo in player_info['fav_maps'].items():
+ fav_maps[gt] = to_json(mapinfo)
recent_games = []
for game in player_info['recent_games']:
- entry = {}
- for key,value in namedtuple_to_dict(game).items():
- entry[key] = fix_json_types(value.to_dict())
- recent_games.append(entry)
+ recent_games.append(to_json(game))
recent_weapons = player_info['recent_weapons']