annotate Modules/DisplayInvested.lua @ 49:9ff6a3b02332 ticket7

I have changed my mind, price distribution will always be based on the AH price, unless none is available, then all items have the same weight which is what was being done before.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Wed, 21 Jul 2010 01:00:14 -0700
parents 5e981c1df761
children bdf3aba93aa9
rev   line source
Asa@28 1 local addonName, addonTable = ...;
Asa@28 2 local ItemAuditor = _G[addonName]
Asa@28 3
Asa@28 4 local AceGUI = LibStub("AceGUI-3.0")
Asa@28 5 local ScrollingTable = LibStub("ScrollingTable")
Asa@28 6
Asa@35 7 local priceTypeEach = 1
Asa@35 8 local priceTypeTotal = 2
Asa@35 9
Asa@35 10 local promptFrame = false
Asa@35 11
Asa@35 12 -- Copied from QuickAuctions
Asa@35 13 local function validateMoney(value)
Asa@35 14 local gold = tonumber(string.match(value, "([0-9]+)|c([0-9a-fA-F]+)g|r") or string.match(value, "([0-9]+)g"))
Asa@35 15 local silver = tonumber(string.match(value, "([0-9]+)|c([0-9a-fA-F]+)s|r") or string.match(value, "([0-9]+)s"))
Asa@35 16 local copper = tonumber(string.match(value, "([0-9]+)|c([0-9a-fA-F]+)c|r") or string.match(value, "([0-9]+)c"))
Asa@35 17
Asa@35 18 if( not gold and not silver and not copper ) then
Asa@35 19 return false;
Asa@35 20 -- return L["Invalid monney format entered, should be \"#g#s#c\", \"25g4s50c\" is 25 gold, 4 silver, 50 copper."]
Asa@35 21 end
Asa@35 22
Asa@35 23 return true
Asa@35 24 end
Asa@35 25
Asa@35 26 -- Copied from QuickAuctions
Asa@35 27 local function parseMoney(value)
Asa@35 28 local gold = tonumber(string.match(value, "([0-9]+)|c([0-9a-fA-F]+)g|r") or string.match(value, "([0-9]+)g"))
Asa@35 29 local silver = tonumber(string.match(value, "([0-9]+)|c([0-9a-fA-F]+)s|r") or string.match(value, "([0-9]+)s"))
Asa@35 30 local copper = tonumber(string.match(value, "([0-9]+)|c([0-9a-fA-F]+)c|r") or string.match(value, "([0-9]+)c"))
Asa@35 31
Asa@35 32 -- Convert it all into copper
Asa@35 33 return (copper or 0) + ((gold or 0) * COPPER_PER_GOLD) + ((silver or 0) * COPPER_PER_SILVER)
Asa@35 34
Asa@35 35 end
Asa@35 36
Asa@43 37 StaticPopupDialogs["ItemAuditor_NewPrice"] = {
Asa@43 38 text = "New price %s %s",
Asa@43 39 button1 = SAVE,
Asa@43 40 button2 = CANCEL,
Asa@43 41 hasEditBox = 1,
Asa@43 42 showAlert = 1,
Asa@43 43 OnAccept = function()
Asa@43 44 skipCODTracking = true
Asa@43 45 end,
Asa@43 46 EditBoxOnEnterPressed = function()
Asa@43 47 if ( getglobal(this:GetParent():GetName().."Button1"):IsEnabled() == 1 ) then
Asa@43 48 getglobal(this:GetParent():GetName().."Button1"):Click()
Asa@43 49 end
Asa@43 50 end,
Asa@43 51 EditBoxOnTextChanged = function ()
Asa@43 52 local parentName = this:GetParent():GetName()
Asa@43 53 local editBox = getglobal( parentName.."EditBox");
Asa@43 54 local value = editBox:GetText()
Asa@43 55 if validateMoney(value) then
Asa@43 56 getglobal(parentName.."Button1"):Enable();
Asa@43 57 else
Asa@43 58 getglobal(parentName.."Button1"):Disable();
Asa@43 59 end
Asa@43 60 end,
Asa@43 61 EditBoxOnEscapePressed = function()
Asa@43 62 this:GetParent():Hide();
Asa@43 63 ClearCursor();
Asa@43 64 end,
Asa@43 65 timeout = 0,
Asa@43 66 hideOnEscape = 1,
Asa@43 67 exclusive = true,
Asa@43 68 }
Asa@35 69
Asa@35 70 local function PromptForNewPrice(link, type)
Asa@35 71 local investedTotal, investedPerItem, count = ItemAuditor:GetItemCost(link)
Asa@43 72
Asa@43 73 local typeText = "Invested Each"
Asa@35 74 local price = investedPerItem
Asa@43 75 if type == priceTypeTotal then
Asa@43 76 typeText = "Invested Total"
Asa@43 77 price = investedTotal
Asa@43 78
Asa@43 79 end
Asa@35 80
Asa@43 81 StaticPopupDialogs["ItemAuditor_NewPrice"].text = format("Update %s: %s|nThe current value is %s", typeText, link, ItemAuditor:FormatMoney(price))
Asa@43 82
Asa@43 83 StaticPopupDialogs["ItemAuditor_NewPrice"].OnShow = function (self, data)
Asa@43 84 self.editBox:SetText(ItemAuditor:FormatMoney(price, '', true))
Asa@35 85 end
Asa@43 86
Asa@43 87 StaticPopupDialogs["ItemAuditor_NewPrice"].OnAccept = function()
Asa@43 88 local name = this:GetParent():GetName().."EditBox"
Asa@43 89 local button = getglobal(name)
Asa@43 90 local newValue = button:GetText()
Asa@43 91 newValue = parseMoney(newValue)
Asa@35 92
Asa@43 93 local investedTotal, investedPerItem, numOwned = ItemAuditor:GetItemCost(link)
Asa@43 94
Asa@43 95 if type == priceTypeEach then
Asa@43 96 newValue = newValue * numOwned
Asa@43 97 end
Asa@43 98
Asa@43 99 ItemAuditor:SaveValue(link, newValue-investedTotal, 0)
Asa@35 100 end
Asa@46 101 StaticPopup_Show ("ItemAuditor_NewPrice");
Asa@35 102 end
Asa@35 103
Asa@35 104 local function displayMoney(rowFrame, cellFrame, data, cols, row, realrow, column, fShow, table, ...)
Asa@35 105 if fShow == true then
Asa@35 106 local money = data[realrow][column]
Asa@43 107 cellFrame.text:SetText(ItemAuditor:FormatMoney(data[realrow][column]))
Asa@35 108 end
Asa@35 109 end
Asa@35 110
Asa@28 111 local investedCols = {
Asa@43 112 { name= "Item", width = 200, defaultsort = "desc",
Asa@28 113 ['DoCellUpdate'] = function(rowFrame, cellFrame, data, cols, row, realrow, column, fShow, table, ...)
Asa@28 114 if fShow == true then
Asa@28 115 local _, link= strsplit("|", data[realrow][column], 2)
Asa@28 116 cellFrame.text:SetText(link)
Asa@28 117 end
Asa@28 118 end,
Asa@28 119 },
Asa@28 120 { name= "Invested Total", width = 100, align = "RIGHT",
Asa@35 121 ['DoCellUpdate'] = displayMoney,
Asa@28 122 },
Asa@35 123 { name= "Invested Each", width = 100, align = "RIGHT",
Asa@35 124 ['DoCellUpdate'] = displayMoney,
Asa@28 125 },
Asa@43 126 { name= "# Owned", width = 50, align = "RIGHT", },
Asa@28 127 }
Asa@28 128
Asa@28 129 local investedTable = false
Asa@28 130 local function ShowInvested(container)
Asa@28 131 if investedTable == false then
Asa@28 132 local window = container.frame
Asa@28 133 investedTable = ScrollingTable:CreateST(investedCols, 23, nil, nil, window)
Asa@28 134 investedTable.frame:SetPoint("BOTTOMLEFT",window, 10,10)
Asa@28 135 investedTable.frame:SetPoint("TOP", window, 0, -60)
Asa@28 136 investedTable.frame:SetPoint("RIGHT", window, -10,0)
Asa@28 137 investedTable:RegisterEvents({
Asa@28 138 ["OnEnter"] = function (rowFrame, cellFrame, data, cols, row, realrow, column, scrollingTable, ...)
Asa@28 139 if realrow then
Asa@28 140 local _, link= strsplit("|", data[realrow][1], 2)
Asa@28 141
Asa@28 142 GameTooltip:SetOwner(rowFrame, "ANCHOR_CURSOR")
Asa@28 143 GameTooltip:SetHyperlink(link)
Asa@28 144 GameTooltip:Show()
Asa@28 145 end
Asa@28 146 end,
Asa@28 147 ["OnLeave"] = function (rowFrame, cellFrame, data, cols, row, realrow, column, scrollingTable, ...)
Asa@28 148 GameTooltip:Hide()
Asa@28 149 end,
Asa@28 150 ["OnClick"] = function (rowFrame, cellFrame, data, cols, row, realrow, column, scrollingTable, ...)
Asa@35 151 if realrow ~= nil and (column == 2 or column == 3) then
Asa@35 152 -- column.text = row:CreateFontString(col:GetName().."text", "OVERLAY", "GameFontHighlightSmall");
Asa@35 153 local _, link= strsplit("|", data[realrow][1], 2)
Asa@35 154
Asa@35 155 local type=priceTypeEach
Asa@35 156 if column == 2 then
Asa@35 157 type = priceTypeTotal
Asa@35 158 end
Asa@35 159
Asa@35 160 PromptForNewPrice(link, type)
Asa@28 161 end
Asa@28 162 end,
Asa@28 163 });
Asa@28 164 end
Asa@28 165 investedTable:Show()
Asa@28 166
Asa@28 167 local width = 80
Asa@28 168 for i, data in pairs(investedCols) do
Asa@28 169 width = width + data.width
Asa@28 170 end
Asa@28 171 if container.parent then
Asa@28 172 container.parent:SetWidth(width);
Asa@28 173 end
Asa@28 174
Asa@28 175
Asa@28 176 UpdateInvestedData()
Asa@28 177 end
Asa@28 178
Asa@28 179
Asa@28 180 local function switchTab(container, event, group)
Asa@28 181 container:ReleaseChildren()
Asa@28 182
Asa@28 183 if investedTab then investedTab:Hide() end
Asa@28 184
Asa@28 185 if group == "tab_invested" then
Asa@28 186 ShowInvested(container)
Asa@28 187 end
Asa@28 188 end
Asa@28 189
Asa@28 190
Asa@28 191
Asa@28 192 displayFrame = false
Asa@28 193 local function CreateFrames()
Asa@28 194 if not displayFrame then
Asa@28 195 -- Create the frame container
Asa@28 196 displayFrame = AceGUI:Create("Frame")
Asa@38 197 ItemAuditor:RegisterFrame(displayFrame)
Asa@28 198 local window = displayFrame.frame;
Asa@43 199 -- I have no idea why AceGUI insists on using FULLSCREEN_DIALOG by default.
Asa@43 200 window:SetFrameStrata("MEDIUM")
Asa@28 201 displayFrame:SetTitle("ItemAuditor")
Asa@28 202 displayFrame:SetStatusText("")
Asa@28 203
Asa@28 204 displayFrame:SetLayout("Fill")
Asa@28 205
Asa@28 206 window:SetHeight(500);
Asa@28 207
Asa@28 208 local width = 80
Asa@28 209 for i, data in pairs(investedCols) do
Asa@28 210 width = width + data.width
Asa@28 211 end
Asa@28 212 window:SetWidth(width);
Asa@28 213
Asa@28 214 local tab = AceGUI:Create("TabGroup")
Asa@28 215 tab:SetLayout("Flow")
Asa@28 216 tab:SetTabs({{text="Invested", value="tab_invested"}})
Asa@28 217 tab:SetCallback("OnGroupSelected", switchTab)
Asa@28 218 tab:SelectTab("tab_invested")
Asa@28 219
Asa@28 220 displayFrame:AddChild(tab)
Asa@28 221 end
Asa@28 222 displayFrame:Show()
Asa@28 223 end
Asa@28 224
Asa@28 225
Asa@28 226
Asa@28 227
Asa@28 228
Asa@28 229 function UpdateInvestedData()
Asa@28 230 if investedTable then
Asa@28 231 tableData = {} --reset
Asa@28 232 local totalInvested = 0
Asa@28 233
Asa@28 234 local i = 1
Asa@28 235 local data
Asa@35 236 local items = ItemAuditor.db.factionrealm.items
Asa@35 237 local includedItems = {}
Asa@35 238 for safeLink in pairs(items) do
Asa@35 239 local investedTotal, investedPerItem, count = ItemAuditor:GetItemCost(safeLink)
Asa@35 240 local itemName, link = GetItemInfo(safeLink)
Asa@35 241 if investedTotal > 0 and link ~= nil then
Asa@28 242 tableData[i] = {
Asa@28 243 itemName.."|"..link,
Asa@28 244 investedTotal,
Asa@28 245 investedPerItem,
Asa@28 246 count,
Asa@28 247 }
Asa@28 248
Asa@28 249 totalInvested = totalInvested + investedTotal
Asa@28 250
Asa@28 251 i = i + 1
Asa@35 252 includedItems[safeLink] = true
Asa@35 253 end
Asa@35 254 end
Asa@35 255
Asa@35 256 local inventory = ItemAuditor:GetCurrentInventory()
Asa@35 257
Asa@35 258 for link, count in pairs(inventory.items) do
Asa@35 259 if includedItems[link] == nil then
Asa@35 260 local count = Altoholic:GetItemCount(ItemAuditor:GetIDFromLink(link))
Asa@35 261 local itemName, link = GetItemInfo(link)
Asa@35 262 tableData[i] = {
Asa@35 263 itemName.."|"..link,
Asa@35 264 0,
Asa@35 265 0,
Asa@35 266 count,
Asa@35 267 }
Asa@35 268
Asa@35 269 -- totalInvested = totalInvested + investedTotal
Asa@35 270
Asa@35 271 i = i + 1
Asa@28 272 end
Asa@28 273 end
Asa@28 274
Asa@28 275 if investedTable.frame:IsShown() then
Asa@28 276 displayFrame:SetStatusText("Total Invested: "..ItemAuditor:FormatMoney(totalInvested))
Asa@28 277 end
Asa@28 278
Asa@28 279 investedTable:SetData(tableData, true)
Asa@28 280 end
Asa@28 281 end
Asa@28 282
Asa@28 283 function ItemAuditor:CreateFrames()
Asa@28 284 CreateFrames()
Asa@28 285 end
Asa@28 286