import logging
import sqlalchemy.sql.functions as func
import sqlalchemy.sql.expression as expr
+from collections import namedtuple
from datetime import datetime, timedelta
-from pyramid.response import Response
from sqlalchemy import desc
from webhelpers.paginate import Page, PageURL
from xonstat.models import *
-from xonstat.util import page_url
+from xonstat.util import page_url, html_colors
+from xonstat.views.helpers import RecentGame, recent_games_q
log = logging.getLogger(__name__)
def map_index(request):
"""
- Provides a list of all the current maps.
+ Provides a list of all the current maps.
"""
return _map_index_data(request)
"""
Provides a JSON-serialized list of all the current maps.
"""
- view_data = _map_index_data(request)
+ view_data = _map_index_data(request)
maps = [m.to_dict() for m in view_data['maps']]
leaderboard_count = 10
recent_games_count = 20
+ # captime tuples
+ Captime = namedtuple('Captime', ['player_id', 'nick_html_colors',
+ 'fastest_cap', 'game_id'])
+
try:
gmap = DBSession.query(Map).filter_by(map_id=map_id).one()
- # recent games on this map
- recent_games = DBSession.query(Game, Server, Map, PlayerGameStat).\
- filter(Game.server_id==Server.server_id).\
- filter(Game.map_id==Map.map_id).\
- filter(Game.map_id==map_id).\
- filter(PlayerGameStat.game_id==Game.game_id).\
- filter(PlayerGameStat.rank==1).\
- order_by(expr.desc(Game.start_dt)).all()[0:recent_games_count]
+ # recent games played in descending order
+ rgs = recent_games_q(map_id=map_id).limit(recent_games_count).all()
+ recent_games = [RecentGame(row) for row in rgs]
# top players by score
top_scorers = DBSession.query(Player.player_id, Player.nick,
group_by(Server.name).\
group_by(Server.server_id).all()[0:leaderboard_count]
+ # top captimes
+ captimes_raw = DBSession.query(Player.player_id, Player.nick,
+ PlayerCaptime.fastest_cap, PlayerCaptime.game_id).\
+ filter(PlayerCaptime.map_id == map_id).\
+ filter(Player.player_id == PlayerCaptime.player_id).\
+ order_by(PlayerCaptime.fastest_cap).\
+ limit(10).all()
+
+ captimes = [Captime(c.player_id, html_colors(c.nick),
+ c.fastest_cap, c.game_id) for c in captimes_raw]
+
except Exception as e:
gmap = None
return {'gmap':gmap,
'top_scorers':top_scorers,
'top_players':top_players,
'top_servers':top_servers,
+ 'captimes':captimes,
}
leaderboard_count = 10
recent_games_count = 20
- for i in range(recent_games_count-len(mapinfo_data['recent_games'])):
- mapinfo_data['recent_games'].append(('-', '-', '-', '-'))
-
for i in range(leaderboard_count-len(mapinfo_data['top_scorers'])):
mapinfo_data['top_scorers'].append(('-', '-', '-'))
mapinfo_data['top_servers'].append(('-', '-', '-'))
return mapinfo_data
+
+
+def map_info_json(request):
+ """
+ List the information stored about a given map. JSON.
+ """
+ return [{'status':'not implemented'}]