text: #!/usr/bin/env python
  # 
  # tournament.py -- implementation of a Swiss-system tournament
  #
  # Original template downloaded from 
  # https://github.com/udacity/fullstack-nanodegree-vm/tree/master/vagrant/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."""
  
      db = connect()
      c = db.cursor()
      c.execute("DELETE FROM matches;")
      c.execute("UPDATE playerdata SET wins = 0, total = 0;")
      db.commit()
      db.close()
      return
  
  
  def deletePlayers():
      """Remove all the player records from the database."""
  
      db = connect()
      c = db.cursor()
      c.execute("DELETE FROM playerdata;")
      db.commit()
      db.close()
      return
  
  
  def countPlayers():
      """Returns the number of players currently registered."""
  
      db = connect()
      c = db.cursor()
      c.execute("SELECT COUNT(*) FROM playerdata;")
      rows = c.fetchall()
      db.close()
      return rows[0][0]
  
  
  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).
      """
      
      db = connect()
      c = db.cursor()
      c.execute("INSERT INTO playerdata (name) VALUES (%s);", (name,))
      db.commit()
      db.close()
      return
  
  
  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
      """
  
      db = connect()
      c = db.cursor()
      c.execute("SELECT pid, name, wins, total FROM playerdata ORDER BY wins DESC;")
      standings = c.fetchall()
      db.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
      """
  
      db = connect()
      c = db.cursor()
      c.execute("INSERT INTO matches VALUES (%s, %s);", (winner,loser))
      c.execute("UPDATE playerdata SET wins = wins + 1, total = total + 1 WHERE pid = %s;", (winner,))
      c.execute("UPDATE playerdata SET total = total + 1 WHERE pid = %s;", (loser,))
      db.commit()
      db.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
      """
  
      db = connect()
      c = db.cursor()
      c.execute("SELECT pid, name from playerdata ORDER BY wins DESC;")
      rows = c.fetchall()
      standingsList = []
      for i in xrange(len(rows) / 2):
          standingsList.append(rows[i * 2] + rows[i * 2 + 1])
      db.close()
      return standingsList
  
  """"End of program""""
  