def show_inventory(bot, update, hero): items = Hero.get(chat_id=update.effective_chat.id).items listing = '\n'.join([f"{item.type.title}" for item in items]) if listing == '': update.message.reply_text('Your inventory is empty') else: update.message.reply_text(listing)
def fight(bot, job): hero = Hero.get(id=job.context) if hero.location.type != Location.FIGHT or hero.state.name == 'FIGHT': return rand = random.random() start = 0 dwells = list( MobDwells.select().where(MobDwells.location == hero.location)) start = 0 mob_type = None for dwell in dwells: end = start + dwell.chance if start <= rand < end: mob_type = dwell.mob break start = end assert mob_type mob = MobInstance.create(type=mob_type, hp_value=mob_type.hp_base) hero.state = HeroState.get(name='FIGHT') hero.attacked_by = mob hero.save() bot.send_message(chat_id=hero.chat_id, text=f"You have encountered {mob.type.name}", reply_markup=ReplyKeyboardMarkup( [["Attack", "Guard", "Run away"]], resize_keyboard=True))
def start(bot, update): try: hero = Hero.get(chat_id=update.effective_chat.id) except Hero.DoesNotExist: update.message.reply_text('You are not registered yet.\n' + 'Register with /register %nickname% command') else: update.message.reply_text(f'Name: {hero.name}\nHP:{hero.hp_value}')
def do_heal(bot, job): hero = Hero.get(id=job.context) hero.activity = None hero.hp_value = hero.hp_base hero.state = HeroState.get(name="IDLE") hero.save() bot.send_message(chat_id=hero.chat_id, text=f"Your hero recovered!") return actions(bot, None, hero)
def wrapped(bot, update, *args, **kwargs): try: hero = Hero.get(chat_id=update.effective_chat.id) except Hero.DoesNotExist: update.message.reply_text( 'You are not registered yet.\n' + 'Register with /register %nickname% command') return else: return func(bot, update, hero, *args, **kwargs)
def register(bot, update, args, job_queue): nickname = args[0] try: hero = Hero.create(name=nickname, hp_base=100, location=Location.get(type=Location.START), chat_id=update.effective_chat.id, state=HeroState.get(name='IDLE')) except IntegrityError: update.message.reply_text(f'Nickname {nickname} already exists') else: update.message.reply_text(f'Welcome, {nickname}') actions(bot, update, hero)
def revive(bot, job): hero = Hero.get(id=job.context) hero.hp_value = hero.hp_base activity = hero.activity hero.activity = None hero.state = HeroState.get(name="IDLE") hero.location = Location.select().where( Location.type == Location.START).order_by(fn.Random()).limit(1).get() hero.save() activity.delete_instance() bot.send_message(chat_id=hero.chat_id, text=f"You respawned in {hero.location.name}!") return actions(bot, None, hero)
def handle_shopping(bot, update, hero, job_queue): action = update.message.text.split(" ", 1) if len(action) == 1 and action[0].lower() == "leave": hero.state = HeroState.get(name="IDLE") hero.save() return actions(bot, update, hero) if len(action) != 2: update.message.reply_text("I didn't understood you") return shop_actions(bot, update, hero) action, request = action try: request = request[1:-1] requested_item = Item.get(title=request) except Item.DoesNotExist: update.message.reply_text(f"Cannot find item '{request}'") else: shop_location = hero.location if action.lower() == "buy": updated = ShopSlot.update( count=ShopSlot.count - 1).where((ShopSlot.count > 0) & (ShopSlot.item == requested_item) & (ShopSlot.location == shop_location)).execute() if updated == 1: slot = ShopSlot.get(item=requested_item) updated = Hero.update( gold=Hero.gold - slot.price).where((Hero.id == hero.id) & (Hero.gold >= slot.price)).execute() if updated == 1: item = ItemInstance.create( type=requested_item, owner=hero, usages_left=requested_item.usages) update.message.reply_text( f"You bought '{item.type.title}'") if slot.count == 0: slot.delete_instance() else: update.message.reply_text( f"You don't have enough money, you have: {hero.gold}, needed: {slot.price}" ) ShopSlot.update(count=ShopSlot.count + 1).where( (ShopSlot.item == requested_item) & (ShopSlot.location == shop_location)).execute() else: update.message.reply_text(f"Shop has no item '{request}'") elif action.lower() == "sell": item_inst = None for item in hero.items: if item.type == requested_item: item_inst = item break if item_inst is None: update.message.reply_text( f"You don't have '{requested_item.title}'") else: _, created = ShopSlot.get_or_create(item=requested_item, location=shop_location, defaults={ "price": item_inst.type.price, "count": 1 }) if not created: with settings.DB.atomic(): ShopSlot.update(count=ShopSlot.count + 1).where( (ShopSlot.item == requested_item) & (ShopSlot.location == shop_location)).execute() item_inst.delete_instance() Hero.update(gold=Hero.gold + item_inst.type.price).where( Hero.id == hero.id).execute() update.message.reply_text(f"You sold '{item_inst.type.title}'") else: update.message.reply_text("I didn't understood you") hero = Hero.get(id=hero.id) return shop_actions(bot, update, hero)