def change_lang(bot, chat_id, chosen_lang): MessageHelper.delete_messages_by_type(bot, chat_id, Constants.MESSAGE_TYPES.LANG_MENU) player = DBAccessor.get_player(chat_id) query = DBAccessor.get_update_query_player(lang=chosen_lang) DBAccessor.update_player(_id=player.chat_id, update=query) bot.send_message(chat_id=chat_id, text="Your language has been updated!")
def build_msg_duel_invite_accept(bot, chat_id, init_player_id): player = DBAccessor.get_player(int(chat_id)) friend = DBAccessor.get_player(int(init_player_id)) new_duel = Duel.Duel( participant_1=Duel.Participant(player_id=player.chat_id, action=Duel.ActionExchangePoke()), participant_2=Duel.Participant(player_id=friend.chat_id, action=Duel.ActionExchangePoke()), round=0) player.duels.append(new_duel.event_id) friend.duels.append(new_duel.event_id) MessageHelper.delete_messages_by_type( bot, chat_id, Constants.MESSAGE_TYPES.DUEL_INVITE_MSG) DBAccessor.insert_new_duel(duel=new_duel) query_player = DBAccessor.get_update_query_player(duels=player.duels) query_friend = DBAccessor.get_update_query_player(duels=friend.duels) DBAccessor.update_player(_id=player.chat_id, update=query_player) DBAccessor.update_player(_id=friend.chat_id, update=query_friend) if player.get_messages( Constants.MESSAGE_TYPES.MENU_MSG) is not None or len( player.get_messages(Constants.MESSAGE_TYPES.MENU_MSG)) > 0: MenuMessageBuilder.update_menu_message( bot, chat_id, player.get_messages(Constants.MESSAGE_TYPES.MENU_MSG)[-1]._id) if friend.get_messages( Constants.MESSAGE_TYPES.MENU_MSG) is not None or len( friend.get_messages(Constants.MESSAGE_TYPES.MENU_MSG)) > 0: MenuMessageBuilder.update_menu_message( bot, init_player_id, friend.get_messages(Constants.MESSAGE_TYPES.MENU_MSG)[-1]._id) bot.send_message(chat_id=init_player_id, text='{} accepted the challenge!'.format(player.username)) start_duel(bot=bot, chat_id=player.chat_id, event_id=new_duel.event_id) start_duel(bot=bot, chat_id=friend.chat_id, event_id=new_duel.event_id)
def build_msg_duel_invite_deny(bot, chat_id, init_player_id): player = DBAccessor.get_player(int(chat_id)) init_player = DBAccessor.get_player(int(init_player_id)) MessageHelper.delete_messages_by_type( bot, player.chat_id, Constants.MESSAGE_TYPES.DUEL_INVITE_MSG) bot.send_message(chat_id=player.chat_id, text='Duel cancelled') bot.send_message( chat_id=init_player.chat_id, text='Your friend is currently not interested in a challenge.')
def build_msg_trade(bot, chat_id, player_id=None): if player_id is None: MessageHelper.build_choose_friend_message( bot=bot, chat_id=chat_id, mode=Constants.CHOOSE_FRIEND_MODE.TRADE) return else: player = DBAccessor.get_player(chat_id) friend = DBAccessor.get_player(int(player_id)) if player.trade is not None: bot.send_message(chat_id=player.chat_id, text='You are currently trading with {}'.format( DBAccessor.get_player( int(player.trade.partner_id)).username)) return if friend.trade is not None: bot.send_message( chat_id=player.chat_id, text='Your friend is currently trading with somebody else') return keys = [[ InlineKeyboardButton( text='Yes', callback_data=Constants.CALLBACK.TRADE_IVITE_CONFIRM( player.chat_id)), InlineKeyboardButton( text='No', callback_data=Constants.CALLBACK.TRADE_INVITE_DENY( player.chat_id)) ]] reply_keyboard = InlineKeyboardMarkup(inline_keyboard=keys) invite_msg = bot.send_message(chat_id=player_id, text='Your friend ' + str(player.username) + ' wants to trade. Are you interested?', reply_markup=reply_keyboard) friend.messages_to_delete.append( Message.Message(_id=invite_msg.message_id, _title=Constants.MESSAGE_TYPES.TRADE_INVITE_MSG, _time_sent=time.time())) query_friend = DBAccessor.get_update_query_player( messages_to_delete=friend.messages_to_delete) DBAccessor.update_player(_id=friend.chat_id, update=query_friend) player.trade = Trade(partner_id=friend.chat_id) query_player = DBAccessor.get_update_query_player(trade=player.trade) DBAccessor.update_player(_id=player.chat_id, update=query_player) msg = bot.send_message(chat_id=chat_id, text='Choose your Pokemon to trade:') MessageHelper.append_message_to_player( player.chat_id, message_id=msg.message_id, type=Constants.MESSAGE_TYPES.BAG_MSG) BagMessageBuilder.build_msg_bag(bot=bot, chat_id=player.chat_id, page_number=0, trade_mode=True)
def trade_invite_confirm(bot, chat_id, init_player_id): player = DBAccessor.get_player(chat_id) MessageHelper.delete_messages_by_type( bot, chat_id, Constants.MESSAGE_TYPES.TRADE_INVITE_MSG) player.trade = Trade(partner_id=init_player_id) query_player = DBAccessor.get_update_query_player(trade=player.trade) DBAccessor.update_player(_id=player.chat_id, update=query_player) BagMessageBuilder.build_msg_bag(bot=bot, chat_id=player.chat_id, page_number=0, trade_mode=True)
def notify_partner(bot, chat_id, partner_id): msg_ping = bot.send_message( chat_id=partner_id, text= 'Your trading-partner is waiting for a response and is getting impatient.' ) MessageHelper.append_message_to_player( partner_id, message_id=msg_ping.message_id, type=Constants.MESSAGE_TYPES.TRADE_STATUS_MSG) trade_status(bot, partner_id)
def build_msg_duel_action_chosen_source(bot, chat_id, duel_id, source_id): duel_id = int(duel_id) MessageHelper.delete_messages_by_type( bot=bot, chat_id=chat_id, type=Constants.MESSAGE_TYPES.DUEL_CHOOSE_MSG) duel = DBAccessor.get_duel_by_id(int(duel_id)) duel.get_participant_by_id(chat_id).action.set_source( bot, duel.get_participant_by_id(chat_id), source_id) duel.update_participant(chat_id) if duel.participant_1.action.completed and duel.participant_2.action.completed: calc_round(bot, duel_id) else: build_msg_duel_active(bot, chat_id=chat_id, duel_id=duel.event_id)
def abort_duel(bot, chat_id, duel_id): duel = DBAccessor.get_duel_by_id(int(duel_id)) if duel is None: return False if not (duel.participant_1.player_id == int(chat_id) or duel.participant_2.player_id == int(chat_id)): bot.send_message( chat_id=chat_id, text='Wrong duel to be aborted. This should not happen.') player = DBAccessor.get_player(int(chat_id)) friend = DBAccessor.get_player( duel.participant_1.player_id if duel.participant_1. player_id is not player.chat_id else duel.participant_2.player_id) MessageHelper.delete_messages_by_type( bot, player.chat_id, Constants.MESSAGE_TYPES.DUEL_STATUS_MSG) MessageHelper.delete_messages_by_type( bot, friend.chat_id, Constants.MESSAGE_TYPES.DUEL_STATUS_MSG) MessageHelper.delete_messages_by_type( bot, friend.chat_id, Constants.MESSAGE_TYPES.DUEL_CHOOSE_MSG) MessageHelper.delete_messages_by_type( bot, friend.chat_id, Constants.MESSAGE_TYPES.DUEL_TEAM_MSG) bot.send_message(chat_id=player.chat_id, text='You surrendered. {} wins!'.format(friend.username)) bot.send_message(chat_id=friend.chat_id, text='{} aborted the duel. You win!'.format( player.username)) DBAccessor.delete_duel(duel.event_id) player.duels.remove(duel.event_id) friend.duels.remove(duel.event_id) query_player = DBAccessor.get_update_query_player(duels=player.duels) query_friend = DBAccessor.get_update_query_player(duels=friend.duels) DBAccessor.update_player(_id=player.chat_id, update=query_player) DBAccessor.update_player(_id=friend.chat_id, update=query_friend) return True
def toggle_encounter(bot, chat_id): player = DBAccessor.get_player(chat_id) MessageHelper.delete_messages_by_type( bot=bot, chat_id=chat_id, type=Constants.MESSAGE_TYPES.MENU_INFO_MSG) if player.encounters: ToggleCatchMessageBuilder.build_no_catch_message(bot=bot, chat_id=chat_id) else: ToggleCatchMessageBuilder.build_catch_message(bot=bot, chat_id=chat_id) if player.get_messages( Constants.MESSAGE_TYPES.MENU_MSG) is not None or len( player.get_messages(Constants.MESSAGE_TYPES.MENU_MSG)) > 0: menu_msg_id = player.get_messages( Constants.MESSAGE_TYPES.MENU_MSG)[-1]._id update_menu_message(bot, chat_id, menu_msg_id)
def build_nc_msg(bot, update): player = DBAccessor.get_player(update.message.chat_id) if player is None or player.nc_msg_state == Constants.NC_MSG_States.INFO.value: PokeInfoMessageBuilder.build_msg_info(bot=bot, update=update) elif player.nc_msg_state == Constants.NC_MSG_States.USERNAME.value: FriendlistMessageBuilder.search_friend_in_players(bot=bot, update=update) elif player.nc_msg_state == Constants.NC_MSG_States.DISPLAY_EDIT_NAME.value: PokeDisplayBuilder.poke_change_name(bot=bot, chat_id=update.message.chat_id, new_name=update.message.text) else: bot.send_message(chat_id=update.message.chat_id, text='Invalid Message State, Please Try Again!') MessageHelper.reset_states(bot, update.message.chat_id)
def send_lang_menu(bot, chat_id): MessageHelper.delete_messages_by_type(bot, chat_id, Constants.MESSAGE_TYPES.LANG_MENU) keys = [] for lang in EichState.string_dicts.keys(): keys.append([ InlineKeyboardButton( text=lang, callback_data=Constants.CALLBACK.CHANGE_LANGUAGE(lang)) ]) reply_markup = InlineKeyboardMarkup(inline_keyboard=keys) msg = bot.send_message(chat_id=chat_id, text='Choose your preferred language:', reply_markup=reply_markup) MessageHelper.append_message_to_player(chat_id, msg.message_id, Constants.MESSAGE_TYPES.LANG_MENU)
def trade_invite_deny(bot, chat_id, init_player_id): init_player_id = int(init_player_id) player = DBAccessor.get_player(chat_id) init_player = DBAccessor.get_player(init_player_id) MessageHelper.delete_messages_by_type( bot, player.chat_id, Constants.MESSAGE_TYPES.TRADE_INVITE_MSG) if init_player.trade is not None: DBAccessor.update_player(init_player_id, {'$unset': {'trade': 1}}) MessageHelper.delete_messages_by_type(bot, init_player_id, Constants.MESSAGE_TYPES.BAG_MSG) bot.send_message(chat_id=player.chat_id, text='Trade cancelled') bot.send_message( chat_id=init_player.chat_id, text= 'Your friend is currently not interested in trading cute Pok\xe9mon.')
def build_msg_duel_action_chosen_target(bot, chat_id, duel_id, target_id): MessageHelper.delete_messages_by_type( bot=bot, chat_id=chat_id, type=Constants.MESSAGE_TYPES.DUEL_CHOOSE_MSG) duel = DBAccessor.get_duel_by_id(int(duel_id)) if chat_id == duel.participant_1.player_id: duel.participant_1.action.perform(bot, duel.participant_1, target_id) query = DBAccessor.get_update_query_duel( participant_1=duel.participant_1) elif chat_id == duel.participant_2.player_id: duel.participant_2.action.perform(bot, duel.participant_2, target_id) query = DBAccessor.get_update_query_duel( participant_2=duel.participant_2) else: raise AttributeError('Duel Participants incorrect: Duel_id: {}'.format( duel.event_id)) DBAccessor.update_duel(_id=duel.event_id, update=query) if duel.participant_1.action.completed and duel.participant_2.action.completed: calc_round(bot, duel_id)
def notify_opponent(bot, chat_id, duel_id): duel = DBAccessor.get_duel_by_id(int(duel_id)) if duel is None: bot.send_message( chat_id=duel.get_counterpart_by_id(chat_id).player_id, text='The fight is over, go home\n(or start a new one)') MessageHelper.delete_messages_by_type( bot, chat_id, Constants.MESSAGE_TYPES.DUEL_STATUS_MSG) msg_ping = bot.send_message( chat_id=duel.get_counterpart_by_id(chat_id).player_id, text='Your opponent for you to take action and is getting impatient.') MessageHelper.append_message_to_player( duel.get_counterpart_by_id(chat_id).player_id, message_id=msg_ping.message_id, type=Constants.MESSAGE_TYPES.DUEL_CHOOSE_MSG) build_msg_duel_active(bot, duel.get_counterpart_by_id(chat_id).player_id, duel_id)
def send_settings_menu(bot, update): player = DBAccessor.get_player(update.message.chat_id) if player is not None: MessageHelper.delete_messages_by_type( bot=bot, chat_id=update.message.chat_id, type=Constants.MESSAGE_TYPES.SETTINGS_MSG) else: msg = bot.send_message( chat_id=update.message.chat_id, text= 'I do not know you yet. To be recognized next time, update your /username or type /catch\n' 'Latter will enable encounters as well.') return text, reply_markup = build_msg_settings_menu() msg = bot.send_message(chat_id=update.message.chat_id, text=text, reply_markup=reply_markup) MessageHelper.append_message_to_player( player.chat_id, msg.message_id, Constants.MESSAGE_TYPES.SETTINGS_MSG)
def poke_change_name(bot, chat_id, new_name): player = DBAccessor.get_player(chat_id) pokemon = DBAccessor.get_pokemon_by_id(int(player.edit_pokemon_id)) if pokemon is None: bot.send_message( chat_id=player.chat_id, text='Couldn\'t find editing pokemon. Blame the devs pls') # MessageHelper.reset_states(bot=bot, chat_id=player.chat_id) # TODO: do this (remove comment) return MessageHelper.delete_messages_by_type( bot, chat_id, Constants.MESSAGE_TYPES.POKE_DISPLAY_MSG) query = DBAccessor.get_update_query_pokemon(name=new_name) DBAccessor.update_pokemon(_id=pokemon.poke_id, update=query) build_poke_display(bot=bot, chat_id=chat_id, trade_mode=False, page_num=0, poke_id=pokemon.poke_id) query = DBAccessor.get_update_query_player( nc_msg_state=Constants.NC_MSG_States.INFO, edit_pokemon_id=None) DBAccessor.update_player(_id=chat_id, update=query)
def build_poke_display(bot, chat_id, trade_mode, page_num, poke_id): page_num = int(page_num) trade_mode = bool(int(trade_mode)) player = DBAccessor.get_player(chat_id) pokemon = DBAccessor.get_pokemon_by_id(int(poke_id)) # Delete msgs MessageHelper.delete_messages_by_type(bot, chat_id, Constants.MESSAGE_TYPES.BAG_MSG) MessageHelper.delete_messages_by_type( bot, chat_id, Constants.MESSAGE_TYPES.POKE_DISPLAY_MSG) text = 'Pokedex ID: ' + str(pokemon.pokedex_id) + '\n' + \ 'Name: ' + str(pokemon.name) + '\n' + \ 'Level: ' + str(pokemon.level) + '\n' bio = BytesIO() bio.name = 'image_displ_' + str(chat_id) + '.png' image = Pokemon.get_pokemon_portrait_image( pokemon_sprite=pokemon.sprites['front']) image.save(bio, 'PNG') bio.seek(0) reply_keyboard = get_display_keyboard_editing( poke_id=pokemon.poke_id, page_num=page_num) if not trade_mode else get_display_keyboard_trading( poke_id=pokemon.poke_id, page_num=page_num) try: msg = bot.send_photo(chat_id=chat_id, photo=bio, caption=text, parse_mode=ParseMode.MARKDOWN, reply_markup=reply_keyboard) player.messages_to_delete.append( Message.Message(_id=msg.message_id, _title=Constants.MESSAGE_TYPES.POKE_DISPLAY_MSG, _time_sent=time.time())) update = DBAccessor.get_update_query_player( messages_to_delete=player.messages_to_delete) DBAccessor.update_player(_id=player.chat_id, update=update) except ConnectionResetError as e: logging.error(e)
def send_menu_message(bot, update): player = DBAccessor.get_player(update.message.chat_id) if player is not None: MessageHelper.delete_messages_by_type( bot=bot, chat_id=update.message.chat_id, type=Constants.MESSAGE_TYPES.MENU_MSG) else: msg = bot.send_message( chat_id=update.message.chat_id, text= 'I do not know you yet. To be recognized next time, type /catch\n' 'This will enable encounters as well.') return text, reply_markup = build_msg_menu( player.chat_id, player.encounters if player is not None else False, trade=player.trade, duels=player.duels) msg = bot.send_message(chat_id=update.message.chat_id, text=text, reply_markup=reply_markup) if player is None: new_player = Player.Player( update.message.chat_id, messages_to_delete=[ Message.Message(_id=msg.message_id, _title=Constants.MESSAGE_TYPES.MENU_MSG, _time_sent=time.time()) ], encounters=False) DBAccessor.insert_new_player(new_player) else: MessageHelper.append_message_to_player( player.chat_id, msg.message_id, Constants.MESSAGE_TYPES.MENU_MSG)
def command_handler_clear_duels(bot, update): MessageHelper.clear_duels(bot, update)
def command_handler_reset(bot, update): MessageHelper.reset_states(bot, update.message.chat_id)
def build_team_selection(bot, chat_id, duel_id, page_number): player = DBAccessor.get_player(chat_id) duel = DBAccessor.get_duel_by_id(int(duel_id)) page_number = int(page_number) pokecount = 8 pokemon_sprite_list = [] caption = '' if len(player.pokemon) > pokecount: if (page_number + 1) * pokecount <= len(player.pokemon): caption = '*Page Number: * {} Pok\xe9 {}-{}'.format( str(page_number), str((page_number * pokecount) + 1), (str( (page_number + 1) * pokecount))) else: caption = '{}/{}\n'.format(str(len(player.pokemon)), str(len(player.pokemon))) list_start = pokecount * page_number list_end = pokecount * (page_number + 1) if len( player.pokemon) >= pokecount * (page_number + 1) else len( player.pokemon) page_list = player.pokemon[list_start:list_end] keys = [] checkmark = emojize(":white_check_mark:", use_aliases=True) skeleton = emojize(":skeleton:", use_aliases=True) for pokemon_id in page_list: pokemon = DBAccessor.get_pokemon_by_id(pokemon_id) if pokemon.health > 0: keys.append([ InlineKeyboardButton( text=checkmark + ' ' + pokemon.name + ' ' + checkmark if duel.get_participant_by_id(chat_id).team is not None and pokemon_id in duel.get_participant_by_id(chat_id).team else pokemon.name, callback_data=Constants.CALLBACK.DUEL_TEAM_NOMINATE( event_id=duel_id, page_number=page_number, poke_id=pokemon_id)) ]) else: keys.append([ InlineKeyboardButton(text=skeleton + ' ' + pokemon.name + ' ' + skeleton) ]) pokemon_sprite_list.append(pokemon.sprites['front']) image = Pokemon.build_pokemon_bag_image(pokemon_sprite_list) MessageHelper.delete_messages_by_type( bot=bot, chat_id=chat_id, type=Constants.MESSAGE_TYPES.POKE_DISPLAY_MSG) MessageHelper.delete_messages_by_type(bot=bot, chat_id=chat_id, type=Constants.MESSAGE_TYPES.BAG_MSG) if image is None: msg = bot.send_message(chat_id=chat_id, text='Your bag is empty, catch some pokemon!') return page_keys = [] if page_number > 0: page_keys.append( InlineKeyboardButton( text='\u2190', callback_data=Constants.CALLBACK.DUEL_TEAM_PAGE( duel_id, page_number - 1))) if len(player.pokemon) > list_end: page_keys.append( InlineKeyboardButton( text='\u2192', callback_data=Constants.CALLBACK.DUEL_TEAM_PAGE( duel_id, page_number + 1))) control_keys = [ InlineKeyboardButton( text='Abort', callback_data=Constants.CALLBACK.DUEL_TEAM_ABORT(event_id=duel_id)) ] if duel.get_participant_by_id(chat_id).team is not None and len( duel.get_participant_by_id( chat_id).team) >= Constants.DUEL_MIN_POKEMON_PER_TEAM: control_keys.append( InlineKeyboardButton( text='Accept Team', callback_data=Constants.CALLBACK.DUEL_TEAM_ACCEPT( event_id=duel_id))) if len(page_keys) > 0: keys.append(page_keys) keys.append(control_keys) reply_markup = InlineKeyboardMarkup(inline_keyboard=keys) return image, caption, reply_markup
def start_custom_team(bot, chat_id, event_id): MessageHelper.delete_messages_by_type( bot, chat_id, Constants.MESSAGE_TYPES.DUEL_TEAM_DEFAULT_CUSTOM) send_team_selection(bot, chat_id, event_id, 0)
def build_msg_duel_start_nofriend(bot, chat_id): MessageHelper.build_choose_friend_message( bot=bot, chat_id=chat_id, mode=Constants.CHOOSE_FRIEND_MODE.DUEL)
def build_msg_duel_active(bot, chat_id, duel_id): duel = DBAccessor.get_duel_by_id(int(duel_id)) if duel is not None and duel.participant_1.action is not None and duel.participant_2.action is not None and duel.participant_1.action.completed and duel.participant_2.action.completed: calc_round(bot, duel_id) if duel.get_participant_by_id(chat_id).team is not None: poke_player = [ DBAccessor.get_pokemon_by_id(i) for i in duel.get_participant_by_id(chat_id).team ] else: poke_player = None if duel.get_participant_by_id(chat_id).pokemon is not None: champion_player = DBAccessor.get_pokemon_by_id( duel.get_participant_by_id(chat_id).pokemon) else: champion_player = None if duel.get_counterpart_by_id(chat_id).pokemon is not None: champion_opponent = DBAccessor.get_pokemon_by_id( duel.get_counterpart_by_id(chat_id).pokemon) else: champion_opponent = None if duel.get_participant_by_id(chat_id).pokemon is not None: # Display Champion first for i, poke in enumerate(poke_player): if poke.poke_id == int( duel.get_participant_by_id(chat_id).pokemon): poke_player[0], poke_player[i] = poke_player[i], poke_player[0] break img = Pokemon.build_pokemon_duel_info_image(poke_player, champion_player, champion_opponent) keys = [] # keys = [[InlineKeyboardButton(text='Attack', callback_data=Constants.CALLBACK.DUEL_ACTION_ATTACK)], # [InlineKeyboardButton(text='Exchange Pokemon', callback_data=Constants.CALLBACK.DUEL_ACTION_POKEMON)], # [InlineKeyboardButton(text='Use Item', callback_data=Constants.CALLBACK.DUEL_ACTION_ITEM)]] caption = '' if duel.get_participant_by_id(chat_id).team is None: # TODO: Should this happen? Probably the team has to be chosen at this point already. keys.append([ InlineKeyboardButton( text='Choose Team', callback_data=Constants.CALLBACK.DUEL_TEAM_PAGE( duel.event_id, 0)) ]) elif duel.get_participant_by_id(chat_id).action is None: if duel.get_participant_by_id(chat_id).team is None or len( duel.get_participant_by_id( chat_id).team) < Constants.DUEL_MIN_POKEMON_PER_TEAM: keys.append( InlineKeyboardButton( text='Set up team', callback_data=Constants.CALLBACK.DUEL_START_CUSTOM( duel.event_id))) caption += 'You have to choose a team first!' else: if duel.get_participant_by_id(chat_id).pokemon is not None: keys.append([ InlineKeyboardButton( text='Attack', callback_data=Constants.CALLBACK.DUEL_ACTION_ATTACK( event_id=duel_id)) ]) keys.append([ InlineKeyboardButton( text='Nominate Champion', callback_data=Constants.CALLBACK.DUEL_ACTION_POKEMON( event_id=duel.event_id)) ]) keys.append([ InlineKeyboardButton( text='Use Item', callback_data=Constants.CALLBACK.DUEL_ACTION_ITEM( event_id=duel_id)) ]) else: keys.append([ InlineKeyboardButton(text='Remind {} to choose'.format( DBAccessor.get_player( duel.get_counterpart_by_id(chat_id).player_id).username), callback_data=Constants.CALLBACK.DUEL_NOTIFY( event_id=duel_id)) ]) if type(duel.get_participant_by_id( chat_id).action) == Duel.ActionAttack: caption += '\nYou chose to attack' if duel.get_participant_by_id( chat_id ).action.source is None and not duel.get_participant_by_id( chat_id).action.completed: keys.append([ InlineKeyboardButton( text='Choose Attack', callback_data=Constants.CALLBACK.DUEL_ACTION_ATTACK( event_id=duel.event_id)) ]) caption += ' but you have not chosen an attack yet!' elif type(duel.get_participant_by_id( chat_id).action) == Duel.ActionExchangePoke: if duel.get_participant_by_id( chat_id ).action.source is None and not duel.get_participant_by_id( chat_id).action.completed: keys.append([ InlineKeyboardButton( text='Nominate Champion', callback_data=Constants.CALLBACK.DUEL_ACTION_POKEMON( event_id=duel.event_id)) ]) caption += '\nYou chose to switch your champion but you have not chosen a champion yet!' else: caption += '\nYou chose to switch to {} as champion'.format( DBAccessor.get_pokemon_by_id( duel.get_participant_by_id( chat_id).action.source).name) elif type(duel.get_participant_by_id( chat_id).action) == Duel.ActionUseItem: caption += '\nYou chose to use an item' if duel.get_participant_by_id( chat_id ).action.source is None and not duel.get_participant_by_id( chat_id).action.completed: keys.append([ InlineKeyboardButton( text='Use Item', callback_data=Constants.CALLBACK.DUEL_ACTION_ITEM( event_id=duel.event_id)) ]) caption += ' but you have not chosen a champion yet!' keys.append([ InlineKeyboardButton(text='Surrender and abort duel', callback_data=Constants.CALLBACK.DUEL_ABORT( event_id=duel.event_id)) ]) reply_markup = InlineKeyboardMarkup(inline_keyboard=keys) player = DBAccessor.get_player(chat_id) MessageHelper.delete_messages_by_type( bot, player.chat_id, Constants.MESSAGE_TYPES.DUEL_STATUS_MSG) if img is not None: bio = BytesIO() bio.name = 'image_duel_info_' + str(chat_id) + '.png' img.save(bio, 'PNG') bio.seek(0) msg = bot.send_photo(chat_id=chat_id, photo=bio, caption='Current Status' + caption, reply_markup=reply_markup, parse_mode=ParseMode.MARKDOWN) else: msg = bot.send_message( chat_id=chat_id, text='Neither have you chosen your Pokemon team for ' 'this duel nor has your opponent nominated a champion.', reply_markup=reply_markup) player.messages_to_delete.append( Message.Message(_id=msg.message_id, _title=Constants.MESSAGE_TYPES.DUEL_STATUS_MSG, _time_sent=time.time())) query = DBAccessor.get_update_query_player( messages_to_delete=player.messages_to_delete) DBAccessor.update_player(_id=player.chat_id, update=query)
def build_msg_duel_action_attack(bot, chat_id, duel_id): duel = DBAccessor.get_duel_by_id(int(duel_id)) duel.get_participant_by_id(chat_id).action = Duel.ActionAttack( duel_id=duel.event_id) duel.update_participant(chat_id) participant_player = duel.get_participant_by_id(chat_id) # participant = duel.get_counterpart_by_id(chat_id) poke1 = DBAccessor.get_pokemon_by_id(participant_player.pokemon) # poke2 = DBAccessor.get_pokemon_by_id(participant.pokemon) if participant_player.pokemon is None: bot.send_message( chat_id=participant_player.player_id, text= 'Your pokemon-champion is somehow not set! You have to choose again' ) raise AttributeError('Champion is None: Duel_id: {}'.format( duel.event_id)) elif poke1.moves is None or len(poke1.moves) is 0: bot.send_message( chat_id=participant_player.player_id, text='Your pokemon-champion has not enough attack moves!') raise AttributeError( 'Champion has no moves: Duel_id: {} Poke_id: {}'.format( duel.event_id, participant_player.pokemon)) keys = [] cross = emojize(":x:", use_aliases=True) for move in poke1.moves: # id # accuracy # power # pp # priority # target # type.name # names[find language.name == 'en'].name # move = Move.Move.get_move(m['url']) blocked = cross if move.target not in [ 'selected-pokemon', 'all-opponents' ] else '' keys.append([ InlineKeyboardButton( text='{}{} Acc:{} Pow:{} Prio:{}'.format( blocked, move.name, move.accuracy, move.power, move.priority), callback_data=Constants.CALLBACK.DUEL_ACTION_CHOSEN( event_id=duel.event_id, source_id=move.move_id)) ]) MessageHelper.delete_messages_by_type( bot=bot, chat_id=chat_id, type=Constants.MESSAGE_TYPES.DUEL_CHOOSE_MSG) MessageHelper.delete_messages_by_type( bot=bot, chat_id=chat_id, type=Constants.MESSAGE_TYPES.DUEL_STATUS_MSG) image = Pokemon.get_pokemon_portrait_image( pokemon_sprite=poke1.sprites['front']) if image is not None: bio = BytesIO() bio.name = 'image_duel_choose_attack_' + str(chat_id) + '.png' image.save(bio, 'PNG') bio.seek(0) reply_markup = InlineKeyboardMarkup(inline_keyboard=keys) msg = bot.send_photo(chat_id=chat_id, photo=bio, reply_markup=reply_markup, caption='Choose {}\'s attack'.format(poke1.name), parse_mode=ParseMode.MARKDOWN) else: msg = bot.send_message( chat_id=chat_id, text='Your team is empty, nominate some pokemon!') player = DBAccessor.get_player(int(chat_id)) player.messages_to_delete.append( Message.Message(_id=msg.message_id, _title=Constants.MESSAGE_TYPES.DUEL_CHOOSE_MSG, _time_sent=time.time())) update = DBAccessor.get_update_query_player( messages_to_delete=player.messages_to_delete) DBAccessor.update_player(_id=player.chat_id, update=update)
def build_msg_bag(bot, chat_id, trade_mode, page_number): page_number = int(page_number) trade_mode = bool(int(trade_mode)) pokecount = 8 player = DBAccessor.get_player(chat_id) if player is None: bot.send_message( chat_id=chat_id, text= 'I have not met you yet. Want to be a Pok\xe9mon trainer? Type /catch.' ) return pokelist = player.pokemon_team + player.pokemon pokemon_sprite_list = [] caption = '' if len(pokelist) > pokecount: caption = '*Page Number: *' + str(page_number) + ' Pok\xe9 ' + str( (page_number * pokecount) + 1) + '-' + (str((page_number + 1) * pokecount) if (page_number + 1) * pokecount <= len(pokelist) else str(len(pokelist))) + '/' + str(len(pokelist)) + '\n' list_start = pokecount * page_number list_end = pokecount * (page_number + 1) if len( pokelist) >= pokecount * (page_number + 1) else len(pokelist) page_list = pokelist[list_start:list_end] keys = [] for pokemon_id in page_list: pokemon = DBAccessor.get_pokemon_by_id(pokemon_id) if pokemon is None: logging.error('Pokemon with id {} None!'.format(pokemon)) pokelist.remove(pokemon_id) DBAccessor.update_player( chat_id, DBAccessor.get_update_query_player(pokelist)) keys.append([ InlineKeyboardButton( text=pokemon.name, callback_data=Constants.CALLBACK.POKE_DISPLAY_CONFIG( trade_mode=trade_mode, page_number=page_number, pokemon_id=pokemon.poke_id)) ]) pokemon_sprite_list.append(pokemon.sprites['front']) image = Pokemon.build_pokemon_bag_image(pokemon_sprite_list) MessageHelper.delete_messages_by_type( bot=bot, chat_id=chat_id, type=Constants.MESSAGE_TYPES.POKE_DISPLAY_MSG) MessageHelper.delete_messages_by_type(bot=bot, chat_id=chat_id, type=Constants.MESSAGE_TYPES.BAG_MSG) if image is not None: bio = BytesIO() bio.name = 'image_bag_' + str(chat_id) + '.png' image.save(bio, 'PNG') bio.seek(0) keys.append([]) if page_number > 0: keys[-1].append( InlineKeyboardButton(text='\u2190', callback_data=Constants.CALLBACK.BAG_PAGE( trade_mode, page_number - 1))) if len(pokelist) > list_end: keys[-1].append( InlineKeyboardButton(text='\u2192', callback_data=Constants.CALLBACK.BAG_PAGE( trade_mode, page_number + 1))) reply_markup = InlineKeyboardMarkup(inline_keyboard=keys) msg = bot.send_photo(chat_id=chat_id, photo=bio, reply_markup=reply_markup, caption=caption, parse_mode=ParseMode.MARKDOWN) else: msg = bot.send_message(chat_id=chat_id, text='Your bag is empty, catch some pokemon!') player.messages_to_delete.append( Message.Message(_id=msg.message_id, _title=Constants.MESSAGE_TYPES.BAG_MSG, _time_sent=time.time())) update = DBAccessor.get_update_query_player( messages_to_delete=player.messages_to_delete) DBAccessor.update_player(_id=player.chat_id, update=update)
def build_encounter_message(bot): logging.info('Encounter') cursor = DBAccessor.get_encounter_players_cursor() for player in cursor: draw = random.random() now = time.time() last_enc = float(player.last_encounter) if player.encounter is not None: if now - last_enc >= 900: # Reset player's catch state for i in player.get_messages( Constants.MESSAGE_TYPES.ENCOUNTER_MSG): try: bot.delete_message(chat_id=player.chat_id, message_id=i._id) except telegram.error.BadRequest as e: logging.error(e) query = { '$set': { 'last_encounter': now }, '$unset': { 'encounter': 1 } } DBAccessor.update_player(_id=player.chat_id, update=query) logging.info('reset encounter for player ' + str(player.chat_id)) continue chance = pow(1 / (24 * 60 * 60) * (now - last_enc), math.e) if 0 < draw < chance: pokemon_name = EichState.names_dict['pokenames'][random.choice( list(EichState.names_dict['pokenames'].keys()))] pokemon_direction = random.randint(0, 8) pokemon = Pokemon.get_random_poke( Pokemon.get_pokemon_json(pokemon_name), 10) keys = [[ InlineKeyboardButton( text='\u2196', callback_data=Constants.CALLBACK.CATCH(0)), InlineKeyboardButton( text='\u2191', callback_data=Constants.CALLBACK.CATCH(1)), InlineKeyboardButton(text='\u2197', callback_data=Constants.CALLBACK.CATCH(2)) ], [ InlineKeyboardButton( text='\u2190', callback_data=Constants.CALLBACK.CATCH(3)), InlineKeyboardButton( text='o', callback_data=Constants.CALLBACK.CATCH(4)), InlineKeyboardButton( text='\u2192', callback_data=Constants.CALLBACK.CATCH(5)) ], [ InlineKeyboardButton( text='\u2199', callback_data=Constants.CALLBACK.CATCH(6)), InlineKeyboardButton( text='\u2193', callback_data=Constants.CALLBACK.CATCH(7)), InlineKeyboardButton( text='\u2198', callback_data=Constants.CALLBACK.CATCH(8)) ]] reply_markup = InlineKeyboardMarkup(inline_keyboard=keys) sprites = { k: v for k, v in pokemon.sprites.items() if v is not None } sprite = pokemon.sprites[random.choice(list(sprites.keys()))] image = Pokemon.build_pokemon_catch_img( pokemon_sprite=sprite, direction=pokemon_direction) bio = BytesIO() bio.name = 'catch_img_' + str(player.chat_id) + '.png' image.save(bio, 'PNG') bio.seek(0) DBAccessor.insert_new_pokemon(pokemon) try: MessageHelper.delete_messages_by_type( bot, chat_id=player.chat_id, type=Constants.MESSAGE_TYPES.ENCOUNTER_MSG) msg = bot.send_photo(chat_id=player.chat_id, text='catch Pokemon!', photo=bio, reply_markup=reply_markup) player.messages_to_delete.append( Message.Message( _id=msg.message_id, _title=Constants.MESSAGE_TYPES.ENCOUNTER_MSG, _time_sent=now)) encounter = Encounter(pokemon_direction=pokemon_direction, pokemon=pokemon) query = { '$set': { 'last_encounter': now, 'messages_to_delete': [i.serialize_msg() for i in player.messages_to_delete], 'encounter': encounter.serialize() } } except telegram.error.Unauthorized as e: query = { '$set': { 'last_encounter': now, 'encounters': False }, '$unset': { 'encounter': 1 } } logging.error(e) DBAccessor.update_player(_id=player.chat_id, update=query)
def logfile_handler(bot, job): MessageHelper.shorten_logfile()
def build_choose_from_team(bot, chat_id, duel_id): duel_id = int(duel_id) player = DBAccessor.get_player(chat_id) duel = DBAccessor.get_duel_by_id(int(duel_id)) friend_id = duel.participant_1.player_id if duel.participant_1.player_id != chat_id else duel.participant_2.player_id friend = DBAccessor.get_player(int(friend_id)) # If the Player should be informed about other participant's poke team # caption='' # for poke in friend.pokemon_team: # caption += '{} Lvl: {} HP: {}/{}\n'.format(poke.name, poke.level, poke.health, poke.max_health) if player is None: bot.send_message( chat_id=chat_id, text= 'I have not met you yet. Want to be a Pok\xe9mon trainer? Type /catch.' ) return elif len(duel.get_participant_by_id(chat_id).team ) is 0 or duel.get_participant_by_id(chat_id).team is None: bot.send_message( chat_id=chat_id, text= 'Your Pok\xe9mon team is empty. Choose candidates from your /bag.') return elif len(duel.get_participant_by_id(chat_id).team) > 6: bot.send_message( chat_id=chat_id, text= 'You somehow managed to add more than 6 Pok\xe9mon to your team. Outrageous! ' 'Please remove some :) (and blame the devs)') return pokemon_sprite_list, keys = [], [] for pokemon_id in duel.get_participant_by_id(chat_id).team: pokemon = DBAccessor.get_pokemon_by_id(pokemon_id) keys.append([ InlineKeyboardButton( text='{} Level:{} Health:{}/{}'.format(pokemon.name, pokemon.level, pokemon.health, pokemon.max_health), callback_data=Constants.CALLBACK.DUEL_ACTION_CHOSEN( event_id=duel.event_id, source_id=pokemon.poke_id)) ]) pokemon_sprite_list.append(pokemon.sprites['front']) MessageHelper.delete_messages_by_type( bot=bot, chat_id=chat_id, type=Constants.MESSAGE_TYPES.DUEL_CHOOSE_MSG) image = Pokemon.build_pokemon_bag_image( pokemon_sprite_list=pokemon_sprite_list, max_row_len=3) if image is not None: bio = BytesIO() bio.name = 'image_duel_choose_' + str(chat_id) + '.png' image.save(bio, 'PNG') bio.seek(0) reply_markup = InlineKeyboardMarkup(inline_keyboard=keys) msg = bot.send_photo( chat_id=chat_id, photo=bio, reply_markup=reply_markup, caption='Choose your champion in battle against {}!'.format( friend.username), parse_mode=ParseMode.MARKDOWN) else: msg = bot.send_message( chat_id=chat_id, text='Your team is empty, nominate some pokemon!') player.messages_to_delete.append( Message.Message(_id=msg.message_id, _title=Constants.MESSAGE_TYPES.DUEL_CHOOSE_MSG, _time_sent=time.time())) update = DBAccessor.get_update_query_player( messages_to_delete=player.messages_to_delete) DBAccessor.update_player(_id=player.chat_id, update=update)
def start_default_team(bot, chat_id, event_id): MessageHelper.delete_messages_by_type( bot, chat_id, Constants.MESSAGE_TYPES.DUEL_TEAM_DEFAULT_CUSTOM) build_msg_duel_action_pokemon(bot=bot, chat_id=chat_id, duel_id=event_id)