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])
    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()
Пример #3
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