def do_bet(message): if message.chat.id != message.from_user.id: bot.send_message(message.chat.id, _('This command can not be used on group chats.')) return get_user(message) chat_id = message.chat.id query = get_matches() matches = query.filter(Match.score1 == None).filter( Match.start_date > datetime.datetime.now()).all() query = get_bets() user_bets = query.filter(Bet.player_id == message.from_user.id).all() markup = types.ReplyKeyboardMarkup(row_width=len(matches)) to_bet = 0 for m in matches: skip = 0 for b in user_bets: if b.match == m.id: skip = 1 break if skip == 1: continue else: to_bet += 1 markup.add(str(m.id) + ' - ' + m.team1 + ' - ' + m.team2) if (not matches or not to_bet): bot.send_message( message.chat.id, _('No matches available (Or you already bet on all).')) return markup.add(_('Cancel')) bot.send_message(chat_id, _('Choose a match:'), reply_markup=markup) userStep[message.from_user.id] = 51
def list_bets(message): query = get_matches() matches = query.filter(Match.score1 == None).all() text = '' query = get_bets() for m in matches: bets = query.filter(Bet.match == m.id).all() date = m.start_date.strftime('%d-%m-%Y') hour = m.start_date.strftime('%H:%M') bets1 = 0 bets2 = 0 for b in bets: if b.bet == m.team1: bets1 += 1 elif b.bet == m.team2: bets2 += 1 odd1 = 0 odd2 = 0 if bets1 or bets2: odd1 = bets1 * 100 / (bets1 + bets2) odd2 = bets2 * 100 / (bets1 + bets2) text += (emoji(':calendar:') + date + ' ' + emoji(':clock1:') + hour + ' ' + emoji(' :fast_forward:') + ' *' + m.team1 + '* ' + str(odd1) + '%' + ' ' + emoji(':vs:') + ' ' + str(odd2) + '% *' + m.team2 + '*\n') if (not matches): bot.send_message(message.chat.id, _('No matches available.')) else: bot.send_message(message.chat.id, text, parse_mode='markdown')
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
def mybets(message): query = get_bets() bets = query.filter(Bet.player_id == message.from_user.id).all() text = '' query = get_matches() for b in bets: m = query.filter(Match.id == b.match).first() if m and (m.start_date > datetime.datetime.now() or m.score1 == None): text += (m.team1 + ' ' + emoji(':vs:') + ' ' + m.team2 + ' - ' + emoji(':video_game:') + ' <b>' + b.bet + '</b>\n') if (not bets or not text): bot.send_message(message.chat.id, _('No bets available.')) else: bot.send_message(message.chat.id, text, parse_mode='html')
def set_match_winner_db(message): userStep[message.from_user.id] = None chat_id = message.chat.id markup = types.ReplyKeyboardHide() mid = to_bet[message.from_user.id] query = get_bets() # TODO Add unique constraint on sql match and return error if already bet already_bet = query.filter(Bet.player_id == message.from_user.id).filter( Bet.match == mid[0]).first() if already_bet: bot.send_message(chat_id, _('You have already bet on this match.'), reply_markup=markup) return new_bet = Bet(player_id=message.from_user.id, match=mid[0], bet=message.text) add(new_bet) bot.send_message(chat_id, _('Bet correctly done.'), reply_markup=markup) userStep[message.from_user.id] = None