def list_challenge(): dbcon = None cursor = None try: dbcon = database.connectdb() cursor = dbcon.cursor() cursor.execute("SELECT * FROM challenge") result = cursor.fetchall() os.system("cls") print("{:<15}{:<50}".format("ID", "DESCRIPTION")) print(66 * "-") for row in result: print("{:<15}{:<50}".format(row[0], row[1])) print(66 * "-") except mysql.connector.Error: print("\nException while listing questions") finally: if dbcon.is_connected: cursor.close() dbcon.close()
def delete_challenge(challenge_id): dbcon = None cursor = None try: dbcon = database.connectdb() cursor = dbcon.cursor() cursor.execute( "DELETE FROM question WHERE question_challenge_id = '{}'". format(challenge_id)) cursor.execute( "DELETE FROM challenge WHERE challenge_id = '{}'".format( challenge_id)) dbcon.commit() print("\nChallenge deleted successfully") input("Enter a key to continue: ") except mysql.connector.Error: print("\nException while trying to delete challenge") finally: if dbcon.is_connected(): cursor.close() dbcon.close()
def list_players(): dbcon = None cursor = None try: dbcon = database.connectdb() cursor = dbcon.cursor() cursor.execute("SELECT * FROM player") result = cursor.fetchall() os.system("cls") print("{:<30}{:<30}{:>10}".format("USER NAME", "PASSWORD", "SCORE")) print(70 * "-") for row in result: print("{:<30}{:<30}{:>10}".format(row[0], row[1], row[2])) print(70 * "-") except mysql.connector.Error: print("\nException while listing users") finally: if dbcon.is_connected: cursor.close() dbcon.close()
def show_player_leaderboard(): counter = 0 dbcon = None cursor = None try: dbcon = database.connectdb() cursor = dbcon.cursor() cursor.execute("SELECT player_name, player_score FROM player ORDER BY player_score DESC") result = cursor.fetchall() os.system("cls") print("{:<20}{:<40}{:>15}".format("POSITION", "TEAM NAME", "SCORE")) print(75 * "-") for row in result: if row[0] != "ADMIN": counter += 1 print("{:<20}{:<40}{:>15}".format(counter, row[0], row[1])) print(75 * "-") input("\nEnter a key to continue: ") except mysql.connector.Error: print("\nError fetching data from MySQL table") finally: if dbcon.is_connected(): cursor.close() dbcon.close()
def modify_player(player_name, option, modification): dbcon = None cursor = None try: dbcon = database.connectdb() cursor = dbcon.cursor() if option == 1: sql = "UPDATE player SET player_name = %s WHERE player_name = %s" var = (modification, player_name) cursor.execute(sql, var) elif option == 2: sql = "UPDATE player SET player_password = %s WHERE player_name = %s" var = (modification, player_name) cursor.execute(sql, var) elif option == 3: sql = "UPDATE player SET player_score = %s WHERE player_name = %s" var = (modification, player_name) cursor.execute(sql, var) dbcon.commit() except mysql.connector.Error: print("\nException while trying to modify player") finally: if dbcon.is_connected(): cursor.close() dbcon.close()
def update_player_score(points, current_player, option): dbcon = None cursor = None try: dbcon = database.connectdb() cursor = dbcon.cursor() if option == 0: sql = "UPDATE player SET player_score = player_score + %s WHERE player_name = %s" val = (points, current_player.player_name) cursor.execute(sql, val) else: if not Player.get_player_score(current_player) == 0: sql = "UPDATE player SET player_score = player_score - ((%s * 5)/100) WHERE player_name = %s" val = (points, current_player.player_name) cursor.execute(sql, val) dbcon.commit() except mysql.connector.Error: print("Error trying to update player score") return True finally: if dbcon.is_connected(): cursor.close() dbcon.close()
def check_player(player_name, player_password): dbcon = None cursor = None try: sql = "SELECT * FROM player WHERE player_name = %s AND player_password = %s" val = (player_name, player_password) dbcon = database.connectdb() cursor = dbcon.cursor() cursor.execute(sql, val) row = cursor.fetchone() if row is not None: return True except mysql.connector.Error: print("Error trying to find player") return True finally: if dbcon.is_connected(): cursor.close() dbcon.close()
def contains_questions(challenge_id): dbcon = None cursor = None try: dbcon = database.connectdb() cursor = dbcon.cursor() cursor.execute( "SELECT * FROM question WHERE question_challenge_id = '{}'". format(challenge_id)) row = cursor.fetchone() if row is not None: return True except mysql.connector.Error: print("\nException while checking challenge") finally: if dbcon.is_connected(): cursor.close() dbcon.close()
def get_player_score(current_player): dbcon = None cursor = None try: dbcon = database.connectdb() cursor = dbcon.cursor() cursor.execute( "SELECT player_score FROM player WHERE player_name = '{}'". format(current_player.player_name)) result = cursor.fetchone() return result[0] except mysql.connector.Error: print("Error trying to print score") return True finally: if dbcon.is_connected(): cursor.close() dbcon.close()
def modify_challenge(challenge_id, modification): dbcon = None cursor = None try: dbcon = database.connectdb() cursor = dbcon.cursor() sql = "UPDATE challenge SET challenge_description = %s WHERE challenge_id = %s" var = (modification, challenge_id) cursor.execute(sql, var) dbcon.commit() print("\nChallenge modified successfully") input("Enter a key to continue: ") except mysql.connector.Error: print("\nException while trying to modify challenge") finally: if dbcon.is_connected(): cursor.close() dbcon.close()
def check_challenge(challenge_id): dbcon = None cursor = None try: dbcon = database.connectdb() cursor = dbcon.cursor() cursor.execute( "SELECT challenge_id FROM challenge WHERE challenge_id = '{}'". format(challenge_id)) row = cursor.fetchone() if row is not None: return True except mysql.connector.Error: print("\nException while trying to find challenge") return True finally: if dbcon.is_connected(): cursor.close() dbcon.close()
def delete_player(player_name): dbcon = None cursor = None try: dbcon = database.connectdb() cursor = dbcon.cursor() cursor.execute( "DELETE FROM player WHERE player_name = '{}'".format( player_name)) dbcon.commit() print("\nPlayer deleted successfully") input("Enter a key to continue: ") except mysql.connector.Error: print("\nException while trying to delete player") finally: if dbcon.is_connected(): cursor.close() dbcon.close()
def add_challenge(self): dbcon = None cursor = None try: dbcon = database.connectdb() cursor = dbcon.cursor() sql = "INSERT INTO challenge VALUES(%s, %s)" var = (self.challenge_id, self.challenge_description) cursor.execute(sql, var) dbcon.commit() print("\nChallenge successfully added") input("Enter a key to continue: ") except mysql.connector.Error: print("\nException while trying to add challenge") input("Enter a key to continue: ") finally: if dbcon.is_connected(): cursor.close() dbcon.close()
def check_player_name(player_name): dbcon = None cursor = None try: dbcon = database.connectdb() cursor = dbcon.cursor() cursor.execute( "SELECT player_score FROM player WHERE player_name = '{}'". format(player_name)) row = cursor.fetchone() if row is not None: return True except mysql.connector.Error: print("Error trying to find player") return True finally: if dbcon.is_connected(): cursor.close() dbcon.close()
def add_player(self): dbcon = None cursor = None try: sql = "INSERT INTO player VALUES(%s, %s, %s)" val = (self.player_name, self.player_password, self.player_score) dbcon = database.connectdb() cursor = dbcon.cursor() cursor.execute(sql, val) dbcon.commit() print("\nPlayer added successfully") input("Enter a key to continue: ") except mysql.connector.Error: print("\nError trying to add player") finally: if dbcon.is_connected(): cursor.close() dbcon.close()