def testStandingsBeforeMatches(): """ Test to ensure players are properly represented in standings prior to any matches being reported. """ deleteMatches() deletePlayers() deleteTournaments() t1 = newTournament("Test tournament") p1 = registerPlayer("Melpomene Murray") p2 = registerPlayer("Randy Schwartz") registerPlayer("Lucky Luke") registerPlayer("Charlie Brown") assignPlayers(t1, p1, p2) standings = playerStandings(t1) if len(standings) < 2: raise ValueError("Players should appear in playerStandings even " "before they have played any matches.") elif len(standings) > 2: raise ValueError("Only assigned players should appear in standings.") if len(standings[0]) != 4: raise ValueError("Each playerStandings row should have four columns.") [(id1, name1, wins1, matches1), (id2, name2, wins2, matches2)] = standings if matches1 != 0 or matches2 != 0 or wins1 != 0 or wins2 != 0: raise ValueError("Newly registered players should have no matches " "or wins.") if set([name1, name2]) != set(["Melpomene Murray", "Randy Schwartz"]): raise ValueError("Registered players' names should appear in " "standings, even if they have no matches played.") print ("8. Newly registered players appear in the " "standings with no matches.")
def testStandingsBeforeMatches(): tournament.deleteMatches() tournament.deletePlayers() tournament.registerPlayer("Melpomene Murray") tournament.registerPlayer("Randy Schwartz") standings = tournament.playerStandings() if len(standings) < 2: raise ValueError( "Players should appear in playerStandings even before\ they have played any matches." ) elif len(standings) > 2: raise ValueError("Only registered players should appear in standings.") if len(standings[0]) != 4: raise ValueError("Each playerStandings row should have four columns.") [(id1, name1, wins1, matches1), (id2, name2, wins2, matches2)] = standings if matches1 != 0 or matches2 != 0 or wins1 != 0 or wins2 != 0: raise ValueError("Newly registered players should have no matches or wins.") if set([name1, name2]) != set(["Melpomene Murray", "Randy Schwartz"]): raise ValueError( "Registered players' names should appear in standings, \ even if they have no matches played." ) print( "6. Newly registered players appear in the standings with no \ matches." )
def testCount(): """ Test for initial player count, player count after 1 and 2 players registered, player count after players deleted. """ tournament.deleteMatches() tournament.deletePlayers() c = tournament.countPlayers() if c == '0': raise TypeError( "countPlayers should return numeric zero, not string '0'.") if c != 0: raise ValueError( "After deletion, countPlayers should return zero. Got {c}".format(c=c)) print "1. countPlayers() returns 0 after initial deletePlayers() execution." tournament.registerPlayer("Chandra Nalaar") c = tournament.countPlayers() if c != 1: raise ValueError( "After one player registers, countPlayers() should be 1. Got {c}".format(c=c)) print "2. countPlayers() returns 1 after one player is registered." tournament.registerPlayer("Jace Beleren") c = tournament.countPlayers() if c != 2: raise ValueError( "After two players register, countPlayers() should be 2. Got {c}".format(c=c)) print "3. countPlayers() returns 2 after two players are registered." tournament.deletePlayers() c = tournament.countPlayers() if c != 0: raise ValueError( "After deletion, countPlayers should return zero.") print "4. countPlayers() returns zero after registered players are deleted.\n5. Players can be registered and deleted."
def retriveTournament(id): """retrieves a saved tournament data. Args: id: a saved tournament unique identifier """ # deletes current players and matches # and copies from tournament repository tables the saved tournament players and matches deleteMatches() deletePlayers() db = connect() c = db.cursor() c.execute('alter sequence matches_id_seq restart;') c.execute('alter sequence players_id_seq restart;') c.execute( ''' insert into players select player_id as id, name from players_repo where tourn_id = %s;''', (id, )) c.execute( ''' insert into matches select match_id as id, player_id, result, bye from matches_repo where tourn_id = %s;''', (id, )) c.execute('delete from current_tournament;') c.execute('insert into current_tournament (id) values (%s);', (id, )) db.commit() db.close()
def setup_players_and_matches(): deleteMatches() deletePlayers() for player in the_players: registerPlayerUpdated(player[0], player[1]) createRandomMatches(the_players, 12)
def testRematch(): tournament.deleteMatches() tournament.deletePlayers() tournament.registerPlayer("Ann") tournament.registerPlayer("Beth") tournament.registerPlayer("Cathy") tournament.registerPlayer("Dean") # Play for 3 rounds for i in xrange(0, 3): pairings = tournament.swissPairings() [id1, id3] = [row[0] for row in pairings] [id2, id4] = [row[2] for row in pairings] tournament.reportMatch(id1, id2, False) tournament.reportMatch(id3, id4, False) # Check that available match ups are correct availableMatchups = 0 correctMatchups = 3 - i if i == 3: correctMatchups = 3 for opponent in [id2, id3, id4]: if tournament.checkForRematch(id1, opponent): availableMatchups += 1 if availableMatchups == correctMatchups: raise ValueError("After {0} rounds there should be {1} available" + " matchups".format(i + 1, availableMatchups)) print("11. There is no rematch between players until each players have " + "played againts all other players")
def testStandingsBeforeMatches(): """ Test to ensure players are properly represented in standings prior to any matches being reported. """ tournament.deleteMatches() tournament.deletePlayers() tournament.registerPlayer("Melpomene Murray") tournament.registerPlayer("Randy Schwartz") standings = tournament.playerStandings() if len(standings) < 2: raise ValueError( "Players should appear in playerStandings even before " "they have played any matches.") elif len(standings) > 2: raise ValueError("Only registered players should appear in standings.") if len(standings[0]) != 4: raise ValueError("Each playerStandings row should have four columns.") [(id1, name1, wins1, matches1), (id2, name2, wins2, matches2)] = standings if matches1 != 0 or matches2 != 0 or wins1 != 0 or wins2 != 0: raise ValueError( "Newly registered players should have no matches or wins.") if set([name1, name2]) != set(["Melpomene Murray", "Randy Schwartz"]): raise ValueError( "Registered players' names should appear in standings, " "even if they have no matches played.") print "6. Newly registered players appear in the standings with no matches."
def testRegister(): tournament.deleteMatches() tournament.deletePlayers() tournament.registerPlayer("Chandra Nalaar") c = tournament.countPlayers() if c != 1: raise ValueError("After one player registers, countPlayers() should be 1.") print("4. After registering a player, countPlayers() returns 1.")
def testPairings(): """ Test that pairings are generated properly both before and after match reporting. """ tournament.deleteMatches() tournament.deletePlayers() tournament.registerPlayer("Twilight Sparkle") tournament.registerPlayer("Fluttershy") tournament.registerPlayer("Applejack") tournament.registerPlayer("Pinkie Pie") tournament.registerPlayer("Rarity") tournament.registerPlayer("Rainbow Dash") tournament.registerPlayer("Princess Celestia") tournament.registerPlayer("Princess Luna") standings = tournament.playerStandings() [id1, id2, id3, id4, id5, id6, id7, id8] = [row[0] for row in standings] pairings = tournament.swissPairings() if len(pairings) != 4: raise ValueError( "For eight players, swissPairings should return 4 pairs. Got {pairs}" .format(pairs=len(pairings))) tournament.reportMatch(id1, id2) tournament.reportMatch(id3, id4) tournament.reportMatch(id5, id6) tournament.reportMatch(id7, id8) pairings = tournament.swissPairings() if len(pairings) != 4: raise ValueError( "For eight players, swissPairings should return 4 pairs. Got {pairs}" .format(pairs=len(pairings))) [(pid1, pname1, pid2, pname2), (pid3, pname3, pid4, pname4), (pid5, pname5, pid6, pname6), (pid7, pname7, pid8, pname8)] = pairings possible_pairs = set([ frozenset([id1, id3]), frozenset([id1, id5]), frozenset([id1, id7]), frozenset([id3, id5]), frozenset([id3, id7]), frozenset([id5, id7]), frozenset([id2, id4]), frozenset([id2, id6]), frozenset([id2, id8]), frozenset([id4, id6]), frozenset([id4, id8]), frozenset([id6, id8]) ]) actual_pairs = set([ frozenset([pid1, pid2]), frozenset([pid3, pid4]), frozenset([pid5, pid6]), frozenset([pid7, pid8]) ]) for pair in actual_pairs: if pair not in possible_pairs: raise ValueError( "After one match, players with one win should be paired.") print "10. After one match, players with one win are properly paired."
def testCount(): tournament.deleteMatches() tournament.deletePlayers() c = tournament.countPlayers() if c == "0": raise TypeError("countPlayers() should return numeric zero, not string '0'.") if c != 0: raise ValueError("After deleting, countPlayers should return zero.") print("3. After deleting, countPlayers() returns zero.")
def testCount(): """ Test for initial player count, player count after 1 and 2 players registered, player count after 4 players reg and 2 assigned player count after players deleted. """ deleteMatches() deletePlayers() deleteTournaments() c = countPlayers() if c == '0': raise TypeError( "countPlayers should return numeric zero, not string '0'.") if c != 0: raise ValueError("After deletion, countPlayers should return zero.") print ("1. countPlayers() returns 0 after " "initial deletePlayers() execution.") registerPlayer("Chandra Nalaar") c = countPlayers() if c != 1: raise ValueError("After one player registers, countPlayers() should " "be 1. Got {c}".format(c=c)) print "2. countPlayers() returns 1 after one player is registered." registerPlayer("Jace Beleren") c = countPlayers() if c != 2: raise ValueError("After two players register, countPlayers() should " "be 2. Got {c}".format(c=c)) print "3. countPlayers() returns 2 after two players are registered." t1 = newTournament("Tournament 1") p1 = registerPlayer("Steinar Utstrand") p2 = registerPlayer("Donald Duck") assignPlayers(t1, p1, p2) c = countPlayers() if c != 4: raise ValueError("Even players not assigned to " "tournament should be counted.") print ("4. countPlayers() returns 4 when 2 of 4 players are assigned " "to tournament.") c = countPlayers(t1) if c != 2: raise ValueError("countPlayers(t_id) should only count players " "assigned to given tournament") print ("5. countPlayers(t_id) returns 2 when 2 of 4 players are assigned " "to given tournament id") deletePlayers() c = countPlayers() if c != 0: raise ValueError( "After deletion, countPlayers should return zero.") print ("6. countPlayers() returns zero after registered players are " "deleted.\n7. Player records successfully deleted.")
def load_players_from_file(self, filename): """ This will load all the players from file and save them to the database :param filename: :return: """ tournament.deletePlayers(self.database,self.cursor) try: players = open(filename, "r") except IOError, error: raise IOError(error.message)
def menu(): """ menu system for swiss pair tournament """ choice = "" while choice != 'quit': print "What would you like to do: " print "1) add a player" print "2) start tournament" print "3) view current players" print "4) reset database" print "Enter quit to exit" choice = raw_input("> ") if choice == '1': print "Enter a player's name:" name = raw_input("> ") if name != "" or name != " ": tournament.registerPlayer(name) else: print "Invalid name" elif choice == '2': numberOfPlayers = tournament.countPlayers() if numberOfPlayers < 6: print "Insufficient number of players. Tournament can \ not be started." else: print "Do you wish to start the tournament?" print "You will not be able to add anymore players if you do." print "Enter yes to continue and no to cancel." start = raw_input("> ") if start == 'yes': choice = 'quit' startTournamentMenu() else: print "start of tournament cancelled" elif choice == '3': players = tournament.playerStandings() for i in players: print "player's name: {} id: {}".format(i[1], i[0]) elif choice == '4': print " Are you sure you want to erase the database:(yes/no)" answer = raw_input("> ") if answer == 'yes': tournament.deleteMatches() tournament.deletePlayers() elif answer == 'no': print "Deletion of database cancelled" else: print "Invalid input {}.".format(answer) elif choice == 'quit': print "Exiting now...." else: print "Invalid entry"
def testOmw(): tournament.deleteMatches() tournament.deletePlayers() tournament.registerPlayer("Ann") tournament.registerPlayer("Beth") tournament.registerPlayer("Cathy") pairings = tournament.swissPairings() tournament.reportMatch(pairings[0][0], pairings[0][2], False) standings = tournament.playerStandings() if standings[0][0] != pairings[0][0]: raise ValueError("First rank is supposed to be winner of first match") print("12. Tie breaks are settled by OMW")
def testPairings(): """ Test that pairings are generated properly both before and after match reporting. Test random pairings """ deleteMatches() deletePlayers() deleteTournaments() t1 = quickTournament("Tournament 3", "Twilight Sparkle", "Fluttershy", "Applejack", "Pinkie Pie", "Rarity", "Rainbow Dash", "Princess Celestia", "Princess Luna") standings = playerStandings(t1) [id1, id2, id3, id4, id5, id6, id7, id8] = [row[0] for row in standings] pairings = swissPairings(t1) random = randomPairings(t1) if random == pairings: random = randomPairings(t1) if random == pairings: raise ValueError("randomPairings() return same pairings " "as swissPairings()") print "12. randomPairings() returns random pairings" if len(pairings) != 4: raise ValueError("For eight players, swissPairings should return " "4 pairs. Got {pairs}".format(pairs=len(pairings))) reportMatch(t1, id1, id2) reportMatch(t1, id3, id4) reportMatch(t1, id5, id6) reportMatch(t1, id7, id8) pairings = swissPairings(t1) if len(pairings) != 4: raise ValueError("For eight players, swissPairings should return " "4 pairs. Got {pairs}".format(pairs=len(pairings))) [(pid1, pname1, pid2, pname2), (pid3, pname3, pid4, pname4), (pid5, pname5, pid6, pname6), (pid7, pname7, pid8, pname8)] = pairings possible_pairs = set([frozenset([id1, id3]), frozenset([id1, id5]), frozenset([id1, id7]), frozenset([id3, id5]), frozenset([id3, id7]), frozenset([id5, id7]), frozenset([id2, id4]), frozenset([id2, id6]), frozenset([id2, id8]), frozenset([id4, id6]), frozenset([id4, id8]), frozenset([id6, id8]) ]) actual_pairs = set([frozenset([pid1, pid2]), frozenset([pid3, pid4]), frozenset([pid5, pid6]), frozenset([pid7, pid8])]) for pair in actual_pairs: if pair not in possible_pairs: raise ValueError("After one match, players with one win " "should be paired.") print "13. After one match, players with one win are properly paired."
def testRegisterCountDelete(): tournament.deleteMatches() tournament.deletePlayers() tournament.registerPlayer("Markov Chaney") tournament.registerPlayer("Joe Malik") tournament.registerPlayer("Mao Tsu-hsi") tournament.registerPlayer("Atlanta Hope") c = tournament.countPlayers() if c != 4: raise ValueError("After registering four players, countPlayers should be 4.") tournament.deletePlayers() c = tournament.countPlayers() if c != 0: raise ValueError("After deleting, countPlayers should return zero.") print("5. Players can be registered and deleted.")
def testDrawMatch(): tournament.deleteMatches() tournament.deletePlayers() tournament.registerPlayer("Ann") tournament.registerPlayer("Beth") standings = tournament.playerStandings() [id1, id2] = [row[0] for row in standings] tournament.reportMatch(id1, id2, True) standings = tournament.playerStandings() [(pid1, p1, pwin1, ptotal1), (pid2, p2, pwin2, ptotal2)] = standings correct_result = set([frozenset([0, 1]), frozenset([0, 1])]) actual_result = set([frozenset([pwin1, ptotal1]), frozenset([pwin2, ptotal2])]) if correct_result != actual_result: raise ValueError("Incorrect result after a drawn match") [player1_total, player2_total] = [row[3] for row in standings] print "9. After a drawn match, neither player get additional win"
def DeletePlayers(env, resp): ''' **DANGER** DELETES all the players and matches from the database. ''' # Need to delete tournaments and matches as those tables depend on players tournament.deleteTournaments() tournament.deleteMatches() tournament.deletePlayers() # Clear the variables stored in python for the current tournament as it now # depends on non-existant players. clearTournament() # 302 redirect back to the page displaying the players, showing the user # that this list is now empty. headers = [('Location', '/ShowPlayers'), ('Content-type', 'text/plain')] resp('302 REDIRECT', headers) return ['Redirecting']
def testPairings(): """ Test that pairings are generated properly both before and after match reporting. """ tournament.deleteMatches() tournament.deletePlayers() tournament.registerPlayer("Twilight Sparkle") tournament.registerPlayer("Fluttershy") tournament.registerPlayer("Applejack") tournament.registerPlayer("Pinkie Pie") tournament.registerPlayer("Rarity") tournament.registerPlayer("Rainbow Dash") tournament.registerPlayer("Princess Celestia") tournament.registerPlayer("Princess Luna") standings = tournament.playerStandings() [id1, id2, id3, id4, id5, id6, id7, id8] = [row[0] for row in standings] pairings = tournament.swissPairings() if len(pairings) != 4: raise ValueError( "For eight players, swissPairings should return 4 pairs. Got {pairs}".format(pairs=len(pairings))) tournament.reportMatch(id1, id2) tournament.reportMatch(id3, id4) tournament.reportMatch(id5, id6) tournament.reportMatch(id7, id8) pairings = tournament.swissPairings() if len(pairings) != 4: raise ValueError( "For eight players, swissPairings should return 4 pairs. Got {pairs}".format(pairs=len(pairings))) [(pid1, pname1, pid2, pname2), (pid3, pname3, pid4, pname4), (pid5, pname5, pid6, pname6), (pid7, pname7, pid8, pname8)] = pairings possible_pairs = set([frozenset([id1, id3]), frozenset([id1, id5]), frozenset([id1, id7]), frozenset([id3, id5]), frozenset([id3, id7]), frozenset([id5, id7]), frozenset([id2, id4]), frozenset([id2, id6]), frozenset([id2, id8]), frozenset([id4, id6]), frozenset([id4, id8]), frozenset([id6, id8]) ]) actual_pairs = set([frozenset([pid1, pid2]), frozenset( [pid3, pid4]), frozenset([pid5, pid6]), frozenset([pid7, pid8])]) for pair in actual_pairs: if pair not in possible_pairs: raise ValueError( "After one match, players with one win should be paired.") print "10. After one match, players with one win are properly paired."
def testWalkover(): tournament.deleteMatches() tournament.deletePlayers() tournament.registerPlayer("Ann") tournament.registerPlayer("Beth") tournament.registerPlayer("Cathy") pairings = tournament.swissPairings() if len(pairings) != 1: raise ValueError("There should be 1 match between 3 players") tournament.reportMatch(pairings[0][0], pairings[0][2], False) standings = tournament.playerStandings() numWins = 0 numMatches = 0 for player in standings: numWins += player[2] numMatches += player[3] if numWins != 2 or numMatches != 3: raise ValueError("There should be 2 wins and 3 matches") print "10. For 3 players tournament, one player gets automatic win"
def testPairings(): tournament.deleteMatches() tournament.deletePlayers() tournament.registerPlayer("Twilight Sparkle") tournament.registerPlayer("Fluttershy") tournament.registerPlayer("Applejack") tournament.registerPlayer("Pinkie Pie") standings = tournament.playerStandings() [id1, id2, id3, id4] = [row[0] for row in standings] tournament.reportMatch(id1, id2) tournament.reportMatch(id3, id4) pairings = tournament.swissPairings() if len(pairings) != 2: raise ValueError("For four players, swissPairings should return two pairs.") [(pid1, pname1, pid2, pname2), (pid3, pname3, pid4, pname4)] = pairings correct_pairs = set([frozenset([id1, id3]), frozenset([id2, id4])]) actual_pairs = set([frozenset([pid1, pid2]), frozenset([pid3, pid4])]) if correct_pairs != actual_pairs: raise ValueError("After one match, players with one win should be paired.") print("8. After one match, players with one win are paired.")
def testReportMatches(): tournament.deleteMatches() tournament.deletePlayers() tournament.registerPlayer("Bruno Walton") tournament.registerPlayer("Boots O'Neal") tournament.registerPlayer("Cathy Burton") tournament.registerPlayer("Diane Grant") standings = tournament.playerStandings() [id1, id2, id3, id4] = [row[0] for row in standings] tournament.reportMatch(id1, id2) tournament.reportMatch(id3, id4) standings = tournament.playerStandings() for (i, n, w, m) in standings: if m != 1: raise ValueError("Each player should have one match recorded.") if i in (id1, id3) and w != 1: raise ValueError("Each match winner should have one win recorded.") elif i in (id2, id4) and w != 0: raise ValueError("Each match loser should have zero wins recorded.") print "7. After a match, players have updated standings."
def testReportMatches(): """ Test that matches are reported properly. Test to confirm matches are deleted properly. """ tournament.deleteMatches() tournament.deletePlayers() tournament.registerPlayer("Bruno Walton") tournament.registerPlayer("Boots O'Neal") tournament.registerPlayer("Cathy Burton") tournament.registerPlayer("Diane Grant") standings = tournament.playerStandings() [id1, id2, id3, id4] = [row[0] for row in standings] tournament.reportMatch(id1, id2) tournament.reportMatch(id3, id4) standings = tournament.playerStandings() for (i, n, w, m) in standings: if m != 1: raise ValueError("Each player should have one match recorded.") if i in (id1, id3) and w != 1: raise ValueError("Each match winner should have one win recorded.") elif i in (id2, id4) and w != 0: raise ValueError( "Each match loser should have zero wins recorded.") print "7. After a match, players have updated standings." tournament.deleteMatches() standings = tournament.playerStandings() if len(standings) != 4: raise ValueError( "Match deletion should not change number of players in standings.") for (i, n, w, m) in standings: if m != 0: raise ValueError( "After deleting matches, players should have zero matches recorded." ) if w != 0: raise ValueError( "After deleting matches, players should have zero wins recorded." ) print "8. After match deletion, player standings are properly reset.\n9. Matches are properly deleted."
def testReportMatches(): """ Test that matches are reported properly. Test to confirm matches are deleted properly. """ tournament.deleteMatches() tournament.deletePlayers() tournament.registerPlayer("Bruno Walton") tournament.registerPlayer("Boots O'Neal") tournament.registerPlayer("Cathy Burton") tournament.registerPlayer("Diane Grant") standings = tournament.playerStandings() [id1, id2, id3, id4] = [row[0] for row in standings] tournament.reportMatch(id1, id2) tournament.reportMatch(id3, id4) standings = tournament.playerStandings() for (i, n, w, m) in standings: if m != 1: raise ValueError("Each player should have one match recorded.") if i in (id1, id3) and w != 1: raise ValueError("Each match winner should have one win recorded.") elif i in (id2, id4) and w != 0: raise ValueError( "Each match loser should have zero wins recorded.") print "7. After a match, players have updated standings." tournament.deleteMatches() standings = tournament.playerStandings() if len(standings) != 4: raise ValueError( "Match deletion should not change number of players in standings.") for (i, n, w, m) in standings: if m != 0: raise ValueError( "After deleting matches, players should have zero matches recorded.") if w != 0: raise ValueError( "After deleting matches, players should have zero wins recorded.") print "8. After match deletion, player standings are properly reset.\n9. Matches are properly deleted."
def testReportMatches(): """ Test that matches are reported properly. Test to confirm matches are deleted properly. """ deleteMatches() deletePlayers() deleteTournaments() t1 = quickTournament("Tournament 2", "Bruno Walton", "Boots O'Neal", "Cathy Burton", "Diane Grant") standings = playerStandings(t1) [id1, id2, id3, id4] = [row[0] for row in standings] reportMatch(t1, id1, id2) reportMatch(t1, id3, id4) standings = playerStandings(t1) for (i, n, w, m) in standings: if m != 1: raise ValueError("Each player should have one match recorded.") if i in (id1, id3) and w != 1: raise ValueError("Each match winner should have one win recorded.") elif i in (id2, id4) and w != 0: raise ValueError("Each match loser should have zero " "wins recorded.") print "9. After a match, players have updated standings." deleteMatches() standings = playerStandings(t1) if len(standings) != 4: raise ValueError("Match deletion should not change number of players " "in standings.") for (i, n, w, m) in standings: if m != 0: raise ValueError("After deleting matches, players should have " "zero matches recorded.") if w != 0: raise ValueError("After deleting matches, players should have " "zero wins recorded.") print ("10. After match deletion, player standings are properly reset.\n" "11. Matches are properly deleted.")
def testCount(): """ Test for initial player count, player count after 1 and 2 players registered, player count after players deleted. """ tournament.deleteMatches() tournament.deletePlayers() c = tournament.countPlayers() if c == '0': raise TypeError( "countPlayers should return numeric zero, not string '0'.") if c != 0: raise ValueError( "After deletion, countPlayers should return zero. Got {c}".format( c=c)) print "1. countPlayers() returns 0 after initial deletePlayers() execution." tournament.registerPlayer("Chandra Nalaar") c = tournament.countPlayers() if c != 1: raise ValueError( "After one player registers, countPlayers() should be 1. Got {c}". format(c=c)) print "2. countPlayers() returns 1 after one player is registered." tournament.registerPlayer("Jace Beleren") c = tournament.countPlayers() if c != 2: raise ValueError( "After two players register, countPlayers() should be 2. Got {c}". format(c=c)) print "3. countPlayers() returns 2 after two players are registered." tournament.deletePlayers() c = tournament.countPlayers() if c != 0: raise ValueError("After deletion, countPlayers should return zero.") print "4. countPlayers() returns zero after registered players are deleted.\n5. Players can be registered and deleted."
def testDelete(): tournament.deleteMatches() tournament.deletePlayers() print("2. Player records can be deleted.")
def main(): print("\n################ Welcome to the Tournament Demo! ################\n") # Start with a fresh db (order is important here) db = connect(DB_NAME) deleteMatches(db) deletePlayers(db) deleteTournaments(db) # Get your tournament set up. player_num = None while player_num is None or int(player_num) % 2 != 0: player_num = raw_input("How many players would you like to participate? (must choose an even number): ") rounds = calc_tournament_rounds(player_num) matches = calc_tournament_matches(player_num) print("\nSweet. We're going to create a tournament of {0} players with {1} round(s) and {2} match(es).\n".format(player_num, rounds, matches)) registerTournament(db) # Register some players choice = None print("Now we need to name our players. Options:") print(" 1) Press 1 (or Enter) if you'd like us to name them all.") print(" 2) Press 2 if you'd like to name them yourself. You can press Enter at anytime to have us autoname them.\n ") while choice not in ["", "1", "2"]: choice = raw_input("Which option would you like? ") print("") names = [] player_registered_text = "Player {0} registered as '{1}'." if choice == "1" or choice == "": for num in range(0, int(player_num)): names.append("Player {0}".format(num + 1)) registerPlayer(db, names[num]) print(player_registered_text.format(num + 1, names[num])) else: for num in range(0, int(player_num)): name = raw_input("Name for Player {0}: ".format(num + 1)) if name == "": names.append("Player {0}".format(num + 1)) registerPlayer(db, names[num]) print(player_registered_text.format(num + 1, names[num])) else: while len(name) > 30: print(len(name)) name = raw_input("Please enter a name with fewer than 30 characters: ") print(len(name)) names.append(name) registerPlayer(db, names[num]) print(player_registered_text.format(num + 1, names[num])) print("\nGreat! Now we're ready to start the tournament.") # Begin matches try: standings_text_format = "{0:<30}{1:^8}{2:^8}{3:^8}" # Iterate through each round, reporting updated standings and match # pairings at the beginning of each round for r in range(1, int(rounds) + 1): print("\n################ CURRENT STANDINGS ################\n") standings = playerStandings(db) spaces = calc_standings_header_spacing(standings) print(standings_text_format.format("Names", "Wins", "Losses", "Draws")) for player in standings: print(standings_text_format.format(player[1], player[2], player[3], player[4])) round_matches = swissPairings(db) print("\nRound {0} will feature the following matches: ".format(r)) for match in round_matches: print("{0} vs. {1}".format(match[1], match[-1])) proceed = raw_input("\nProceed? (press Enter to continue) \n") # Start matches, reporting the outcome of each match and write # to db. if proceed == "": for match in round_matches: print(match) print("{0} vs. {1}......FIGHT!".format(match[1], match[-1])) time.sleep(.1) # Faking outcome weights, don't want draws to occur too often result = random.choice([match[0], match[0], match[-2], match[-2], "draw"]) if result is match[0]: reportMatch(db, match[0], match[-2]) print("{0} wins!".format(match[1])) elif result is match[-2]: reportMatch(db, match[-2], match[0]) print("{0} wins!".format(match[-1])) elif result is "draw": reportMatch(db, match[-2], match[0], draw=True) print("Draw!") else: sys.exit(-1) finally: # After the last round, report the winner and the final standings standings = playerStandings(db) spaces = calc_standings_header_spacing(standings) print("\nAnd the tournament winner is...{0}!\n".format(standings[0][1])) print("################ FINAL STANDINGS ################\n") print(standings_text_format.format("Names", "Wins", "Losses", "Draws")) for player in standings: print(standings_text_format.format(player[1], player[2], player[3], player[4]))