def show_puzzles_menu(update, context): msg = update.message user_id = msg.from_user.id reply_markup = get_options_keyboard(context.chat_data, user_id) msg.reply_text('Choose a puzzle to view ...', reply_markup=reply_markup) return CHOOSE_PUZZLE
def try_answer(update, context): msg = update.message user_id = msg.from_user.id reply_markup = get_options_keyboard(context.chat_data, user_id, mode='TRY') msg.reply_text('Choose a challenge to try', reply_markup=reply_markup) return CHOOSE_CHALL_TO_ANSWER
def show_challs_menu(update, context): msg = update.message user_id = msg.from_user.id reply_markup = get_options_keyboard(context.chat_data, user_id, mode='SHOW') msg.reply_text('Choose a challenge to view', reply_markup=reply_markup) return CHOOSE_CHALL
def void(update, context): user_id = update.effective_user.id puzzles = context.chat_data[user_id] puzzle_idx = context.user_data['cur_puzzle_idx'] query = update.callback_query query.answer() context.user_data['is_voided'] = True puzzles[puzzle_idx].is_voided = True puzzles[puzzle_idx].set_void_title() bot = context.bot try: last_description = context.user_data['last_description'] bot.delete_message(chat_id=user_id, message_id=last_description.message_id) except: pass user_data_str = read_dp(str(user_id)) if user_data_str: user_progress = loads(user_data_str)['progress'] user_voids = loads(user_data_str)['voids'] user_score = loads(user_data_str)['score'] user_hints = sum(loads(user_data_str)['hints'].values()) else: user_progress = list() user_voids = list() user_score = 0 user_hints = 0 reply_markup = get_options_keyboard(context.chat_data, user_id) if len(user_progress) + len(user_voids) == len(puzzles) - 1: if GIVEN_HINTS - user_hints == 1: query.edit_message_text( f'You have just voided the final puzzle!\n\nYour final score is {int(user_score) + HINT_POINTS*(GIVEN_HINTS - user_hints)} because you had extra {HINT_POINTS} points from the {GIVEN_HINTS - user_hints} unused hint! Nicely done!\n\nNow choose a puzzle to view ...', reply_markup=reply_markup) elif GIVEN_HINTS - user_hints != 0: query.edit_message_text( f'You have just voided the final puzzle!\n\nYour final score is {int(user_score) + HINT_POINTS*(GIVEN_HINTS - user_hints)} because you had extra {HINT_POINTS} points from each of the {GIVEN_HINTS - user_hints} unused hints! Nicely done!\n\nNow choose a puzzle to view ...', reply_markup=reply_markup) else: query.edit_message_text( f'You have just voided the final puzzle!\n\nYour final score is {user_score}. Woohoo!\n\nNow choose a puzzle to view ...', reply_markup=reply_markup) elif len(user_progress) + len(user_voids) == len(puzzles) - 2: query.edit_message_text( f'You have just voided the puzzle! Final puzzle unlocked!\n\nYour score is now {user_score}.\n\nNow choose a puzzle to view ...', reply_markup=reply_markup) else: query.edit_message_text( f'You have just voided the puzzle!\n\nYour score is now {user_score}.\n\nNow choose a puzzle to view ...', reply_markup=reply_markup) save_user_progress(str(user_id), context, False) return CHOOSE_PUZZLE
def return_to_challs_menu(update, context): query = update.callback_query user_id = update.effective_user.id query.answer() bot = context.bot try: last_description = context.user_data['last_description'] bot.delete_message(chat_id=user_id, message_id=last_description.message_id) except: pass bot.delete_message(chat_id=user_id, message_id=query.message.message_id) reply_markup = get_options_keyboard(context.chat_data, user_id, mode='SHOW') query.message.reply_text('Choose a challenge to view', reply_markup=reply_markup) return CHOOSE_CHALL
def choose_puzzle(update, context): query = update.callback_query user_id = update.effective_user.id query.answer() puzzle_idx = query.data puzzles = context.chat_data[user_id] # We need to check whether the selected puzzle is the final puzzle user_data_str = read_dp(str(user_id)) if user_data_str: user_progress = loads(user_data_str)['progress'] user_voids = loads(user_data_str)['voids'] else: user_progress = list() user_voids = list() bot = context.bot # If it is not the final puzzle or the final puzzle is already unlocked if not puzzles[puzzle_idx].is_final or len(user_progress) + len( user_voids) >= len(puzzles) - 1: context.user_data['cur_puzzle_idx'] = puzzle_idx context.user_data['score'] = puzzles[puzzle_idx].score context.user_data['username'] = update.effective_user.username context.user_data['is_voided'] = puzzles[puzzle_idx].is_voided name = puzzles[puzzle_idx].name description = puzzles[puzzle_idx].description is_completed = puzzles[puzzle_idx].is_completed is_voided = puzzles[puzzle_idx].is_voided bot.delete_message(chat_id=user_id, message_id=query.message.message_id) keyboard = [[ InlineKeyboardButton(text='Try this puzzle 🧩', callback_data='try'), InlineKeyboardButton(text='Back to puzzles\' list 📃', callback_data='back') ], [ InlineKeyboardButton(text='Void this puzzle ❌', callback_data='ask_void'), InlineKeyboardButton(text='Ask for hint ❓', callback_data='ask_hint') ], [ InlineKeyboardButton(text='Done ✔️', callback_data='done') ]] txt = [f'Showing "{name}".'] # Update response for completed puzzle if is_completed or is_voided: # We can set the index to any number as long as there is no IndexError first_answer = puzzles[puzzle_idx].answers[0] if is_completed: txt.append( f'\nYou already completed this puzzle! The answer was "{first_answer}".' ) elif is_voided: # txt.append(f'\nYou already voided this puzzle!') txt.append( f' You already voided this puzzle! No way back, but the answer was "{first_answer}".\n' ) keyboard = [[keyboard[0][1], keyboard[2][0]]] # only back and done reply_markup = InlineKeyboardMarkup(keyboard) context.user_data['last_description'] = send_description( description, user_id, bot) query.message.reply_text(text=''.join(txt), reply_markup=reply_markup) return CHOOSE_OPTION # Else, the locked final puzzle is selected else: reply_markup = get_options_keyboard(context.chat_data, user_id) try: query.edit_message_text( 'Final puzzle still locked! Choose another puzzle to view...', reply_markup=reply_markup) except: pass # unmodified message return CHOOSE_PUZZLE