コード例 #1
0
def set_bet_db(message):
    chat_id = message.chat.id
    markup = types.ReplyKeyboardHide()
    bot.send_message(chat_id,
                     _('Winner correctly selected.'),
                     reply_markup=markup)
    query = get_matches()
    match = query.filter(
        Match.id == to_winner[message.from_user.id]['id']).first()
    match.score1 = to_winner[message.from_user.id]['score1']
    match.score2 = message.text

    update()
    query = get_bets()
    bets = query.filter(
        Bet.match == to_winner[message.from_user.id]['id']).all()
    for b in bets:
        query = get_ranking()
        ranking = query.filter(Ranking.player_id == b.player_id).first()
        if not ranking:
            ranking = Ranking(player_id=b.player_id)
            add(ranking)
        ranking.total += 1
        winner = ('team1' if int(message.text) < int(
            to_winner[message.from_user.id]['score1']) else 'team2')
        if to_winner[message.from_user.id][winner] == b.bet:
            ranking.wins += 1
        update()
    userStep[message.from_user.id] = None
コード例 #2
0
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)
コード例 #3
0
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()
コード例 #4
0
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()