def get_scoreboard_page(board, page_number, gid=None): """ Retrieve a specific scoreboard page. Must be logged in to retrieve a page from the 'groups' board. Args: board: scoreboard to retrieve a page from (global, student, groups) page_number: page number to retrieve Raises: PicoException: if the 'groups' board is selected but no user is logged in """ start = SCOREBOARD_PAGE_LEN * (page_number - 1) end = start + SCOREBOARD_PAGE_LEN - 1 scoreboard = [] if board == "groups": user = api.user.get_user() group = api.group.get_group(gid=gid) if not (user["tid"] in group["members"] or user["tid"] in group["teachers"] or user["tid"] == group["owner"]): raise PicoException("You are not a member of this class", status_code=401) group_board = get_scoreboard_cache(gid=gid) raw_board = group_board.range(start, end, with_scores=True, reverse=True) scoreboard = [decode_scoreboard_item(item) for item in raw_board] elif board == "global": global_board = get_scoreboard_cache(country=None, include_ineligible=True) raw_board = global_board.range(start, end, with_scores=True, reverse=True) scoreboard = [decode_scoreboard_item(item) for item in raw_board] elif board == "student": student_board = get_scoreboard_cache(country=None, include_ineligible=False) raw_board = student_board.range(start, end, with_scores=True, reverse=True) scoreboard = [decode_scoreboard_item(item) for item in raw_board] return { 'name': board, 'pages': math.ceil(len(scoreboard) / SCOREBOARD_PAGE_LEN), 'start_page': 1, 'scoreboard': scoreboard }
def output_item(item): data = decode_scoreboard_item(item) return { "name": data["name"], "affiliation": data["affiliation"], "score_progression": get_score_progression(tid=data["tid"]), }
def output_item(item): data = decode_scoreboard_item(item) return { 'name': data['name'], 'affiliation': data['affiliation'], 'score_progression': get_score_progression(tid=data['tid']) }
def get_scoreboard_page(scoreboard_key, page_number=None): """ Get a scoreboard page. If a page is not specified, will attempt to return the page containing the current team, falling back to the first page if neccessary. Args: scoreboard_key (dict): scoreboard key Kwargs: page_number (int): page to retrieve, defaults to None (which attempts to return the current team's page) Returns: (list: scoreboard page, int: current page, int: number of pages) """ board_cache = get_scoreboard_cache(**scoreboard_key) if not page_number: try: user = api.user.get_user() team = api.team.get_team(tid=user['tid']) team_position = board_cache.rank(get_scoreboard_key(team), reverse=True) or 0 page_number = math.floor(team_position / SCOREBOARD_PAGE_LEN) + 1 except PicoException: page_number = 1 start = SCOREBOARD_PAGE_LEN * (page_number - 1) end = start + SCOREBOARD_PAGE_LEN - 1 board_page = [decode_scoreboard_item(item) for item in board_cache.range( start, end, with_scores=True, reverse=True)] available_pages = max(math.ceil(len(board_cache) / SCOREBOARD_PAGE_LEN), 1) return board_page, page_number, available_pages
def get_initial_scoreboard(): """ Retrieve the initial scoreboard (first pages of global and student views). If a user is logged in, the initial pages will instead be those on which that user appears, and their group scoreboards will also be returned. Returns: dict of scoreboard information """ user = None team = None pagelen = SCOREBOARD_PAGE_LEN if api.user.is_logged_in(): user = api.user.get_user() team = api.team.get_team(tid=user['tid']) result = {'tid': 0, 'groups': []} # Include all arguments global_board = get_scoreboard_cache(country=None, include_ineligible=True) result['global'] = { 'name': 'global', 'pages': math.ceil(len(global_board) / pagelen), 'start_page': 1, 'scoreboard': [], } if user is None: raw_board = global_board.range(0, pagelen - 1, with_scores=True, desc=True) result['global']['scoreboard'] = [ decode_scoreboard_item(item) for item in raw_board ] else: result['tid'] = team['tid'] # Display global board at particular page user is ranked at global_pos = global_board.rank(get_scoreboard_key(team), reverse=True) or 0 start_slice = math.floor(global_pos / pagelen) * pagelen raw_board = global_board.range(start_slice, start_slice + pagelen - 1, with_scores=True, desc=True) result['global']['scoreboard'] = [ decode_scoreboard_item(item) for item in raw_board ] result['global']['start_page'] = math.ceil((global_pos + 1) / pagelen) # Eligible student board, starting at first page # result['country'] = user["country"] student_board = get_scoreboard_cache(country=None, include_ineligible=False) student_pos = student_board.rank(get_scoreboard_key(team), reverse=True) or 0 start_slice = math.floor(student_pos / pagelen) * pagelen raw_student_board = student_board.range(start_slice, start_slice + pagelen - 1, with_scores=True, desc=True) result['student'] = { 'name': 'student', 'pages': math.ceil(len(student_board) / pagelen), 'scoreboard': [decode_scoreboard_item(item) for item in raw_student_board], 'start_page': math.ceil((student_pos + 1) / pagelen), } # Each classroom/group for group in api.team.get_groups(user['tid']): group_board = get_scoreboard_cache(gid=group['gid']) group_pos = group_board.rank(get_scoreboard_key(team), reverse=True) or 0 start_slice = math.floor(group_pos / pagelen) * pagelen raw_group_board = group_board.range(start_slice, start_slice + pagelen - 1, with_scores=True, desc=True) result['groups'].append({ 'gid': group['gid'], 'name': group['name'], 'scoreboard': [decode_scoreboard_item(item) for item in raw_group_board], 'pages': math.ceil(len(group_board) / pagelen), 'start_page': math.ceil((group_pos + 1) / pagelen), }) return result