Example #1
0
def player_game_index_data(request):
    RecentGame = namedtuple('RecentGame', [
        'game_id', 'game_type_cd', 'winner', 'game_create_dt', 'game_epoch',
        'game_fuzzy', 'server_id', 'server_name', 'map_id', 'map_name', 'team',
        'rank', 'elo_delta'
    ])

    player_id = request.matchdict['player_id']

    if request.params.has_key('page'):
        current_page = request.params['page']
    else:
        current_page = 1

    try:
        player = DBSession.query(Player).filter_by(player_id=player_id).\
                filter(Player.active_ind == True).one()

        games_q = DBSession.query(Game.game_id, Game.game_type_cd, Game.winner,
                Game.create_dt, Server.server_id,
                Server.name.label('server_name'), Map.map_id,
                Map.name.label('map_name'), PlayerGameStat.team,
                PlayerGameStat.rank, PlayerGameStat.elo_delta).\
            filter(PlayerGameStat.game_id == Game.game_id).\
            filter(PlayerGameStat.player_id == player_id).\
            filter(Game.server_id == Server.server_id).\
            filter(Game.map_id == Map.map_id).\
            order_by(Game.game_id.desc())

        games = Page(games_q, current_page, items_per_page=10, url=page_url)

        # replace the items in the canned pagination class with more rich ones
        games.items = [
            RecentGame(game_id=row.game_id,
                       game_type_cd=row.game_type_cd,
                       winner=row.winner,
                       game_create_dt=row.create_dt,
                       game_epoch=timegm(row.create_dt.timetuple()),
                       game_fuzzy=pretty_date(row.create_dt),
                       server_id=row.server_id,
                       server_name=row.server_name,
                       map_id=row.map_id,
                       map_name=row.map_name,
                       team=row.team,
                       rank=row.rank,
                       elo_delta=row.elo_delta) for row in games.items
        ]

    except Exception as e:
        player = None
        games = None

    return {
        'player_id': player.player_id,
        'player': player,
        'games': games,
    }
Example #2
0
def player_game_index_data(request):
    RecentGame = namedtuple('RecentGame', ['game_id', 'game_type_cd', 'winner',
        'game_create_dt', 'game_epoch', 'game_fuzzy', 'server_id',
        'server_name', 'map_id', 'map_name', 'team', 'rank', 'elo_delta'])

    player_id = request.matchdict['player_id']

    if request.params.has_key('page'):
        current_page = request.params['page']
    else:
        current_page = 1

    try:
        player = DBSession.query(Player).filter_by(player_id=player_id).\
                filter(Player.active_ind == True).one()

        games_q = DBSession.query(Game.game_id, Game.game_type_cd, Game.winner,
                Game.create_dt, Server.server_id,
                Server.name.label('server_name'), Map.map_id,
                Map.name.label('map_name'), PlayerGameStat.team,
                PlayerGameStat.rank, PlayerGameStat.elo_delta).\
            filter(PlayerGameStat.game_id == Game.game_id).\
            filter(PlayerGameStat.player_id == player_id).\
            filter(Game.server_id == Server.server_id).\
            filter(Game.map_id == Map.map_id).\
            order_by(Game.game_id.desc())

        games = Page(games_q, current_page, items_per_page=10, url=page_url)

        # replace the items in the canned pagination class with more rich ones
        games.items = [RecentGame(
            game_id        = row.game_id,
            game_type_cd   = row.game_type_cd,
            winner         = row.winner,
            game_create_dt = row.create_dt,
            game_epoch     = timegm(row.create_dt.timetuple()),
            game_fuzzy     = pretty_date(row.create_dt),
            server_id      = row.server_id,
            server_name    = row.server_name,
            map_id         = row.map_id,
            map_name       = row.map_name,
            team           = row.team,
            rank           = row.rank,
            elo_delta      = row.elo_delta
        ) for row in games.items]

    except Exception as e:
        player = None
        games = None

    return {
            'player_id':player.player_id,
            'player':player,
            'games':games,
           }
Example #3
0
 def __init__(self, row):
     self.fastest_cap = row.fastest_cap
     self.create_dt = row.create_dt
     self.create_dt_epoch = timegm(row.create_dt.timetuple())
     self.create_dt_fuzzy = pretty_date(row.create_dt)
     self.player_id = row.player_id
     self.game_id = row.game_id
     self.map_id = row.map_id
     self.map_name = row.map_name
     self.server_id = row.server_id
     self.server_name = row.server_name
Example #4
0
 def __init__(self, row):
     self.fastest_cap = row.fastest_cap
     self.create_dt = row.create_dt
     self.create_dt_epoch = timegm(row.create_dt.timetuple())
     self.create_dt_fuzzy = pretty_date(row.create_dt)
     self.player_id = row.player_id
     self.player_nick = row.player_nick
     self.player_nick_stripped = strip_colors(row.player_nick)
     self.player_nick_html = html_colors(row.player_nick)
     self.game_id = row.game_id
     self.server_id = row.server_id
     self.server_name = row.server_name
Example #5
0
 def __init__(self, row):
     self.fastest_cap = row.fastest_cap
     self.create_dt = row.create_dt
     self.create_dt_epoch = timegm(row.create_dt.timetuple())
     self.create_dt_fuzzy = pretty_date(row.create_dt)
     self.player_id = row.player_id
     self.player_nick = row.player_nick
     self.player_nick_stripped = strip_colors(row.player_nick)
     self.player_nick_html = html_colors(row.player_nick)
     self.game_id = row.game_id
     self.server_id = row.server_id
     self.server_name = row.server_name
Example #6
0
    def __init__(self, row):
        self.game_id          = row.game_id
        self.game_type_cd     = row.game_type_cd
        self.game_type_descr  = row.game_type_descr
        self.winner           = row.winner
        self.start_dt         = row.start_dt
        self.fuzzy_date       = pretty_date(row.start_dt)
        self.epoch            = timegm(row.start_dt.timetuple())
        self.server_id        = row.server_id
        self.server_name      = row.server_name
        self.map_id           = row.map_id
        self.map_name         = row.map_name
        self.player_id        = row.player_id
        self.nick             = row.nick
        self.nick_html_colors = html_colors(row.nick)
        self.rank             = row.rank
        self.team             = row.team

        try:
            self.elo_delta = row.elo_delta
        except:
            self.elo_delta = None
Example #7
0
    def __init__(self, row):
        self.game_id = row.game_id
        self.game_type_cd = row.game_type_cd
        self.game_type_descr = row.game_type_descr
        self.winner = row.winner
        self.start_dt = row.start_dt
        self.fuzzy_date = pretty_date(row.start_dt)
        self.epoch = timegm(row.start_dt.timetuple())
        self.server_id = row.server_id
        self.server_name = row.server_name
        self.map_id = row.map_id
        self.map_name = row.map_name
        self.player_id = row.player_id
        self.nick = row.nick
        self.nick_html_colors = html_colors(row.nick)
        self.rank = row.rank
        self.team = row.team

        try:
            self.elo_delta = row.elo_delta
        except:
            self.elo_delta = None
Example #8
0
def player_captimes_data(request):
    player_id = int(request.matchdict['player_id'])
    if player_id <= 2:
        player_id = -1;

    if request.params.has_key('page'):
        current_page = request.params['page']
    else:
        current_page = 1

    PlayerCaptimes = namedtuple('PlayerCaptimes', ['fastest_cap',
            'create_dt', 'create_dt_epoch', 'create_dt_fuzzy',
            'player_id', 'game_id', 'map_id', 'map_name', 'server_id', 'server_name'])

    player = DBSession.query(Player).filter_by(player_id=player_id).one()

    #pct_q = DBSession.query('fastest_cap', 'create_dt', 'player_id', 'game_id', 'map_id',
    #            'map_name', 'server_id', 'server_name').\
    #        from_statement(
    #            "SELECT ct.fastest_cap, "
    #                   "ct.create_dt, "
    #                   "ct.player_id, "
    #                   "ct.game_id, "
    #                   "ct.map_id, "
    #                   "m.name map_name, "
    #                   "g.server_id, "
    #                   "s.name server_name "
    #            "FROM   player_map_captimes ct, "
    #                   "games g, "
    #                   "maps m, "
    #                   "servers s "
    #            "WHERE  ct.player_id = :player_id "
    #              "AND  g.game_id = ct.game_id "
    #              "AND  g.server_id = s.server_id "
    #              "AND  m.map_id = ct.map_id "
    #            #"ORDER  BY ct.fastest_cap "
    #            "ORDER  BY ct.create_dt desc"
    #        ).params(player_id=player_id)

    try:
        pct_q = DBSession.query(PlayerCaptime.fastest_cap, PlayerCaptime.create_dt,
                PlayerCaptime.player_id, PlayerCaptime.game_id, PlayerCaptime.map_id,
                Map.name.label('map_name'), Game.server_id, Server.name.label('server_name')).\
                filter(PlayerCaptime.player_id==player_id).\
                filter(PlayerCaptime.game_id==Game.game_id).\
                filter(PlayerCaptime.map_id==Map.map_id).\
                filter(Game.server_id==Server.server_id).\
                order_by(expr.desc(PlayerCaptime.create_dt))

        player_captimes = Page(pct_q, current_page, items_per_page=20, url=page_url)

        # replace the items in the canned pagination class with more rich ones
        player_captimes.items = [PlayerCaptimes(
                fastest_cap=row.fastest_cap,
                create_dt=row.create_dt,
                create_dt_epoch=timegm(row.create_dt.timetuple()),
                create_dt_fuzzy=pretty_date(row.create_dt),
                player_id=row.player_id,
                game_id=row.game_id,
                map_id=row.map_id,
                map_name=row.map_name,
                server_id=row.server_id,
                server_name=row.server_name
                ) for row in player_captimes.items]

    except Exception as e:
        player = None
        player_captimes = None

    return {
            'player_id':player_id,
            'player':player,
            'captimes':player_captimes,
            #'player_url':request.route_path('player_info', id=player_id),
        }
Example #9
0
def get_overall_stats(player_id):
    """
    Provides a breakdown of stats by gametype played by player_id.

    Returns a dictionary of namedtuples with the following members:
        - total_kills
        - total_deaths
        - k_d_ratio
        - last_played (last time the player played the game type)
        - last_played_epoch (same as above, but in seconds since epoch)
        - last_played_fuzzy (same as above, but in relative date)
        - total_playing_time (total amount of time played the game type)
        - total_playing_time_secs (same as the above, but in seconds)
        - total_pickups (ctf only)
        - total_captures (ctf only)
        - cap_ratio (ctf only)
        - total_carrier_frags (ctf only)
        - game_type_cd
        - game_type_descr

    The key to the dictionary is the game type code. There is also an
    "overall" game_type_cd which sums the totals and computes the total ratios.
    """
    OverallStats = namedtuple('OverallStats', ['total_kills', 'total_deaths',
        'k_d_ratio', 'last_played', 'last_played_epoch', 'last_played_fuzzy',
        'total_playing_time', 'total_playing_time_secs', 'total_pickups', 'total_captures', 'cap_ratio',
        'total_carrier_frags', 'game_type_cd', 'game_type_descr'])

    raw_stats = DBSession.query('game_type_cd', 'game_type_descr',
            'total_kills', 'total_deaths', 'last_played', 'total_playing_time',
            'total_pickups', 'total_captures', 'total_carrier_frags').\
            from_statement(
                "SELECT g.game_type_cd, "
                       "gt.descr game_type_descr, "
                       "Sum(pgs.kills)         total_kills, "
                       "Sum(pgs.deaths)        total_deaths, "
                       "Max(pgs.create_dt)     last_played, "
                       "Sum(pgs.alivetime)     total_playing_time, "
                       "Sum(pgs.pickups)       total_pickups, "
                       "Sum(pgs.captures)      total_captures, "
                       "Sum(pgs.carrier_frags) total_carrier_frags "
                "FROM   games g, "
                       "cd_game_type gt, "
                       "player_game_stats pgs "
                "WHERE  g.game_id = pgs.game_id "
                  "AND  g.game_type_cd = gt.game_type_cd "
                  "AND  g.players @> ARRAY[:player_id] "
                  "AND  pgs.player_id = :player_id "
                "GROUP  BY g.game_type_cd, game_type_descr "
                "UNION "
                "SELECT 'overall'              game_type_cd, "
                       "'Overall'              game_type_descr, "
                       "Sum(pgs.kills)         total_kills, "
                       "Sum(pgs.deaths)        total_deaths, "
                       "Max(pgs.create_dt)     last_played, "
                       "Sum(pgs.alivetime)     total_playing_time, "
                       "Sum(pgs.pickups)       total_pickups, "
                       "Sum(pgs.captures)      total_captures, "
                       "Sum(pgs.carrier_frags) total_carrier_frags "
                "FROM   player_game_stats pgs "
                "WHERE  pgs.player_id = :player_id "
            ).params(player_id=player_id).all()

    # to be indexed by game_type_cd
    overall_stats = {}

    for row in raw_stats:
        # individual gametype ratio calculations
        try:
            k_d_ratio = float(row.total_kills)/row.total_deaths
        except:
            k_d_ratio = None

        try:
            cap_ratio = float(row.total_captures)/row.total_pickups
        except:
            cap_ratio = None

        # everything else is untouched or "raw"
        os = OverallStats(total_kills=row.total_kills,
                total_deaths=row.total_deaths,
                k_d_ratio=k_d_ratio,
                last_played=row.last_played,
                last_played_epoch=timegm(row.last_played.timetuple()),
                last_played_fuzzy=pretty_date(row.last_played),
                total_playing_time=row.total_playing_time,
                total_playing_time_secs=int(datetime_seconds(row.total_playing_time)),
                total_pickups=row.total_pickups,
                total_captures=row.total_captures,
                cap_ratio=cap_ratio,
                total_carrier_frags=row.total_carrier_frags,
                game_type_cd=row.game_type_cd,
                game_type_descr=row.game_type_descr)

        overall_stats[row.game_type_cd] = os

    # We have to edit "overall" stats to exclude deaths in CTS.
    # Although we still want to record deaths, they shouldn't
    # count towards the overall K:D ratio.
    if 'cts' in overall_stats:
        os = overall_stats['overall']

        try:
            k_d_ratio = float(os.total_kills)/(os.total_deaths - overall_stats['cts'].total_deaths)
        except:
            k_d_ratio = None

        non_cts_deaths = os.total_deaths - overall_stats['cts'].total_deaths


        overall_stats['overall'] = OverallStats(
                total_kills             = os.total_kills,
                total_deaths            = non_cts_deaths,
                k_d_ratio               = k_d_ratio,
                last_played             = os.last_played,
                last_played_epoch       = os.last_played_epoch,
                last_played_fuzzy       = os.last_played_fuzzy,
                total_playing_time      = os.total_playing_time,
                total_playing_time_secs = os.total_playing_time_secs,
                total_pickups           = os.total_pickups,
                total_captures          = os.total_captures,
                cap_ratio               = os.cap_ratio,
                total_carrier_frags     = os.total_carrier_frags,
                game_type_cd            = os.game_type_cd,
                game_type_descr         = os.game_type_descr)

    return overall_stats
Example #10
0
 def fuzzy_date(self):
     return pretty_date(self.create_dt)
Example #11
0
def get_overall_stats(player_id):
    """
    Provides a breakdown of stats by gametype played by player_id.

    Returns a dictionary of namedtuples with the following members:
        - total_kills
        - total_deaths
        - k_d_ratio
        - last_played (last time the player played the game type)
        - last_played_epoch (same as above, but in seconds since epoch)
        - last_played_fuzzy (same as above, but in relative date)
        - total_playing_time (total amount of time played the game type)
        - total_playing_time_secs (same as the above, but in seconds)
        - total_pickups (ctf only)
        - total_captures (ctf only)
        - cap_ratio (ctf only)
        - total_carrier_frags (ctf only)
        - game_type_cd
        - game_type_descr

    The key to the dictionary is the game type code. There is also an
    "overall" game_type_cd which sums the totals and computes the total ratios.
    """
    OverallStats = namedtuple('OverallStats', [
        'total_kills', 'total_deaths', 'k_d_ratio', 'last_played',
        'last_played_epoch', 'last_played_fuzzy', 'total_playing_time',
        'total_playing_time_secs', 'total_pickups', 'total_captures',
        'cap_ratio', 'total_carrier_frags', 'game_type_cd', 'game_type_descr'
    ])

    raw_stats = DBSession.query('game_type_cd', 'game_type_descr',
            'total_kills', 'total_deaths', 'last_played', 'total_playing_time',
            'total_pickups', 'total_captures', 'total_carrier_frags').\
            from_statement(
                "SELECT g.game_type_cd, "
                       "gt.descr game_type_descr, "
                       "Sum(pgs.kills)         total_kills, "
                       "Sum(pgs.deaths)        total_deaths, "
                       "Max(pgs.create_dt)     last_played, "
                       "Sum(pgs.alivetime)     total_playing_time, "
                       "Sum(pgs.pickups)       total_pickups, "
                       "Sum(pgs.captures)      total_captures, "
                       "Sum(pgs.carrier_frags) total_carrier_frags "
                "FROM   games g, "
                       "cd_game_type gt, "
                       "player_game_stats pgs "
                "WHERE  g.game_id = pgs.game_id "
                  "AND  g.game_type_cd = gt.game_type_cd "
                  "AND  g.players @> ARRAY[:player_id] "
                  "AND  pgs.player_id = :player_id "
                "GROUP  BY g.game_type_cd, game_type_descr "
                "UNION "
                "SELECT 'overall'              game_type_cd, "
                       "'Overall'              game_type_descr, "
                       "Sum(pgs.kills)         total_kills, "
                       "Sum(pgs.deaths)        total_deaths, "
                       "Max(pgs.create_dt)     last_played, "
                       "Sum(pgs.alivetime)     total_playing_time, "
                       "Sum(pgs.pickups)       total_pickups, "
                       "Sum(pgs.captures)      total_captures, "
                       "Sum(pgs.carrier_frags) total_carrier_frags "
                "FROM   player_game_stats pgs "
                "WHERE  pgs.player_id = :player_id "
            ).params(player_id=player_id).all()

    # to be indexed by game_type_cd
    overall_stats = {}

    for row in raw_stats:
        # individual gametype ratio calculations
        try:
            k_d_ratio = float(row.total_kills) / row.total_deaths
        except:
            k_d_ratio = None

        try:
            cap_ratio = float(row.total_captures) / row.total_pickups
        except:
            cap_ratio = None

        # everything else is untouched or "raw"
        os = OverallStats(total_kills=row.total_kills,
                          total_deaths=row.total_deaths,
                          k_d_ratio=k_d_ratio,
                          last_played=row.last_played,
                          last_played_epoch=timegm(
                              row.last_played.timetuple()),
                          last_played_fuzzy=pretty_date(row.last_played),
                          total_playing_time=row.total_playing_time,
                          total_playing_time_secs=int(
                              datetime_seconds(row.total_playing_time)),
                          total_pickups=row.total_pickups,
                          total_captures=row.total_captures,
                          cap_ratio=cap_ratio,
                          total_carrier_frags=row.total_carrier_frags,
                          game_type_cd=row.game_type_cd,
                          game_type_descr=row.game_type_descr)

        overall_stats[row.game_type_cd] = os

    # We have to edit "overall" stats to exclude deaths in CTS.
    # Although we still want to record deaths, they shouldn't
    # count towards the overall K:D ratio.
    if 'cts' in overall_stats:
        os = overall_stats['overall']

        try:
            k_d_ratio = float(os.total_kills) / (
                os.total_deaths - overall_stats['cts'].total_deaths)
        except:
            k_d_ratio = None

        non_cts_deaths = os.total_deaths - overall_stats['cts'].total_deaths

        overall_stats['overall'] = OverallStats(
            total_kills=os.total_kills,
            total_deaths=non_cts_deaths,
            k_d_ratio=k_d_ratio,
            last_played=os.last_played,
            last_played_epoch=os.last_played_epoch,
            last_played_fuzzy=os.last_played_fuzzy,
            total_playing_time=os.total_playing_time,
            total_playing_time_secs=os.total_playing_time_secs,
            total_pickups=os.total_pickups,
            total_captures=os.total_captures,
            cap_ratio=os.cap_ratio,
            total_carrier_frags=os.total_carrier_frags,
            game_type_cd=os.game_type_cd,
            game_type_descr=os.game_type_descr)

    return overall_stats
Example #12
0
def player_captimes_data(request):
    player_id = int(request.matchdict['player_id'])
    if player_id <= 2:
        player_id = -1

    if request.params.has_key('page'):
        current_page = request.params['page']
    else:
        current_page = 1

    PlayerCaptimes = namedtuple('PlayerCaptimes', [
        'fastest_cap', 'create_dt', 'create_dt_epoch', 'create_dt_fuzzy',
        'player_id', 'game_id', 'map_id', 'map_name', 'server_id',
        'server_name'
    ])

    player = DBSession.query(Player).filter_by(player_id=player_id).one()

    #pct_q = DBSession.query('fastest_cap', 'create_dt', 'player_id', 'game_id', 'map_id',
    #            'map_name', 'server_id', 'server_name').\
    #        from_statement(
    #            "SELECT ct.fastest_cap, "
    #                   "ct.create_dt, "
    #                   "ct.player_id, "
    #                   "ct.game_id, "
    #                   "ct.map_id, "
    #                   "m.name map_name, "
    #                   "g.server_id, "
    #                   "s.name server_name "
    #            "FROM   player_map_captimes ct, "
    #                   "games g, "
    #                   "maps m, "
    #                   "servers s "
    #            "WHERE  ct.player_id = :player_id "
    #              "AND  g.game_id = ct.game_id "
    #              "AND  g.server_id = s.server_id "
    #              "AND  m.map_id = ct.map_id "
    #            #"ORDER  BY ct.fastest_cap "
    #            "ORDER  BY ct.create_dt desc"
    #        ).params(player_id=player_id)

    try:
        pct_q = DBSession.query(PlayerCaptime.fastest_cap, PlayerCaptime.create_dt,
                PlayerCaptime.player_id, PlayerCaptime.game_id, PlayerCaptime.map_id,
                Map.name.label('map_name'), Game.server_id, Server.name.label('server_name')).\
                filter(PlayerCaptime.player_id==player_id).\
                filter(PlayerCaptime.game_id==Game.game_id).\
                filter(PlayerCaptime.map_id==Map.map_id).\
                filter(Game.server_id==Server.server_id).\
                order_by(expr.desc(PlayerCaptime.create_dt))

        player_captimes = Page(pct_q,
                               current_page,
                               items_per_page=20,
                               url=page_url)

        # replace the items in the canned pagination class with more rich ones
        player_captimes.items = [
            PlayerCaptimes(fastest_cap=row.fastest_cap,
                           create_dt=row.create_dt,
                           create_dt_epoch=timegm(row.create_dt.timetuple()),
                           create_dt_fuzzy=pretty_date(row.create_dt),
                           player_id=row.player_id,
                           game_id=row.game_id,
                           map_id=row.map_id,
                           map_name=row.map_name,
                           server_id=row.server_id,
                           server_name=row.server_name)
            for row in player_captimes.items
        ]

    except Exception as e:
        player = None
        player_captimes = None

    return {
        'player_id': player_id,
        'player': player,
        'captimes': player_captimes,
        #'player_url':request.route_path('player_info', id=player_id),
    }
Example #13
0
 def joined_pretty_date(self):
     return pretty_date(self.create_dt)
Example #14
0
 def fuzzy_date(self):
     return pretty_date(self.start_dt)
Example #15
0
def get_overall_stats(player_id):
    """
    Provides a breakdown of stats by gametype played by player_id.

    Returns a dictionary of namedtuples with the following members:
        - total_kills
        - total_deaths
        - k_d_ratio
        - last_played (last time the player played the game type)
        - last_played_epoch (same as above, but in seconds since epoch)
        - last_played_fuzzy (same as above, but in relative date)
        - total_playing_time (total amount of time played the game type)
        - total_pickups (ctf only)
        - total_captures (ctf only)
        - cap_ratio (ctf only)
        - total_carrier_frags (ctf only)
        - game_type_cd

    The key to the dictionary is the game type code. There is also an
    "overall" game_type_cd which sums the totals and computes the total ratios.
    """
    OverallStats = namedtuple('OverallStats', [
        'total_kills', 'total_deaths', 'k_d_ratio', 'last_played',
        'last_played_epoch', 'last_played_fuzzy', 'total_playing_time',
        'total_pickups', 'total_captures', 'cap_ratio', 'total_carrier_frags',
        'game_type_cd'
    ])

    raw_stats = DBSession.query('game_type_cd', 'total_kills',
            'total_deaths', 'last_played', 'total_playing_time',
            'total_pickups', 'total_captures', 'total_carrier_frags').\
            from_statement(
                "SELECT g.game_type_cd, "
                       "Sum(pgs.kills)         total_kills, "
                       "Sum(pgs.deaths)        total_deaths, "
                       "Max(pgs.create_dt)     last_played, "
                       "Sum(pgs.alivetime)     total_playing_time, "
                       "Sum(pgs.pickups)       total_pickups, "
                       "Sum(pgs.captures)      total_captures, "
                       "Sum(pgs.carrier_frags) total_carrier_frags "
                "FROM   games g, "
                       "player_game_stats pgs "
                "WHERE  g.game_id = pgs.game_id "
                  "AND  pgs.player_id = :player_id "
                "GROUP  BY g.game_type_cd "
            ).params(player_id=player_id).all()

    # to be indexed by game_type_cd
    overall_stats = {}

    # sums for the "overall" game type (which is fake)
    overall_kills = 0
    overall_deaths = 0
    overall_last_played = None
    overall_playing_time = datetime.timedelta(seconds=0)
    overall_carrier_frags = 0

    for row in raw_stats:
        # running totals or mins
        overall_kills += row.total_kills or 0
        overall_deaths += row.total_deaths or 0

        if overall_last_played is None or row.last_played > overall_last_played:
            overall_last_played = row.last_played

        overall_playing_time += row.total_playing_time

        # individual gametype ratio calculations
        try:
            k_d_ratio = float(row.total_kills) / row.total_deaths
        except:
            k_d_ratio = None

        try:
            cap_ratio = float(row.total_captures) / row.total_pickups
        except:
            cap_ratio = None

        overall_carrier_frags += row.total_carrier_frags or 0

        # everything else is untouched or "raw"
        os = OverallStats(total_kills=row.total_kills,
                          total_deaths=row.total_deaths,
                          k_d_ratio=k_d_ratio,
                          last_played=row.last_played,
                          last_played_epoch=timegm(
                              row.last_played.timetuple()),
                          last_played_fuzzy=pretty_date(row.last_played),
                          total_playing_time=row.total_playing_time,
                          total_pickups=row.total_pickups,
                          total_captures=row.total_captures,
                          cap_ratio=cap_ratio,
                          total_carrier_frags=row.total_carrier_frags,
                          game_type_cd=row.game_type_cd)

        overall_stats[row.game_type_cd] = os

    # and lastly, the overall stuff
    try:
        overall_k_d_ratio = float(overall_kills) / overall_deaths
    except:
        overall_k_d_ratio = None

    os = OverallStats(total_kills=overall_kills,
                      total_deaths=overall_deaths,
                      k_d_ratio=overall_k_d_ratio,
                      last_played=overall_last_played,
                      last_played_epoch=timegm(
                          overall_last_played.timetuple()),
                      last_played_fuzzy=pretty_date(overall_last_played),
                      total_playing_time=overall_playing_time,
                      total_pickups=None,
                      total_captures=None,
                      cap_ratio=None,
                      total_carrier_frags=overall_carrier_frags,
                      game_type_cd='overall')

    overall_stats['overall'] = os

    return overall_stats
Example #16
0
 def joined_pretty_date(self):
     return pretty_date(self.create_dt)
Example #17
0
 def fuzzy_date(self):
     return pretty_date(self.start_dt)
Example #18
0
def get_overall_stats(player_id):
    """
    Provides a breakdown of stats by gametype played by player_id.

    Returns a dictionary of namedtuples with the following members:
        - total_kills
        - total_deaths
        - k_d_ratio
        - last_played (last time the player played the game type)
        - last_played_epoch (same as above, but in seconds since epoch)
        - last_played_fuzzy (same as above, but in relative date)
        - total_playing_time (total amount of time played the game type)
        - total_pickups (ctf only)
        - total_captures (ctf only)
        - cap_ratio (ctf only)
        - total_carrier_frags (ctf only)
        - game_type_cd

    The key to the dictionary is the game type code. There is also an
    "overall" game_type_cd which sums the totals and computes the total ratios.
    """
    OverallStats = namedtuple('OverallStats', ['total_kills', 'total_deaths',
        'k_d_ratio', 'last_played', 'last_played_epoch', 'last_played_fuzzy',
        'total_playing_time', 'total_pickups', 'total_captures', 'cap_ratio',
        'total_carrier_frags', 'game_type_cd'])

    raw_stats = DBSession.query('game_type_cd', 'total_kills',
            'total_deaths', 'last_played', 'total_playing_time',
            'total_pickups', 'total_captures', 'total_carrier_frags').\
            from_statement(
                "SELECT g.game_type_cd, "
                       "Sum(pgs.kills)         total_kills, "
                       "Sum(pgs.deaths)        total_deaths, "
                       "Max(pgs.create_dt)     last_played, "
                       "Sum(pgs.alivetime)     total_playing_time, "
                       "Sum(pgs.pickups)       total_pickups, "
                       "Sum(pgs.captures)      total_captures, "
                       "Sum(pgs.carrier_frags) total_carrier_frags "
                "FROM   games g, "
                       "player_game_stats pgs "
                "WHERE  g.game_id = pgs.game_id "
                  "AND  pgs.player_id = :player_id "
                "GROUP  BY g.game_type_cd "
            ).params(player_id=player_id).all()

    # to be indexed by game_type_cd
    overall_stats = {}

    # sums for the "overall" game type (which is fake)
    overall_kills = 0
    overall_deaths = 0
    overall_last_played = None
    overall_playing_time = datetime.timedelta(seconds=0)
    overall_carrier_frags = 0

    for row in raw_stats:
        # running totals or mins
        overall_kills += row.total_kills or 0
        overall_deaths += row.total_deaths or 0

        if overall_last_played is None or row.last_played > overall_last_played:
            overall_last_played = row.last_played

        overall_playing_time += row.total_playing_time

        # individual gametype ratio calculations
        try:
            k_d_ratio = float(row.total_kills)/row.total_deaths
        except:
            k_d_ratio = None

        try:
            cap_ratio = float(row.total_captures)/row.total_pickups
        except:
            cap_ratio = None

        overall_carrier_frags += row.total_carrier_frags or 0

        # everything else is untouched or "raw"
        os = OverallStats(total_kills=row.total_kills,
                total_deaths=row.total_deaths,
                k_d_ratio=k_d_ratio,
                last_played=row.last_played,
                last_played_epoch=timegm(row.last_played.timetuple()),
                last_played_fuzzy=pretty_date(row.last_played),
                total_playing_time=row.total_playing_time,
                total_pickups=row.total_pickups,
                total_captures=row.total_captures,
                cap_ratio=cap_ratio,
                total_carrier_frags=row.total_carrier_frags,
                game_type_cd=row.game_type_cd)

        overall_stats[row.game_type_cd] = os

    # and lastly, the overall stuff
    try:
        overall_k_d_ratio = float(overall_kills)/overall_deaths
    except:
        overall_k_d_ratio = None

    os = OverallStats(total_kills=overall_kills,
            total_deaths=overall_deaths,
            k_d_ratio=overall_k_d_ratio,
            last_played=overall_last_played,
            last_played_epoch=timegm(overall_last_played.timetuple()),
            last_played_fuzzy=pretty_date(overall_last_played),
            total_playing_time=overall_playing_time,
            total_pickups=None,
            total_captures=None,
            cap_ratio=None,
            total_carrier_frags=overall_carrier_frags,
            game_type_cd='overall')

    overall_stats['overall'] = os

    return overall_stats