+ data = player_captimes_data(request)
+ page = request.params.get("page", 1)
+
+ # perform any necessary JSON conversions
+ player_id = data["player_id"]
+ player = data["player"].to_dict()
+ captimes = [ct.to_dict() for ct in data["captimes"].items]
+
+ return {
+ "player" : player,
+ "captimes" : captimes,
+ "page" : page,
+ }
+
+
+def player_weaponstats_data_json(request):
+ player_id = int(request.matchdict["id"])
+ if player_id <= 2:
+ player_id = -1;
+
+ game_type_cd = request.params.get("game_type", None)
+ if game_type_cd == "overall":
+ game_type_cd = None
+
+ limit = 20
+ if request.params.has_key("limit"):
+ limit = int(request.params["limit"])
+
+ if limit < 0:
+ limit = 20
+ if limit > 50:
+ limit = 50
+
+
+ # the game_ids of the most recently played ones
+ # of the given game type is used for a subquery
+ games_list = DBSession.query(Game.game_id).\
+ filter(Game.players.contains([player_id]))
+
+ if game_type_cd is not None:
+ games_list = games_list.filter(Game.game_type_cd == game_type_cd)
+
+ games_list = games_list.order_by(Game.game_id.desc()).limit(limit)
+
+ weapon_stats_raw = DBSession.query(PlayerWeaponStat).\
+ filter(PlayerWeaponStat.player_id == player_id).\
+ filter(PlayerWeaponStat.game_id.in_(games_list)).\
+ all()
+
+ games_to_weapons = {}
+ weapons_used = {}
+ sum_avgs = {}
+ for ws in weapon_stats_raw:
+ if ws.game_id not in games_to_weapons:
+ games_to_weapons[ws.game_id] = [ws.weapon_cd]
+ else:
+ games_to_weapons[ws.game_id].append(ws.weapon_cd)
+
+ weapons_used[ws.weapon_cd] = weapons_used.get(ws.weapon_cd, 0) + 1
+ sum_avgs[ws.weapon_cd] = sum_avgs.get(ws.weapon_cd, 0) + float(ws.hit)/float(ws.fired)
+
+ # Creating zero-valued weapon stat entries for games where a weapon was not
+ # used in that game, but was used in another game for the set. This makes
+ # the charts look smoother
+ for game_id in games_to_weapons.keys():
+ for weapon_cd in set(weapons_used.keys()) - set(games_to_weapons[game_id]):
+ weapon_stats_raw.append(PlayerWeaponStat(player_id=player_id,
+ game_id=game_id, weapon_cd=weapon_cd))
+
+ # averages for the weapons used in the range
+ avgs = {}
+ for w in weapons_used.keys():
+ avgs[w] = round(sum_avgs[w]/float(weapons_used[w])*100, 2)
+
+ weapon_stats_raw = sorted(weapon_stats_raw, key = lambda x: x.game_id)
+ games = sorted(games_to_weapons.keys())
+ weapon_stats = [ws.to_dict() for ws in weapon_stats_raw]
+
+ return {
+ "weapon_stats": weapon_stats,
+ "weapons_used": weapons_used.keys(),
+ "games": games,
+ "averages": avgs,
+ }
+
+
+def player_versus_data(request):
+ try:
+ p1_id = int(request.params.get("p1", None))
+ p2_id = int(request.params.get("p2", None))
+
+ p1_wins = 0
+ p2_wins = 0
+
+ players = DBSession.query(Player).filter(sa.or_(Player.player_id ==
+ p1_id, Player.player_id == p2_id)).order_by(Player.player_id).all()
+
+
+ if len(players) < 2:
+ raise Exception("Not enough players found.")
+
+ # assign the players from the array retrieved above
+ if players[0].player_id == p1_id:
+ p1 = players[0]
+ p2 = players[1]
+ else:
+ p1 = players[1]
+ p2 = players[0]
+
+ # note that wins and losses are from p1's perspective
+ win_loss_sql = """select win_loss, count(1)
+ from (
+ select case
+ when pgsp1.score >= pgsp2.score then 'win'
+ else 'loss'
+ end win_loss
+ from games g join player_game_stats pgsp1
+ on g.game_id = pgsp1.game_id and pgsp1.player_id = :p1
+ join player_game_stats pgsp2
+ on g.game_id = pgsp2.game_id and pgsp2.player_id = :p2
+ where g.players @> ARRAY[:p1,:p2]
+ and g.game_type_cd = 'duel'
+ and pgsp1.create_dt between g.create_dt - interval '1 hour'
+ and g.create_dt + interval '1 hour'
+ and pgsp2.create_dt between g.create_dt - interval '1 hour'
+ and g.create_dt + interval '1 hour'
+ ) wl
+ group by win_loss
+ """
+
+ wins_losses = DBSession.query("win_loss", "count").\
+ from_statement(win_loss_sql).\
+ params(p1=p1_id, p2=p2_id).all()
+
+ for row in wins_losses:
+ if row.win_loss == "win":
+ p1_wins = row.count
+ elif row.win_loss == "loss":
+ p2_wins = row.count
+
+ # grab the 20 most recent games between the two
+ rgs_raw = recent_games_q(player_id=p1_id, player_id_2=p2_id,
+ game_type_cd="duel").limit(20).all()
+
+ rgs = [RecentGame(row) for row in rgs_raw]
+
+ except Exception as e:
+ log.debug(e)
+
+ p1_id = None
+ p2_id = None
+ p1 = None
+ p2 = None
+ p1_wins = None
+ p2_wins = None
+ rgs = None
+
+ return {
+ "p1_id" : p1_id,
+ "p2_id" : p2_id,
+ "p1" : p1,
+ "p2" : p2,
+ "p1_wins" : p1_wins,
+ "p2_wins" : p2_wins,
+ "recent_games" : rgs,
+ }
+
+
+def player_versus(request):
+ return player_versus_data(request)