LazyDope / avrae-stuff

Here you will find my aliases that I've made public.
5 stars 2 forks source link

The Floats continue to not work #14

Closed Adalbar3333 closed 1 year ago

Adalbar3333 commented 1 year ago

Hello!!! Sorry for being annoying about it...

I'm still having problems using the baglib's decimal function to alter lower coin types. I tried using the function of coins exactly, however, it does not set the coins when I use baglib.save_bags. Additionally, when I have the modify_coins, it does not use the float as expected.

Here is the code I am using.

!test <drac2>

using(bagLib = '4119d62e-6a98-4153-bea9-0a99bb36da2c')

alter = ["+11.53","gp"]

ch                = character()
bagsLoaded        = bagLib.load_bags()
old_coins         = bagLib.get_coins(bagsLoaded)[1].copy()
autoCoins         = bagLib.settings.get('autoCoins',get("autocoins","0")=="1")
compact           = bagLib.settings.get('compactCoins',get("compactcoins","0")=="1")
openMode          = bagLib.settings.get('openMode','all').lower()
delta, coin_error = bagLib.parsecoins(alter)
focus             = None
error             = coin_error
cmd               = ctx.prefix + ctx.alias
title             = f"{name} looks inside"
positive          = False

if delta:
   focus, error = bagLib.modify_coins(bagsLoaded, autoCoins=autoCoins, delta = delta)
   if not error:
      delta = list(delta.items())
      delta.sort(key=lambda x: x[1], reverse=True)
      if delta[0][1]>0:
         title = f"{name} adds"
         positive = True
      else:
         title = f"{name} removes"

if not error:
    for idx, cointup in enumerate(delta):
        coin, quantity = cointup
        title += f"{'' if idx == 0 else ',' if (positive and quantity>0) or (not positive and quantity<0) else ', and removes'} {bagLib.round_nicely(abs(quantity))} {coin}"
        if positive and quantity<0:
            positive = False

bag_name = "Coin Purse" if bagLib.use_coin_purse(bagsLoaded) else bagLib.coinPouchName
if delta:
    title += f" to their {bag_name}" if positive else f" from their {bag_name}"
else:
    title += f" their {bag_name}"

if not focus:
    bagLib.get_coins(bagsLoaded)

if bagsLoaded:
    success = bagLib.save_bags(bagsLoaded, error)
else:
    success = -1 if error else 1

return ch.get_cvar('bags')

</drac2>

You can totally just ping me on the Avrae server or PM me via discord rather than replying here and just close it after confirming the fix.

Originally posted by @Adalbar3333 in https://github.com/LazyDope/avrae-stuff/issues/13#issuecomment-1647048536

Adalbar3333 commented 1 year ago

Additional Issue:

Utilizing the !coins alias on the workshop does not handle coins as desired.

If you have the lesser coins (silver and copper in the above example), it will remove the coin. Though, I did expect that if it had the gold above it, it will remove 1 gold, and add it to the silver, or copper as needed.

Here is the code I have in an alias of mine to handle coins, though it may need cleanup and organization to be added to your alias. I just want to help improve it to allow more people to handle their custom coins easier, rather than needing to use the coinpurse built in now.

This def I have also handles if it needs to use the coinpurse option if the user does not have a "Coin Pouch" bag.

def handle_coins(costs, ctype=coinTypes[0]):
    bagsLoaded = load_yaml(get('bags', '[]'))
    maybepouch = [i for i in bagsLoaded if i[0] == 'Coin Pouch'] or coinRates != DEFAULT_coinRates and [["Coin Pouch", {"cp": 0, "sp": 0, "ep": 0, "gp": 0, "pp": 0}]]
    if maybepouch:
        pouch = maybepouch[0]
        if pouch not in bagsLoaded:
            bagsLoaded.append(pouch)
            pouch = [i for i in bagsLoaded if i[0] == 'Coin Pouch'][0]
        coins = pouch[1]
        coins.update({ctype:coins[ctype]+costs})

        for coin in coinTypes[:-1]: # for each coin type except for the last one
            larger = coinTypes[coinTypes.index(coin)+1] # get the coin type that is one larger (cp -> sp, etc)
            rate = int(coinRates[coin]/coinRates[larger]) # find the rate between the current coin and the larger coin. (cp/sp = 100/10=10)
            p = coins[coin]//rate # find the new value by dividing the current amount of coins by the rate
            if coins[coin] < 0: # if our coin subtraction brought this coin type below 0 coins:
                coins.update( # Update our coin pouch
                {
                    larger:coins[larger]+p, # Subtract the new value to the larger coin type (since p is negative)
                    coin:coins[coin]-p*rate # Add the new value multiplied by the rate to the smaller coin type
                } 
                )
        # If they dont have the coin, it errors.
        error = any([coins[x]<0 for x in coins])
        if not error:
            character().set_cvar("bags",dump_json(bagsLoaded))

    else:
        purse = character().coinpurse
        delta = parse_coins(f"{costs}{ctype}")
        if not (error:=-delta.total > purse.total):
            purse.modify_coins(delta.pp, delta.gp, delta.ep, delta.sp, delta.cp)
        coins = {coin:purse[coin] for coin in coinTypes}
    return error, coins
LazyDope commented 1 year ago

This should be fixed with aa89c92