+def update_player(session, player, events):
+ """
+ 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):
+ """
+ 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):
+ hashkeys = set(events_by_hashkey.keys())
+ players_by_hashkey = {}
+
+ bot = session.query(Player).filter(Player.player_id == 1).one()
+ anon = session.query(Player).filter(Player.player_id == 2).one()
+
+ # fill in the bots and anonymous players
+ for hashkey in events_by_hashkey.keys():
+ if hashkey.startswith("bot#"):
+ players_by_hashkey[hashkey] = bot
+ hashkeys.remove(hashkey)
+ elif hashkey.startswith("player#"):
+ players_by_hashkey[hashkey] = anon
+ hashkeys.remove(hashkey)
+
+ # We are left with the "real" players and can now fetch them by their collective hashkeys.
+ # Those that are returned here are pre-existing players who need to be updated.
+ for p, hk in session.query(Player, 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)
+
+ # 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
+
+