def _create_stats(self, session, player_filter=None, replay_ids=None): average = QueryFilterBuilder().with_stat_query( self.get_player_stat_query()) std_devs = QueryFilterBuilder().with_stat_query( self.get_player_stat_std_query()) if player_filter is not None: average.with_players(player_filter) std_devs.with_players(player_filter) if replay_ids is not None: average.with_replay_ids(replay_ids) std_devs.with_replay_ids(replay_ids) average = average.build_query(session).filter( PlayerGame.time_in_game > 0).first() std_devs = std_devs.build_query(session).filter( PlayerGame.time_in_game > 0).first() average = { n.get_field_name(): round(float(s), 2) for n, s in zip(self.player_stats.stat_list, average) if s is not None } std_devs = { n.get_field_name(): round(float(s), 2) for n, s in zip(self.player_stats.stat_list, std_devs) if s is not None } return {'average': average, 'std_dev': std_devs}
def create_with_filters(page: int, limit: int, session=None, **kwargs) -> 'MatchHistory': tags = JsonTag.get_tags_from_query_params(**kwargs, session=session) builder = QueryFilterBuilder().as_game() if len(tags) > 0: builder.with_tags([tag.id for tag in tags]) QueryFilterBuilder.apply_arguments_to_query(builder, kwargs) query = builder.build_query(session) if 'min_length' in kwargs: query = query.filter(Game.length > kwargs['min_length']) if 'max_length' in kwargs: query = query.filter(Game.length < kwargs['max_length']) if 'map' in kwargs: query = query.filter(Game.map == kwargs['map']) count = query.count() games = query.order_by(desc(Game.match_date))[page * limit:(page + 1) * limit] matches = MatchHistory( count, [Replay.create_from_game(game) for game in games]) return matches
def _get_counts(self, session, player_filter=None, replay_ids=None): stats = QueryFilterBuilder().with_stat_query((PlayerGame,)) if player_filter is not None: stats.with_players(player_filter) if replay_ids is not None: stats.with_replay_ids(replay_ids) return stats.build_query(session).filter(PlayerGame.time_in_game > 0).count()
def __init__(self, player_wrapper: PlayerWrapper): super().__init__() self.player_wrapper = player_wrapper # this Object needs to be pooled per a session so only one is used at a time self.player_stats_filter = QueryFilterBuilder() if not ignore_filtering(): self.player_stats_filter.with_relative_start_time(days_ago=30).with_safe_checking().sticky()
def _create_group_stats_from_query(self, session, query, player_filter=None, replay_ids=None): stats = QueryFilterBuilder().with_stat_query(query) if player_filter is not None: stats.with_players(player_filter) if replay_ids is not None: stats.with_replay_ids(replay_ids) stats = stats.build_query(session).filter(PlayerGame.time_in_game > 0).first() stats = {n.get_query_key(): round(float(s), 2) for n, s in zip(self.player_stats.stat_list, stats) if s is not None} return stats
def have_permission_to_change_game(game_hash: str, user_id, session=None) -> bool: entry = session.query(GameVisibility).filter(GameVisibility.player == user_id, GameVisibility.game == game_hash).first() if entry is None: # Check if user has right to change visibility: builder = QueryFilterBuilder().as_game().set_replay_id(game_hash).with_players([user_id]) if builder.build_query(session).first() is not None: return True else: # Replay might actually exist, but user should not know. raise ReplayNotFound() else: return True
def api_v1_get_playergames_by_rank(session=None): if 'days' in request.args: days = int(request.args['days']) else: days = 3 * 30 builder = QueryFilterBuilder().with_stat_query([PlayerGame]).with_relative_start_time(days) QueryFilterBuilder.apply_arguments_to_query(builder, request.args) games = builder.build_query(session).order_by(func.random())[:1000] columns = [c.name for c in games[0].__table__.columns] data = { 'data': [[getattr(g, c.name) for c in g.__table__.columns] for g in games], 'columns': columns } return jsonify(data)
def create_with_filters(page: int, limit: int, session=None, **kwargs) -> 'MatchHistory': # TODO: move this somewhere else and make it reusable if limit > 100: limit = 100 builder = QueryFilterBuilder().as_game().with_stat_query([Game]) QueryFilterBuilder.apply_arguments_to_query(builder, kwargs) query = builder.build_query(session) if 'min_length' in kwargs: query = query.filter(Game.length > kwargs['min_length']) if 'max_length' in kwargs: query = query.filter(Game.length < kwargs['max_length']) if 'map' in kwargs: query = query.filter(Game.map == kwargs['map']) count = query.count() games = query.order_by(desc(Game.match_date))[page * limit: (page + 1) * limit] matches = MatchHistory(count, [Replay.create_from_game(game) for game in games]) return matches
def test_query_builder(self): query = QueryFilterBuilder().with_relative_start_time(days_ago=10).sticky().clean() print(query)
from backend.database.wrapper.query_filter_builder import QueryFilterBuilder if __name__ == "__main__": query = QueryFilterBuilder().with_relative_start_time( days_ago=10).sticky().clean() print(query)
def __init__(self): super().__init__() # this Object needs to be pooled per a session so only one is used at a time self.base_query = QueryFilterBuilder().with_relative_start_time( days_ago=self.get_timeframe()).with_safe_checking().sticky()