WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   AddOn Help/Support (https://www.wowinterface.com/forums/forumdisplay.php?f=3)
-   -   Stuf LUA help (https://www.wowinterface.com/forums/showthread.php?t=52788)

hikokun 10-03-15 01:16 PM

Stuf LUA help
 
I'm looking for a custom lua code to show curhp/maxhp followed by perchp with two decimals for Stuf Unit Frames. The current code I'm using which only shows perchp with two decimals -

Code:

function(unit, cache, textframe)
  if UnitExists(unit) then
    local perchp = 100 * UnitHealth(unit)/UnitHealthMax(unit)
    return "%.2f%%",perchp
  else
    local perchp = 100 * UnitHealth("player")/UnitHealthMax("player")
    return "%.2f%%",perchp
  end
end

I'd like a code for both of the following setups if possible

300000/500000 | 60.00%
60.00% | 300000/500000


Thanks for any help!

Nikita S. Doroshenko 10-03-15 06:15 PM

Quote:

Originally Posted by hikokun (Post 311289)
I'm looking for a custom lua code to show curhp/maxhp followed by perchp with two decimals for Stuf Unit Frames. The current code I'm using which only shows perchp with two decimals -

Code:

function(unit, cache, textframe)
  if UnitExists(unit) then
    local perchp = 100 * UnitHealth(unit)/UnitHealthMax(unit)
    return "%.2f%%",perchp
  else
    local perchp = 100 * UnitHealth("player")/UnitHealthMax("player")
    return "%.2f%%",perchp
  end
end

I'd like a code for both of the following setups if possible

300000/500000 | 60.00%
60.00% | 300000/500000


Thanks for any help!

Could you please paste a code that calls this function with this parameters (unit, cache, textframe).
Because we need to know what previous function do with our return.
Or you can show us whole .lua or .xml file, it will be more easy.

Solution will looks something like this:
Lua Code:
  1. local stamp = UnitHealth("player").." / "..UnitHealthMax("player").." | "..perchp
  2. return stamp
But then we need to change function that get our return value.

Nikita S. Doroshenko 10-03-15 06:24 PM

Quote:

Originally Posted by hikokun (Post 311289)
I'm looking for a custom lua code to show curhp/maxhp followed by perchp with two decimals for Stuf Unit Frames. The current code I'm using which only shows perchp with two decimals -

Code:

function(unit, cache, textframe)
  if UnitExists(unit) then
    local perchp = 100 * UnitHealth(unit)/UnitHealthMax(unit)
    return "%.2f%%",perchp
  else
    local perchp = 100 * UnitHealth("player")/UnitHealthMax("player")
    return "%.2f%%",perchp
  end
end

I'd like a code for both of the following setups if possible

300000/500000 | 60.00%
60.00% | 300000/500000


Thanks for any help!

I think i fount a solution, try to replace your function with this one:

Lua Code:
  1. function(unit, cache, textframe)
  2.     if UnitExists(unit) then
  3.         local curhp = UnitHealth(unit)
  4.         local maxhp = UnitHealthMax(unit)
  5.         local perchp = 100 * curhp/maxhp
  6.         local stamp = curhp.." / "..maxhp.." | ".."%.2f%%"
  7.         return stamp,perchp
  8.     else
  9.         local curhp = UnitHealth("player")
  10.         local maxhp = UnitHealthMax("player")
  11.         local perchp = 100 * curhp/maxhp
  12.         local stamp = curhp.." / "..maxhp.." | ".."%.2f%%"
  13.         return stamp,perchp
  14.     end
  15. end

Or cleaner:

Lua Code:
  1. function(unit, cache, textframe)
  2.     local curhp, maxhp
  3.     if UnitExists(unit) then
  4.         curhp = UnitHealth(unit)
  5.         maxhp = UnitHealthMax(unit)
  6.     else
  7.         curhp = UnitHealth("player")
  8.         maxhp = UnitHealthMax("player")
  9.     end
  10.     local perchp = 100 * curhp/maxhp
  11.     local stamp = curhp.." / "..maxhp.." | ".."%.2f%%"
  12.     return stamp,perchp
  13. end

hikokun 10-03-15 07:15 PM

Quote:

Originally Posted by Nikita S. Doroshenko (Post 311297)
Lua Code:
  1. function(unit, cache, textframe)
  2.     local curhp, maxhp
  3.     if UnitExists(unit) then
  4.         curhp = UnitHealth(unit)
  5.         maxhp = UnitHealthMax(unit)
  6.     else
  7.         curhp = UnitHealth("player")
  8.         maxhp = UnitHealthMax("player")
  9.     end
  10.     local perchp = 100 * curhp/maxhp
  11.     local stamp = curhp.." / "..maxhp.." | ".."%.2f%%"
  12.     return stamp,perchp
  13. end

This one works perfect, thank you! I attempted (with no knowledge of lua) to also make it into the format of

60.00% | 300000/500000

but the text would disappear each time. How would I go about switching it around?

Edit:
Lua Code:
  1. function(unit, cache, textframe)
  2.         local curhp, maxhp
  3.         if UnitExists(unit) then
  4.             curhp = UnitHealth(unit)
  5.             maxhp = UnitHealthMax(unit)
  6.         else
  7.             curhp = UnitHealth("player")
  8.             maxhp = UnitHealthMax("player")
  9.         end
  10.         local perchp = 100 * curhp/maxhp
  11.         local stamp = "%.2f%%".." | "..curhp.." / "..maxhp
  12.         return stamp,perchp
  13.     end

Figured it out after messing around a bit! However, I've now noticed that the numbers are not being shortened (I have my STUF options to shorten starting at 1 million).

Instead of showing 1M, its showing 1000000. Any way to fix this?

Ideally I'd like it to show like this -

60.00% | 3.0M / 5.0M
5.00% | 250000 / 5.0M

Phanx 10-03-15 08:34 PM

The simplest way would be to just call the Blizzard function for short number display:
Code:

        -- After this line:
        local perchp = 100 * curhp/maxhp
        -- add these two lines:
        curhp = AbbreviateLargeNumbers(curhp)
        maxhp = AbbreviateLargeNumbers(maxhp)


hikokun 11-06-17 02:59 PM

Long ago I had figured out a solution but I've now lost it and I'm looking for it again --

The above solution only shortens it as so -

60.00% | 3000K / 5000K

I would prefer it to start shortening at 1000000 as so -

60.00% | 3.0M / 5.0M
5.00% | 250000 / 5.0M

Thanks for any help!

Edit:
Lua Code:
  1. function(unit, cache, textframe)
  2.             local curhp, maxhp
  3.             if UnitExists(unit) then
  4.                 curhp = UnitHealth(unit)
  5.                 maxhp = UnitHealthMax(unit)
  6.             else
  7.                 curhp = UnitHealth("player")
  8.                 maxhp = UnitHealthMax("player")
  9.             end
  10.             local perchp = 100 * curhp/maxhp
  11.             local stamp = "%.2f%%".." | "..curhp.." / "..maxhp
  12.             return stamp,perchp
  13.         end

Current code I'm using.

Ammako 11-06-17 05:09 PM

This is something I do to format numbers and group into thousands/millions.

lua Code:
  1. local function formatPlayerNumbers(amount)
  2.     local formatted, k = amount
  3.  
  4.     if amount >= 100000 and amount < 100000000 then
  5.         formatted = string.sub(formatted, 0, (string.len(formatted) - 3)) .. " K"
  6.     elseif amount >= 100000000 then
  7.         formatted = string.sub(formatted, 0, (string.len(formatted) - 6)) .. " M"
  8.     else
  9.         while true do  
  10.             formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  11.             if (k==0) then
  12.                 break
  13.             end
  14.         end
  15.     end
  16.  
  17.     return formatted
  18. end

Of course, you'll have to tweak the amounts at lines 4 and 6 to get it to shorten 5,000,000 as 5M rather than 5,000K, but you get the point.

Kakjens 11-06-17 05:31 PM

Could you explain what happens in lines 9-14 because for something that should have a simple display the code seems too complex.

Ammako 11-06-17 06:01 PM

Couldn't tell you honestly. When I needed a function like that I googled it and this is what I got as a result.
It works so I didn't bother messing with it any further, but if you find that this part of the code isn't actually needed it can be removed, of course.

hikokun 11-06-17 06:02 PM

Quote:

Originally Posted by Ammako (Post 325733)
This is something I do to format numbers and group into thousands/millions.

lua Code:
  1. local function formatPlayerNumbers(amount)
  2.     local formatted, k = amount
  3.  
  4.     if amount >= 100000 and amount < 100000000 then
  5.         formatted = string.sub(formatted, 0, (string.len(formatted) - 3)) .. " K"
  6.     elseif amount >= 100000000 then
  7.         formatted = string.sub(formatted, 0, (string.len(formatted) - 6)) .. " M"
  8.     else
  9.         while true do  
  10.             formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  11.             if (k==0) then
  12.                 break
  13.             end
  14.         end
  15.     end
  16.  
  17.     return formatted
  18. end

Of you're, you'll have to tweak the amounts at lines 4 and 6 to get it to shorten 5,000,000 as 5M rather than 5,000K, but you get the point.

I appreciate the code, but how do I incorporate it into my previous code? I've tried a few different ways and all it does is causes the text to disappear.

Fizzlemizz 11-06-17 06:10 PM

gsub returns 1, the new string and 2, the number of matches. When the number of matches == 0 (in this case k) break out of the loop.

Kakjens 11-07-17 01:27 AM

Quote:

Originally Posted by Ammako (Post 325733)
This is something I do to format numbers and group into thousands/millions.

lua Code:
  1. local function formatPlayerNumbers(amount)
  2.     local formatted, k = amount
  3.  
  4.     if amount >= 100000 and amount < 100000000 then
  5.         formatted = string.sub(formatted, 0, (string.len(formatted) - 3)) .. " K"
  6.     elseif amount >= 100000000 then
  7.         formatted = string.sub(formatted, 0, (string.len(formatted) - 6)) .. " M"
  8.     else
  9.         while true do  
  10.             formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  11.             if (k==0) then
  12.                 break
  13.             end
  14.         end
  15.     end
  16.  
  17.     return formatted
  18. end

Of you're, you'll have to tweak the amounts at lines 4 and 6 to get it to shorten 5,000,000 as 5M rather than 5,000K, but you get the point.

hikokun, while debugging I suggest in line 16 of this code to add print(formatted). And in your code, in line 11 to replace curhp with formatPlayerNumbers(curhp), and maxhp with formatPlayerNumbers(maxhp).

Fizzlemizz, thanks for explanation but I meant to ask what the code accomplishes instead of what it does.

But won't this become unneeded after 7.3.5 with ilvl (and stat) squishes?

Ammako 11-07-17 01:53 AM

Holy crap, I just realized. "Of you're." What kind of typo is that?

Anyway patterns are too much for my brain to comprehend tbh, but the end result is that it takes the number and adds comma separator (so 10000 becomes 10,000.)
If you feel like you can understand it you can probably read up on documentation to figure out exactly what the pattern matches.

Maybe it won't be as useful anymore once they do the next numbers squish, but that's hardly a reason not to make addons for the current game. ;p

Kakjens 11-07-17 03:09 AM

Ammako, human language is relatively fault-tolerant (unlike programming ones), so was able to guess what you intended to write.
Thanks for explaining the result of this loop with gsub and link. No experience with pattern matching but I believe it does something like: "If, starting from beginning, there's at least one digit (1-st group) followed by three digits (2-nd group), then between 1-st and second group insert comma". Sounds somehow similar to BreakupLageNumbers.
Seeing that the applicable numbers for this procedure are between 999 and 100000, I wonder if it would be more efficient to use extra "if" instead of "while" loop.
Edit. About writing the code which won't be used after this patch: I would be really hesitant to do it. I would think about whether having the exact desired output would be worth the trouble or be content with, for example, AbbreviateLargeNumbers.

Fizzlemizz 11-07-17 10:11 AM

Quote:

Originally Posted by Kakjens (Post 325739)
But won't this become unneeded after 7.3.5 with ilvl (and stat) squishes?

It will be needed, because in future patches, the numbers will go up again and most likely get the these sizes before something happens...again.

Using the while statement is more of a maintenance thing so you don't have to keep adding "if" statements as the numbers get larger while requiring less iterations when the numbers a smaller.

Ammako 11-07-17 03:13 PM

Ok so I took the time to understand the pattern, I might as well explain how it works.
  • ^ only matches starting from the beginning of the string.
  • -? the string will match whether it has a - character at the front or not (meaning -100000 would be broken up as -100,000 just the same as 100000 would be broken up as 100,000.)
  • %d+ matches one or more digits.
  • ^(-?%d+) matches infinitely from the beginning of the string, whether it has a leading - character or not, if there is at least one digit, until it hits a non-digit character.
  • (%d%d%d) matches three digits in a row.
  • ^(-?%d+)(%d%d%d) matches a string, whether it has a leading - character or not, with at least four leading digits (with the last three being sorted into their own separate group.)

So when you feed it a number, it'll start looking from the beginning of the string for something that may or may not begin with a - character and has at least four leading digits, stopping whenever it hits anything else (in this case, a comma character.)
After it matches, it adds a comma between the two parts.

^(-?%d+)(%d%d%d) : 10000000 --> (10000),(000) --> (10),(000),000 --> 10,000,000 (no more matches.)


All times are GMT -6. The time now is 11:17 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI