def top_10_rate(message): chat_id = message.chat.id query = get_ranking() session = get_session() rankings = session.execute( 'select id, player_id, (wins * 100 / total) as percentage, total' ' from ranking group by player_id, percentage, total, id ' 'having total > (select max(total) from ranking) / 2 ' 'order by percentage desc limit 10').fetchall() text = _('No user with any resolved bet.') if rankings: text = '🏆 TOP 10 WIN RATE 🏆 \n' if rankings else '' text += '═══════════════════\n' query = get_users() for ra in rankings: username = query.filter(User.player_id == ra[1]).first() # textu = '{0: <15}'.format(username.telegram) textu = '<pre>' textu += username.telegram.encode('UTF-8') textu += ' ' * (20 - len(textu)) + ' ' textu += '</pre>' textu += emoji(':game_die:').encode('UTF-8') textu += ' ' + str(ra[2]) + '% \n' text += textu bot.send_message(chat_id, text, parse_mode="html")
def top_10(message): chat_id = message.chat.id query = get_ranking() rankings = (query.order_by(Ranking.wins.desc()).order_by( Ranking.total.desc()).all()) text = _('No user with any resolved bet.') if rankings: text = (emoji(':trophy:') + ' ' + _('TOP 10 WINS') + ' ' + emoji(':trophy:') + '\n') text += '-----------------\n' query = get_users() count = 0 for ra in rankings: if count == 10: break count += 1 username = query.filter(User.player_id == ra.player_id).first() textu = '<pre>' textu += username.telegram.encode('UTF-8') textu += ' ' * (20 - len(textu)) + ' ' textu += '</pre>' # text += '{0: <15}'.format(username.telegram) textu += emoji(':trophy:') + ' ' + str(ra.wins) textu += emoji(':video_game:') + ' ' + str(ra.total) + '\n' text += textu bot.send_message(chat_id, text, parse_mode="html")
def get_user(message): player_id = message.from_user.id query = get_users() user = query.filter(User.player_id == player_id).first() if user and user.telegram != message.from_user.first_name: user.telegram = unicodedata.normalize( 'NFC', message.from_user.first_name).encode('ascii', 'ignore') update() if not user: user = User(player_id=message.from_user.id, telegram=message.from_user.username) add(user)
def send_welcome(message): cid = message.chat.id query = get_users() user = query.filter(User.player_id == message.from_user.id).first() help_text = _('Talk me in private to get the command list.') if user: cid = message.from_user.id help_text = _('The following commands are available:') + '\n' for key in commands: help_text += "/" + key + ": " help_text += commands[key] + "\n" bot.send_message(cid, help_text)
def mestats(message): chat_id = message.chat.id query = get_ranking() ra = query.filter(Ranking.player_id == message.from_user.id).first() query = get_users() username = query.filter(User.player_id == message.from_user.id).first() text = _('You do not have any resolved bet yet.') if username and username.telegram: text = username.telegram.encode('UTF-8') + ' \n' text += emoji(':trophy:') + ' ' + str(ra.wins) + ' \n' text += emoji(':1234:') + ' ' + str(ra.total) + ' \n' text += (emoji(':chart_with_upwards_trend:') + ' ' + str(ra.wins * 100 / ra.total) + '%') bot.send_message(chat_id, text)
def notify(message): chat_id = message.chat.id if chat_id != message.from_user.id: bot.send_message(message.chat.id, _('This command can not be used on group chats.')) return chat_id = message.chat.id query = get_users() user = query.filter(User.player_id == message.from_user.id).first() if user.notify: user.notify = 0 bot.send_message(chat_id, _('You will no longer be notified.')) else: user.notify = 1 bot.send_message(chat_id, _('You will be notified when a match is added.')) update()
def add_match_db(message): userStep[message.from_user.id] = None teams = to_add[message.from_user.id] date = teams['date'] + ' ' + teams['hour'] try: date_time = datetime.datetime.strptime(date, '%d-%m-%Y %H:%M') except ValueError: bot.send_message(message.chat.id, _('The format of the date/hour is ' + 'incorrect.')) return new_match = Match(team1=teams['Team1'], team2=teams['Team2'], start_date=date_time) try: add(new_match) except Exception: bot.send_message(message.chat.id, _('An error ocurred adding the' + 'match.')) return bot.send_message(message.chat.id, _('Added correctly.')) # Notify query = get_users() notify = query.filter(User.notify == 1).all() for u in notify: try: bot.send_message( u.player_id, _('New match added - %(1)s %(vs)s %(2)s') % { '1': teams['Team1'], 'vs': emoji(':vs:'), '2': teams['Team2'] }) except Exception: # Set notify to 0 if error (because user stopped bot /stop) user = query.filter(User.player_id == message.from_user.id).first() user.notify = 0 update()