#!/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."""


def deletePlayers():
    """Remove all the player records from the database."""


def countPlayers():
    """Returns the number of players currently registered."""


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).
    """


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
    """


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
    """
 
 
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
    """



@@ -16,18 +16,17 @@
 python%0A#
- 
 %0A
+
 # tourna
@@ -83,16 +83,27 @@
 ment%0A#%0A%0A
+import os%0A%0A
 import p
@@ -111,16 +111,77 @@
 ycopg2%0A%0A
+DSN = os.environ.get(%22TOURNAMENT_DSN%22, %22dbname=tournament%22)%0A%0A
 %0Adef con
@@ -639,19 +639,17 @@
 tabase.%0A
-  
 %0A
+
     The 
@@ -791,19 +791,17 @@
  code.)%0A
-  
 %0A
+
     Args
@@ -1042,27 +1042,27 @@
 ce, or a
+%0A   
  player
-%0A   
  tied fo
@@ -1652,19 +1652,17 @@
     %22%22%22%0A
- %0A 
+%0A
 %0Adef swi
@@ -1748,19 +1748,17 @@
  match.%0A
-  
 %0A
+
     Assu
@@ -2025,11 +2025,9 @@
 gs.%0A
-  
 %0A
+
     
@@ -2271,18 +2271,18 @@
 's name%0A
+
     %22%22%22%0A
 %0A%0A
@@ -2271,18 +2271,16 @@
 's name%0A    %22%22%22%0A
-%0A%0A

clean up whitespace