WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Sorting Tooltip for money by Faction (https://www.wowinterface.com/forums/showthread.php?t=56900)

cokedrivers 12-15-18 10:04 AM

Sorting Tooltip for money by Faction
 
currently i have a mouseover tooltip in mt datatext that shows me my currents relms charators total gold.

Code:

                        local totalGold = 0                               
                        GameTooltip:AddLine("Character's: ")                       
                        local thisRealmList = BasicDB.Currency[myPlayerRealm];
                        for k,v in pairs(thisRealmList) do
                                GameTooltip:AddDoubleLine(k, formatMoney(v), 1, 1, 1, 1, 1, 1)
                                totalGold=totalGold+v;
                        end

Which Gives me this return:


I have both Horde and Alliance I would like to see how much gold i have on each faction beings you cannot send gold to other factions.

Any help would be great.

P.S. If you need more code my whole addon is on GitHub Here. And this is in the Datatext.lua file under bags.

Thanks
Coke

cokedrivers 12-21-18 07:19 AM

So noone knows anyway to get this info?

im trying to do it this way:
Code:

                        local myPlayerName  = UnitName("player")
                        local myPlayerFaction = UnitFactionGroup("player");
                        if not BasicDB.Currency then BasicDB.Currency = {} end
                        if not BasicDB.Currency[myPlayerRealm] then BasicDB.Currency[myPlayerRealm]={} end
                        BasicDB.Currency[myPlayerRealm][myPlayerFaction][myPlayerName] = GetMoney()

but it keeps thowing up and error:
Code:

34x BasicUI\Modules\Datapanel.lua:558: attempt to index field '?' (a nil value)
BasicUI\Modules\Datapanel.lua:558: in function <BasicUI\Modules\Datapanel.lua:510>

Locals:
self = <unnamed> {
 0 = <userdata>
}
event = "PLAYER_ENTERING_WORLD"
totalSlots = 120
freeSlots = 68
itemLink = "|cffffffff|Hitem:160452::::::::114:581::::::|h[]|h|r"
subtype = nil
isBag = true
NewMoney = 122614145
Change = 0
myPlayerName = "Øsmosis"
myPlayerFaction = "Alliance"
(*temporary) = nil
(*temporary) = 122614145
(*temporary) = 122614145
(*temporary) = "Alliance"
(*temporary) = "Alliance"
(*temporary) = <userdata>
(*temporary) = "/"
(*temporary) = "120"
(*temporary) = "attempt to index field '?' (a nil value)"
Text = <unnamed> {
 0 = <userdata>
}
OldMoney = 122614145
Spent = 0
Profit = 0
myPlayerRealm = "Fenris"

But im not on Osmosis im ona different toon

Rilgamon 12-21-18 08:22 AM

Check which value throws the error in 558 and make sure it has a value before you use it.
You're not clear if you want to only see the same faction listed or list and sum up both.
My code of zz_Money shows only the same faction. So perhaps you can check it for what you want to do.

cokedrivers 12-22-18 11:21 AM

Quote:

Originally Posted by Rilgamon (Post 331107)
Check which value throws the error in 558 and make sure it has a value before you use it.
You're not clear if you want to only see the same faction listed or list and sum up both.
My code of zz_Money shows only the same faction. So perhaps you can check it for what you want to do.

Thanks This Helped some.

So here is the current plugin code for the Bags/Money:
Code:

        if db.bags then
                local bagsPlugin = CreateFrame('Frame', nil, Datapanel)
                bagsPlugin:EnableMouse(true)
                bagsPlugin:SetFrameStrata('BACKGROUND')
                bagsPlugin:SetFrameLevel(3)

                local Text = bagsPlugin:CreateFontString(nil, 'OVERLAY')
                Text:SetFont(db.font, db.fontSize,'THINOUTLINE')
                PlacePlugin(db.bags, Text)

                local Profit        = 0
                local Spent                = 0
                local OldMoney        = 0
                local myPlayerName  = UnitName('player');
                local myPlayerRealm = GetRealmName();
                local myPlayerFaction = UnitFactionGroup('player')
                       
                local function formatMoney(c)
                        local str = ""
                        if not c or c < 0 then
                                return str
                        end
                       
                        if c >= 10000 then
                                local g = math.floor(c/10000)
                                c = c - g*10000
                                str = str..BreakUpLargeNumbers(g).."|cFFFFD800g|r "
                        end
                        if c >= 100 then
                                local s = math.floor(c/100)
                                c = c - s*100
                                str = str..s.."|cFFC7C7C7s|r "
                        end
                        if c >= 0 then
                                str = str..c.."|cFFEEA55Fc|r"
                        end
                       
                        return str
                end
               
                local function OnEvent(self, event)
                        local totalSlots, freeSlots = 0, 0
                        local itemLink, subtype, isBag
                        for i = 0,NUM_BAG_SLOTS do
                                isBag = true
                                if i > 0 then
                                        itemLink = GetInventoryItemLink('player', ContainerIDToInventoryID(i))
                                        if itemLink then
                                                subtype = select(7, GetItemInfo(itemLink))
                                                if (subtype == 'Mining Bag') or (subtype == 'Gem Bag') or (subtype == 'Engineering Bag') or (subtype == 'Enchanting Bag') or (subtype == 'Herb Bag') or (subtype == 'Inscription Bag') or (subtype == 'Leatherworking Bag') or (subtype == 'Fishing Bag')then
                                                        isBag = false
                                                end
                                        end
                                end
                                if isBag then
                                        totalSlots = totalSlots + GetContainerNumSlots(i)
                                        freeSlots = freeSlots + GetContainerNumFreeSlots(i)
                                end
                                Text:SetText(hexa.."Bags: "..hexb.. freeSlots.. '/' ..totalSlots)
                                        if freeSlots < 6 then
                                                Text:SetTextColor(1,0,0)
                                        elseif freeSlots < 10 then
                                                Text:SetTextColor(1,0,0)
                                        elseif freeSlots > 10 then
                                                Text:SetTextColor(1,1,1)
                                        end
                                self:SetAllPoints(Text)
                               
                        end
                        --if event == "PLAYER_LOGIN" then
                                --OldMoney = GetMoney()
                        --end
                       
                        local NewMoney = GetMoney()

                        db['Currency'] = db['Currency'] or {}
                        db['Currency'][myPlayerRealm] = db['Currency'][myPlayerRealm] or {}
                        db['Currency'][myPlayerRealm][myPlayerFaction] = db['Currency'][myPlayerRealm][myPlayerFaction] or {}
                        db['Currency'][myPlayerRealm][myPlayerFaction][myPlayerName] = db['Currency'][myPlayerRealm][myPlayerFaction][myPlayerName] or NewMoney

                        OldMoney = db['Currency'][myPlayerRealm][myPlayerFaction][myPlayerName] or NewMoney

                        local Change = NewMoney - OldMoney

                        if (OldMoney > NewMoney) then
                                Spent = Spent - Change
                        else
                                Profit = Profit + Change
                        end                       
                       
                        self:SetAllPoints(Text)       
                       
                        db['Currency'][myPlayerRealm][myPlayerFaction][myPlayerName] = NewMoney
                       
                               
                end

                bagsPlugin:RegisterEvent("PLAYER_MONEY")
                bagsPlugin:RegisterEvent("SEND_MAIL_MONEY_CHANGED")
                bagsPlugin:RegisterEvent("SEND_MAIL_COD_CHANGED")
                bagsPlugin:RegisterEvent("PLAYER_TRADE_MONEY")
                bagsPlugin:RegisterEvent("TRADE_MONEY_CHANGED")
                bagsPlugin:RegisterEvent("PLAYER_LOGIN")
                bagsPlugin:RegisterEvent("BAG_UPDATE")
               
                bagsPlugin:SetScript('OnMouseDown',
                        function()
                                if db.bag ~= true then
                                        ToggleAllBags()
                                else
                                        ToggleBag(0)
                                end
                        end
                )
                bagsPlugin:SetScript('OnEvent', OnEvent)       
                bagsPlugin:SetScript("OnEnter", function(self)
                        local anchor, panel, xoff, yoff = DataTextTooltipAnchor(Text)
                        GameTooltip:SetOwner(panel, anchor, xoff, yoff)
                        GameTooltip:ClearLines()
                        GameTooltip:AddDoubleLine(hexa..PLAYER_NAME.."'s"..hexb.."|cffffd700 Currency|r", formatMoney(OldMoney), 1, 1, 1, 1, 1, 1)
                        GameTooltip:AddLine' '                       
                        GameTooltip:AddLine("This Session: ")                               
                        GameTooltip:AddDoubleLine("Earned:", formatMoney(Profit), 1, 1, 1, 1, 1, 1)
                        GameTooltip:AddDoubleLine("Spent:", formatMoney(Spent), 1, 1, 1, 1, 1, 1)
                        if Profit < Spent then
                                GameTooltip:AddDoubleLine("Deficit:", formatMoney(Profit-Spent), 1, 0, 0, 1, 1, 1)
                        elseif (Profit-Spent)>0 then
                                GameTooltip:AddDoubleLine("Profit:", formatMoney(Profit-Spent), 0, 1, 0, 1, 1, 1)
                        end               
                        GameTooltip:AddLine' '
                       
                        local totalGold = 0                               
                        GameTooltip:AddLine("Character's: ")
                        local thisRealmList = db['Currency'][myPlayerRealm];
                        if db['Currency'][myPlayerRealm][myPlayerFaction] == ('Alliance') then
                                for k,v in pairs(thisRealmList) do
                                        GameTooltip:AddLine('Alliance:')
                                        GameTooltip:AddDoubleLine(k, formatMoney(v), 1, 1, 1, 1, 1, 1)
                                        totalGold = totalGold + v;
                                end
                        elseif db['Currency'][myPlayerRealm][myPlayerFaction] == ('Horde') then
                                for k,v in pairs(thisRealmList) do
                                        GameTooltip:AddLine('Horde:')
                                        GameTooltip:AddDoubleLine(k, formatMoney(v), 1, 1, 1, 1, 1, 1)
                                        totalGold = totalGold + v;
                                end
                        elseif db['Currency'][myPlayerRealm][myPlayerFaction] == ('Neutral') then
                                for k,v in pairs(thisRealmList) do
                                        GameTooltip:AddLine('Neutral:')
                                        GameTooltip:AddDoubleLine(k, formatMoney(v), 1, 1, 1, 1, 1, 1)
                                        totalGold = totalGold + v;
                                end
                        end       

                        GameTooltip:AddLine' '
                        GameTooltip:AddLine("Server:")
                        GameTooltip:AddDoubleLine("Total: ", formatMoney(totalGold), 1, 1, 1, 1, 1, 1)

                        for i = 1, GetNumWatchedTokens() do
                                local name, count, extraCurrencyType, icon, itemID = GetBackpackCurrencyInfo(i)
                                if name and i == 1 then
                                        GameTooltip:AddLine(" ")
                                        GameTooltip:AddLine(CURRENCY..":")
                                end
                                local r, g, b = 1,1,1
                                if itemID then r, g, b = GetItemQualityColor(select(3, GetItemInfo(itemID))) end
                                if name and count then GameTooltip:AddDoubleLine(name, count, r, g, b, 1, 1, 1) end
                        end
                        GameTooltip:AddLine' '
                        GameTooltip:AddLine("|cffeda55fClick|r to Open Bags")                       
                        GameTooltip:Show()
                end)
               
                bagsPlugin:SetScript("OnLeave", function() GameTooltip:Hide() end)                       

        end

and this code creates the first post image:
Code:

        if db.bags then
                local bagsPlugin = CreateFrame('Frame', nil, Datapanel)
                bagsPlugin:EnableMouse(true)
                bagsPlugin:SetFrameStrata('BACKGROUND')
                bagsPlugin:SetFrameLevel(3)

                local Text = bagsPlugin:CreateFontString(nil, 'OVERLAY')
                Text:SetFont(db.font, db.fontSize,'THINOUTLINE')
                PlacePlugin(db.bags, Text)

                local Profit        = 0
                local Spent                = 0
                local OldMoney        = 0
                local myPlayerRealm = GetRealmName();
               
               
                local function formatMoney(c)
                        local str = ""
                        if not c or c < 0 then
                                return str
                        end
                       
                        if c >= 10000 then
                                local g = math.floor(c/10000)
                                c = c - g*10000
                                str = str..BreakUpLargeNumbers(g).."|cFFFFD800g|r "
                        end
                        if c >= 100 then
                                local s = math.floor(c/100)
                                c = c - s*100
                                str = str..s.."|cFFC7C7C7s|r "
                        end
                        if c >= 0 then
                                str = str..c.."|cFFEEA55Fc|r"
                        end
                       
                        return str
                end
               
                local function OnEvent(self, event)
                        local totalSlots, freeSlots = 0, 0
                        local itemLink, subtype, isBag
                        for i = 0,NUM_BAG_SLOTS do
                                isBag = true
                                if i > 0 then
                                        itemLink = GetInventoryItemLink('player', ContainerIDToInventoryID(i))
                                        if itemLink then
                                                subtype = select(7, GetItemInfo(itemLink))
                                                if (subtype == 'Mining Bag') or (subtype == 'Gem Bag') or (subtype == 'Engineering Bag') or (subtype == 'Enchanting Bag') or (subtype == 'Herb Bag') or (subtype == 'Inscription Bag') or (subtype == 'Leatherworking Bag') or (subtype == 'Fishing Bag')then
                                                        isBag = false
                                                end
                                        end
                                end
                                if isBag then
                                        totalSlots = totalSlots + GetContainerNumSlots(i)
                                        freeSlots = freeSlots + GetContainerNumFreeSlots(i)
                                end
                                Text:SetText(hexa.."Bags: "..hexb.. freeSlots.. '/' ..totalSlots)
                                        if freeSlots < 6 then
                                                Text:SetTextColor(1,0,0)
                                        elseif freeSlots < 10 then
                                                Text:SetTextColor(1,0,0)
                                        elseif freeSlots > 10 then
                                                Text:SetTextColor(1,1,1)
                                        end
                                self:SetAllPoints(Text)
                               
                        end
                        if event == "PLAYER_ENTERING_WORLD" then
                                OldMoney = GetMoney()
                        end
                       
                        local NewMoney        = GetMoney()
                        local Change = NewMoney-OldMoney -- Positive if we gain money
                       
                        if OldMoney>NewMoney then                -- Lost Money
                                Spent = Spent - Change
                        else                                                        -- Gained Money
                                Profit = Profit + Change
                        end
                       
                        self:SetAllPoints(Text)

                        local myPlayerName  = UnitName("player")
                        local myPlayerFaction = UnitFactionGroup("player");
                        if not BasicDB.Currency then BasicDB.Currency = {} end
                        if not BasicDB.Currency[myPlayerRealm] then BasicDB.Currency[myPlayerRealm]={} end
                        BasicDB.Currency[myPlayerRealm][myPlayerName] = GetMoney()       
                               
                        OldMoney = NewMoney       
                               
                end

                bagsPlugin:RegisterEvent("PLAYER_MONEY")
                bagsPlugin:RegisterEvent("SEND_MAIL_MONEY_CHANGED")
                bagsPlugin:RegisterEvent("SEND_MAIL_COD_CHANGED")
                bagsPlugin:RegisterEvent("PLAYER_TRADE_MONEY")
                bagsPlugin:RegisterEvent("TRADE_MONEY_CHANGED")
                bagsPlugin:RegisterEvent("PLAYER_ENTERING_WORLD")
                bagsPlugin:RegisterEvent("BAG_UPDATE")
               
                bagsPlugin:SetScript('OnMouseDown',
                        function()
                                if db.bag ~= true then
                                        ToggleAllBags()
                                else
                                        ToggleBag(0)
                                end
                        end
                )
                bagsPlugin:SetScript('OnEvent', OnEvent)       
                bagsPlugin:SetScript("OnEnter", function(self)
                        local anchor, panel, xoff, yoff = DataTextTooltipAnchor(Text)
                        GameTooltip:SetOwner(panel, anchor, xoff, yoff)
                        GameTooltip:ClearLines()
                        GameTooltip:AddDoubleLine(hexa..PLAYER_NAME.."'s"..hexb.."|cffffd700 Gold|r", formatMoney(OldMoney), 1, 1, 1, 1, 1, 1)
                        GameTooltip:AddLine' '                       
                        GameTooltip:AddLine("This Session: ")                               
                        GameTooltip:AddDoubleLine("Earned:", formatMoney(Profit), 1, 1, 1, 1, 1, 1)
                        GameTooltip:AddDoubleLine("Spent:", formatMoney(Spent), 1, 1, 1, 1, 1, 1)
                        if Profit < Spent then
                                GameTooltip:AddDoubleLine("Deficit:", formatMoney(Profit-Spent), 1, 0, 0, 1, 1, 1)
                        elseif (Profit-Spent)>0 then
                                GameTooltip:AddDoubleLine("Profit:", formatMoney(Profit-Spent), 0, 1, 0, 1, 1, 1)
                        end                               
                        --GameTooltip:AddDoubleLine("Total:", formatMoney(OldMoney), 1, 1, 1, 1, 1, 1)
                        GameTooltip:AddLine' '
                       
                        local totalGold = 0                               
                        GameTooltip:AddLine("Character's: ")
                        local myPlayerFaction, _ = UnitFactionGroup("player");
                        local thisRealmList = BasicDB.Currency[myPlayerRealm];
                        for k,v in pairs(thisRealmList) do
                                GameTooltip:AddDoubleLine(k, formatMoney(v), 1, 1, 1, 1, 1, 1)
                                totalGold=totalGold+v;
                        end

                        GameTooltip:AddLine' '
                        GameTooltip:AddLine("Server:")
                        GameTooltip:AddDoubleLine("Total: ", formatMoney(totalGold), 1, 1, 1, 1, 1, 1)

                        for i = 1, GetNumWatchedTokens() do
                                local name, count, extraCurrencyType, icon, itemID = GetBackpackCurrencyInfo(i)
                                if name and i == 1 then
                                        GameTooltip:AddLine(" ")
                                        GameTooltip:AddLine(CURRENCY..":")
                                end
                                local r, g, b = 1,1,1
                                if itemID then r, g, b = GetItemQualityColor(select(3, GetItemInfo(itemID))) end
                                if name and count then GameTooltip:AddDoubleLine(name, count, r, g, b, 1, 1, 1) end
                        end
                        GameTooltip:AddLine' '
                        GameTooltip:AddLine("|cffeda55fClick|r to Open Bags")
                        GameTooltip:AddLine("|cffeda55fType|r /resetcurrency to Reset Currency Totals")                       
                        GameTooltip:Show()
                end)
               
                bagsPlugin:SetScript("OnLeave", function() GameTooltip:Hide() end)                       
                -- reset gold data
                local function RESETCURRENCY()
                        local myPlayerRealm = GetRealmName();
                        local myPlayerFaction = select(1, UnitFactionGroup("player"));
                        local myPlayerName  = UnitName("player");
                       
                        BasicDB.Currency = {}
                        BasicDB.Currency[myPlayerRealm]={}
                        BasicDB.Currency[myPlayerRealm][myPlayerFaction][myPlayerName] = GetMoney();
                end
                SLASH_RESETCURRENCY1 = "/resetcurrency"
                SlashCmdList["RESETCURRENCY"] = RESETCURRENCY       

        end

Here is the SavedVariables it creates:
Code:

BasicDB = {
        ["namespaces"] = {
                ["Nameplate"] = {
                },
                ["Misc"] = {
                },
                ["Buff"] = {
                },
                ["Powerbar"] = {
                },
                ["Tooltip"] = {
                },
                ["Chat"] = {
                },
                ["Unitframe"] = {
                },
                ["Actionbar"] = {
                },
                ["Datapanel"] = {
                        ["profiles"] = {
                                ["Default"] = {
                                        ["Currency"] = {
                                                ["Fenris"] = {
                                                        ["Horde"] = {
                                                                ["Unknøwn"] = 17661878,
                                                                ["ßullwinkle"] = 95560850,
                                                        },
                                                        ["Alliance"] = {
                                                                ["Silhøuette"] = 867196155,
                                                                ["Cokeman"] = 434537262,
                                                        },
                                                },
                                        },
                                },
                        },
                },
        },
        ["profileKeys"] = {
                ["ßullwinkle - Fenris"] = "Default",
                ["Silhøuette - Fenris"] = "Default",
                ["Unknøwn - Fenris"] = "Default",
                ["Cokeman - Fenris"] = "Default",
        },
        ["profiles"] = {
                ["Default"] = {
                },
        },
}

and yet nothings shows in the toolitp here is a screenshot:


As you can see nothing loads.

I think it might be useless to get this to do what i want.

Below is what im looking for in text format:

Code:

<Players Name>'s Currency          100g 00s 02c

This Session:
Earned:        100g0s0c
Spent:          20g0s0c

Character's:
Alliance:
<Players Name>          10,000g50s35c

Horde:
<Players Name>        1,000g0s0c

Nuetral:
<Players Name>          100g0s0c

Server:
Total        11,100g50s35c

Currency:
Nethershard        29

Click to Open Bags

If this is not possible please let me know so i can stop trying.

Thanks
Coke

Rilgamon 12-22-18 02:29 PM

You have the data. So its totally possible to display it.

Lua Code:
  1. if db['Currency'][myPlayerRealm][myPlayerFaction] == ('Alliance') then

In situations like this print out the variables so you can quickly see if the values are what you expect.
Here it seems you mixed up the content and the meaning of the key.
'myPlayerFaction' represents the value you try to compare.
db['Currency'][myPlayerRealm][myPlayerFaction] represents the list of your current faction.
From your described result to list all factions this means you have (at least) two faults here.
It should not be an if-elseif-clause if you want it all printed.

Lua Code:
  1. GameTooltip:AddLine('Alliance:') -- dont put the header in your loop
  2. for name,money in pairs(db['Currency'][myPlayerRealm]['Alliance']) do
  3.   GameTooltip:AddDoubleLine(name, formatMoney(money), 1, 1, 1, 1, 1, 1)
  4. end

cokedrivers 12-24-18 08:57 AM

Quote:

Originally Posted by Rilgamon (Post 331130)
You have the data. So its totally possible to display it.

Lua Code:
  1. if db['Currency'][myPlayerRealm][myPlayerFaction] == ('Alliance') then

In situations like this print out the variables so you can quickly see if the values are what you expect.
Here it seems you mixed up the content and the meaning of the key.
'myPlayerFaction' represents the value you try to compare.
db['Currency'][myPlayerRealm][myPlayerFaction] represents the list of your current faction.
From your described result to list all factions this means you have (at least) two faults here.
It should not be an if-elseif-clause if you want it all printed.

Lua Code:
  1. GameTooltip:AddLine('Alliance:') -- dont put the header in your loop
  2. for name,money in pairs(db['Currency'][myPlayerRealm]['Alliance']) do
  3.   GameTooltip:AddDoubleLine(name, formatMoney(money), 1, 1, 1, 1, 1, 1)
  4. end

Thank You Rilgamon for the help i finally got it figured out and the code you last posted helped dramatically.

Here is a screen shot of the new Tooltip:


Again thank you for the help.

Happy Gaming and Coding to All.
Coke

cokedrivers 12-27-18 08:39 AM

So I've been playing around with the code and i think I finally have it where I like it here are some Screen Shots...

Alliance Only - Horde Only - Neutral Only -

Alliance and Neutral - And Finally All 3 Factions -

The spacing for missing factions sucks but i had to do a IF for nil so i used GameTooltip:AddDoubleLine(" ", " ") for nil then the tooltip for the Faction if its available. another reason for the bigs gaps are because of the space between factions if all 3 of them are shown.

Well let me know if they look decent or if they are just to cluttered.

Thanks
Coke

JDoubleU00 12-27-18 11:32 AM

Just curious, are you neutral characters Pandaren that have not picked a faction?

cokedrivers 12-27-18 11:45 AM

Quote:

Originally Posted by JDoubleU00 (Post 331176)
Just curious, are you neutral characters Pandaren that have not picked a faction?

Yes and i believe that Demon Hunters are the same.

Seerah 12-28-18 12:45 PM

Quote:

Originally Posted by cokedrivers (Post 331174)
The spacing for missing factions sucks but i had to do a IF for nil so i used GameTooltip:AddDoubleLine(" ", " ") for nil then the tooltip for the Faction if its available. another reason for the bigs gaps are because of the space between factions if all 3 of them are shown.

:confused: You should be able to fix all that.

cokedrivers 12-29-18 11:23 AM

Quote:

Originally Posted by Seerah (Post 331184)
:confused: You should be able to fix all that.

Would it be like a if nil then return? I tried that and it killed the whole plugin. I'm not anywears near a coder i just try to limp threw it.


I'll try to research more on being able to hide code without taking up space

Thanks

cokedrivers 12-29-18 11:31 AM

so this is the code I use that hides or shows the Factions gold:
Code:

                        local totalGold = 0;       
                        local totalAlianceGold = 0;
                        local totalHordeGold = 0;
                        local totalNeutralGold = 0;
                       
                        if db['Gold'][myPlayerRealm]['Alliance'] == nil then
                                GameTooltip:AddDoubleLine(" ", " ")
                        else
                                GameTooltip:AddLine("Alliance Character's:")
                                for k,v in pairs(db['Gold'][myPlayerRealm]['Alliance']) do
                                        GameTooltip:AddDoubleLine(iconAlliance..k, formatMoney(v), 1, 1, 1, 1, 1, 1)
                                        totalAlianceGold = totalAlianceGold + v;
                                end
                                GameTooltip:AddDoubleLine("Total Alliance Gold", formatMoney(totalAlianceGold))
                        end
                       
                        GameTooltip:AddDoubleLine(" ", " ")       
                       
                        if db['Gold'][myPlayerRealm]['Horde'] == nil then
                                GameTooltip:AddDoubleLine(" ", " ")
                        else
                                GameTooltip:AddLine("Horde Character's:")
                                for k,v in pairs(db['Gold'][myPlayerRealm]['Horde']) do
                                        GameTooltip:AddDoubleLine(iconHorde..k, formatMoney(v), 1, 1, 1, 1, 1, 1)
                                        totalHordeGold = totalHordeGold + v;
                                end
                                GameTooltip:AddDoubleLine("Total Horde Gold", formatMoney(totalHordeGold))
                        end
                       
                        GameTooltip:AddDoubleLine(" ", " ")       
                       
                        if db['Gold'][myPlayerRealm]['Neutral'] == nil then
                                GameTooltip:AddDoubleLine(" ", " ")
                        else
                                GameTooltip:AddLine("Neutral Character's:")
                                for k,v in pairs(db['Gold'][myPlayerRealm]['Neutral']) do
                                        GameTooltip:AddDoubleLine(iconNuetral..k, formatMoney(v), 1, 1, 1, 1, 1, 1)
                                        totalNeutralGold = totalNeutralGold + v;
                                end
                                GameTooltip:AddDoubleLine("Total Neutral Gold", formatMoney(totalNeutralGold))
                        end
                       
                        local totalServerGold = totalAlianceGold + totalHordeGold + totalNeutralGold
                       
                        GameTooltip:AddLine" "
                        GameTooltip:AddDoubleLine("Total Gold for "..myPlayerRealm, formatMoney(totalServerGold))

can it be condensed?

jlam 12-29-18 02:51 PM

Quote:

Originally Posted by cokedrivers (Post 331192)
can it be condensed?

Here is one way to shorten it. You're doing the same thing per faction, so you can create a loop that does the same thing. I've renamed some variables to make it more obvious what values they hold. I also think that by adding the blank line as a header for each faction's gold, it fixes the "extra" blank lines in your tooltip. Please note that I dry-coded this, so there may be typos and other errors.
Code:

local factions = { 'Alliance', 'Horde', 'Neutral' }
local factionGold = {
        Alliance = 0,
        Horde = 0,
        Neutral = 0,
}
local factionIcon = {
        Alliance = iconAlliance,
        Horde = iconHorde,
        Neutral = iconNeutral,
}
local totalRealmGold = 0

local goldDB = (db and db.Gold) and db.Gold[myPlayerRealm] or nil
if goldDB then
        for _, faction in pairs(factions) do
                if goldDB[faction] then
                        GameTooltip:AddDoubleLine(" ", " ")
                        GameTooltip:AddLine(faction .. " Characters:")
                        for name, amount in pairs(goldDB[faction]) do
                                GameTooltip:AddDoubleLine(factionIcon[faction] .. name, formatMoney(amount), 1, 1, 1, 1, 1, 1)
                                factionGold[faction] = factionGold[faction] + amount
                        end
                        GameTooltip:AddDoubleLine("Total " .. faction .. " Gold", formatMoney(factionGold[faction]))
                        totalRealmGold = totalRealmGold + factionGold[faction]
                end
        end
        GameTooltip:AddDoubleLine("Total Gold for " .. myPlayerRealm, formatMoney(totalRealmGold))
end


Seerah 12-29-18 04:57 PM

Quote:

Originally Posted by cokedrivers (Post 331192)
so this is the code I use that hides or shows the Factions gold:
...
can it be condensed?

First, there should be no apostrophe in "Characters". ;) Oh, and your totalAllianceGold variable was misspelled, but at least it matched everywhere. :D

Second, you're getting the gaps because you're adding the gaps. ;) (Oh, and you also don't need to add a double-line to create a spacer, it can just be a regular line. This way you don't end up with more strings created in memory than necessary.)
Lua Code:
  1. local totalGold = 0;   
  2. local totalAlianceGold = 0;
  3. local totalHordeGold = 0;
  4. local totalNeutralGold = 0;
  5.  
  6. if db['Gold'][myPlayerRealm]['Alliance'] == nil then
  7.     GameTooltip:AddDoubleLine(" ", " ")     --no alliance characters, but let's add a line anyway
  8. else
  9.     GameTooltip:AddLine("Alliance Character's:")
  10.     for k,v in pairs(db['Gold'][myPlayerRealm]['Alliance']) do
  11.         GameTooltip:AddDoubleLine(iconAlliance..k, formatMoney(v), 1, 1, 1, 1, 1, 1)
  12.         totalAlianceGold = totalAlianceGold + v;
  13.     end
  14.     GameTooltip:AddDoubleLine("Total Alliance Gold", formatMoney(totalAlianceGold))
  15. end
  16.  
  17. GameTooltip:AddDoubleLine(" ", " ")     --now let's add a spacer between factions
  18.        
  19. if db['Gold'][myPlayerRealm]['Horde'] == nil then
  20.     GameTooltip:AddDoubleLine(" ", " ")     --no horde characters, but let's add a line anyway
  21. else
  22.     GameTooltip:AddLine("Horde Character's:")
  23.     for k,v in pairs(db['Gold'][myPlayerRealm]['Horde']) do
  24.         GameTooltip:AddDoubleLine(iconHorde..k, formatMoney(v), 1, 1, 1, 1, 1, 1)
  25.         totalHordeGold = totalHordeGold + v;
  26.     end
  27.     GameTooltip:AddDoubleLine("Total Horde Gold", formatMoney(totalHordeGold))
  28. end
  29.    
  30. GameTooltip:AddDoubleLine(" ", " ")     --now let's add a spacer
  31.            
  32. if db['Gold'][myPlayerRealm]['Neutral'] == nil then
  33.     GameTooltip:AddDoubleLine(" ", " ")
  34. else
  35.     GameTooltip:AddLine("Neutral Character's:")
  36.     for k,v in pairs(db['Gold'][myPlayerRealm]['Neutral']) do
  37.         GameTooltip:AddDoubleLine(iconNuetral..k, formatMoney(v), 1, 1, 1, 1, 1, 1)
  38.         totalNeutralGold = totalNeutralGold + v;
  39.     end
  40.     GameTooltip:AddDoubleLine("Total Neutral Gold", formatMoney(totalNeutralGold))
  41. end
  42.            
  43. local totalServerGold = totalAlianceGold + totalHordeGold + totalNeutralGold
  44.    
  45. GameTooltip:AddLine" "
  46. GameTooltip:AddDoubleLine("Total Gold for "..myPlayerRealm, formatMoney(totalServerGold))

For example, I've commented above how it is that you are getting 4 blank lines instead of one if you have only neutral characters on a server.

If you follow the logic of what you are trying to do, you want an empty line (a spacer) after every faction's section. This is even true for the last displayed faction in the tooltip, since you want a spacer before the total gold on the realm. If a faction's info will not be present in the tooltip, then you don't need the spacer. If you wish to keep your code formatted as is (rather than jlam's condensed version), then do this instead.
Lua Code:
  1. local totalGold = 0
  2. local totalAllianceGold = 0
  3. local totalHordeGold = 0
  4. local totalNeutralGold = 0
  5.  
  6. if db['Gold'][myPlayerRealm]['Alliance'] then     --so long as this will never have a value of false, you really only care if a value exists
  7.     GameTooltip:AddLine("Alliance Characters:")     --faction heading
  8.     for k,v in pairs(db['Gold'][myPlayerRealm]['Alliance']) do     --display all characters
  9.         GameTooltip:AddDoubleLine(iconAlliance..k, formatMoney(v), 1, 1, 1, 1, 1, 1)
  10.         totalAllianceGold = totalAllianceGold + v
  11.     end
  12.     GameTooltip:AddDoubleLine("Total Alliance Gold", formatMoney(totalAllianceGold))     --faction total
  13.         GameTooltip:AddLine("")     --add a spacer after this faction
  14. end
  15.  
  16. if db['Gold'][myPlayerRealm]['Horde'] then
  17.     GameTooltip:AddLine("Horde Characters:")     --faction heading
  18.     for k,v in pairs(db['Gold'][myPlayerRealm]['Horde']) do     --display all characters
  19.         GameTooltip:AddDoubleLine(iconHorde..k, formatMoney(v), 1, 1, 1, 1, 1, 1)
  20.         totalHordeGold = totalHordeGold + v
  21.     end
  22.     GameTooltip:AddDoubleLine("Total Horde Gold", formatMoney(totalHordeGold))     --faction total
  23.         GameTooltip:AddLine("")     --add a spacer after this faction
  24. end
  25.    
  26. if db['Gold'][myPlayerRealm]['Neutral'] then
  27.     GameTooltip:AddLine("Neutral Characters:")     --faction heading
  28.     for k,v in pairs(db['Gold'][myPlayerRealm]['Neutral']) do     --display all characters
  29.         GameTooltip:AddDoubleLine(iconNuetral..k, formatMoney(v), 1, 1, 1, 1, 1, 1)
  30.         totalNeutralGold = totalNeutralGold + v
  31.     end
  32.     GameTooltip:AddDoubleLine("Total Neutral Gold", formatMoney(totalNeutralGold))     --faction total
  33.         GameTooltip:AddLine("")     --add a spacer after this faction
  34. end
  35.            
  36. local totalServerGold = totalAlianceGold + totalHordeGold + totalNeutralGold
  37. GameTooltip:AddDoubleLine("Total Gold for "..myPlayerRealm, formatMoney(totalServerGold))     --server total

cokedrivers 12-30-18 10:02 AM

Quote:

Originally Posted by Seerah (Post 331195)
First, there should be no apostrophe in "Characters". ;) Oh, and your totalAllianceGold variable was misspelled, but at least it matched everywhere. :D

Second, you're getting the gaps because you're adding the gaps. ;) (Oh, and you also don't need to add a double-line to create a spacer, it can just be a regular line. This way you don't end up with more strings created in memory than necessary.)

...

For example, I've commented above how it is that you are getting 4 blank lines instead of one if you have only neutral characters on a server.

If you follow the logic of what you are trying to do, you want an empty line (a spacer) after every faction's section. This is even true for the last displayed faction in the tooltip, since you want a spacer before the total gold on the realm. If a faction's info will not be present in the tooltip, then you don't need the spacer. If you wish to keep your code formatted as is (rather than jlam's condensed version), then do this instead.

...

Thank You for the help, I did need to change one thing for it to work correctly and that was to change GameTooltip:AddLine("")


to GameTooltip:AddLine(" ")

because without the space the gap was not added. but thank you very much.

Also thanks for the spell check :D

Coke

Seerah 12-30-18 12:46 PM

I guess I forgot that it wouldn't work with an empty string. :o


All times are GMT -6. The time now is 08:53 PM.

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