def create_first_round(self): new_round = Round.create_round() serialized_round = vars(new_round) new_matchs_id = [] self.round_table.insert(serialized_round) list_of_players_by_ranking = self.list_of_players_by_ranking() Play = Query() for i in range(4): new_match = Match.create_match( self.player_table.get( Play.ranking == list_of_players_by_ranking[0][i]["ranking"] ).doc_id, self.player_table.get( Play.ranking == list_of_players_by_ranking[1][i]["ranking"] ).doc_id, ) serialized_match = vars(new_match) self.match_table.insert(serialized_match) new_matchs_id.append(self.match_table.all()[-1].doc_id) list_of_rounds = self.tournament_table.all()[-1]["rounds"] list_of_rounds.append(self.round_table.all()[-1].doc_id) self.round_table.update( {"list_of_match": new_matchs_id}, doc_ids=[self.round_table.all()[-1].doc_id], ) self.tournament_table.update( {"rounds": list_of_rounds}, doc_ids=[self.tournament_table.all()[-1].doc_id], ) self.warning.round_create(str(self.round_table.all()[-1]["current_round"])) self.table.matchs() return self.manage_tournament()
def create_round(self): """ Create a round. Update tournament and round table """ new_round = Round.create_round() serialized_round = vars(new_round) new_matchs_id = [] serialized_round["current_round"] = ( self.round_table.all()[-1]["current_round"] + 1 ) serialized_round["name"] = "Round " + str(serialized_round["current_round"]) self.round_table.insert(serialized_round) list_of_players_by_score = self.list_of_players_by_score() all_matchs_of_a_tournament = self.list_of_all_matchs_of_a_tournament() Play = Query() list_matchs = [] j = 0 try: for _ in range(4): k = 1 while self.is_match_already_play( all_matchs_of_a_tournament, self.player_table.get( Play.ranking == list_of_players_by_score[j]["ranking"] ).doc_id, self.player_table.get( Play.ranking == list_of_players_by_score[j + 1]["ranking"] ).doc_id, ): self.is_match_already_play( all_matchs_of_a_tournament, self.player_table.get( Play.ranking == list_of_players_by_score[j]["ranking"] ).doc_id, self.player_table.get( Play.ranking == list_of_players_by_score[j + 1 + k]["ranking"] ).doc_id, ) ( list_of_players_by_score[j + 1], list_of_players_by_score[j + 1 + k], ) = ( list_of_players_by_score[j + 1 + k], list_of_players_by_score[j + 1], ) k += 1 list_matchs.append( ( self.player_table.get( Play.ranking == list_of_players_by_score[j]["ranking"] ).doc_id, self.player_table.get( Play.ranking == list_of_players_by_score[j + 1]["ranking"] ).doc_id, ) ) j += 2 except IndexError: list_matchs.pop(-1) if ( self.is_permuted_match_existed( all_matchs_of_a_tournament, list_of_players_by_score, -1, -3 ) is not True and self.is_permuted_match_existed( all_matchs_of_a_tournament, list_of_players_by_score, -2, -4 ) is not True ): list_matchs.append( ( list_of_players_by_score[-1].doc_id, list_of_players_by_score[-3].doc_id, ) ) list_matchs.append( ( list_of_players_by_score[-2].doc_id, list_of_players_by_score[-4].doc_id, ) ) elif ( self.is_permuted_match_existed( all_matchs_of_a_tournament, list_of_players_by_score, -1, -4 ) is not True and self.is_permuted_match_existed( all_matchs_of_a_tournament, list_of_players_by_score, -2, -3 ) is not True ): list_matchs.append( ( list_of_players_by_score[-1].doc_id, list_of_players_by_score[-4].doc_id, ) ) list_matchs.append( ( list_of_players_by_score[-2].doc_id, list_of_players_by_score[-3].doc_id, ) ) elif ( self.is_permuted_match_existed( all_matchs_of_a_tournament, list_of_players_by_score, -1, -3 ) is not True and self.is_permuted_match_existed( all_matchs_of_a_tournament, list_of_players_by_score, -2, -5 ) is not True and self.is_permuted_match_existed( all_matchs_of_a_tournament, list_of_players_by_score, -4, -6 ) is not True ): list_matchs.pop(-1) list_matchs.append( ( list_of_players_by_score[-1].doc_id, list_of_players_by_score[-3].doc_id, ) ) list_matchs.append( ( list_of_players_by_score[-2].doc_id, list_of_players_by_score[-5].doc_id, ) ) list_matchs.append( ( list_of_players_by_score[-4].doc_id, list_of_players_by_score[-6].doc_id, ) ) elif ( self.is_permuted_match_existed( all_matchs_of_a_tournament, list_of_players_by_score, -1, -4 ) is not True and self.is_permuted_match_existed( all_matchs_of_a_tournament, list_of_players_by_score, -2, -5 ) is not True and self.is_permuted_match_existed( all_matchs_of_a_tournament, list_of_players_by_score, -3, -6 ) is not True ): list_matchs.pop(-1) list_matchs.append( ( list_of_players_by_score[-1].doc_id, list_of_players_by_score[-4].doc_id, ) ) list_matchs.append( ( list_of_players_by_score[-2].doc_id, list_of_players_by_score[-5].doc_id, ) ) list_matchs.append( ( list_of_players_by_score[-3].doc_id, list_of_players_by_score[-6].doc_id, ) ) elif ( self.is_permuted_match_existed( all_matchs_of_a_tournament, list_of_players_by_score, -2, -3 ) is not True and self.is_permuted_match_existed( all_matchs_of_a_tournament, list_of_players_by_score, -1, -5 ) is not True and self.is_permuted_match_existed( all_matchs_of_a_tournament, list_of_players_by_score, -4, -6 ) is not True ): list_matchs.pop(-1) list_matchs.append( ( list_of_players_by_score[-2].doc_id, list_of_players_by_score[-3].doc_id, ) ) list_matchs.append( ( list_of_players_by_score[-1].doc_id, list_of_players_by_score[-5].doc_id, ) ) list_matchs.append( ( list_of_players_by_score[-4].doc_id, list_of_players_by_score[-6].doc_id, ) ) elif ( self.is_permuted_match_existed( all_matchs_of_a_tournament, list_of_players_by_score, -2, -4 ) is not True and self.is_permuted_match_existed( all_matchs_of_a_tournament, list_of_players_by_score, -1, -5 ) is not True and self.is_permuted_match_existed( all_matchs_of_a_tournament, list_of_players_by_score, -3, -6 ) is not True ): list_matchs.pop(-1) list_matchs.append( ( list_of_players_by_score[-2].doc_id, list_of_players_by_score[-4].doc_id, ) ) list_matchs.append( ( list_of_players_by_score[-1].doc_id, list_of_players_by_score[-5].doc_id, ) ) list_matchs.append( ( list_of_players_by_score[-3].doc_id, list_of_players_by_score[-6].doc_id, ) ) elif ( self.is_permuted_match_existed( all_matchs_of_a_tournament, list_of_players_by_score, -1, -5 ) is not True and self.is_permuted_match_existed( all_matchs_of_a_tournament, list_of_players_by_score, -2, -6 ) is not True and self.is_permuted_match_existed( all_matchs_of_a_tournament, list_of_players_by_score, -3, -4 ) is not True ): list_matchs.pop(-1) list_matchs.append( ( list_of_players_by_score[-1].doc_id, list_of_players_by_score[-5].doc_id, ) ) list_matchs.append( ( list_of_players_by_score[-2].doc_id, list_of_players_by_score[-6].doc_id, ) ) list_matchs.append( ( list_of_players_by_score[-3].doc_id, list_of_players_by_score[-4].doc_id, ) ) elif ( self.is_permuted_match_existed( all_matchs_of_a_tournament, list_of_players_by_score, -1, -6 ) is not True and self.is_permuted_match_existed( all_matchs_of_a_tournament, list_of_players_by_score, -2, -5 ) is not True and self.is_permuted_match_existed( all_matchs_of_a_tournament, list_of_players_by_score, -3, -4 ) is not True ): list_matchs.pop(-1) list_matchs.append( ( list_of_players_by_score[-1].doc_id, list_of_players_by_score[-6].doc_id, ) ) list_matchs.append( ( list_of_players_by_score[-2].doc_id, list_of_players_by_score[-5].doc_id, ) ) list_matchs.append( ( list_of_players_by_score[-3].doc_id, list_of_players_by_score[-4].doc_id, ) ) for match in list_matchs: new_match = Match.create_match( match[0], match[1], ) serialized_match = vars(new_match) self.match_table.insert(serialized_match) new_matchs_id.append(self.match_table.all()[-1].doc_id) list_of_rounds = self.tournament_table.all()[-1]["rounds"] list_of_rounds.append(self.round_table.all()[-1].doc_id) self.round_table.update( {"list_of_match": new_matchs_id}, doc_ids=[self.round_table.all()[-1].doc_id], ) self.tournament_table.update( {"rounds": list_of_rounds}, doc_ids=[self.tournament_table.all()[-1].doc_id], ) self.warning.round_create(str(self.round_table.all()[-1]["current_round"])) self.table.matchs() return self.manage_tournament()