From 5a605fae688cc083f82227364014dd0e6b9e4988 Mon Sep 17 00:00:00 2001 From: Ant Zucaro Date: Sat, 22 Apr 2017 08:30:09 -0400 Subject: [PATCH] Simplify the player create/update function. --- xonstat/batch/badges/gen_badges.py | 2 +- xonstat/views/submission.py | 48 +++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/xonstat/batch/badges/gen_badges.py b/xonstat/batch/badges/gen_badges.py index f9d97d6..5875f8f 100644 --- a/xonstat/batch/badges/gen_badges.py +++ b/xonstat/batch/badges/gen_badges.py @@ -17,7 +17,7 @@ NUM_PLAYERS = None PLAYER_ID = None # we look for players who have activity within the past DELTA hours -DELTA = 6 +DELTA = 50000 VERBOSE = False diff --git a/xonstat/views/submission.py b/xonstat/views/submission.py index 9c2cd9c..55e1fd7 100644 --- a/xonstat/views/submission.py +++ b/xonstat/views/submission.py @@ -753,11 +753,6 @@ def create_game_stat(session, game, gmap, player, events): pgstat.rank = int(events.get('rank', None)) pgstat.scoreboardpos = int(events.get('scoreboardpos', pgstat.rank)) - if pgstat.nick != player.nick \ - and player.player_id > 2 \ - and pgstat.nick != 'Anonymous Player': - register_new_nick(session, player, pgstat.nick) - wins = False # gametype-specific stuff is handled here. if passed to us, we store it @@ -967,11 +962,43 @@ def get_ranks(session, player_ids, game_type_cd): def update_player(session, player, events): - pass + """ + Updates a player record using the latest information. + :param session: SQLAlchemy session + :param player: Player model representing what is in the database right now (before updates) + :param events: Dict of player events from the submission + :return: player + """ + nick = events.get('n', 'Anonymous Player')[:128] + if nick != player.nick and not nick.startswith("Anonymous Player"): + register_new_nick(session, player, nick) + + return player def create_player(session, events): - pass + """ + Creates a new player from the list of events. + :param session: SQLAlchemy session + :param events: Dict of player events from the submission + :return: Player + """ + player = Player() + session.add(player) + session.flush() + + nick = events.get('n', None) + if nick: + player.nick = nick[:128] + player.stripped_nick = strip_colors(qfont_decode(player.nick)) + else: + player.nick = "Anonymous Player #{0}".format(player.player_id) + player.stripped_nick = player.nick + + hk = Hashkey(player_id=player.player_id, hashkey=events.get('P', None)) + session.add(hk) + + return player def get_or_create_players(session, events_by_hashkey): @@ -996,6 +1023,9 @@ def get_or_create_players(session, events_by_hashkey): .filter(Player.player_id == Hashkey.player_id)\ .filter(Hashkey.hashkey.in_(hashkeys))\ .all(): + log.debug("Found existing player {} with hashkey {}" + .format(p.player_id, hk.hashkey)) + player = update_player(session, p, events_by_hashkey[hk.hashkey]) players_by_hashkey[hk.hashkey] = player hashkeys.remove(hk.hashkey) @@ -1003,6 +1033,10 @@ def get_or_create_players(session, events_by_hashkey): # The remainder are the players we haven't seen before, so we need to create them. for hashkey in hashkeys: player = create_player(session, events_by_hashkey[hashkey]) + + log.debug("Created player {0} ({2}) with hashkey {1}" + .format(player.player_id, hashkey, player.nick.encode('utf-8'))) + players_by_hashkey[hashkey] = player return players_by_hashkey -- 2.39.2