-
-
Save jeffudacity/d4ccde9860a7ae40070a to your computer and use it in GitHub Desktop.
import random | |
from tournament import connect | |
from tournament import reportMatch | |
from tournament_test import testDelete | |
the_players = [ | |
(1, 'Jeff'), | |
(2, 'Adarsh'), | |
(3, 'Amanda'), | |
(4, 'Eduardo'), | |
(5, 'Philip'), | |
(6, 'Jee') | |
] | |
def registerPlayerUpdated(player_id, name): | |
"""Add a player to the tournament database. | |
The database assigns a unique serial id number for the player. (This | |
should be handled by your SQL database schema, not in your Python code.) | |
Args: | |
name: the player's full name (need not be unique). | |
""" | |
db = connect() | |
db_cursor = db.cursor() | |
query = "INSERT INTO players (id, name) VALUES (%s, %s)" | |
db_cursor.execute(query, (player_id, name)) | |
db.commit() | |
db.close() | |
def createRandomMatches(player_list, num_matches): | |
num_players = len(player_list) | |
for i in xrange(num_matches): | |
print 'match1' | |
player1_index = random.randint(0, num_players - 1) | |
player2_index = random.randint(0, num_players - 1) | |
if player2_index == player1_index: | |
player2_index = (player1_index + 1) % num_players | |
winner_id = player_list[player1_index][0] | |
winner_name = player_list[player1_index][1] | |
loser_id = player_list[player2_index][0] | |
loser_name = player_list[player2_index][1] | |
reportMatch(winner_id, loser_id) | |
print "%s (id=%s) beat %s (id=%s)" % ( | |
winner_name, | |
winner_id, | |
loser_name, | |
loser_id) | |
def setup_players_and_matches(): | |
testDelete() | |
for player in the_players: | |
registerPlayerUpdated(player[0], player[1]) | |
createRandomMatches(the_players, 100) | |
if __name__ == '__main__': | |
setup_players_and_matches() |
I have the same problem. did you solve it? @christianherr18
For anyone who is having trouble with this, I added this code to the bottom/end of the createRandomMatches function and it seemed to update the matches table:
db = connect()
db_cursor = db.cursor()
query = "INSERT INTO matches(winner, loser) VALUES (%s, %s)"
db_cursor.execute(query, (winner_id, loser_id))
db.commit()
db.close()
There are a few issues (bugs) in this code. First, the testDelete function doesn't exist in the latest tournament_test.py. It was replaced by deleteMatches and deletePlayers in tournament.py at some point. You will need to import both functions from tournament at the top and put them in place of testDelete.
from tournament import deleteMatches
from tournament import deletePlayers
....
def setup_players_and_matches()
deleteMatches()
deletePlayers()
for player in the_players:
registerPlayerUpdated(player[0], player[1])
createRandomMatches(the_players)
Second, random.randint() doesn't guarantee unique numbers. It will generate a random number from the entire list every time and it will regularly generate duplicate numbers while leaving others out. The result being some players are duplicated in the player_list, while others are left out.
Note the number of matches played is not equal for each player after 100 matches (100 matches /4 pairs= 25):
tournament=> select * from standings;
player_id | name | wins | match
-----------+--------+------+-------
5 | Sam | 15 | 25
4 | Tina | 15 | 27
2 | Jonny | 14 | 30
7 | Larry | 13 | 21
8 | George | 12 | 26
6 | Tick | 11 | 26
3 | Wilber | 11 | 23
1 | Jeff | 9 | 22
(8 rows)
A better option is below, however, it still only randomizes the outcome of matches; there is no tournament structure involved in either case. Hint: That would require a standings table that could be used to create a Swiss tournament style pairing after each round.
def createRandomMatches(player_list):
""" Note: I have removed 'num_matches' as a param from this function and replace it with 'rounds' below. However, it can be substituted
for 'num_matches' if you want."""
rounds = 30
num_players = len(player_list)
p_index_lst = range(0, num_players)
r = 1
while r <= rounds:
print "\nRound" + str(r)
r_pindex = random.sample(p_index_lst, num_players)
while len(r_pindex) > 0:
p1 = r_pindex.pop(0)
p2 = r_pindex.pop(0)
(winner_id, winner_name) = player_list[p1]
(loser_id, loser_name) = player_list[p2]
reportMatch(winner_id, loser_id)
print "%s (id=%s) beat %s (id=%s)" % (
winner_name,
winner_id,
loser_name,
loser_id)
r += 1
It should go with out saying, you need to create psql tables first, before you can use the script.
Cheers!
for some reason when I run the gistfile, the players get added but nothing gets added to the matches table.