From 916374acbb88f26e31490ca5b329a09b25c80da3 Mon Sep 17 00:00:00 2001 From: Jan Behrens Date: Sat, 17 Aug 2013 12:33:01 +0200 Subject: [PATCH] Adding player_elo_info_text template for use within Xonotic (basic player info) --- xonstat/__init__.py | 5 ++- xonstat/templates/player_elo_info_text.mako | 19 +++++++++++ xonstat/views/__init__.py | 5 +-- xonstat/views/player.py | 35 ++++++++++++++++++++- 4 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 xonstat/templates/player_elo_info_text.mako diff --git a/xonstat/__init__.py b/xonstat/__init__.py index 449d3b9..49493d8 100644 --- a/xonstat/__init__.py +++ b/xonstat/__init__.py @@ -54,7 +54,10 @@ def main(global_config, **settings): config.add_route("player_info_json", "/player/{id:\d+}.json") config.add_view(player_info_json, route_name="player_info_json", renderer="jsonp") - config.add_route("player_elo_info_json", "/elo/{hashkey}") + config.add_route("player_elo_info_text", "/elo/{hashkey}") + config.add_view(player_elo_info_text, route_name="player_elo_info_text", renderer="player_elo_info_text.mako") + + config.add_route("player_elo_info_json", "/elo/{hashkey}.json") ## FIXME - doesn't seem to work? config.add_view(player_elo_info_json, route_name="player_elo_info_json", renderer="jsonp") config.add_route("player_accuracy", "/player/{id:\d+}/accuracy") diff --git a/xonstat/templates/player_elo_info_text.mako b/xonstat/templates/player_elo_info_text.mako new file mode 100644 index 0000000..b600045 --- /dev/null +++ b/xonstat/templates/player_elo_info_text.mako @@ -0,0 +1,19 @@ +V 1 +R XonStat/1.0 +T ${now} +S ${request.route_url('player_info', id=player.player_id)} +P ${hashkey} +n ${player.nick} +i ${player.player_id} +% if player.active_ind == True: +e active-ind 1 +% else: +e active-ind 0 +% endif +e location +% for game_type_cd in elos.keys(): +% if game_type_cd != 'overall': +G ${game_type_cd} +e elo ${elos[game_type_cd].elo} +% endif +% endfor diff --git a/xonstat/views/__init__.py b/xonstat/views/__init__.py index d88a139..5073d74 100644 --- a/xonstat/views/__init__.py +++ b/xonstat/views/__init__.py @@ -3,8 +3,9 @@ from xonstat.views.player import player_index, player_info, player_game_index from xonstat.views.player import player_accuracy from xonstat.views.player import player_index_json, player_info_json from xonstat.views.player import player_game_index_json, player_accuracy_json -from xonstat.views.player import player_damage_json, player_hashkey_info_json -from xonstat.views.player import player_hashkey_info_text, player_elo_info_json +from xonstat.views.player import player_damage_json +from xonstat.views.player import player_elo_info_text, player_elo_info_json +from xonstat.views.player import player_hashkey_info_text, player_hashkey_info_json from xonstat.views.player import player_captimes, player_captimes_json from xonstat.views.game import game_index, game_info, rank_index diff --git a/xonstat/views/player.py b/xonstat/views/player.py index fa8cde4..824b980 100644 --- a/xonstat/views/player.py +++ b/xonstat/views/player.py @@ -789,6 +789,7 @@ def player_damage_json(request): def player_hashkey_info_data(request): (idfp, status) = verify_request(request) + print "player_hashkey_info_data [idfp={0} status={1}]".format(idfp, status) # if config is to *not* verify requests and we get nothing back, this # query will return nothing and we'll 404. @@ -904,6 +905,7 @@ def player_elo_info_data(request): Provides elo information on a specific player. Raw data is returned. """ hashkey = request.matchdict['hashkey'] + print "player_elo_info_data [hashkey={0}]".format(hashkey) try: player = DBSession.query(Player).\ filter(Player.player_id == Hashkey.player_id).\ @@ -916,7 +918,11 @@ def player_elo_info_data(request): log.debug(e) raise pyramid.httpexceptions.HTTPNotFound - return {'elos':elos} + return { + 'hashkey':hashkey, + 'player':player, + 'elos':elos, + } def player_elo_info_json(request): @@ -925,15 +931,42 @@ def player_elo_info_json(request): """ elo_info = player_elo_info_data(request) + player = player_info['player'].to_dict() + elos = {} for gt, elo in elo_info['elos'].items(): elos[gt] = to_json(elo.to_dict()) return [{ 'version': 1, + 'player': player, 'elos': elos, }] + +def player_elo_info_text(request): + """ + Provides elo information on a specific player. Plain text. + """ + # UTC epoch + now = timegm(datetime.datetime.utcnow().timetuple()) + + # All player_info fields are converted into JSON-formattable dictionaries + elo_info = player_elo_info_data(request) + + # this is a plain text response, if we don't do this here then + # Pyramid will assume html + request.response.content_type = 'text/plain' + + return { + 'version': 1, + 'now': now, + 'hashkey': elo_info['hashkey'], + 'player': elo_info['player'], + 'elos': elo_info['elos'], + } + + def player_captimes_data(request): player_id = int(request.matchdict['id']) if player_id <= 2: -- 2.39.2