def generate_match_list(contest_id): contest = get_contest_by_id(contest_id) player_list = get_player_list_by_contest_id(contest_id) print player_list.userIds if contest.format == "Single Round-Robin": n = contest.totalPlayers if n % 2 == 0: matches = [] for i in range(0, n - 1): for j in range(i, n - 1): t = (i + j) % (n - 1) + 1 if i == j: player1_id = player_list.userIds[i] player2_id = player_list.userIds[n - 1] else: player1_id = player_list.userIds[i] player2_id = player_list.userIds[j] match = Match(player1Id = player1_id, player2Id = player2_id, score1 = -1, score2 = -1, day = t) match.save() matches.append(match) else: matches = [] for i in range(0, n): for j in range(i, n): t = (i + j) % n + 1 if i == j: player1_id = player_list.userIds[i] player2_id = usermanage.get_user_by_name("", "BYE").id else: player1_id = player_list.userIds[i] player2_id = player_list.userIds[j] match = Match(player1Id = player1_id, player2Id = player2_id, score1 = -1, score2 = -1, day = t) match.save() matches.append(match) matches.sort(key = lambda Match: Match.day) match_list = MatchList(contestId = contest_id, matches = []) for i in matches: match_list.matches.append(i.id) match_list.save()
def load_file(file_name): """ Load game data from a file. Do not forget to drop the collection before re-importing a same file! :param file_name: path to the file """ circuit = "" schedules = dict() with open(file_name, mode="r", encoding="utf-8-sig") as file: for line_number, line in enumerate(file): line = line[:-1] cells = line.split(properties.LIST_SEPARATOR) game = Game() if line_number == 0: circuit = cells[0] continue if line_number == 1: for i in range(3, len(cells), 2): schedules[i] = (cells[i]) continue game.number = int(cells[0]) game.hash = hashlib.sha1("Baden {} Battle".format( cells[0]).encode()).hexdigest() game.name = cells[1] game.leader = cells[2] game.circuit = circuit for i in range(3, len(cells), 2): match = Match() match.time = (i - 1) / 2 match.schedule = schedules[i] match.game_number = int(cells[0]) match.players_number.append(int(cells[i])) match.players_number.append(int(cells[i + 1])) match.save() game.matches.append(match) game.save()