def get_ranking(self):
     if not self.finished():
         return []
     # --
     # Round type : Group or Ranking
     # --
     if self.type == 'G' or self.type == 'R':
         # Get all group rankings
         results_list = []
         for group in self.groups_list():
             results_list.extend(
                 convert_ranking_db_to_team_results(group.get_ranking()))
         # Compute ranks
         ranking_round = compute_ranking(results_list)
     # --
     # Round type : Final (with fianl blocked)
     # --
     elif self.type == 'F':
         # Get ranking of previous round
         prev_round = SchenkelRound.objects.get_prev(self)
         prev_ranking = prev_round.get_ranking()
         # Get ranking of all groups of this round
         ranking = []
         for group in self.groups_list():
             ranking.extend(
                 convert_ranking_db_to_team_results(group.get_ranking()))
         # Remove 2 first teams if final is blocked
         if self.final_is_blocked:
             finalists = ranking[:2]
             results_list = ranking[2:]
             prev_ranking = prev_ranking[2:]
             first_rank = 3
         else:
             finalists = None
             first_rank = 1
         # Merge two lists (if in this round take it, else take ranking from prev round)
         prev_ranking_decorated = {}
         for r in prev_ranking:
             prev_ranking_decorated[r.team] = r
         ranking_decorated = {}
         for r in ranking:
             ranking_decorated[r.team] = r
         global_results = []
         for team, rank in prev_ranking_decorated.items():
             if team in ranking_decorated.keys():
                 global_results.append(ranking_decorated[team])
             else:
                 global_results.append(rank)
         # Compute global ranking
         ranking_round = compute_ranking(global_results, first_rank)
         # Add finalists
         if finalists:
             ranking_round.insert(0, finalists[1])
             ranking_round.insert(0, finalists[0])
     else:
         raise ValueError()
     return ranking_round
 def get_ranking(self):
     if not self.finished():
         return []
     # --
     # Round type : Group or Ranking
     # --
     if self.type == 'G' or self.type == 'R':
         # Get all group rankings
         results_list = []
         for group in self.groups_list():
             results_list.extend(convert_ranking_db_to_team_results(group.get_ranking()))
         # Compute ranks
         ranking_round = compute_ranking(results_list)
     # --
     # Round type : Final (with fianl blocked)
     # --
     elif self.type == 'F':
         # Get ranking of previous round
         prev_round = SchenkelRound.objects.get_prev(self)
         prev_ranking = prev_round.get_ranking()
         # Get ranking of all groups of this round
         ranking = []
         for group in self.groups_list():
             ranking.extend(convert_ranking_db_to_team_results(group.get_ranking()))
         # Remove 2 first teams if final is blocked
         if self.final_is_blocked:
             finalists = ranking[:2]
             results_list = ranking[2:]
             prev_ranking = prev_ranking[2:]
             first_rank = 3
         else:
             finalists = None
             first_rank = 1
         # Merge two lists (if in this round take it, else take ranking from prev round)
         prev_ranking_decorated = {}
         for r in prev_ranking:
             prev_ranking_decorated[r.team] = r
         ranking_decorated = {}
         for r in ranking:
             ranking_decorated[r.team] = r
         global_results = []
         for team, rank in prev_ranking_decorated.items():
             if team in ranking_decorated.keys():
                 global_results.append(ranking_decorated[team])
             else:
                 global_results.append(rank)
         # Compute global ranking
         ranking_round = compute_ranking(global_results, first_rank)
         # Add finalists
         if finalists:
             ranking_round.insert(0, finalists[1])
             ranking_round.insert(0, finalists[0])
     else:
         raise ValueError()
     return ranking_round
 def compute_ranking_for_group(self, group):
     if not group.finished:
         return []
     # --
     # Round type : Group or Ranking (or Final with final not blocked)
     # --
     if self.type == 'G' or self.type == 'R' or not self.final_is_blocked:
         # Get results for the group
         results = group.get_results()
         # Get old ranking if exists
         # and compute new global results
         if self.order > 1:
             # Type 'Group'
             if self.type == 'G':
                 # Get only results of the group for previous round
                 prev_group = SchenkelGroup.objects.get_prev_round(group)
                 prev_ranking_db = prev_group.get_ranking()
             # Type 'Ranking'
             else:
                 # Get all results of all groups for the previous round
                 prev_round = SchenkelRound.objects.get_prev(self)
                 prev_ranking_db = prev_round.get_ranking()
             # Compute new results
             new_results = add_results_to_ranking(
                 convert_ranking_db_to_team_results(prev_ranking_db),
                 results)
         else:
             new_results = results
         # Compute new ranking
         new_ranking = compute_ranking(new_results)
         # Create GoupRanking objects
         new_ranking_objs = []
         for rank in new_ranking:
             obj = SchenkelGroupRanking.objects.create(
                 group=group,
                 team=rank.team,
                 rank=rank.rank,
                 ex_aequo=rank.ex_aequo,
                 points=rank.points,
                 ends=rank.ends,
                 stones=rank.stones,
                 ends_received=rank.ends_received,
                 stones_received=rank.stones_received)
             new_ranking_objs.append(obj)
         # Try to prepare matches of next round
         if self.type != 'F':
             next_round_group = SchenkelGroup.objects.get_next_round(group)
             if next_round_group:
                 next_round_group.populate_matches()
         return new_ranking_objs
     # --
     # Round type : Final (with fianl blocked)
     # --
     elif self.type == 'F':
         # --
         # Only 2 first teams can win the tournament:
         # --
         # Get all results of all groups for the previous round
         prev_round = SchenkelRound.objects.get_prev(self)
         prev_ranking = convert_ranking_db_to_team_results(
             prev_round.get_ranking())
         # Get 2 first teams before compute new ranking
         finalists_prev_ranking = [prev_ranking[0], prev_ranking[1]]
         prev_ranking.remove(finalists_prev_ranking[0])
         prev_ranking.remove(finalists_prev_ranking[1])
         finalists_teams = [
             finalist.team for finalist in finalists_prev_ranking
         ]
         finalists_results = []
         # Get results of group
         results = group.get_results()
         # Get results of 2 first teams
         for result in results:
             if result.team in finalists_teams:
                 finalists_results.append(result)
         # Remove those results of the global list
         for result in finalists_results:
             results.remove(result)
         # Get winner and looser fo final match
         if finalists_results[0].points > finalists_results[1].points:
             winner = finalists_results[0].team
         elif finalists_results[0].points < finalists_results[1].points:
             winner = finalists_results[1].team
         else:
             winner = None
             raise NotImplementedError
         # Compute "normal" ranking for other team
         new_results = add_results_to_ranking(prev_ranking, results)
         new_tmp_ranking = compute_ranking(new_results, first_rank=3)
         # Compute new results for finalists
         finalists_new_results = add_results_to_ranking(
             finalists_prev_ranking, finalists_results)
         # Add the 2 finalist at the begining of the ranking (winner in 1st, looser in 2nd)
         finalists_results[0].ex_aequo = finalists_results[
             1].ex_aequo = False
         if finalists_results[0].team == winner:
             finalists_new_results[1].rank = 2
             new_tmp_ranking.insert(0, finalists_new_results[1])
             finalists_new_results[0].rank = 1
             new_tmp_ranking.insert(0, finalists_new_results[0])
         else:
             finalists_results[0].rank = 2
             new_tmp_ranking.insert(0, finalists_new_results[0])
             finalists_results[1].rank = 1
             new_tmp_ranking.insert(0, finalists_new_results[1])
         # Create Ranking in db
         new_ranking_objs = []
         for rank in new_tmp_ranking:
             obj = SchenkelGroupRanking.objects.create(
                 group=group,
                 team=rank.team,
                 rank=rank.rank,
                 ex_aequo=rank.ex_aequo,
                 points=rank.points,
                 ends=rank.ends,
                 stones=rank.stones,
                 ends_received=rank.ends_received,
                 stones_received=rank.stones_received)
             new_ranking_objs.append(obj)
         return new_ranking_objs
     else:
         raise ValueError()
 def compute_ranking_for_group(self, group):
     if not group.finished:
         return []
     # --
     # Round type : Group or Ranking (or Final with final not blocked)
     # --
     if self.type == 'G' or self.type == 'R' or not self.final_is_blocked:
         # Get results for the group
         results = group.get_results()
         # Get old ranking if exists
         # and compute new global results
         if self.order > 1:
             # Type 'Group'
             if self.type == 'G':
                 # Get only results of the group for previous round
                 prev_group = SchenkelGroup.objects.get_prev_round(group)
                 prev_ranking_db = prev_group.get_ranking()
             # Type 'Ranking'
             else:
                 # Get all results of all groups for the previous round
                 prev_round = SchenkelRound.objects.get_prev(self)
                 prev_ranking_db = prev_round.get_ranking()
             # Compute new results
             new_results = add_results_to_ranking(convert_ranking_db_to_team_results(prev_ranking_db), results)
         else:
             new_results = results
         # Compute new ranking
         new_ranking = compute_ranking(new_results)
         # Create GoupRanking objects
         new_ranking_objs = []
         for rank in new_ranking:
             obj = SchenkelGroupRanking.objects.create(group=group, team=rank.team,
                                                       rank=rank.rank, ex_aequo=rank.ex_aequo,
                                                       points=rank.points, ends=rank.ends, stones=rank.stones,
                                                       ends_received=rank.ends_received, stones_received=rank.stones_received)
             new_ranking_objs.append(obj)
         # Try to prepare matches of next round
         if self.type != 'F':
             next_round_group = SchenkelGroup.objects.get_next_round(group)
             if next_round_group:
                 next_round_group.populate_matches()
         return new_ranking_objs
     # --
     # Round type : Final (with fianl blocked)
     # --
     elif self.type == 'F':
         # --
         # Only 2 first teams can win the tournament:
         # --
         # Get all results of all groups for the previous round
         prev_round = SchenkelRound.objects.get_prev(self)
         prev_ranking = convert_ranking_db_to_team_results(prev_round.get_ranking())
         # Get 2 first teams before compute new ranking
         finalists_prev_ranking = [prev_ranking[0], prev_ranking[1]]
         prev_ranking.remove(finalists_prev_ranking[0])
         prev_ranking.remove(finalists_prev_ranking[1])
         finalists_teams = [finalist.team for finalist in finalists_prev_ranking]
         finalists_results = []
         # Get results of group
         results = group.get_results()
         # Get results of 2 first teams
         for result in results:
             if result.team in finalists_teams:
                 finalists_results.append(result)
         # Remove those results of the global list
         for result in finalists_results:
             results.remove(result)
         # Get winner and looser fo final match
         if finalists_results[0].points > finalists_results[1].points:
             winner = finalists_results[0].team
         elif finalists_results[0].points < finalists_results[1].points:
             winner = finalists_results[1].team
         else:
             winner = None
             raise NotImplementedError
         # Compute "normal" ranking for other team
         new_results = add_results_to_ranking(prev_ranking, results)
         new_tmp_ranking = compute_ranking(new_results, first_rank=3)
         # Compute new results for finalists
         finalists_new_results = add_results_to_ranking(finalists_prev_ranking, finalists_results)
         # Add the 2 finalist at the begining of the ranking (winner in 1st, looser in 2nd)
         finalists_results[0].ex_aequo = finalists_results[1].ex_aequo = False
         if finalists_results[0].team == winner:
             finalists_new_results[1].rank = 2
             new_tmp_ranking.insert(0, finalists_new_results[1])
             finalists_new_results[0].rank = 1
             new_tmp_ranking.insert(0, finalists_new_results[0])
         else:
             finalists_results[0].rank = 2
             new_tmp_ranking.insert(0, finalists_new_results[0])
             finalists_results[1].rank = 1
             new_tmp_ranking.insert(0, finalists_new_results[1])
         # Create Ranking in db
         new_ranking_objs = []
         for rank in new_tmp_ranking:
             obj = SchenkelGroupRanking.objects.create(group=group, team=rank.team,
                                                       rank=rank.rank, ex_aequo=rank.ex_aequo,
                                                       points=rank.points, ends=rank.ends, stones=rank.stones,
                                                       ends_received=rank.ends_received, stones_received=rank.stones_received)
             new_ranking_objs.append(obj)
         return new_ranking_objs
     else:
         raise ValueError()