Esempio n. 1
0
def equip_item(userid, item):
    """Takes an item out of a user's inventory and places it into their equipment."""
    try:
        itemid = items.find_by_name(item)
    except KeyError:
        return f'Error: {item} does not exist.'
    item_level = items.get_attr(itemid, key=items.LEVEL_KEY)
    user_cb_level = xp_to_level(read_user(userid, key=COMBAT_XP_KEY))

    if user_cb_level >= item_level:
        item_name = items.get_attr(itemid)
        if item_in_inventory(userid, itemid):
            slot = str(items.get_attr(itemid, key=items.SLOT_KEY))
            if int(slot) > 0:
                equipment = read_user(userid, key=EQUIPMENT_KEY)
                if slot not in equipment.keys() or equipment[slot] == -1:
                    equipment[slot] = itemid
                else:
                    update_inventory(userid, [equipment[slot]])
                    equipment[slot] = itemid
                update_inventory(userid, [itemid], remove=True)
                update_user(userid, equipment, EQUIPMENT_KEY)
                return f'{item_name} equipped to {SLOTS[slot]}!'
            else:
                return f'Error: {item_name} cannot be equipped.'
        else:
            return f'Error: {item_name} not in inventory.'
    else:
        return f'Error: Insufficient level to equip item ({item_level}). Your current combat level is {user_cb_level}.'
Esempio n. 2
0
def start_gather(userid, item, length=-1, number=-1):
    """Starts a gathering session."""
    out = ''
    if not adv.is_on_adventure(userid):
        try:
            itemid = items.find_by_name(item)
            length = int(length)
            number = int(number)
        except KeyError:
            return f'Error: {item} is not an item.'
        except ValueError:
            return f'Error: {length} is not a valid length of time.'

        if not items.get_attr(itemid, key=items.GATHER_KEY):
            return f'Error: you cannot gather item {items.get_attr(itemid)}.'

        item_name = items.get_attr(itemid)
        gather_level = users.xp_to_level(
            users.read_user(userid, key=users.GATHER_XP_KEY))
        gather_requirement = items.get_attr(itemid, key=items.LEVEL_KEY)
        player_potion = users.read_user(userid, key=users.EQUIPMENT_KEY)['15']
        if player_potion == '435':
            boosted_level = gather_level + 3
        if player_potion == '436':
            boosted_level = gather_level + 6
        else:
            boosted_level = gather_level

        if boosted_level < gather_requirement:
            return f'Error: {item_name} has a gathering requirement ({gather_requirement}) higher ' \
                   f'than your gathering level ({gather_level})'
        quest_req = items.get_attr(itemid, key=items.QUEST_KEY)
        if quest_req not in set(
                users.get_completed_quests(userid)) and quest_req > 0:
            return f'Error: You do not have the required quest to gather this item.'

        if number > 1000 and gather_level == 99:
            number = 1000
        if number > 500 and gather_level < 99:
            number = 500
        if length > 180:
            length = 180

        if int(number) < 0:
            number = calc_number(userid, itemid, length * 60)
            if number > 500:
                number = 500
        elif int(length) < 0:
            length = math.floor(calc_length(userid, itemid, number)[1] / 60)
        else:
            return 'Error: argument missing (number or kill length).'
        gather = adv.format_line(3, userid, adv.get_finish_time(length * 60),
                                 itemid, item_name, number, length)
        adv.write(gather)
        out += f'You are now gathering {items.add_plural(number, itemid)} for {length} minutes.'
    else:
        out = adv.print_adventure(userid)
        out += adv.print_on_adventure_error('gathering')
    return out
Esempio n. 3
0
def unlock_item(userid, item):
    """Unlocks an item, allowing it to be sold again."""
    try:
        itemid = items.find_by_name(item)
    except KeyError:
        return f'No item with name {item} found.'

    item_name = items.get_attr(itemid)
    locked_items = read_user(userid, key=LOCKED_ITEMS_KEY)
    if itemid not in locked_items:
        return f'{item_name} is already unlocked.'
    locked_items.remove(itemid)
    update_user(userid, locked_items, key=LOCKED_ITEMS_KEY)

    return f'{item_name} has been unlocked!'
Esempio n. 4
0
def lock_item(userid, item):
    """Locks an item from being sold accidentally."""
    try:
        itemid = items.find_by_name(item)
    except KeyError:
        return f'No item with name {item} found.'

    item_name = items.get_attr(itemid)
    locked_items = read_user(userid, key=LOCKED_ITEMS_KEY)
    if itemid in locked_items:
        return f'{item_name} is already locked.'
    locked_items.append(itemid)
    update_user(userid, locked_items, key=LOCKED_ITEMS_KEY)

    return f'{item_name} has been locked!'
Esempio n. 5
0
def start_gather(userid, item, length=-1, number=-1):
    """Starts a gathering session."""
    out = ''
    if not adv.is_on_adventure(userid):
        try:
            itemid = items.find_by_name(item)
            length = int(length)
            number = int(number)
        except KeyError:
            return f'Error: {item} is not an item.'
        except ValueError:
            return f'Error: {length} is not a valid length of time.'

        if not items.get_attr(itemid, key=items.GATHER_KEY):
            return f'Error: you cannot gather item {items.get_attr(itemid)}.'

        item_name = items.get_attr(itemid)
        gather_level = users.xp_to_level(users.read_user(userid, key=users.GATHER_XP_KEY))
        gather_requirement = items.get_attr(itemid, key=items.LEVEL_KEY)
        if gather_level < gather_requirement:
            return f'Error: {item_name} has a gathering requirement ({gather_requirement}) higher ' \
                   f'than your gathering level ({gather_level})'

        if number > 500:
            number = 500
        if length > 180:
            length = 180

        if int(number) < 0:
            number = calc_number(userid, itemid, length * 60)
            if number > 500:
                number = 500
        elif int(length) < 0:
            length = math.floor(calc_length(userid, itemid, number)[1] / 60)
        else:
            return 'Error: argument missing (number or kill length).'
        gather = adv.format_line(3, userid, adv.get_finish_time(length * 60), itemid, item_name, number, length)
        adv.write(gather)
        out += f'You are now gathering {number} {items.add_plural(itemid)} for {length} minutes.'
    else:
        out = adv.print_adventure(userid)
        out += adv.print_on_adventure_error('gathering')
    return out
Esempio n. 6
0
def unequip_item(userid, item):
    """Takes an item out of a user's equipment and places it into their inventory."""
    try:
        itemid = items.find_by_name(item)
    except KeyError:
        return f'Error: {item} does not exist.'

    item_name = items.get_attr(itemid)
    equipment = read_user(userid, key=EQUIPMENT_KEY)
    if itemid in equipment.values():
        slot = str(items.get_attr(itemid, key=items.SLOT_KEY))
        if int(slot) > 0:
            equipment = read_user(userid, key=EQUIPMENT_KEY)
            if equipment[slot] == -1:
                return f'{item_name} is not equipped in {SLOTS[str(slot)]}.'
            update_inventory(userid, [itemid])
            equipment[slot] = -1
            update_user(userid, equipment, EQUIPMENT_KEY)
            return f'{item_name} unequipped from {SLOTS[str(slot)]}!'
        else:
            return f'Error: {item_name} cannot be unequipped.'
    else:
        return f'You do not have {item_name} equipped.'
Esempio n. 7
0
    async def trade(self, ctx, *args):
        """Trades to a person a number of a given object for a given price."""
        if ctx.channel.id == SHOP_CHANNEL or ctx.channel.id in GENERAL_CHANNELS:
            if users.read_user(ctx.author.id, key=users.IRONMAN_KEY):
                await ctx.send('Ironmen cannot trade.')
                return

            if len(args) < 4:
                await ctx.send(
                    'Arguments missing. Syntax is `~trade [name] [number] [item] [offer]`.'
                )
                return

            name = args[0]
            for member in ctx.guild.members:
                if name.lower() in member.name.lower():
                    name_member = member
                    break
            else:
                await ctx.send(f'{name} not found in server.')
                return
            if users.read_user(name_member.id, key=users.IRONMAN_KEY):
                await ctx.send('You cannot trade with an ironman.')
                return

            try:
                number = int(args[1])
            except ValueError:
                await ctx.send(f'{args[1]} is not a valid number.')
                return

            try:
                offer = users.parse_int(args[-1])
                itemid = items.find_by_name(' '.join(args[2:-1]))
            except ValueError:
                await ctx.send(f'{args[-1]} is not a valid offer.')
                return
            except KeyError:
                await ctx.send(f"{' '.join(args[2:-1])} is not a valid item.")
                return

            if not users.item_in_inventory(ctx.author.id, itemid, number):
                await ctx.send(
                    f'You do not have {items.add_plural(number, itemid)} in your inventory.'
                )
                return

            if not items.is_tradable(itemid):
                await ctx.send(
                    f'You can not trade this item. ({items.get_attr(itemid)})')
                return

            if not users.item_in_inventory(name_member.id, "0", offer):
                await ctx.send(
                    f'{get_display_name(name_member)} does not have enough gold to buy this many items.'
                )
                return

            name = get_display_name(ctx.author)
            offer_formatted = '{:,}'.format(offer)
            out = f'{items.SHOP_HEADER}{name.title()} wants to sell {name_member.mention} ' \
                  f'{items.add_plural(number, itemid)} for {offer_formatted} coins. To accept this offer, reply ' \
                  f'to this post with a :thumbsup:. Otherwise, this offer will expire in one minute.'
            msg = await ctx.send(out)
            await msg.add_reaction('\N{THUMBS UP SIGN}')

            while True:
                try:
                    reaction, user = await self.bot.wait_for('reaction_add',
                                                             timeout=60)
                    if str(
                            reaction.emoji
                    ) == '👍' and user == name_member and reaction.message.id == msg.id:
                        price = offer * ["0"]
                        users.update_inventory(name_member.id,
                                               price,
                                               remove=True)
                        users.update_inventory(ctx.author.id, price)
                        loot = number * [itemid]
                        users.update_inventory(ctx.author.id,
                                               loot,
                                               remove=True)
                        users.update_inventory(name_member.id, loot)

                        buyer_name = get_display_name(name_member)
                        await ctx.send(
                            f'{items.SHOP_HEADER}{name.title()} successfully sold '
                            f'{items.add_plural(number, itemid)} to {buyer_name} for '
                            f'{offer_formatted} coins!')
                        return
                except asyncio.TimeoutError:
                    await msg.edit(
                        content=
                        f'One minute has passed and your offer has been cancelled.'
                    )
                    return