From: antzucaro Date: Mon, 10 Oct 2011 20:44:35 +0000 (-0400) Subject: Add player nick history tracking. X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=e2690d4a7360c56e3a0f31a131e9eda700ddd0f8;p=xonotic%2Fxonstat.git Add player nick history tracking. --- diff --git a/xonstat/models.py b/xonstat/models.py index 27d50d8..4b92e76 100755 --- a/xonstat/models.py +++ b/xonstat/models.py @@ -148,6 +148,11 @@ class Hashkey(object): return "" % (self.player_id, self.hashkey) +class PlayerNick(object): + def __repr__(self): + return "" % (self.player_id, self.stripped_nick) + + def initialize_db(engine=None): DBSession.configure(bind=engine) Base.metadata.bind = engine @@ -170,6 +175,7 @@ def initialize_db(engine=None): players_table = MetaData.tables['players'] player_weapon_stats_table = MetaData.tables['player_weapon_stats'] servers_table = MetaData.tables['servers'] + player_nicks_table = MetaData.tables['player_nicks'] # now map the tables and the objects together mapper(PlayerAchievement, achievements_table) @@ -186,3 +192,4 @@ def initialize_db(engine=None): mapper(Player, players_table) mapper(PlayerWeaponStat, player_weapon_stats_table) mapper(Server, servers_table) + mapper(PlayerNick, player_nicks_table) diff --git a/xonstat/views/submission.py b/xonstat/views/submission.py index ce15d67..185907c 100755 --- a/xonstat/views/submission.py +++ b/xonstat/views/submission.py @@ -5,10 +5,42 @@ import time from pyramid.response import Response from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound from xonstat.models import * +from xonstat.util import strip_colors log = logging.getLogger(__name__) +def register_new_nick(session, player, new_nick): + """ + Change the player record's nick to the newly found nick. Store the old + nick in the player_nicks table for that player. + + session - SQLAlchemy database session factory + player - player record whose nick is changing + new_nick - the new nickname + """ + # see if that nick already exists + stripped_nick = strip_colors(player.nick) + try: + player_nick = session.query(PlayerNick).filter_by( + player_id=player.player_id, stripped_nick=stripped_nick).one() + except NoResultFound, e: + # player_id/stripped_nick not found, create one + # but we don't store "Anonymous Player #N" + if not re.search('^Anonymous Player #\d+$', player.nick): + player_nick = PlayerNick() + player_nick.player_id = player.player_id + player_nick.stripped_nick = stripped_nick + player_nick.nick = player.nick + session.add(player_nick) + + # We change to the new nick regardless + log.debug('Changing nick from {0} to {1} for player {2}'.format( + player.nick, new_nick, player.player_id)) + player.nick = new_nick + session.add(player) + + def get_or_create_server(session=None, name=None): """ Find a server by name or create one if not found. Parameters: @@ -198,9 +230,10 @@ def create_player_game_stat(session=None, player=None, # if the nick we end up with is different from the one in the # player record, change the nick to reflect the new value - if pgstat.nick != player.nick: - player.nick = pgstat.nick - session.add(player) + if pgstat.nick != player.nick and player.player_id > 1: + log.debug('Registering new nick for {0}: {1}'.format(player.nick, + pgstat.nick)) + register_new_nick(session, player, pgstat.nick) session.add(pgstat) session.flush() @@ -348,7 +381,7 @@ def stats_submit(request): real_players += 1 #TODO: put this into a config setting in the ini file? - if real_players < 2: + if real_players < 1: raise Exception("The number of real players is below the minimum. "\ "Stats will be ignored.")