def my_points(message): allUsers = db.loadAllUsers() sortedUsers = sorted(allUsers, key=itemgetter('score'), reverse=True) highest_score = sortedUsers[0]['score'] userId = message.from_user.id user = db.getUser(userId, userId) if user['lang'] == "fa": msg_text = 'شما تا اینجا ' msg_text += str(user['score']) msg_text += ' امتیاز کسب کردهاید و در رتبهی ' msg_text += str(user['rank']) msg_text += ' قرار دارید.\nبیشترین امتیاز: ' msg_text += str(highest_score) else: msg_text = 'You have earned ' msg_text += str(user['score']) msg_text += ' so far. Your overall rank is: ' msg_text += str(user['rank']) msg_text += '\nHighest Score: ' msg_text += str(highest_score) bot.reply_to(message, msg_text) temp = detail_points(user['userId']) detail_table_msg = 'Rank Name\n \t \t \tES WG GD LG CW T TOT\n-----------------------------------\n' detail_table_msg += str(user['rank']) + '. ' if isinstance(user['first'], str): detail_table_msg += user['first'] + ' ' if isinstance(user['last'], str): detail_table_msg += user['last'] detail_table_msg += '\n \t \t \t ' + str(temp[0]) + ' ' + str( temp[1]) + ' ' + str(temp[2]) + ' ' + str(temp[3]) + ' ' + str( temp[4]) + ' ' + str(temp[5]) + ' ' + str(user['score']) if (user['lang'] == "fa"): detail_table_msg += '\n\nES: نتیجهی دقیق' detail_table_msg += '\nWG: تعداد گل برنده' detail_table_msg += '\nGD: اختلاف گل' detail_table_msg += '\nLG: تعداد گل بازنده' detail_table_msg += '\nCW: برندهی صحیح' detail_table_msg += '\nT: مساوی' detail_table_msg += '\nTOT: جمع امتیازات' else: detail_table_msg += '\n\nES: Exact Score' detail_table_msg += '\nWG: Winner\'s Goals' detail_table_msg += '\nGD: Goal Difference' detail_table_msg += '\nLG: Loser\'s Goals' detail_table_msg += '\nCW: Correct Winner' detail_table_msg += '\nT: Tie' detail_table_msg += '\nTOT: Total Points' try: bot.send_message(chat_id=user['userId'], text=detail_table_msg) except: pass
def overall_table(message): if message.chat.id == message.from_user.id: #It's not a group message! user = db.getUser(message.chat.id, message.chat.id) if user['lang'] == "fa": msg_text = 'برای مشاهدهی امتیاز و رتبهی خود /mypoints را انتخاب کنید.' else: msg_text = '\nChoose /mypoints to see your total points and your rank.' bot.send_message(message.chat.id, msg_text) return 0 allUsers = db.loadAllUsers() sortedUsers = sorted(allUsers, key=itemgetter('score'), reverse=True) highest_score = sortedUsers[0]['score'] chat = db.getChat(message.chat.id) userIds = chat['users'] usersThisChat = [] for userId in userIds: usersThisChat.append(db.getUser(userId, userId)) sortedUsers = sorted(usersThisChat, key=itemgetter('score'), reverse=True) row = 1 msg_text = 'R ' + 'Name' msg_text += ' ' + 'Points\n' # 24 spaces msg_text += '________________________\n' msg_text += '1. Highest Score ' + str(highest_score) + '\n' for user in sortedUsers: try: thisUser = bot.get_chat(user['userId']) except Exception as e: print('This user is causing trouble in make_table:') print(thisUser) logger.error(e) continue line_text = '' length = 0 line_text += str(user['rank']) + '. ' if isinstance(thisUser.first_name, str): line_text += thisUser.first_name line_text += ' ' length += len(thisUser.first_name) if isinstance(thisUser.last_name, str): line_text += thisUser.last_name length += len(thisUser.last_name) if length < 30: for i in range(int(np.ceil(5 / 3 * length)), 30): line_text += ' ' else: line_text += '\n' for i in range(30): line_text += ' ' line_text += str(user['score']) msg_text += line_text + '\n' bot.send_message(message.chat.id, msg_text)
def send_allUsers(msg_text): allUsers = db.loadAllUsers() for user in allUsers: group_score = user['score'] updateObj = {'$set': {'group_score': group_score}} db.setUserFields(user['userId'], user['userId'], updateObj) markup = types.ReplyKeyboardRemove(selective=False) try: bot.send_message(chat_id=user['userId'], text=msg_text + ' /openbets', reply_markup=markup) except Exception as e: logger.error(e) pass
def update_ranks(): allUsers = db.loadAllUsers() sortedUsers = sorted(allUsers, key=itemgetter('score'), reverse=True) user_no = 1 last_rank = 1 last_score = sortedUsers[0]['score'] for user in sortedUsers: if user['score'] == last_score: updateObj = {'$set': {'rank': last_rank}} else: updateObj = {'$set': {'rank': user_no}} last_rank = user_no db.setUserFields(user['userId'], user['userId'], updateObj) user_no += 1 last_score = user['score']
def bet_time(message): userObj = db.getUser(message.chat.id, message.from_user.id) thisUserId = message.from_user.id thisChatId = message.chat.id if userObj['first'] == []: updateObj = {'$set': {'first': message.from_user.first_name}} db.setUserFields(thisChatId, thisUserId, updateObj) if userObj['last'] == []: updateObj = {'$set': {'last': message.from_user.last_name}} db.setUserFields(thisChatId, thisUserId, updateObj) if message.text == 'bullshiteveryone': allUsers = db.loadAllUsers() for user in allUsers: user['bets'] = [] updateObj = {'$set': {'bets': user['bets']}} db.setUserFields(user['userId'], user['userId'], updateObj) # lang = db.getLang(message.chat.id, message.from_user.id) if 'updategame' not in message.text and 'sendreminder' not in message.text and 'send2all' not in message.text: if '-' in message.text: matches = db.loadOpenMatches() for m in matches: if m['flags'] == message.text: updateObj = {'$set': {'toBetMatchId': m['matchId']}} db.setUserFields(thisChatId, thisUserId, updateObj) show_bets(message) elif 'home' in message.text: matches = db.loadOpenMatches() matchId = userObj['toBetMatchId'] bets = userObj['bets'] for i in range(len(bets)): if bets[i]['matchId'] == matchId: bets[i]['winner'] = 'home' updateObj = {'$set': {'bets': bets}} db.setUserFields(thisChatId, thisUserId, updateObj) change_open(matches, userObj, message) elif 'away' in message.text: matches = db.loadOpenMatches() matchId = userObj['toBetMatchId'] bets = userObj['bets'] for i in range(len(bets)): if bets[i]['matchId'] == matchId: bets[i]['winner'] = 'away' updateObj = {'$set': {'bets': bets}} db.setUserFields(thisChatId, thisUserId, updateObj) change_open(matches, userObj, message) elif ':' in message.text: matches = db.loadOpenMatches() matchId = userObj['toBetMatchId'] bets = userObj['bets'] betValue = message.text flag = '' for m in matches: if m['matchId'] == matchId: flag = m['flags'] result = betValue.split(':') try: home = int(result[0]) away = int(result[1]) betValue = str(home) + ':' + str(away) if userObj['lang'] == "fa": accept_text = 'پیشبینی شما پذیرفته شد:\n' accept_text += flag + '\n' + message.text else: accept_text = 'Bet is accepted:\n' accept_text += flag + '\n' + message.text bot.send_message(message.chat.id, accept_text) except: if userObj['lang'] == "fa": reject_text = 'پیشبینی شما پذیرفته نشد. لطفا اعداد را با فرمت یاد شده وارد کنید.\n' else: reject_text = 'Bet was not accepted. Please follow the mentioned format.' bot.send_message(message.chat.id, reject_text) show_bets(message) return 0 isNewBet = True for i in range(len(bets)): if bets[i]['matchId'] == matchId: bets[i]['value'] = betValue isNewBet = False break if isNewBet: bets.append({'matchId': matchId, 'value': betValue}) updateObj = {'$set': {'bets': bets}} db.setUserFields(thisChatId, thisUserId, updateObj) if home > away: for i in range(len(bets)): if bets[i]['matchId'] == matchId: bets[i]['winner'] = 'home' bets[i]['winner'] = 'home' updateObj = {'$set': {'bets': bets}} db.setUserFields(thisChatId, thisUserId, updateObj) elif away > home: for i in range(len(bets)): if bets[i]['matchId'] == matchId: bets[i]['winner'] = 'away' updateObj = {'$set': {'bets': bets}} db.setUserFields(thisChatId, thisUserId, updateObj) else: for i in range(len(bets)): if bets[i]['matchId'] == matchId: bets[i]['winner'] = 'tie' updateObj = {'$set': {'bets': bets}} db.setUserFields(thisChatId, thisUserId, updateObj) if int(matchId) > 48 and home == away: if userObj['lang'] == "fa": tie_text = 'تیم صعودکننده را پیشبینی کنید:' else: tie_text = 'Which team will go through:' tie_text += '\n' + flag flags = flag.split('-') markup = types.ReplyKeyboardMarkup() btnhome = types.KeyboardButton(flags[0] + ' (home)') btnaway = types.KeyboardButton(flags[1] + ' (away)') markup.row(btnhome) markup.row(btnaway) bot.send_message(chat_id=userObj['userId'], text=tie_text, reply_markup=markup) else: change_open(matches, userObj, message) elif 'send2all' in message.text: msgParts = message.text.split('_') msg_text = msgParts[1] send_allUsers(msg_text) elif 'updategame' in message.text: #For Final Score only 'updategame' in text commandParts = message.text.split(' ') matchId = commandParts[1] matchObj = { 'matchId': matchId, 'result': commandParts[2], 'flags': commandParts[3] } db.updateMatch(matchId, matchObj) db.updateUserScores() print(commandParts) if commandParts[2] == 'C': allUsers = db.loadAllUsers() try: for user in allUsers: if user['toBetMatchId'] == commandParts[1]: user['toBetMatchId'] += 1 updateObj = { '$set': { 'toBetMatchId': user['toBetMatchId'] } } db.setUserFields(user['userId'], user['userId'], updateObj) except: pass group_bets(matchId, commandParts[3]) elif commandParts[2] != 'O': count_scores = update_tot_scores() update_ranks() if 'final' in message.text: matchInd = int(matchId) - 1 tot = np.sum(count_scores, axis=1) print('Total Predictors: ' + str(tot[matchInd])) allUsers = db.loadAllUsers() for user in allUsers: if user['lang'] == "fa": count_text = 'پایان بازی:' + '\n' count_text += commandParts[3] + '\n' + commandParts[ 2] + '\n\n' count_text += 'از مجموع کل پیشبینیکنندهها:' + '\n' count_text += 'نتیجهی دقیق (۲۵ امتیاز): ' + '٪' + str( int( np.round(count_scores[matchInd][0] / tot[matchInd] * 100, decimals=0))) + '\n' count_text += 'تعداد گلهای برنده (۱۸ امتیاز): ' + '٪' + str( int( np.round(count_scores[matchInd][1] / tot[matchInd] * 100, decimals=0))) + '\n' count_text += 'اختلاف گل (۱۵ امتیاز): ' + '٪' + str( int( np.round(count_scores[matchInd][2] / tot[matchInd] * 100, decimals=0))) + '\n' count_text += 'تعداد گلهای بازنده (۱۲ امتیاز): ' + '٪' + str( int( np.round(count_scores[matchInd][3] / tot[matchInd] * 100, decimals=0))) + '\n' count_text += 'برندهی درست (۱۰ امتیاز): ' + '٪' + str( int( np.round(count_scores[matchInd][4] / tot[matchInd] * 100, decimals=0))) + '\n' count_text += 'مساوی (۴ امتیاز): ' + '٪' + str( int( np.round(count_scores[matchInd][5] / tot[matchInd] * 100, decimals=0))) + '\n' count_text += 'اشتباه (۰ امتیاز): ' + '٪' + str( int( np.round(count_scores[matchInd][6] / tot[matchInd] * 100, decimals=0))) + '\n' else: count_text = 'Final Score:\n' + commandParts[ 3] + '\n' + commandParts[2] + '\n\n' count_text += 'Exact score (25 pts): ' + str( int( np.round(count_scores[matchInd][0] / tot[matchInd] * 100, decimals=0))) + '%\n' count_text += 'Winner\'s goals (18 pts): ' + str( int( np.round(count_scores[matchInd][1] / tot[matchInd] * 100, decimals=0))) + '%\n' count_text += 'Goal difference (15 pts): ' + str( int( np.round(count_scores[matchInd][2] / tot[matchInd] * 100, decimals=0))) + '%\n' count_text += 'Loser\'s goals (12 pts): ' + str( int( np.round(count_scores[matchInd][3] / tot[matchInd] * 100, decimals=0))) + '%\n' count_text += 'Right winner (10 pts): ' + str( int( np.round(count_scores[matchInd][4] / tot[matchInd] * 100, decimals=0))) + '%\n' count_text += 'Draw (4 pts): ' + str( int( np.round(count_scores[matchInd][5] / tot[matchInd] * 100, decimals=0))) + '%\n' count_text += 'Wrong (0 pts): ' + str( int( np.round(count_scores[matchInd][6] / tot[matchInd] * 100, decimals=0))) + '%\n' try: bot.send_message(chat_id=user['userId'], text=count_text) except: pass allChats = db.loadAllChats() count_text = 'پایان بازی:' + '\n' count_text += commandParts[3] + '\n' + commandParts[2] + '\n\n' count_text += 'از مجموع کل پیشبینیکنندهها:' + '\n' count_text += 'نتیجهی دقیق (۲۵ امتیاز): ' + '٪' + str( int( np.round( count_scores[matchInd][0] / tot[matchInd] * 100, decimals=0))) + '\n' count_text += 'تعداد گلهای برنده (۱۸ امتیاز): ' + '٪' + str( int( np.round( count_scores[matchInd][1] / tot[matchInd] * 100, decimals=0))) + '\n' count_text += 'اختلاف گل (۱۵ امتیاز): ' + '٪' + str( int( np.round( count_scores[matchInd][2] / tot[matchInd] * 100, decimals=0))) + '\n' count_text += 'تعداد گلهای بازنده (۱۲ امتیاز): ' + '٪' + str( int( np.round( count_scores[matchInd][3] / tot[matchInd] * 100, decimals=0))) + '\n' count_text += 'برندهی درست (۱۰ امتیاز): ' + '٪' + str( int( np.round( count_scores[matchInd][4] / tot[matchInd] * 100, decimals=0))) + '\n' count_text += 'مساوی (۴ امتیاز): ' + '٪' + str( int( np.round( count_scores[matchInd][5] / tot[matchInd] * 100, decimals=0))) + '\n' count_text += 'اشتباه (۰ امتیاز): ' + '٪' + str( int( np.round( count_scores[matchInd][6] / tot[matchInd] * 100, decimals=0))) + '\n' for chat in allChats: try: bot.send_message(chat_id=chat['chatId'], text=count_text) except: pass if int(commandParts[1]) == 64: for user in allUsers: temp = detail_points(user['userId']) detail_table_msg = 'Rank Name\n \t \t \tES WG GD LG CW T TOT\n-----------------------------------\n' detail_table_msg += str(user['rank']) + '. ' if isinstance(user['first'], str): detail_table_msg += user['first'] + ' ' if isinstance(user['last'], str): detail_table_msg += user['last'] detail_table_msg += '\n \t \t \t ' + str( temp[0]) + ' ' + str(temp[1]) + ' ' + str( temp[2]) + ' ' + str(temp[3]) + ' ' + str( temp[4]) + ' ' + str( temp[5]) + ' ' + str(user['score']) if (user['lang'] == "fa"): detail_table_msg += '\n\nES: نتیجهی دقیق' detail_table_msg += '\nWG: تعداد گل برنده' detail_table_msg += '\nGD: اختلاف گل' detail_table_msg += '\nLG: تعداد گل بازنده' detail_table_msg += '\nCW: برندهی صحیح' detail_table_msg += '\nT: مساوی' detail_table_msg += '\nTOT: جمع امتیازات' else: detail_table_msg += '\n\nES: Exact Score' detail_table_msg += '\nWG: Winner\'s Goals' detail_table_msg += '\nGD: Goal Difference' detail_table_msg += '\nLG: Loser\'s Goals' detail_table_msg += '\nCW: Correct Winner' detail_table_msg += '\nT: Tie' detail_table_msg += '\nTOT: Total Points' try: bot.send_message(chat_id=user['userId'], text=detail_table_msg) except: pass for chat in allChats: detail_table_msg = 'Rank Name\n \t \t \tES WG GD LG CW T TOT\n-----------------------------------\n' usersThisChat = [] for userId in chat['users']: usersThisChat.append(db.getUser(userId, userId)) sortedUsers = sorted(usersThisChat, key=itemgetter('score'), reverse=True) for user in sortedUsers: temp = detail_points(user['userId']) detail_table_msg += str(user['rank']) + '. ' if isinstance(user['first'], str): detail_table_msg += user['first'] + ' ' if isinstance(user['last'], str): detail_table_msg += user['last'] detail_table_msg += '\n \t \t \t ' + str( temp[0]) + ' ' + str(temp[1]) + ' ' + str( temp[2]) + ' ' + str( temp[3]) + ' ' + str( temp[4]) + ' ' + str( temp[5]) + ' ' + str( user['score']) + '\n' detail_table_msg += '\n\nES: نتیجهی دقیق' detail_table_msg += '\nWG: تعداد گل برنده' detail_table_msg += '\nGD: اختلاف گل' detail_table_msg += '\nLG: تعداد گل بازنده' detail_table_msg += '\nCW: برندهی صحیح' detail_table_msg += '\nT: مساوی' detail_table_msg += '\nTOT: جمع امتیازات' try: bot.send_message(chat_id=chat['chatId'], text=detail_table_msg) except: pass elif 'sendreminder' in message.text: openMatches = db.loadOpenMatches() commandParts = message.text.split(' ') msg_text = '\n پیشبینی بازیهای زیر را فراموش نکنید: \n Remember to bet on the following games:\n \n' for match in openMatches: if match['matchId'] in commandParts: msg_text += match['flags'] msg_text += '\n' msg_text += '\n پیشبینی با شروع هر بازی بسته خواهد شد. \n Bets will be closed as the game starts.' if commandParts[1] == 'a' or commandParts[1] == 'p': send_allUsers(msg_text) if commandParts[1] == 'a' or commandParts[1] == 'g': allChats = db.loadAllChats() flag = 1 for chat in allChats: if flag == 1: msg_text += '\n برای مشاهدهی جدول گروه دستور ' msg_text += '/table ' msg_text += 'را اجرا کنید.' flag = 0 markup = types.ReplyKeyboardRemove(selective=False) try: bot.send_message(chat_id=chat['chatId'], text=msg_text + ' @WorldCup1818bot', reply_markup=markup) except Exception as e: logger.error(e) print('This user is causing trouble in sendreminder:') print(chat)
def new_scoring(message): allMatches = db.loadAllMatches() allUsers = db.loadAllUsers() for user in allUsers: points = [0 for x in range(64)] thisUserBets = user['bets'] for bet in thisUserBets: for match in allMatches: if match['result'] == 'O' or match['result'] == 'C': continue if bet['matchId'] == match['matchId']: final = match['result'].split(':') if '*' in final[0]: match['winner'] = 'home' final[0] = final[0].replace("*", "") elif '*' in final[1]: match['winner'] = 'away' final[1] = final[1].replace("*", "") drawFinal = 0 if int(final[0]) == int(final[1]): drawFinal = 1 winnerFinal = int(final[0]) loserFinal = int(final[0]) elif int(final[0]) > int(final[1]): winnerFinal = int(final[0]) loserFinal = int(final[1]) else: winnerFinal = int(final[1]) loserFinal = int(final[0]) userBet = bet['value'].split(':') drawUser = 0 matchInd = int(match['matchId']) - 1 if int(userBet[0]) == int(userBet[1]): drawUser = 1 winnerUser = int(userBet[0]) loserUser = int(userBet[0]) elif int(userBet[0]) > int(userBet[1]): winnerUser = int(userBet[0]) loserUser = int(userBet[1]) else: winnerUser = int(userBet[1]) loserUser = int(userBet[0]) if winnerUser == winnerFinal and loserUser == loserFinal and ( int(userBet[0]) - int(userBet[1])) * ( int(final[0]) - int(final[1])) >= 0: a = 25 elif winnerUser == winnerFinal and loserUser != loserFinal and drawUser - drawFinal == 0 and ( int(userBet[0]) - int(userBet[1])) * ( int(final[0]) - int(final[1])) > 0: a = 18 elif winnerUser - loserUser == winnerFinal - loserFinal and ( int(userBet[0]) - int(userBet[1])) * ( int(final[0]) - int(final[1])) >= 0: a = 15 elif loserUser == loserFinal and winnerUser != winnerFinal and drawUser - drawFinal == 0 and ( int(userBet[0]) - int(userBet[1])) * ( int(final[0]) - int(final[1])) > 0: a = 12 elif (int(userBet[0]) - int(userBet[1])) * ( int(final[0]) - int(final[1])) > 0: a = 10 elif winnerUser == loserUser: a = 4 else: a = 0 points[int(match['matchId']) - 1] = a updateObj = {'$set': {'points': points}} db.setUserFields(user['userId'], user['userId'], updateObj) return 0
def update_tot_scores(): allMatches = db.loadAllMatches() allUsers = db.loadAllUsers() count_scores = [[0 for x in range(7)] for y in range(64)] for user in allUsers: points = [0 for x in range(64)] thisUserBets = user['bets'] score = 0 for bet in thisUserBets: for match in allMatches: if match['result'] == 'O' or match['result'] == 'C': continue if bet['matchId'] == match['matchId']: final = match['result'].split(':') if '*' in final[0]: match['winner'] = 'home' final[0] = final[0].replace("*", "") elif '*' in final[1]: match['winner'] = 'away' final[1] = final[1].replace("*", "") drawFinal = 0 if int(final[0]) == int(final[1]): drawFinal = 1 winnerFinal = int(final[0]) loserFinal = int(final[0]) elif int(final[0]) > int(final[1]): winnerFinal = int(final[0]) loserFinal = int(final[1]) else: winnerFinal = int(final[1]) loserFinal = int(final[0]) userBet = bet['value'].split(':') drawUser = 0 matchInd = int(match['matchId']) - 1 if int(userBet[0]) == int(userBet[1]): drawUser = 1 winnerUser = int(userBet[0]) loserUser = int(userBet[0]) elif int(userBet[0]) > int(userBet[1]): winnerUser = int(userBet[0]) loserUser = int(userBet[1]) else: winnerUser = int(userBet[1]) loserUser = int(userBet[0]) if winnerUser == winnerFinal and loserUser == loserFinal and ( int(userBet[0]) - int(userBet[1])) * ( int(final[0]) - int(final[1])) >= 0: a = 25 count_scores[matchInd][0] += 1 elif winnerUser == winnerFinal and loserUser != loserFinal and drawUser - drawFinal == 0 and ( int(userBet[0]) - int(userBet[1])) * ( int(final[0]) - int(final[1])) > 0: a = 18 count_scores[matchInd][1] += 1 elif winnerUser - loserUser == winnerFinal - loserFinal and ( int(userBet[0]) - int(userBet[1])) * ( int(final[0]) - int(final[1])) >= 0: a = 15 count_scores[matchInd][2] += 1 elif loserUser == loserFinal and winnerUser != winnerFinal and drawUser - drawFinal == 0 and ( int(userBet[0]) - int(userBet[1])) * ( int(final[0]) - int(final[1])) > 0: a = 12 count_scores[matchInd][3] += 1 elif (int(userBet[0]) - int(userBet[1])) * ( int(final[0]) - int(final[1])) > 0: a = 10 count_scores[matchInd][4] += 1 elif winnerUser == loserUser: a = 4 count_scores[matchInd][5] += 1 else: a = 0 count_scores[matchInd][6] += 1 if int(match['matchId']) < 49: score += a elif 48 < int(match['matchId']) < 57: score += a if bet['winner'] == match['winner']: score += 15 elif 56 < int(match['matchId']) < 61: score += 2 * a if bet['winner'] == match['winner']: score += 30 elif 60 < int(match['matchId']) < 63: score += 3 * a if bet['winner'] == match['winner']: score += 45 elif int(match['matchId']) == 63: score += 3 * a if bet['winner'] == match['winner']: score += 45 elif int(match['matchId']) == 64: score += 4 * a if bet['winner'] == match['winner']: score += 60 points[int(match['matchId']) - 1] = a updateObj = {'$set': {'score': score, 'points': points}} db.setUserFields(user['userId'], user['userId'], updateObj) return count_scores