def setUp(self):
        self.tag_name_alt = TAGS[1]
        self.all_tags = TAGS
        self.test_user_id = TEST_USER_ID
        engine, Session = startup()
        self.session = Session()
        self.test_game_id = TEST_GAME_ID
        self.test_game_ids = self.create_replay_names()

        test_user = self.session.query(Player).filter(Player.platformid == self.test_user_id).first()
        if test_user is None:
            test_user = Player(platformid=self.test_user_id)
            self.session.add(test_user)

        for i in range(len(self.test_game_ids)):
            tag_name = self.all_tags[i]
            tag = self.session.query(Tag).filter(Tag.owner == self.test_user_id, Tag.name == tag_name).first()
            if tag is None:
                tag = Tag(owner=self.test_user_id, name=tag_name)
                self.session.add(tag)

        for game_id in self.test_game_ids:
            game = self.session.query(Game).filter(Game.hash == game_id).first()
            if game is None:
                game = Game(hash=game_id)
                self.session.add(game)

        self.session.commit()

        # add some spice to the games :>
        for i in range(len(self.test_game_ids)):
            game_id = self.test_game_ids[i]
            for j in range(len(self.test_game_ids) - i):
                TagWrapper.add_tag_to_game(self.session, game_id, self.test_user_id, self.all_tags[j])
Exemplo n.º 2
0
def add_objs_to_db(game: Game, player_games: List[PlayerGame], players: List[Player], session,
                   preserve_upload_date=False):
    try:
        match = session.query(Game).filter(Game.hash == game.hash).first()
        if match is not None:
            if preserve_upload_date:
                game.upload_date = match.upload_date
            session.delete(match)
            print('deleting {}'.format(match.hash))
        session.add(game)
    except TypeError as e:
        print('Error object: ', e)
        pass
    for pl in players:  # type: Player
        try:
            match = session.query(Player).filter(Player.platformid == str(pl.platformid)).first()
        except TypeError:
            print('platform id', pl.platformid)
            match = None
        if not match:  # we don't need to add duplicate players
            session.add(pl)
    for pg in player_games:
        match = session.query(PlayerGame).filter(PlayerGame.player == str(pg.player)).filter(
            PlayerGame.game == pg.game).first()
        if match is not None:
            session.delete(match)
        session.add(pg)
    def setUp(self):
        self.tag_name = TAGS[0]
        self.tag_name_alt = TAGS[1]
        self.test_user_id = TEST_USER_ID
        engine, Session = startup()
        self.session = Session()
        self.test_game_id = TEST_GAME_ID

        test_user = self.session.query(Player).filter(Player.platformid == self.test_user_id).first()
        if test_user is None:
            test_user = Player(platformid=self.test_user_id)
            self.session.add(test_user)

        tag = self.session.query(Tag).filter(Tag.owner == self.test_user_id, Tag.name == self.tag_name).first()
        if tag is None:
            tag = Tag(owner=self.test_user_id, name=self.tag_name)
            self.session.add(tag)

        game = self.session.query(Game).filter(Game.hash == self.test_game_id).first()
        if game is None:
            game = Game(hash=self.test_game_id)
            self.session.add(game)

        self.session.commit()
Exemplo n.º 4
0
def convert_pickle_to_db(
        game: game_pb2,
        offline_redis=None) -> (Game, list, list, List[Loadout]):
    """
    Converts pickled games into various database objects.

    :param game: Pickled game to process into Database object
    :return: Game db object, PlayerGame array, Player array
    """
    teamsize = max(len(game.teams[0].player_ids),
                   len(game.teams[1].player_ids))
    player_objs = game.players
    ranks = get_rank_batch([p.id.id for p in player_objs],
                           offline_redis=offline_redis)
    rank_list = []
    mmr_list = []
    for player, rank in ranks.items():
        r = get_rank_obj_by_mapping(rank, playlist=game.game_metadata.playlist)
        rank_list.append(r['tier'])
        mmr_list.append(r['rank_points'])
    replay_id = game.game_metadata.match_guid
    if replay_id == '' or replay_id is None:
        replay_id = game.game_metadata.id
    team0poss = game.teams[0].stats.possession
    team1poss = game.teams[1].stats.possession
    match_date = datetime.datetime.fromtimestamp(game.game_metadata.time)
    match_name = game.game_metadata.name
    g = Game(hash=replay_id,
             players=[str(p.id.id) for p in player_objs],
             ranks=rank_list,
             mmrs=mmr_list,
             map=game.game_metadata.map,
             team0score=game.game_metadata.score.team_0_score,
             team1score=game.game_metadata.score.team_1_score,
             teamsize=teamsize,
             match_date=match_date,
             team0possession=team0poss.possession_time,
             team1possession=team1poss.possession_time,
             name='' if match_name is None else match_name,
             frames=game.game_metadata.frames,
             length=game.game_metadata.length,
             playlist=game.game_metadata.playlist,
             game_server_id=0 if game.game_metadata.game_server_id == '' else
             game.game_metadata.game_server_id,
             server_name=game.game_metadata.server_name,
             replay_id=game.game_metadata.id,
             primary_player=game.game_metadata.primary_player.id)

    player_games = []
    players = []
    teamstats = []
    # print('iterating over players')
    for team in game.teams:
        fields = create_and_filter_proto_field(team,
                                               ['id', 'name', 'is_orange'], [],
                                               TeamStat)
        values = get_proto_values(team, fields)
        kwargs = {k.field_name: v for k, v in zip(fields, values)}
        for k in kwargs:
            if kwargs[k] == 'NaN' or math.isnan(kwargs[k]):
                kwargs[k] = 0.0
        t = TeamStat(game=replay_id, is_orange=team.is_orange, **kwargs)
        teamstats.append(t)

    for p in player_objs:  # type: GamePlayer
        fields = create_and_filter_proto_field(
            p, ['name', 'title_id', 'is_orange'], [
                'api.metadata.CameraSettings', 'api.metadata.PlayerLoadout',
                'api.PlayerId'
            ], PlayerGame)
        values = get_proto_values(p, fields)
        kwargs = {k.field_name: v for k, v in zip(fields, values)}
        for k in kwargs:
            if kwargs[k] == 'NaN' or math.isnan(kwargs[k]):
                kwargs[k] = 0.0
        camera = p.camera_settings
        loadout = p.loadout
        field_of_view = camera.field_of_view
        transition_speed = camera.transition_speed
        pitch = camera.pitch
        swivel_speed = camera.swivel_speed
        stiffness = camera.stiffness
        height = camera.height
        distance = camera.distance
        blue_score = game.game_metadata.score.team_0_score
        orange_score = game.game_metadata.score.team_1_score
        is_orange = p.is_orange

        pid = str(p.id.id)
        rank = None
        mmr = None
        division = None
        if pid in ranks:
            try:
                r = get_rank_obj_by_mapping(
                    ranks[pid], playlist=game.game_metadata.playlist)
                rank = r['tier']
                division = r['division']
                mmr = r['rank_points']
            except:
                rank = 0
                division = 0
                mmr = 0

        if is_orange:
            win = orange_score > blue_score
        else:
            win = blue_score > orange_score
        pg = PlayerGame(player=pid,
                        name=p.name,
                        game=replay_id,
                        field_of_view=field_of_view,
                        transition_speed=transition_speed,
                        pitch=pitch,
                        swivel_speed=swivel_speed,
                        stiffness=stiffness,
                        height=height,
                        distance=distance,
                        car=-1 if loadout is None else loadout.car,
                        is_orange=p.is_orange,
                        rank=rank,
                        division=division,
                        mmr=mmr,
                        win=win,
                        **kwargs)
        player_games.append(pg)
        if len(str(pid)) > 40:
            pid = pid[:40]
        p = Player(platformid=pid,
                   platformname=p.name,
                   avatar="",
                   ranks=[],
                   groups=[])
        players.append(p)

    loadouts = process_loadout_to_obj(game)
    return g, player_games, players, teamstats, loadouts
Exemplo n.º 5
0
def add_objs_to_db(game: Game,
                   player_games: List[PlayerGame],
                   players: List[Player],
                   teamstats: List[TeamStat],
                   loadouts: List[Loadout],
                   session,
                   preserve_upload_date=False,
                   preserve_ranks=True):
    match_exists = False
    # delete playergames/teamstats first, to prevent orphans
    for pg in player_games:
        match = session.query(PlayerGame).filter(
            PlayerGame.player == str(pg.player)).filter(
                PlayerGame.game == pg.game).first()
        if match is not None:
            if preserve_ranks:
                pg.rank = match.rank
                pg.division = match.division
                pg.mmr = match.mmr
            session.delete(match)

        matches = session.query(PlayerGame).filter(
            PlayerGame.player == str(pg.player)).filter(
                PlayerGame.game ==
                game.replay_id).all()  # catch old replay ids
        if matches is not None and len(matches) > 0:
            for match in matches:
                if preserve_ranks:
                    pg.rank = match.rank
                    pg.division = match.division
                    pg.mmr = match.mmr
                session.delete(match)

    matches = session.query(TeamStat).filter(TeamStat.game == game.hash).all()
    if matches is not None:
        for match in matches:
            session.delete(match)

    for loadout in loadouts:
        matches = session.query(Loadout).filter(
            Loadout.game == loadout.game).filter(
                Loadout.player == loadout.player).all()
        for match in matches:
            session.delete(match)
    try:
        matches = session.query(Game).filter(Game.hash == game.hash).all()
        if matches is not None and len(matches) > 0:
            match_exists = True
            for match in matches:
                if preserve_upload_date:
                    game.upload_date = match.upload_date
                if preserve_ranks:
                    game.ranks = match.ranks
                    game.mmrs = match.mmrs
                session.delete(match)
                print('deleting {}'.format(match.hash))

        matches = session.query(Game).filter(
            Game.hash == game.replay_id).all()  # catch old replay ids
        if matches is not None and len(matches) > 0:
            for match in matches:
                if preserve_upload_date:
                    game.upload_date = match.upload_date
                if preserve_ranks:
                    game.ranks = match.ranks
                    game.mmrs = match.mmrs
                session.delete(match)
                print('deleting {}'.format(match.hash))
        session.add(game)
    except TypeError as e:
        print('Error object: ', e)
        pass
    for pl in players:  # type: Player
        try:
            match = session.query(Player).filter(
                Player.platformid == str(pl.platformid)).first()
        except TypeError:
            print('platform id', pl.platformid)
            match = None
        if not match:  # we don't need to add duplicate players
            session.add(pl)
    for pg in player_games:
        session.add(pg)
    for team in teamstats:
        session.add(team)

    for loadout in loadouts:
        session.add(loadout)

    return match_exists
Exemplo n.º 6
0
def api_get_replay_count(session=None):
    result = session.execute(Game.count_query())
    return jsonify(result.scalar())