annotate Summary.lua @ 12:417c3cfb9623

GetItemInfo seems to be changed in 4.0.1, but I can?t find proper documentation on the changes done... Well anyway, whenever your items cache has been reset, Inventorium should continue default to itemname and link ?Unknown (#ITEMID)? and white rarity.
author Zerotorescue
date Sat, 16 Oct 2010 20:58:39 +0200
parents 10a2244f7ff0
children 5006cb0e97c6
rev   line source
Zerotorescue@11 1 local addon = LibStub("AceAddon-3.0"):GetAddon("Inventorium");
Zerotorescue@1 2 local mod = addon:NewModule("Summary", "AceEvent-3.0", "AceTimer-3.0");
Zerotorescue@0 3
Zerotorescue@0 4 local AceGUI = LibStub("AceGUI-3.0");
Zerotorescue@0 5
Zerotorescue@0 6 function mod:OnEnable()
Zerotorescue@0 7 self:RegisterWidgets();
Zerotorescue@0 8
Zerotorescue@1 9 -- Register our own slash commands
Zerotorescue@1 10 addon:RegisterSlash(function()
Zerotorescue@1 11 mod:BuildMain();
Zerotorescue@1 12 mod:Build();
Zerotorescue@1 13 end, "s", "sum", "summary");
Zerotorescue@0 14 end
Zerotorescue@0 15
Zerotorescue@0 16 function mod:RegisterWidgets()
Zerotorescue@0 17 -- Register InlineGroupWithButton-widget
Zerotorescue@0 18 -- This widget adds a button next to the header of an inline group
Zerotorescue@0 19 -- SetPoint doesn't seem usable within AceGUI.
Zerotorescue@0 20
Zerotorescue@0 21 local widgetType = "InlineGroupWithButton";
Zerotorescue@0 22 local widgetVersion = 1;
Zerotorescue@0 23
Zerotorescue@0 24 local function Constructor()
Zerotorescue@0 25 local widget = AceGUI:Create("InlineGroup");
Zerotorescue@0 26 widget.type = widgetType;
Zerotorescue@0 27
Zerotorescue@0 28 widget.MakeButton = function(self, buttonSettings)
Zerotorescue@0 29 if type(buttonSettings) == "table" then
Zerotorescue@0 30 local button = CreateFrame("Button", nil, self.frame, "UIPanelButtonTemplate");
Zerotorescue@0 31 button:SetText(buttonSettings.name);
Zerotorescue@0 32 button:SetHeight(22);
Zerotorescue@0 33 button:SetWidth(120);
Zerotorescue@0 34 button:SetPoint("TOPRIGHT", self.frame, "TOPRIGHT", -10, 5);
Zerotorescue@0 35 button:SetScript("OnClick", buttonSettings.exec);
Zerotorescue@0 36 button.tooltipTitle = buttonSettings.name;
Zerotorescue@0 37 button.tooltip = buttonSettings.desc or "";
Zerotorescue@0 38 button:SetScript("OnEnter", function(self)
Zerotorescue@0 39 GameTooltip:SetOwner(self, "ANCHOR_NONE")
Zerotorescue@0 40 GameTooltip:SetPoint("BOTTOM", self, "TOP")
Zerotorescue@0 41 GameTooltip:SetText(self.tooltipTitle, 1, .82, 0, 1)
Zerotorescue@0 42
Zerotorescue@0 43 if type(self.tooltip) == "string" then
Zerotorescue@0 44 GameTooltip:AddLine(self.tooltip, 1, 1, 1, 1);
Zerotorescue@0 45 end
Zerotorescue@0 46
Zerotorescue@0 47 GameTooltip:Show();
Zerotorescue@0 48 end);
Zerotorescue@0 49 button:SetScript("OnLeave", function(self)
Zerotorescue@0 50 GameTooltip:Hide();
Zerotorescue@0 51 end);
Zerotorescue@0 52 else
Zerotorescue@0 53 error("settings must be a table - usage: MakeButton(table);");
Zerotorescue@0 54 end
Zerotorescue@0 55 end
Zerotorescue@0 56
Zerotorescue@0 57 return widget;
Zerotorescue@0 58 end
Zerotorescue@0 59
Zerotorescue@0 60 AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion);
Zerotorescue@0 61 end
Zerotorescue@0 62
Zerotorescue@1 63 local itemsCache = {};
Zerotorescue@1 64 local CACHE_ITEMS_TOTAL, CACHE_ITEMS_CURRENT, CACHE_ITEMS_PER_UPDATE = 0, 0, 5;
Zerotorescue@1 65 local cacheStart;
Zerotorescue@1 66
Zerotorescue@0 67 function mod:BuildMain()
Zerotorescue@11 68 LibStub("AceConfigDialog-3.0"):Close("InventoriumOptions");
Zerotorescue@0 69
Zerotorescue@10 70 self:CloseFrame();
Zerotorescue@10 71
Zerotorescue@1 72 -- Main Window
Zerotorescue@1 73 mod.frame = AceGUI:Create("Frame");
Zerotorescue@11 74 mod.frame:SetTitle("Inventorium Summary");
Zerotorescue@1 75 mod.frame:SetLayout("Fill");
Zerotorescue@1 76 mod.frame:SetCallback("OnClose", function(widget)
Zerotorescue@7 77 mod:CancelTimer(self.tmrUpdater, true);
Zerotorescue@9 78 mod:CloseFrame();
Zerotorescue@1 79 end);
Zerotorescue@0 80
Zerotorescue@1 81 -- ScrollFrame child
Zerotorescue@1 82 mod.scrollFrame = AceGUI:Create("ScrollFrame");
Zerotorescue@1 83 mod.scrollFrame:SetLayout("Flow");
Zerotorescue@0 84
Zerotorescue@1 85 mod.frame:AddChild(mod.scrollFrame);
Zerotorescue@1 86
Zerotorescue@1 87 -- Reset items cache
Zerotorescue@1 88 table.wipe(itemsCache);
Zerotorescue@0 89 end
Zerotorescue@0 90
Zerotorescue@9 91 function mod:CloseFrame()
Zerotorescue@9 92 if mod.frame then
Zerotorescue@9 93 mod.frame:Release();
Zerotorescue@9 94 mod.frame = nil;
Zerotorescue@10 95
Zerotorescue@10 96 -- Stop caching
Zerotorescue@10 97
Zerotorescue@10 98 -- Stop timer
Zerotorescue@10 99 self:CancelTimer(self.tmrUpdater, true);
Zerotorescue@10 100
Zerotorescue@10 101 -- Reset trackers
Zerotorescue@10 102 CACHE_ITEMS_TOTAL = 0;
Zerotorescue@10 103 CACHE_ITEMS_CURRENT = 0;
Zerotorescue@9 104 end
Zerotorescue@9 105 end
Zerotorescue@9 106
Zerotorescue@0 107 local sortMethod = "item";
Zerotorescue@0 108 local sortDirectory = "ASC";
Zerotorescue@0 109 local function ReSort(subject)
Zerotorescue@0 110 if sortMethod == subject then
Zerotorescue@0 111 sortDirectory = (sortDirectory == "ASC" and "DESC") or "ASC";
Zerotorescue@0 112 else
Zerotorescue@0 113 sortDirectory = "ASC";
Zerotorescue@0 114 end
Zerotorescue@0 115 sortMethod = subject;
Zerotorescue@0 116
Zerotorescue@0 117 mod:Build();
Zerotorescue@0 118 end
Zerotorescue@10 119
Zerotorescue@10 120 -- From http://www.wowwiki.com/API_sort
Zerotorescue@10 121 local function pairsByKeys (t, f)
Zerotorescue@10 122 local a = {}
Zerotorescue@10 123 for n in pairs(t) do table.insert(a, n) end
Zerotorescue@10 124 table.sort(a, f)
Zerotorescue@10 125 local i = 0 -- iterator variable
Zerotorescue@10 126 local iter = function () -- iterator function
Zerotorescue@10 127 i = i + 1
Zerotorescue@10 128 if a[i] == nil then return nil
Zerotorescue@10 129 else return a[i], t[a[i]]
Zerotorescue@10 130 end
Zerotorescue@10 131 end
Zerotorescue@10 132 return iter
Zerotorescue@10 133 end
Zerotorescue@0 134
Zerotorescue@0 135 function mod:Build()
Zerotorescue@10 136 local buildStartTime, times = GetTime(), {};
Zerotorescue@0 137
Zerotorescue@1 138 -- We are going to add hunderds of widgets to this container, but don't want it to also cause hunderds of reflows, thus pause reflowing and just do it once when everything is prepared
Zerotorescue@1 139 -- This appears to be required for each container we wish to pause, so also do this for the contents
Zerotorescue@1 140 mod.scrollFrame:PauseLayout();
Zerotorescue@1 141
Zerotorescue@10 142 mod.scrollFrame:ReleaseChildren();
Zerotorescue@10 143
Zerotorescue@10 144 -- Refresh button
Zerotorescue@10 145 local btnRefresh = AceGUI:Create("Button");
Zerotorescue@10 146 btnRefresh:SetText("Refresh");
Zerotorescue@10 147 -- SetTooltip: Reload all item counts and auction values.
Zerotorescue@10 148 btnRefresh:SetRelativeWidth(.2);
Zerotorescue@10 149 btnRefresh:SetCallback("OnClick", function()
Zerotorescue@10 150 -- Reset items cache
Zerotorescue@10 151 table.wipe(itemsCache);
Zerotorescue@10 152
Zerotorescue@10 153 -- Rebuild itemlist and start caching
Zerotorescue@10 154 mod:Build();
Zerotorescue@10 155 end);
Zerotorescue@10 156
Zerotorescue@10 157 mod.scrollFrame:AddChild(btnRefresh);
Zerotorescue@10 158
Zerotorescue@10 159 -- Speed slider
Zerotorescue@10 160 local btnRefresh = AceGUI:Create("Slider");
Zerotorescue@10 161 btnRefresh:SetLabel("Processing speed");
Zerotorescue@10 162 btnRefresh:SetSliderValues(0.01, 1, 0.01);
Zerotorescue@10 163 btnRefresh:SetIsPercent(true);
Zerotorescue@10 164 -- SetTooltip: Change the speed at which item counts and auction values are being cached. Higher is faster but may drastically reduce your FPS while caching.
Zerotorescue@10 165 btnRefresh:SetRelativeWidth(.3);
Zerotorescue@10 166 btnRefresh:SetCallback("OnMouseUp", function(self, event, value)
Zerotorescue@10 167 CACHE_ITEMS_PER_UPDATE = ceil( value * 100 / 5 ); -- max = 20, min = 1
Zerotorescue@10 168 end);
Zerotorescue@10 169 btnRefresh:SetValue( CACHE_ITEMS_PER_UPDATE * 5 / 100 );
Zerotorescue@10 170
Zerotorescue@10 171 mod.scrollFrame:AddChild(btnRefresh);
Zerotorescue@10 172
Zerotorescue@10 173 times.init = ceil( ( GetTime() - buildStartTime ) * 1000 );
Zerotorescue@10 174 addon:Debug("Time spent legend: (init / sorting / preparing / building / all).");
Zerotorescue@10 175
Zerotorescue@1 176 local playerName = UnitName("player");
Zerotorescue@1 177
Zerotorescue@1 178 -- Go through all our stored groups
Zerotorescue@10 179 for groupName, values in pairsByKeys(addon.db.global.groups, function(a, b) return a:lower() < b:lower(); end) do
Zerotorescue@10 180 local groupStartTime, groupTimes = GetTime(), {};
Zerotorescue@10 181
Zerotorescue@1 182 local trackAt = (values.trackAtCharacters or (values.trackAtCharacters == nil and addon.db.global.defaults.trackAtCharacters));
Zerotorescue@1 183
Zerotorescue@1 184 -- Does this group have any items and do we want to track it at this char?
Zerotorescue@1 185 if values.items and trackAt[playerName] then
Zerotorescue@10 186
Zerotorescue@10 187
Zerotorescue@0 188 -- Get group settings
Zerotorescue@9 189 local minimumStock = (values.minimumStock or (values.minimumStock == nil and addon.db.global.defaults.minimumStock));
Zerotorescue@1 190 local showWhenBelow = (values.summaryThresholdShow or (values.summaryThresholdShow == nil and addon.db.global.defaults.summaryThresholdShow));
Zerotorescue@1 191 local priceThreshold = (values.priceThreshold or (values.priceThreshold == nil and addon.db.global.defaults.priceThreshold));
Zerotorescue@1 192 local hideWhenBelowPriceThreshold = (values.hideFromSummaryWhenBelowPriceThreshold or (values.hideFromSummaryWhenBelowPriceThreshold == nil and addon.db.global.defaults.hideFromSummaryWhenBelowPriceThreshold));
Zerotorescue@0 193
Zerotorescue@10 194
Zerotorescue@0 195 -- Make group container
Zerotorescue@0 196 local iGroup = AceGUI:Create("InlineGroupWithButton");
Zerotorescue@1 197 iGroup:PauseLayout();
Zerotorescue@0 198 iGroup:SetTitle(groupName);
Zerotorescue@0 199 iGroup:SetFullWidth(true);
Zerotorescue@0 200 iGroup:SetLayout("Flow");
Zerotorescue@0 201 iGroup:MakeButton({
Zerotorescue@0 202 name = "Queue",
Zerotorescue@0 203 desc = "Queue all items in this group.",
Zerotorescue@0 204 exec = function()
Zerotorescue@0 205 print(groupName);
Zerotorescue@0 206 end,
Zerotorescue@0 207 });
Zerotorescue@0 208
Zerotorescue@10 209
Zerotorescue@10 210
Zerotorescue@0 211 -- Headers
Zerotorescue@1 212
Zerotorescue@0 213 -- Itemlink
Zerotorescue@0 214 local lblItem = AceGUI:Create("InteractiveLabel");
Zerotorescue@0 215 lblItem:SetText("|cfffed000Item|r");
Zerotorescue@0 216 lblItem:SetFontObject(GameFontHighlight);
Zerotorescue@10 217 lblItem:SetRelativeWidth(.7);
Zerotorescue@0 218 lblItem:SetCallback("OnClick", function() ReSort("item"); end);
Zerotorescue@0 219
Zerotorescue@0 220 iGroup:AddChild(lblItem);
Zerotorescue@0 221
Zerotorescue@0 222 -- Current quantity
Zerotorescue@0 223 local lblQuantity = AceGUI:Create("InteractiveLabel");
Zerotorescue@0 224 lblQuantity:SetText("|cfffed000Cur.|r");
Zerotorescue@0 225 lblQuantity:SetFontObject(GameFontHighlight);
Zerotorescue@10 226 lblQuantity:SetRelativeWidth(.099);
Zerotorescue@0 227 lblQuantity:SetCallback("OnClick", function() ReSort("current"); end);
Zerotorescue@0 228
Zerotorescue@0 229 iGroup:AddChild(lblQuantity);
Zerotorescue@0 230
Zerotorescue@0 231 -- Required stock
Zerotorescue@9 232 local lblMinimumStock = AceGUI:Create("InteractiveLabel");
Zerotorescue@9 233 lblMinimumStock:SetText("|cfffed000Req.|r");
Zerotorescue@9 234 lblMinimumStock:SetFontObject(GameFontHighlight);
Zerotorescue@10 235 lblMinimumStock:SetRelativeWidth(.099);
Zerotorescue@9 236 lblMinimumStock:SetCallback("OnClick", function() ReSort("percentage"); end);
Zerotorescue@0 237
Zerotorescue@9 238 iGroup:AddChild(lblMinimumStock);
Zerotorescue@0 239
Zerotorescue@1 240 -- Lowest value
Zerotorescue@1 241 local lblValue = AceGUI:Create("InteractiveLabel");
Zerotorescue@1 242 lblValue:SetText("|cfffed000Value|r");
Zerotorescue@1 243 lblValue:SetFontObject(GameFontHighlight);
Zerotorescue@10 244 lblValue:SetRelativeWidth(.099);
Zerotorescue@1 245 lblValue:SetCallback("OnClick", function() ReSort("value"); end);
Zerotorescue@1 246
Zerotorescue@1 247 iGroup:AddChild(lblValue);
Zerotorescue@1 248
Zerotorescue@10 249
Zerotorescue@10 250 -- Retrieve items list
Zerotorescue@1 251 if not itemsCache[groupName] then
Zerotorescue@1 252 itemsCache[groupName] = {};
Zerotorescue@0 253
Zerotorescue@12 254 local unknownItemName = "Unknown (#%d)";
Zerotorescue@12 255
Zerotorescue@1 256 -- Sort item list
Zerotorescue@12 257 for itemId, _ in pairs(values.items) do
Zerotorescue@1 258 local itemName, itemLink, itemRarity = GetItemInfo(itemId);
Zerotorescue@1 259
Zerotorescue@12 260 if not itemRarity then
Zerotorescue@12 261 print(itemId .. ": " .. itemName .. " - " .. itemLink);
Zerotorescue@12 262 end
Zerotorescue@12 263
Zerotorescue@1 264 table.insert(itemsCache[groupName], {
Zerotorescue@1 265 id = itemId,
Zerotorescue@12 266 name = itemName or unknownItemName:format(itemId),
Zerotorescue@12 267 link = itemLink or unknownItemName:format(itemId),
Zerotorescue@10 268 value = ((priceThreshold == 0) and 0) or -3,-- if no price threshold is set for this item, then don't look it up either --addon:GetAuctionValue(itemLink),
Zerotorescue@12 269 rarity = itemRarity or 1,
Zerotorescue@9 270 count = -3,--addon:GetItemCount(itemId),
Zerotorescue@1 271 set = {},
Zerotorescue@1 272 });
Zerotorescue@1 273 CACHE_ITEMS_TOTAL = CACHE_ITEMS_TOTAL + 1;
Zerotorescue@1 274 end
Zerotorescue@0 275 end
Zerotorescue@0 276
Zerotorescue@10 277 groupTimes.init = ceil( ( GetTime() - groupStartTime ) * 1000 );
Zerotorescue@10 278
Zerotorescue@10 279
Zerotorescue@10 280
Zerotorescue@10 281 -- Sort items
Zerotorescue@1 282 table.sort(itemsCache[groupName], function(a, b)
Zerotorescue@1 283 if sortMethod == "item" and a.rarity == b.rarity then
Zerotorescue@1 284 -- Do a name-compare for items of the same rarity
Zerotorescue@1 285 -- Otherwise epics first, then junk
Zerotorescue@0 286 if sortDirectory == "ASC" then
Zerotorescue@1 287 return a.name:upper() < b.name:upper();
Zerotorescue@0 288 else
Zerotorescue@1 289 return a.name:upper() > b.name:upper();
Zerotorescue@1 290 end
Zerotorescue@1 291 elseif sortMethod == "item" then
Zerotorescue@1 292 if sortDirectory == "ASC" then
Zerotorescue@1 293 return a.rarity > b.rarity; -- the comparers were reversed because we want epics first
Zerotorescue@1 294 else
Zerotorescue@1 295 return a.rarity < b.rarity; -- the comparers were reversed because we want epics first
Zerotorescue@0 296 end
Zerotorescue@0 297 elseif sortMethod == "current" then
Zerotorescue@0 298 if sortDirectory == "ASC" then
Zerotorescue@0 299 return a.count < b.count;
Zerotorescue@0 300 else
Zerotorescue@0 301 return a.count > b.count;
Zerotorescue@0 302 end
Zerotorescue@0 303 elseif sortMethod == "percentage" then
Zerotorescue@0 304 if sortDirectory == "ASC" then
Zerotorescue@10 305 return ( a.count / minimumStock ) < ( b.count / minimumStock );
Zerotorescue@0 306 else
Zerotorescue@10 307 return ( a.count / minimumStock ) > ( b.count / minimumStock );
Zerotorescue@0 308 end
Zerotorescue@1 309 elseif sortMethod == "value" then
Zerotorescue@1 310 if sortDirectory == "ASC" then
Zerotorescue@1 311 return a.value < b.value;
Zerotorescue@1 312 else
Zerotorescue@1 313 return a.value > b.value;
Zerotorescue@1 314 end
Zerotorescue@0 315 end
Zerotorescue@0 316 end);
Zerotorescue@0 317
Zerotorescue@10 318 groupTimes.sorting = ceil( ( GetTime() - groupStartTime ) * 1000 );
Zerotorescue@10 319
Zerotorescue@10 320
Zerotorescue@10 321
Zerotorescue@10 322
Zerotorescue@10 323 -- Show itemslist
Zerotorescue@1 324 for i, item in pairs(itemsCache[groupName]) do
Zerotorescue@9 325 if ( item.count / minimumStock ) < showWhenBelow and not (hideWhenBelowPriceThreshold and item.value < priceThreshold) then
Zerotorescue@0 326 local btnItemLink = AceGUI:Create("ItemLinkButton");
Zerotorescue@10 327 --btnItemLink:SetUserData("exec", function()
Zerotorescue@10 328 -- print("Nothing happening yet.");
Zerotorescue@10 329 --end);
Zerotorescue@10 330 btnItemLink:SetRelativeWidth(.7);
Zerotorescue@1 331 btnItemLink:SetItemId(item.id);
Zerotorescue@0 332
Zerotorescue@0 333 iGroup:AddChild(btnItemLink);
Zerotorescue@0 334
Zerotorescue@0 335 -- Current quantity
Zerotorescue@0 336 local lblQuantity = AceGUI:Create("Label");
Zerotorescue@9 337 lblQuantity:SetText(self:DisplayItemCount(item.count, minimumStock));
Zerotorescue@10 338 lblQuantity:SetRelativeWidth(.099);
Zerotorescue@0 339
Zerotorescue@0 340 iGroup:AddChild(lblQuantity);
Zerotorescue@0 341
Zerotorescue@0 342 -- Required stock
Zerotorescue@9 343 local lblMinimumStock = AceGUI:Create("Label");
Zerotorescue@9 344 lblMinimumStock:SetText(minimumStock);
Zerotorescue@10 345 lblMinimumStock:SetRelativeWidth(.099);
Zerotorescue@0 346
Zerotorescue@9 347 iGroup:AddChild(lblMinimumStock);
Zerotorescue@1 348
Zerotorescue@1 349 -- Value
Zerotorescue@1 350 local lblValue = AceGUI:Create("Label");
Zerotorescue@1 351 lblValue:SetText(self:DisplayMoney(item.value, priceThreshold));
Zerotorescue@10 352 lblValue:SetRelativeWidth(.099);
Zerotorescue@1 353
Zerotorescue@1 354 iGroup:AddChild(lblValue);
Zerotorescue@1 355
Zerotorescue@1 356 -- Remember references to the value and current fields so we can fill them later
Zerotorescue@1 357 if item.set then
Zerotorescue@10 358 -- -3 means the price is unknown, queue look up
Zerotorescue@10 359 if item.value == -3 then
Zerotorescue@10 360 item.set.value = lblValue;
Zerotorescue@10 361 end
Zerotorescue@10 362 if item.count == -3 then
Zerotorescue@10 363 item.set.current = lblQuantity;
Zerotorescue@10 364 end
Zerotorescue@10 365
Zerotorescue@10 366 -- Don't queue if we already know everything we want to know
Zerotorescue@10 367 if item.value ~= -3 and item.count ~= -3 then
Zerotorescue@10 368 item.set = nil;
Zerotorescue@10 369 end
Zerotorescue@1 370 end
Zerotorescue@0 371 end
Zerotorescue@0 372 end
Zerotorescue@0 373
Zerotorescue@10 374 groupTimes.preparing = ceil( ( GetTime() - groupStartTime ) * 1000 );
Zerotorescue@10 375
Zerotorescue@1 376 iGroup:ResumeLayout();
Zerotorescue@1 377 mod.scrollFrame:AddChild(iGroup); -- this can take up to .5 seconds, might need to look into again at a later time
Zerotorescue@10 378
Zerotorescue@10 379 groupTimes.building = ceil( ( GetTime() - groupStartTime ) * 1000 );
Zerotorescue@0 380 end
Zerotorescue@10 381
Zerotorescue@10 382 addon:Debug(("Building of %s took %d ms (%d / %d / %d / %d / %d)."):format(groupName, ceil( ( GetTime() - groupStartTime ) * 1000 ), groupTimes.init, groupTimes.sorting, groupTimes.preparing, groupTimes.building, ceil( ( GetTime() - buildStartTime ) * 1000 )));
Zerotorescue@0 383 end
Zerotorescue@1 384
Zerotorescue@1 385 mod.scrollFrame:ResumeLayout();
Zerotorescue@1 386 mod.scrollFrame:DoLayout();
Zerotorescue@1 387
Zerotorescue@10 388 addon:Debug(("Done building summary after %d ms."):format(ceil( ( GetTime() - buildStartTime ) * 1000 )));
Zerotorescue@10 389
Zerotorescue@1 390 if CACHE_ITEMS_TOTAL > 0 then
Zerotorescue@1 391 cacheStart = GetTime();
Zerotorescue@7 392 self:CancelTimer(self.tmrUpdater, true);
Zerotorescue@1 393 self.tmrUpdater = self:ScheduleRepeatingTimer("UpdateNextItem", .01); -- Once every 100 frames (or once every x frames if you have less than 100 FPS, basically, once every frame)
Zerotorescue@1 394 end
Zerotorescue@1 395 end
Zerotorescue@1 396
Zerotorescue@1 397 function mod:UpdateNextItem()
Zerotorescue@1 398 local i = 0;
Zerotorescue@1 399
Zerotorescue@1 400 for groupName, items in pairs(itemsCache) do
Zerotorescue@1 401 local minimumStock = addon:GetOptionByKey(groupName, "minimumStock");
Zerotorescue@1 402 local priceThreshold = addon:GetOptionByKey(groupName, "priceThreshold");
Zerotorescue@1 403
Zerotorescue@1 404 for _, item in pairs(items) do
Zerotorescue@1 405 if item.set then
Zerotorescue@10 406 if item.count == -3 then
Zerotorescue@10 407 -- Only if item count was queued, update it
Zerotorescue@10 408 item.count = addon:GetItemCount(item.id);
Zerotorescue@10 409 if item.set.current and item.set.current.SetText then
Zerotorescue@10 410 item.set.current:SetText(self:DisplayItemCount(item.count, minimumStock));
Zerotorescue@10 411 end
Zerotorescue@1 412 end
Zerotorescue@1 413
Zerotorescue@10 414 if item.value == -3 then
Zerotorescue@10 415 -- Only if item value was queued, update it
Zerotorescue@10 416 item.value = addon:GetAuctionValue(item.link);
Zerotorescue@10 417 if item.set.value and item.set.value.SetText then
Zerotorescue@10 418 item.set.value:SetText(self:DisplayMoney(item.value, priceThreshold));
Zerotorescue@10 419 end
Zerotorescue@1 420 end
Zerotorescue@1 421
Zerotorescue@1 422 item.set = nil;
Zerotorescue@1 423
Zerotorescue@1 424 i = i + 1;
Zerotorescue@1 425 CACHE_ITEMS_CURRENT = CACHE_ITEMS_CURRENT + 1;
Zerotorescue@1 426
Zerotorescue@9 427 if mod.frame then
Zerotorescue@9 428 mod.frame:SetStatusText(("Caching auction values and item-counts... %d%% has already been processed."):format(floor(CACHE_ITEMS_CURRENT / CACHE_ITEMS_TOTAL * 100)));
Zerotorescue@9 429 end
Zerotorescue@1 430
Zerotorescue@1 431 if i >= CACHE_ITEMS_PER_UPDATE then
Zerotorescue@1 432 return;
Zerotorescue@1 433 end
Zerotorescue@1 434 end
Zerotorescue@1 435 end
Zerotorescue@1 436 end
Zerotorescue@1 437
Zerotorescue@1 438 -- Reset trackers
Zerotorescue@1 439 CACHE_ITEMS_TOTAL = 0;
Zerotorescue@1 440 CACHE_ITEMS_CURRENT = 0;
Zerotorescue@1 441
Zerotorescue@1 442 -- Stop timer
Zerotorescue@1 443 self:CancelTimer(self.tmrUpdater, true);
Zerotorescue@10 444
Zerotorescue@10 445 -- Rebuild list so hidden items due to too low prices get added
Zerotorescue@10 446 self:Build();
Zerotorescue@1 447
Zerotorescue@1 448 -- Announce
Zerotorescue@10 449 mod.frame:SetStatusText("All required prices and itemcounts have been cached. This process took " .. ceil(GetTime() - cacheStart) .. " seconds.");
Zerotorescue@1 450
Zerotorescue@1 451 -- Forget time
Zerotorescue@1 452 cacheStart = nil;
Zerotorescue@0 453 end
Zerotorescue@0 454
Zerotorescue@0 455 function mod:ColorCode(num, required)
Zerotorescue@0 456 local percentage = ( num / required );
Zerotorescue@0 457
Zerotorescue@0 458 if percentage >= addon.db.global.defaults.colors.green then
Zerotorescue@0 459 return ("|cff00ff00%d|r"):format(num);
Zerotorescue@0 460 elseif percentage >= addon.db.global.defaults.colors.yellow then
Zerotorescue@0 461 return ("|cffffff00%d|r"):format(num);
Zerotorescue@0 462 elseif percentage >= addon.db.global.defaults.colors.orange then
Zerotorescue@0 463 return ("|cffff9933%d|r"):format(num);
Zerotorescue@0 464 elseif percentage >= addon.db.global.defaults.colors.red then
Zerotorescue@0 465 return ("|cffff0000%d|r"):format(num);
Zerotorescue@0 466 end
Zerotorescue@0 467 end
Zerotorescue@0 468
Zerotorescue@1 469 function mod:DisplayMoney(value, priceThreshold)
Zerotorescue@7 470 if value == -1 then
Zerotorescue@7 471 return "|cff0000ffNone up|r";
Zerotorescue@7 472 elseif value == -2 then
Zerotorescue@7 473 return "|cff0000ffNo AH mod|r";
Zerotorescue@9 474 elseif value == -3 then
Zerotorescue@9 475 return "|cffffff00Unknown|r";
Zerotorescue@7 476 elseif value < priceThreshold then
Zerotorescue@1 477 return ("|cffff0000%s|r"):format(addon:ReadableMoney(value or 0, true));
Zerotorescue@1 478 else
Zerotorescue@1 479 return addon:ReadableMoney(value or 0, true);
Zerotorescue@1 480 end
Zerotorescue@1 481 end
Zerotorescue@1 482
Zerotorescue@9 483 function mod:DisplayItemCount(value, minimumStock)
Zerotorescue@9 484 if value == -3 then
Zerotorescue@9 485 return "|cffffff00Unknown|r";
Zerotorescue@9 486 else
Zerotorescue@9 487 return self:ColorCode(value, minimumStock);
Zerotorescue@9 488 end
Zerotorescue@9 489 end
Zerotorescue@9 490
Zerotorescue@0 491 function mod:NumberFormat(num)
Zerotorescue@0 492 local formatted = string.gsub(num, "(%d)(%d%d%d)$", "%1,%2", 1);
Zerotorescue@0 493
Zerotorescue@0 494 while true do
Zerotorescue@0 495 formatted, matches = string.gsub(formatted, "(%d)(%d%d%d),", "%1,%2,", 1);
Zerotorescue@0 496
Zerotorescue@0 497 if matches == 0 then
Zerotorescue@0 498 break;
Zerotorescue@0 499 end
Zerotorescue@0 500 end
Zerotorescue@0 501
Zerotorescue@0 502 return formatted;
Zerotorescue@1 503 end
Zerotorescue@1 504
Zerotorescue@1 505 --[[
Zerotorescue@1 506 No longer used, we're now caching complete item data instead of just AH values.
Zerotorescue@1 507 local AuctionValueCache = {};
Zerotorescue@1 508 function mod:GetAuctionValue(link)
Zerotorescue@1 509 local itemId = addon:GetItemId(link);
Zerotorescue@1 510
Zerotorescue@1 511 if AuctionValueCache[itemId] then
Zerotorescue@1 512 return AuctionValueCache[itemId];
Zerotorescue@1 513 else
Zerotorescue@1 514 local value = addon:GetAuctionValue(link);
Zerotorescue@1 515
Zerotorescue@1 516 AuctionValueCache[itemId] = value;
Zerotorescue@1 517
Zerotorescue@1 518 -- Reset the cache 1 minute after last updating it
Zerotorescue@1 519 self:CancelTimer(self.tmrResetCache, true);
Zerotorescue@1 520 self.tmrResetCache = self:ScheduleTimer(function()
Zerotorescue@1 521 table.wipe(AuctionValueCache);
Zerotorescue@1 522
Zerotorescue@1 523 mod.frame:SetStatusText("The auction item value cache has been reset.");
Zerotorescue@1 524 end, 60);
Zerotorescue@1 525 mod.frame:SetStatusText("");
Zerotorescue@1 526
Zerotorescue@1 527 return value;
Zerotorescue@1 528 end
Zerotorescue@1 529 end
Zerotorescue@1 530 ]]