def add_score(player_id, level_number, score):
    """Add a score to the database.

    This function adds a score for a given player and
    level to the database.
    """

    connection, cursor = processing.connect_to_database()
    # Insert the score
    cursor.execute("INSERT INTO scores VALUES(%s, %s, %s)", (player_id, level_number, score))
    connection.commit()
def add_user_to_database(username):
    """Add a username to the database.

    This function will add a player with the given username
    to the players table of the highscores database.
    """

    connection, cursor = processing.connect_to_database()
    # Insert the new user
    cursor.execute("INSERT INTO players VALUES(null, %s)", (username,))
    connection.commit()
def change_player_name(username, player_id):
    """Change the name of a given player.

    This function changes the name of the player
    corresponding to the given player id to the
    given name.
    """

    connection, cursor = processing.connect_to_database()
    # Change the appropriate username
    cursor.execute("UPDATE players SET player_name=%s" "WHERE player_id=%s", (username, player_id))
    connection.commit()
def get_top_ten(level_number):
    """Return a list of the top 10 high scores of a given level.

    This function retrieves a list of the top 10 high scores for
    the given level in descending order from the database, and
    then returns the list.
    """

    connection, cursor = processing.connect_to_database()
    # Get the top 10 high scores from the database, list the highest first
    cursor.execute("SELECT players.player_name, levels.level_name, score FROM scores "
                   "INNER JOIN players ON scores.player_id = players.player_id "
                   "INNER JOIN levels ON scores.level_id = levels.level_id "
                   "WHERE scores.level_id=%s"
                   "ORDER BY score DESC LIMIT 10", level_number)

    highscores = [(row[0], row[1], row[2]) for row in cursor.fetchall()]
    return highscores
def level_exists(level_number):
    """Check if a given level number already exists.

    This function checks if an entry for the given level already exists
    in the database. If it does exist, it returns False, if it doesn't exist,
    it returns True.
    """

    connection, cursor = processing.connect_to_database()
    # Get entry for that level number
    cursor.execute("SELECT * FROM levels WHERE level_id=%s", (level_number,))
    level = [(row[0], row[1]) for row in cursor.fetchall()]

    # Length of list is 0 if level doesn't exist
    if len(level) == 0:
        return False
    else:
        return True
def score_empty(player_id, level_number):
    """Check if a username does not already exist.

    This function checks if an entry for the given level and player
    does not already exist in the highscores database. If it doesn't
    already exist, it returns True. Otherwise, it returns False
    """

    connection, cursor = processing.connect_to_database()
    # Get entry for that player and level
    cursor.execute("SELECT * FROM scores " "WHERE player_id=%s AND level_id=%s", (player_id, level_number))
    scores = [(row[0], row[1]) for row in cursor.fetchall()]

    # Length of list is 0 if score for that player/level doesn't exist
    if len(scores) == 0:
        return True
    else:
        return False
def get_best_score(player_name, level_number):
    """Return the best score for the given player and level.

    This function returns the best score for the given player
    on the given level. If the player does not have a score on
    that level, it returns None.
    """

    connection, cursor = processing.connect_to_database()
    cursor.execute("SELECT score FROM scores "
                   "INNER JOIN players ON scores.player_id = players.player_id "
                   "INNER JOIN levels ON scores.level_id = levels.level_id "
                   "WHERE players.player_name=%s AND levels.level_id=%s", (player_name, level_number))
    best_score = [(row[0]) for row in cursor.fetchall()]

    # If length isn't greater than 0 there is no existing best score
    if len(best_score) > 0:
        return best_score[0]
    else:
        return None