def set_result(message): arguments = message.text.split()[1:] race_choosed = arguments[0] + " " + arguments[1] if (len(arguments) != 5): bot.send_message(message.chat.id, "Error. Usage: /result GP Spain HAM BOT VER") return None if (message.from_user.id == int(os.getenv('ADMIN_ID'))): races = [] # all_races() if race_choosed not in races: bot.send_message(message.chat.id, "Error, race not found") return None podium = [arguments[2], arguments[3], arguments[4]] for driver in podium: if driver not in drivers: bot.send_message(message.chat.id, "Error, driver not found") return None cursor.execute("SELECT * FROM predictions WHERE race=?", (race_choosed, )) predictions_fetched = cursor.fetchall() predictions = [] for prediction in predictions_fetched: driver_prediction = [prediction[1], prediction[2], prediction[3]] cursor.execute("SELECT * FROM users WHERE user_id=?", (prediction[0], )) username = cursor.fetchall() predictions.append( Prediction(username[0][0], driver_prediction, prediction[4])) for prediction in predictions: sum_points = 0 if prediction.driver_prediction[0] == podium[0]: sum_points += 2 if prediction.driver_prediction[1] == podium[1]: sum_points += 2 if prediction.driver_prediction[2] == podium[2]: sum_points += 2 for driver in prediction.driver_prediction: if driver in podium: sum_points += 1 cursor.execute("SELECT * FROM users WHERE username=?", (prediction.username, )) user = cursor.fetchall() updated_user = list(user[0]) updated_user[2] += sum_points points: int = updated_user[2] user_id: int = user[0][1] cursor.execute("UPDATE users SET points=(?) WHERE user_id=(?)", (points, user_id)) conn.commit()
def register_user(message): reply_message = 'Welcome ' + \ str(message.from_user.first_name) + ' to F1 predictions!' try: cursor.execute("SELECT * FROM users WHERE username=?", (message.from_user.username, )) user = cursor.fetchall() except mariadb.Error as e: logger.error( f'Error SELECTING FROM users WHERE username... MariaDB output: {e}' ) if not user: try: cursor.execute( "INSERT IGNORE INTO users (username,user_id,points) VALUES (?, ?, ?)", (message.from_user.username, message.from_user.id, 0)) conn.commit() except mariadb.Error as e: logger.error(f'Error INSERTING username... MariaDB output: {e}') bot.reply_to(message, reply_message) logger.info('User created in database') return True bot.reply_to(message, "Wait, are you registered? I think so...")
def get_predictions(message): current_race = check_race() cursor.execute("SELECT * FROM predictions WHERE race=?", (current_race, )) predictions_fetched = cursor.fetchall() predictions = [] for prediction in predictions_fetched: driver_prediction = [prediction[1], prediction[2], prediction[3]] cursor.execute("SELECT * FROM users WHERE user_id=?", (prediction[0], )) username = cursor.fetchall() predictions.append( Prediction(username[0][0], driver_prediction, prediction[4])) table = create_predictions_table(predictions) bot.send_message(message.chat.id, \ f'<b>{current_race}</b>' + f'<pre>{table}</pre>', parse_mode=ParseMode.HTML) conn.commit()
def flakeRead(self, serverID): ids = [] counts = [] cursor.execute('SELECT * FROM Flake' + str(serverID) + ' ORDER BY Count DESC') rows = cursor.fetchall() for row in rows: ids.append(row[0]) counts.append(row[1]) return ids, counts
def get_list(message): try: cursor.execute("SELECT * FROM users") users_fetched = cursor.fetchall() except mariadb.Error as e: logger.error(f'Error SELECTING FROM users... MariaDB output: {e}') users = [] for user in users_fetched: users.append(User(user[0], user[1], user[2])) ordered_users = sorted(users, key=lambda x: x.points, reverse=True) table = create_standings_table(ordered_users) bot.send_message(message.chat.id, f'<pre>{table}</pre>', parse_mode=ParseMode.HTML)