def leaderboards_api() -> Response: """ Grab a slice of results from a 0-indexed resultset of leaderboard entries. Input: { 'competitionId': <int?>, 'competitionSeriesId': <int?> 'page': <int>, 'pageSize': <int>, 'sortBy': <str>, 'sortOrder': <'ASC'|'DESC'>, 'seasonId': <int|'all'?>, 'q': <str> } Output: { 'page': <int>, 'objects': [<entry>], 'total': <int> } """ order_by = query.leaderboard_order_by(request.args.get('sortBy'), request.args.get('sortOrder')) page_size = int(request.args.get('pageSize', DEFAULT_LIVE_TABLE_PAGE_SIZE)) page = int(request.args.get('page', 0)) start = page * page_size limit = f'LIMIT {start}, {page_size}' q = request.args.get('q', '').strip() where = query.text_match_where(query.person_query(), q) if q else 'TRUE' try: competition_id = int(request.args.get('competitionId', '')) where += f' AND (c.id = {competition_id})' season_id = None except ValueError: season_id = seasons.season_id(str(request.args.get('seasonId')), None) try: competition_series_id = int(request.args.get('competitionSeriesId', '')) where += f' AND (cs.id = {competition_series_id})' except ValueError: pass entries = comp.load_leaderboard(where=where, group_by='p.id', order_by=order_by, limit=limit, season_id=season_id) prepare_leaderboard(entries) total = comp.load_leaderboard_count(where=where, season_id=season_id) r = {'page': page, 'total': total, 'objects': entries} resp = return_json(r, camelize=True) resp.set_cookie('page_size', str(page_size)) return resp
def matches_api() -> Response: """ Grab a slice of results from a 0-indexed resultset of matches. Input: { 'competitionId': <int?>, 'page': <int>, 'pageSize': <int>, 'q': <str>, 'sortBy': <str>, 'sortOrder': <'ASC'|'DESC'>, 'seasonId': <int|'all'?> } Output: { 'page': <int>, 'objects': [<entry>], 'total': <int> } """ order_by = query.matches_order_by(request.args.get('sortBy'), request.args.get('sortOrder')) page_size = int(request.args.get('pageSize', DEFAULT_LIVE_TABLE_PAGE_SIZE)) page = int(request.args.get('page', 0)) start = page * page_size limit = f'LIMIT {start}, {page_size}' q = request.args.get('q', '').strip() person_where = query.text_match_where(query.person_query(), q) if q else 'TRUE' opponent_where = query.text_match_where(query.person_query('o'), q) if q else 'TRUE' where = f'({person_where} OR {opponent_where})' try: competition_id = int(request.args.get('competitionId', '')) where += f' AND (c.id = {competition_id})' season_id = None except ValueError: season_id = seasons.season_id(str(request.args.get('seasonId')), None) entries = match.load_matches(where=where, order_by=order_by, limit=limit, season_id=season_id, show_active_deck_names=session.get( 'admin', False)) prepare_matches(entries) total = match.load_matches_count(where=where, season_id=season_id) r = {'page': page, 'total': total, 'objects': entries} resp = return_json(r, camelize=True) resp.set_cookie('page_size', str(page_size)) return resp
def h2h_api() -> Response: """ Grab a slice of results from a 0-indexed resultset of head-to-head entries. Input: { 'page': <int>, 'pageSize': <int>, 'personId': <int>, 'sortBy': <str>, 'sortOrder': <'ASC'|'DESC'>, 'seasonId': <int|'all'>, 'q': <str> } Output: { 'page': <int>, 'objects': [<entry>], 'total': <int> } """ order_by = query.head_to_head_order_by(request.args.get('sortBy'), request.args.get('sortOrder')) page_size = int(request.args.get('pageSize', DEFAULT_LIVE_TABLE_PAGE_SIZE)) page = int(request.args.get('page', 0)) start = page * page_size limit = f'LIMIT {start}, {page_size}' season_id = seasons.season_id(str(request.args.get('seasonId')), None) person_id = int(request.args.get('personId', 0)) q = request.args.get('q', '').strip() where = query.text_match_where('opp.mtgo_username', q) if q else 'TRUE' entries = ps.load_head_to_head(person_id, where=where, order_by=order_by, limit=limit, season_id=season_id) for entry in entries: entry.opp_url = url_for('.person', mtgo_username=entry.opp_mtgo_username, season_id=None if season_id == seasons.current_season_num() else season_id) total = ps.load_head_to_head_count(person_id=person_id, where=where, season_id=season_id) r = {'page': page, 'total': total, 'objects': entries} resp = return_json(r, camelize=True) resp.set_cookie('page_size', str(page_size)) return resp
def test_season_id() -> None: assert seasons.season_id(1) == 1 found = False try: seasons.season_id(999) except DoesNotExistException: found = True assert found found = False try: assert seasons.season_id('ISD') except DoesNotExistException: found = True assert found assert seasons.season_id('HOU') == 5 assert seasons.season_id('hou') == 5 assert seasons.season_id('ALL') == 'all' assert seasons.season_id('all') == 'all'
def decks_api() -> Response: """ Grab a slice of results from a 0-indexed resultset of decks. Input: { 'archetypeId': <int?>, 'cardName': <str?>, 'competitionId': <int?>, 'deckType': <'league'|'tournament'|'all'>, 'page': <int>, 'pageSize': <int>, 'personId': <int?>, 'sortBy': <str>, 'sortOrder': <'ASC'|'DESC'>, 'seasonId': <int|'all'> } Output: { 'page': <int>, 'objects': [<deck>] 'total': <int> } """ order_by = query.decks_order_by(request.args.get('sortBy'), request.args.get('sortOrder'), request.args.get('competitionId')) page_size = int(request.args.get('pageSize', DEFAULT_LIVE_TABLE_PAGE_SIZE)) page = int(request.args.get('page', 0)) start = page * page_size limit = f'LIMIT {start}, {page_size}' # Don't restrict by season if we're loading something with a date by its id. season_id = 'all' if request.args.get( 'competitionId') else seasons.season_id( str(request.args.get('seasonId')), None) where = query.decks_where(request.args, session.get('admin'), session.get('person_id')) total = deck.load_decks_count(where=where, season_id=season_id) ds = deck.load_decks(where=where, order_by=order_by, limit=limit, season_id=season_id) prepare_decks(ds) r = {'page': page, 'total': total, 'objects': ds} resp = return_json(r, camelize=True) resp.set_cookie('page_size', str(page_size)) return resp
def cards2_api() -> Response: """ Grab a slice of results from a 0-indexed resultset of cards. Input: { 'deckType': <'league'|'tournament'|'all'>, 'page': <int>, 'pageSize': <int>, 'personId': <int?>, 'sortBy': <str>, 'sortOrder': <'ASC'|'DESC'>, 'seasonId': <int|'all'>, 'q': <str> } Output: { 'page': <int>, 'objects': [<card>], 'total': <int> } """ order_by = query.cards_order_by(request.args.get('sortBy'), request.args.get('sortOrder')) page_size = int(request.args.get('pageSize', DEFAULT_LIVE_TABLE_PAGE_SIZE)) page = int(request.args.get('page', 0)) start = page * page_size limit = f'LIMIT {start}, {page_size}' person_id = request.args.get('personId') or None tournament_only = request.args.get('deckType') == 'tournament' season_id = seasons.season_id(str(request.args.get('seasonId')), None) q = request.args.get('q', '').strip() additional_where = query.card_search_where(q) if q else 'TRUE' cs = card.load_cards(additional_where=additional_where, order_by=order_by, limit=limit, person_id=person_id, tournament_only=tournament_only, season_id=season_id) prepare_cards(cs, tournament_only=tournament_only) total = card.load_cards_count(additional_where=additional_where, person_id=person_id, season_id=season_id) r = {'page': page, 'total': total, 'objects': cs} resp = return_json(r, camelize=True) resp.set_cookie('page_size', str(page_size)) return resp
def people_api() -> Response: """ Grab a slice of results from a 0-indexed resultset of people. Input: { 'page': <int>, 'pageSize': <int>, 'sortBy': <str>, 'sortOrder': <'ASC'|'DESC'>, 'seasonId': <int|'all'>, 'q': <str> } Output: { 'page': <int>, 'objects': [<person>], 'total': <int> } """ order_by = query.people_order_by(request.args.get('sortBy'), request.args.get('sortOrder')) page_size = int(request.args.get('pageSize', DEFAULT_LIVE_TABLE_PAGE_SIZE)) page = int(request.args.get('page', 0)) start = page * page_size limit = f'LIMIT {start}, {page_size}' season_id = seasons.season_id(str(request.args.get('seasonId')), None) q = request.args.get('q', '').strip() where = query.text_match_where(query.person_query(), q) if q else 'TRUE' people = ps.load_people(where=where, order_by=order_by, limit=limit, season_id=season_id) prepare_people(people) total = ps.load_people_count(where=where, season_id=season_id) r = {'page': page, 'total': total, 'objects': people} resp = return_json(r, camelize=True) resp.set_cookie('page_size', str(page_size)) return resp
def pull_season_id(_endpoint: str, values: Dict[str, Any]) -> None: v = values.pop('season_id') g.season_id = seasons.season_id(v)