def load_items_to_backpack(self, temp): backpack = temp.split(';\n') if len(backpack) != 1: result = [] for i in range(len(backpack) - 1): temp = backpack[i] x = Item() x.load(temp) result.append(x) self.add_to_backpack(result)
def load_item(self, temp): if temp[0] == 'gold': self.gold = int(temp[1]) elif temp[1] != '0': x = Item() x.load(temp[1]) if temp[0] == 'head': self.head = [x] if temp[0] == 'arms': self.arms = [x] if temp[0] == 'body': self.body = [x] if temp[0] == 'legs': self.legs = [x] if temp[0] == 'weapon': self.weapon = [x]
def buy_menu(bot, call): game_user = game_control() game_user.load(call.from_user.id) if 'back' in call.data.split('<-'): text, keyboard = main_menu(game_user) bot.edit_message_text(chat_id=game_user.user_id, message_id=call.message.message_id, text=text, reply_markup=keyboard, parse_mode='Markdown') return 0 conn = sqlite3.connect('game_folder/shop.bd') c = conn.cursor() c.execute('SELECT * FROM items WHERE user_id = ?', [game_user.user_id]) items = c.fetchall() conn.close() if len(items) == 0: keyboard = items_for_selling(game_user.user_id) else: keyboard = telebot.types.InlineKeyboardMarkup(True) result_items = [] items_lines = [] prices = [] for i in range(len(items)): temp = Item() temp.load(items[i][1][:-1]) line = temp.inventory_line() result_items.append(temp) items_lines.append(temp.description[:25]) price = int(items[i][2]) prices.append(price) price = ' ({0} gold)'.format(price) callback_data = 'buy<-' + temp.description[:25] button = telebot.types.InlineKeyboardButton( line + price, callback_data=callback_data) keyboard.row(button) if call.data != 'buy' and call.data.split('<-')[1] in items_lines: idx = items_lines.index(call.data.split('<-')[1]) if game_user.character.inventory.gold - prices[idx] >= 0: game_user.character.inventory.gold -= prices[idx] game_user.character.add_to_backpack([result_items[idx]]) result_items.pop(idx) prices.pop(idx) keyboard = telebot.types.InlineKeyboardMarkup(True) for i in range(len(result_items)): line = result_items[i].inventory_line() price = ' ({0} gold)'.format(prices[i]) callback_data = 'buy<-' + result_items[i].description button = telebot.types.InlineKeyboardButton( line + price, callback_data=callback_data) keyboard.row(button) else: bot.send_message(game_user.user_id, 'You don\'t have enough money') return 0 button = telebot.types.InlineKeyboardButton('Back', callback_data='buy<-back') keyboard.row(button) text = 'Let\'s see how I can help you\nYou have *{0} gold*'.format( game_user.character.inventory.gold) game_user.save(call.from_user.id) bot.edit_message_text(chat_id=game_user.user_id, message_id=call.message.message_id, text=text, reply_markup=keyboard, parse_mode='Markdown') return 0