-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtournament.py
164 lines (145 loc) · 5.36 KB
/
tournament.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
# tournament.py -- implementation of a Swiss-system tournament
import psycopg2
def connect():
"""Connect to the PostgreSQL database. Returns a database connection."""
return psycopg2.connect("dbname=tournament")
def deleteMatches():
"""Remove all the match records from the database."""
conn = connect()
# Open a cursor to perform database operations
cursor = conn.cursor()
query = "DELETE FROM Matches;"
# Execute a sql command
cursor.execute(query)
# Make the changes to the database persistent
conn.commit()
# Close communication with the database
conn.close()
cursor.close()
def deletePlayers():
"""Remove all the player records from the database."""
conn = connect()
# Open a cursor to perform database operations
cursor = conn.cursor()
query = "DELETE FROM Players;"
# Execute a sql command
cursor.execute(query)
# Make the changes to the database persistent
conn.commit()
conn.close()
cursor.close()
def countPlayers():
"""Returns the number of players currently registered."""
conn = connect()
# Open a cursor to perform database operations
cursor = conn.cursor()
query = "SELECT COUNT(PlayerID) FROM Players;"
cursor.execute(query)
# Retrieves the next row of the query result set
playersNumber = cursor.fetchone()[0]
conn.close()
cursor.close()
return playersNumber
def registerPlayer(name):
"""Adds 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).
"""
conn = connect()
# Open a cursor to perform database operations
cursor = conn.cursor()
query = "INSERT INTO Players (PlayerName) VALUES (%s);"
# Execute a sql command
cursor.execute(query, (name, ))
conn.commit()
conn.close()
cursor.close()
def playerStandings():
"""Returns a list of the players and their win records, sorted by wins.
The first entry in the list should be the player in first place, or a
player tied for first place if there is currently a tie.
Returns:
A list of tuples, each of which contains (id, name, wins, matches):
id: the player's unique id (assigned by the database)
name: the player's full name (as registered)
wins: the number of matches the player has won
matches: the number of matches the player has played
"""
conn = connect()
# Open a cursor to perform database operations
cursor = conn.cursor()
query = "SELECT * FROM player_standings;"
# Execute a sql command
cursor.execute(query)
standings = cursor.fetchall()
conn.close()
cursor.close()
return standings
def playerStandingsOMW():
""" Returns a list of the players and their win records, sorted
by their wins and opponent match wins (When two players have
the same number of wins, rank them according to the total
number of wins by players they have played against).
Returns:
A list of tuples, each of which contains (id, name, wins, matches):
id: the player's unique id(assigned by the database)
name: the player's full name (as registered)
wins: the number of matches the player has won
matches: the number of matches the player has played
"""
conn = connect()
# Open a cursor to perform database operations
cursor = conn.cursor()
query = "SELECT * FROM player_standings_omw;"
# Execute a sql command
cursor.execute(query)
# Retrieves all the rows of the query result set
standings = cursor.fetchall()
# Close communication with the database
conn.close()
cursor.close()
return standings
def reportMatch(winner, loser):
"""Records the outcome of a single match between two players.
Args:
winner: the id number of the player who won
loser: the id number of the player who lost
"""
conn = connect()
# Open a cursor to perform database operations
cursor = conn.cursor()
query = "INSERT INTO Matches (Player1_ID, Player2_ID, Winner, Loser) \
VALUES (%s, %s, %s, %s);"
# Execute a sql command with the given arguments
cursor.execute(query, (winner, loser, winner, loser, ))
# Make the changes to the database persistent
conn.commit()
conn.close()
cursor.close()
def swissPairings():
"""Returns a list of pairs of players for the next round of a match.
Assuming that there are an even number of players registered, each player
appears exactly once in the pairings. Each player is paired with another
player with an equal or nearly-equal win record, that is, a player adjacent
to him or her in the standings.
Returns:
A list of tuples, each of which contains (id1, name1, id2, name2)
id1: the first player's unique id
name1: the first player's name
id2: the second player's unique id
name2: the second player's name
"""
standings = playerStandings()
pairings = []
paired = []
for player in standings:
if len(paired) < 4:
paired.append(player[0])
paired.append(player[1])
if len(paired) == 4:
pairings.append(tuple(paired))
paired = []
return pairings