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() if __name__ == "__main__": # Turn on debug mode cgitb.enable() # Print necessary headers print("Content-Type: text/html; charset=utf-8\n\n") form = cgi.FieldStorage() current_name = processing.get_player_name(form) new_name = processing.get_new_name(form) if current_name is not None and new_name is not None: player_id = processing.get_player_id(current_name) if player_id is not None and processing.username_is_unique(new_name): change_player_name(new_name, player_id) print(json.dumps(new_name)) else: # If player doesn't exist in database or new name isn't unique print(json.dumps(None)) else: # If original name/new name not provided or new name already taken print(json.dumps(None))
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() if __name__ == '__main__': # Turn on debug mode cgitb.enable() # Print necessary headers print("Content-Type: text/html; charset=utf-8\n\n") form = cgi.FieldStorage() username = processing.get_player_name(form) if username is not None and processing.username_is_unique(username): add_user_to_database(username) print(json.dumps(username)) else: # If username already taken or not provided print(json.dumps(None))