def get_group_scores(gid=None, name=None): """ Get the group scores. Args: gid: The group id name: The group name Returns: A dictionary containing name, tid, and score """ key_args = {'gid': gid} scoreboard_cache = get_scoreboard_cache(**key_args) scoreboard_cache.clear() member_teams = [ api.team.get_team(tid=tid) for tid in api.group.get_group(gid=gid)['members'] ] result = {} for team in member_teams: if team["size"] > 0: score = get_score(tid=team['tid']) key = get_scoreboard_key(team) result[key] = score if result: scoreboard_cache.add(result) return scoreboard_cache
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_all_team_scores(country=None, include_ineligible=False): """ Get the score for every team in the database. Args: country: optional restriction by country include_ineligible: include ineligible teams Returns: A list of dictionaries with name and score """ key_args = {'country': country, 'include_ineligible': include_ineligible} teams = api.team.get_all_teams(**key_args) scoreboard_cache = get_scoreboard_cache(**key_args) scoreboard_cache.clear() result = {} all_groups = api.group.get_all_groups() for team in teams: # Get the full version of the group. groups = [ group for group in all_groups if team["tid"] in group["members"] or team["tid"] in group["teachers"] or team["tid"] == group["owner"] ] # Determine if the user is exclusively a member of hidden groups. # If they are, they won't be processed. if (len(groups) == 0 or any([not (group["settings"]["hidden"]) for group in groups])): score = get_score(tid=team['tid']) if score > 0: key = get_scoreboard_key(team=team) result[key] = score if result: scoreboard_cache.add(result) return scoreboard_cache
def get_all_team_scores(scoreboard_id=None): """ Get the score for every team in the database. Args: scoreboard_id: Optional, limit to teams eligible for this scoreboard Returns: A list of dictionaries with name and score """ key_args = {"scoreboard_id": scoreboard_id} teams = api.team.get_all_teams(**key_args) scoreboard_cache = get_scoreboard_cache(**key_args) result = {} all_groups = api.group.get_all_groups() for team in teams: # Get the full version of the group. groups = [ group for group in all_groups if team["tid"] in group["members"] or team["tid"] in group["teachers"] or team["tid"] == group["owner"] ] # Determine if the user is exclusively a member of hidden groups. # If they are, they won't be processed. if len(groups) == 0 or any( [not (group["settings"]["hidden"]) for group in groups]): score = get_score(tid=team["tid"]) if score > 0: key = get_scoreboard_key(team=team) result[key] = score scoreboard_cache.clear() if result: scoreboard_cache.add(result) return scoreboard_cache
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