def check_ans(chat_id, qst_number, answer): if qst_number <= game_config.qst_test: qst = mongodb.get_question(mongodb.tests, qst_number) if int(answer) == qst['right_answer']: temp = mongodb.get_field_value( chat_id, mongodb.AvailableFields.RIGHT_ANSWERS) mongodb.update_db( chat_id, {mongodb.AvailableFields.RIGHT_ANSWERS: temp + 1}) else: qst = mongodb.get_question(mongodb.non_tests, qst_number - game_config.qst_test) if answer.lower() == qst['right_answer'].lower(): temp = mongodb.get_field_value( chat_id, mongodb.AvailableFields.RIGHT_ANSWERS) mongodb.update_db( chat_id, {mongodb.AvailableFields.RIGHT_ANSWERS: temp + 1})
def already_in_game(message, bot): current_qst_number = mongodb.get_field_value(message.chat.id, mongodb.AvailableFields.QST_COUNT) if message.text == '!hint': tickets_now = mongodb.get_field_value(message.chat.id, mongodb.AvailableFields.TICKETS) if tickets_now <= 5: bot.send_message(message.chat.id, 'Недостаточно тикетов') else: quiz.give_hint(current_qst_number) mongodb.update_db(message.chat.id, {mongodb.AvailableFields.TICKETS: tickets_now - 5}) elif current_qst_number > game_config.qst_amount: bot.send_message(message.chat.id, 'Вы ответили на все вопросы\nОжидайте конца игры') elif current_qst_number == game_config.qst_amount: quiz.check_ans(message.chat.id, current_qst_number, message.text) bot.send_message(message.chat.id, 'Вы ответили на все вопросы\nОжидайте конца игры') mongodb.update_db(message.chat.id, {mongodb.AvailableFields.QST_COUNT: current_qst_number + 1}) else: quiz.check_ans(message.chat.id, current_qst_number, message.text) bot.send_message(message.chat.id, quiz.get_next_question(current_qst_number + 1)) mongodb.update_db(message.chat.id, {mongodb.AvailableFields.QST_COUNT: current_qst_number + 1})
def update_stat(bot): users = mongodb.get_users() for x in users: if x[mongodb.AvailableFields.STATE] == mongodb.AvailableStates.IN_GAME: right_ans = mongodb.get_field_value( x[mongodb.AvailableFields.ID], mongodb.AvailableFields.RIGHT_ANSWERS) total_ans = mongodb.get_field_value( x[mongodb.AvailableFields.ID], mongodb.AvailableFields.TOTAL_RIGHT_ANSWERS) total_games = mongodb.get_field_value( x[mongodb.AvailableFields.ID], mongodb.AvailableFields.TOTAL_GAMES) average = mongodb.get_field_value(x[mongodb.AvailableFields.ID], mongodb.AvailableFields.AVERAGE) new_average = (total_ans + right_ans) // (total_games + 1) new_tickets = right_ans res_str = "Ваша статистика\nВсего игр - {0}({1})\n" \ "Правильных ответов за игру - {2} из 30\n" \ "Среднее правильных ответов по играм - {3}({4})".format(total_games + 1, total_games, right_ans, new_average, average) bot.send_message(x[mongodb.AvailableFields.ID], res_str) to_update = { mongodb.AvailableFields.RIGHT_ANSWERS: 0, mongodb.AvailableFields.AVERAGE: new_average, mongodb.AvailableFields.TOTAL_GAMES: total_games + 1, mongodb.AvailableFields.TOTAL_RIGHT_ANSWERS: total_ans + right_ans, mongodb.AvailableFields.QST_COUNT: 1, mongodb.AvailableFields.STATE: mongodb.AvailableStates.AFK, mongodb.AvailableFields.TICKETS: new_tickets } mongodb.update_db(x[mongodb.AvailableFields.ID], to_update) users = mongodb.get_users() users = sorted(users, key=lambda k: k[mongodb.AvailableFields.AVERAGE], reverse=True) for idx, x in enumerate(users): mongodb.update_db(x[mongodb.AvailableFields.ID], {mongodb.AvailableFields.GLOBAL_RANK: idx + 1})
def reset_stat(chat_id): tickets_now = mongodb.get_field_value(chat_id, mongodb.AvailableFields.TICKETS) if tickets_now < 100: return False else: to_update = {mongodb.AvailableFields.GLOBAL_RANK: 0, mongodb.AvailableFields.TOTAL_GAMES: 0, mongodb.AvailableFields.AVERAGE: 0, mongodb.AvailableFields.TOTAL_RIGHT_ANSWERS: 0, mongodb.AvailableFields.TICKETS: tickets_now - 10} mongodb.update_db(chat_id, to_update) return True
def tickets(chat_id): return mongodb.get_field_value(chat_id, mongodb.AvailableFields.TICKETS)