return "<Hashkey(%s, %s)>" % (self.player_id, self.hashkey)
+class PlayerNick(object):
+ def __repr__(self):
+ return "<PlayerNick(%s, %s)>" % (self.player_id, self.stripped_nick)
+
+
def initialize_db(engine=None):
DBSession.configure(bind=engine)
Base.metadata.bind = engine
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)
mapper(Player, players_table)
mapper(PlayerWeaponStat, player_weapon_stats_table)
mapper(Server, servers_table)
+ mapper(PlayerNick, player_nicks_table)
from pyramid.response import Response\r
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound\r
from xonstat.models import *\r
+from xonstat.util import strip_colors\r
\r
log = logging.getLogger(__name__)\r
\r
\r
+def register_new_nick(session, player, new_nick):\r
+ """\r
+ Change the player record's nick to the newly found nick. Store the old\r
+ nick in the player_nicks table for that player.\r
+\r
+ session - SQLAlchemy database session factory\r
+ player - player record whose nick is changing\r
+ new_nick - the new nickname\r
+ """\r
+ # see if that nick already exists\r
+ stripped_nick = strip_colors(player.nick)\r
+ try:\r
+ player_nick = session.query(PlayerNick).filter_by(\r
+ player_id=player.player_id, stripped_nick=stripped_nick).one()\r
+ except NoResultFound, e:\r
+ # player_id/stripped_nick not found, create one\r
+ # but we don't store "Anonymous Player #N"\r
+ if not re.search('^Anonymous Player #\d+$', player.nick):\r
+ player_nick = PlayerNick()\r
+ player_nick.player_id = player.player_id\r
+ player_nick.stripped_nick = stripped_nick\r
+ player_nick.nick = player.nick\r
+ session.add(player_nick)\r
+\r
+ # We change to the new nick regardless\r
+ log.debug('Changing nick from {0} to {1} for player {2}'.format(\r
+ player.nick, new_nick, player.player_id))\r
+ player.nick = new_nick\r
+ session.add(player)\r
+\r
+\r
def get_or_create_server(session=None, name=None):\r
"""\r
Find a server by name or create one if not found. Parameters:\r
\r
# if the nick we end up with is different from the one in the\r
# player record, change the nick to reflect the new value\r
- if pgstat.nick != player.nick:\r
- player.nick = pgstat.nick\r
- session.add(player)\r
+ if pgstat.nick != player.nick and player.player_id > 1:\r
+ log.debug('Registering new nick for {0}: {1}'.format(player.nick, \r
+ pgstat.nick))\r
+ register_new_nick(session, player, pgstat.nick)\r
\r
session.add(pgstat)\r
session.flush()\r
real_players += 1\r
\r
#TODO: put this into a config setting in the ini file?\r
- if real_players < 2:\r
+ if real_players < 1:\r
raise Exception("The number of real players is below the minimum. "\\r
"Stats will be ignored.")\r
\r