comparison Modules/Summary.lua @ 122:6724bc8eface

Reduced usage of global functions by defining them locally.
author Zerotorescue
date Sat, 15 Jan 2011 18:52:01 +0100
parents d3fbb5676a5e
children 396c2960d54d
comparison
equal deleted inserted replaced
121:ca6280dc2f5b 122:6724bc8eface
1 local addon = select(2, ...); -- Get a reference to the main addon object 1 local addon = select(2, ...); -- Get a reference to the main addon object
2 local mod = addon:NewModule("Summary", "AceEvent-3.0", "AceTimer-3.0"); -- register a new module, Summary: resposible for building the summary window 2 local mod = addon:NewModule("Summary", "AceEvent-3.0", "AceTimer-3.0"); -- register a new module, Summary: resposible for building the summary window
3 3
4 local _G = _G; -- prevent looking up of the global table
5 local sformat, sgsub, supper, mceil, mfloor, tinsert, twipe, tsort = _G.string.format, _G.string.gsub, _G.string.upper, _G.math.ceil, _G.math.floor, _G.table.insert, _G.table.wipe, _G.table.sort;
6 local pairs, type, select = _G.pairs, _G.type, _G.select;
7
4 local unknownItemName = "Unknown (#%d)"; 8 local unknownItemName = "Unknown (#%d)";
5 9
6 local CACHE_ITEMS_TOTAL, CACHE_ITEMS_CURRENT, itemsCache = 0, 0, {}; 10 local CACHE_ITEMS_TOTAL, CACHE_ITEMS_CURRENT, itemsCache = 0, 0, {};
7 local AceGUI, cacheStart; 11 local AceGUI, cacheStart;
8
9 local _G = _G; -- prevent looking up of the global table
10 local sformat, sgsub, supper, tinsert, pairs, ceil, GetItemInfo = _G.string.format, _G.string.gsub, _G.string.upper, _G.table.insert, _G.pairs, _G.ceil, _G.GetItemInfo; -- prevent looking up of the most used globals all the time
11 12
12 function mod:OnEnable() 13 function mod:OnEnable()
13 -- Register the summary specific widget 14 -- Register the summary specific widget
14 addon:GetModule("Widgets"):InlineGroupWithButton(); 15 addon:GetModule("Widgets"):InlineGroupWithButton();
15 16
83 mod.scrollFrame:SetLayout("Flow"); 84 mod.scrollFrame:SetLayout("Flow");
84 85
85 mod.frame:AddChild(mod.scrollFrame); 86 mod.frame:AddChild(mod.scrollFrame);
86 87
87 -- Reset items cache 88 -- Reset items cache
88 table.wipe(itemsCache); 89 twipe(itemsCache);
89 end 90 end
90 91
91 function mod:CloseFrame() 92 function mod:CloseFrame()
92 if mod.frame then 93 if mod.frame then
93 mod.frame:Release(); 94 mod.frame:Release();
119 120
120 -- From http://www.wowwiki.com/API_sort 121 -- From http://www.wowwiki.com/API_sort
121 local function pairsByKeys (t, f) 122 local function pairsByKeys (t, f)
122 local a = {} 123 local a = {}
123 for n in pairs(t) do tinsert(a, n) end 124 for n in pairs(t) do tinsert(a, n) end
124 table.sort(a, f) 125 tsort(a, f)
125 local i = 0 -- iterator variable 126 local i = 0 -- iterator variable
126 local iter = function () -- iterator function 127 local iter = function () -- iterator function
127 i = i + 1 128 i = i + 1
128 if a[i] == nil then return nil 129 if a[i] == nil then return nil
129 else return a[i], t[a[i]] 130 else return a[i], t[a[i]]
145 local btnRefresh = AceGUI:Create("Button"); 146 local btnRefresh = AceGUI:Create("Button");
146 btnRefresh:SetText("Refresh"); 147 btnRefresh:SetText("Refresh");
147 btnRefresh:SetRelativeWidth(.2); 148 btnRefresh:SetRelativeWidth(.2);
148 btnRefresh:SetCallback("OnClick", function() 149 btnRefresh:SetCallback("OnClick", function()
149 -- Reset items cache 150 -- Reset items cache
150 table.wipe(itemsCache); 151 twipe(itemsCache);
151 152
152 -- Rebuild itemlist and start caching 153 -- Rebuild itemlist and start caching
153 mod:Build(); 154 mod:Build();
154 end); 155 end);
155 btnRefresh:SetCallback("OnEnter", ShowTooltip); 156 btnRefresh:SetCallback("OnEnter", ShowTooltip);
169 sdrSpeed:SetLabel("Processing speed"); 170 sdrSpeed:SetLabel("Processing speed");
170 sdrSpeed:SetSliderValues(0.01, 5, 0.01); -- min, max, interval 171 sdrSpeed:SetSliderValues(0.01, 5, 0.01); -- min, max, interval
171 sdrSpeed:SetIsPercent(true); 172 sdrSpeed:SetIsPercent(true);
172 sdrSpeed:SetRelativeWidth(.3); 173 sdrSpeed:SetRelativeWidth(.3);
173 sdrSpeed:SetCallback("OnMouseUp", function(self, event, value) 174 sdrSpeed:SetCallback("OnMouseUp", function(self, event, value)
174 addon.db.profile.defaults.summary.speed = ceil( ( ( value * 100 ) / 5) - .5 ); 175 addon.db.profile.defaults.summary.speed = mceil( ( ( value * 100 ) / 5) - .5 );
175
176 CACHE_ITEMS_PER_UPDATE = addon.db.profile.defaults.summary.speed; -- max = 20, min = 1
177 end); 176 end);
178 sdrSpeed:SetValue( addon.db.profile.defaults.summary.speed * 5 / 100 ); 177 sdrSpeed:SetValue( addon.db.profile.defaults.summary.speed * 5 / 100 );
179 sdrSpeed:SetCallback("OnEnter", ShowTooltip); 178 sdrSpeed:SetCallback("OnEnter", ShowTooltip);
180 sdrSpeed:SetCallback("OnLeave", HideTooltip); 179 sdrSpeed:SetCallback("OnLeave", HideTooltip);
181 sdrSpeed.frame.tooltipTitle = "Caching Processing Speed"; 180 sdrSpeed.frame.tooltipTitle = "Caching Processing Speed";
220 btnQueueAll.frame.tooltipTitle = "Queue all"; 219 btnQueueAll.frame.tooltipTitle = "Queue all";
221 btnQueueAll.frame.tooltip = "Queue everything that requires restocking within every single visible group."; 220 btnQueueAll.frame.tooltip = "Queue everything that requires restocking within every single visible group.";
222 221
223 mod.scrollFrame:AddChild(btnQueueAll); 222 mod.scrollFrame:AddChild(btnQueueAll);
224 223
225 times.init = ceil( ( GetTime() - buildStartTime ) * 1000 ); 224 times.init = mceil( ( GetTime() - buildStartTime ) * 1000 );
226 addon:Debug("Time spent legend: (init / sorting / preparing / building / all)."); 225 addon:Debug("Time spent legend: (init / sorting / preparing / building / all).");
227 226
228 local playerName = UnitName("player"); 227 local playerName = UnitName("player");
229 228
230 -- Go through all our stored groups 229 -- Go through all our stored groups
337 336
338 CACHE_ITEMS_TOTAL = CACHE_ITEMS_TOTAL + 1; 337 CACHE_ITEMS_TOTAL = CACHE_ITEMS_TOTAL + 1;
339 end 338 end
340 end 339 end
341 340
342 groupTimes.init = ceil( ( GetTime() - groupStartTime ) * 1000 ); 341 groupTimes.init = mceil( ( GetTime() - groupStartTime ) * 1000 );
343 342
344 343
345 344
346 -- Sort items 345 -- Sort items
347 table.sort(itemsCache[groupName], function(a, b) 346 tsort(itemsCache[groupName], function(a, b)
348 local aRarity = a.rarity or 1; 347 local aRarity = a.rarity or 1;
349 local bRarity = b.rarity or 1; 348 local bRarity = b.rarity or 1;
350 349
351 if sortMethod == "item" and aRarity == bRarity then 350 if sortMethod == "item" and aRarity == bRarity then
352 -- Do a name-compare for items of the same rarity 351 -- Do a name-compare for items of the same rarity
385 return a.value > b.value; 384 return a.value > b.value;
386 end 385 end
387 end 386 end
388 end); 387 end);
389 388
390 groupTimes.sorting = ceil( ( GetTime() - groupStartTime ) * 1000 ); 389 groupTimes.sorting = mceil( ( GetTime() - groupStartTime ) * 1000 );
391 390
392 391
393 392
394 393
395 -- Show itemslist 394 -- Show itemslist
446 item.set.globalCount = lblQuantity; 445 item.set.globalCount = lblQuantity;
447 item.set.localCount = lblLocal; 446 item.set.localCount = lblLocal;
448 end 447 end
449 end 448 end
450 449
451 groupTimes.preparing = ceil( ( GetTime() - groupStartTime ) * 1000 ); 450 groupTimes.preparing = mceil( ( GetTime() - groupStartTime ) * 1000 );
452 451
453 iGroup:ResumeLayout(); 452 iGroup:ResumeLayout();
454 mod.scrollFrame:AddChild(iGroup); -- this can take up to .5 seconds, might need to look into again at a later time 453 mod.scrollFrame:AddChild(iGroup); -- this can take up to .5 seconds, might need to look into again at a later time
455 454
456 groupTimes.building = ceil( ( GetTime() - groupStartTime ) * 1000 ); 455 groupTimes.building = mceil( ( GetTime() - groupStartTime ) * 1000 );
457 end 456 end
458 457
459 if groupStartTime and groupTimes then 458 if groupStartTime and groupTimes then
460 addon:Debug("Building of %s took %d ms (%d / %d / %d / %d / %d).", groupName, ceil( ( GetTime() - groupStartTime ) * 1000 ), groupTimes.init or 0, groupTimes.sorting or 0, groupTimes.preparing or 0, groupTimes.building or 0, ceil( ( GetTime() - buildStartTime ) * 1000 )); 459 addon:Debug("Building of %s took %d ms (%d / %d / %d / %d / %d).", groupName, mceil( ( GetTime() - groupStartTime ) * 1000 ), groupTimes.init or 0, groupTimes.sorting or 0, groupTimes.preparing or 0, groupTimes.building or 0, mceil( ( GetTime() - buildStartTime ) * 1000 ));
461 end 460 end
462 end 461 end
463 462
464 mod.scrollFrame:ResumeLayout(); 463 mod.scrollFrame:ResumeLayout();
465 mod.scrollFrame:DoLayout(); 464 mod.scrollFrame:DoLayout();
466 465
467 addon:Debug("Done building summary after %d ms.", ceil( ( GetTime() - buildStartTime ) * 1000 )); 466 addon:Debug("Done building summary after %d ms.", mceil( ( GetTime() - buildStartTime ) * 1000 ));
468 467
469 if CACHE_ITEMS_TOTAL > 0 then 468 if CACHE_ITEMS_TOTAL > 0 then
470 cacheStart = GetTime(); 469 cacheStart = GetTime();
471 self:CancelTimer(self.tmrUpdater, true); 470 self:CancelTimer(self.tmrUpdater, true);
472 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) 471 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)
515 514
516 i = i + 1; 515 i = i + 1;
517 CACHE_ITEMS_CURRENT = CACHE_ITEMS_CURRENT + 1; 516 CACHE_ITEMS_CURRENT = CACHE_ITEMS_CURRENT + 1;
518 517
519 if mod.frame then 518 if mod.frame then
520 mod.frame:SetStatusText(sformat("Caching auction values and item-counts... %d%% has already been processed.", floor(CACHE_ITEMS_CURRENT / CACHE_ITEMS_TOTAL * 100))); 519 mod.frame:SetStatusText(sformat("Caching auction values and item-counts... %d%% has already been processed.", mfloor(CACHE_ITEMS_CURRENT / CACHE_ITEMS_TOTAL * 100)));
521 end 520 end
522 521
523 if i >= addon.db.profile.defaults.summary.speed then 522 if i >= addon.db.profile.defaults.summary.speed then
524 return; 523 return;
525 end 524 end
536 535
537 -- Rebuild list so hidden items due to too low prices get added 536 -- Rebuild list so hidden items due to too low prices get added
538 self:Build(); 537 self:Build();
539 538
540 -- Announce 539 -- Announce
541 mod.frame:SetStatusText("All required prices and itemcounts have been cached. This process took " .. ceil(GetTime() - cacheStart) .. " seconds."); 540 mod.frame:SetStatusText("All required prices and itemcounts have been cached. This process took " .. mceil(GetTime() - cacheStart) .. " seconds.");
542 541
543 -- Forget time 542 -- Forget time
544 cacheStart = nil; 543 cacheStart = nil;
545 end 544 end
546 545
562 end 561 end
563 end 562 end
564 563
565 function mod:NumberFormat(num) 564 function mod:NumberFormat(num)
566 local formatted = sgsub(num, "(%d)(%d%d%d)$", "%1,%2", 1); 565 local formatted = sgsub(num, "(%d)(%d%d%d)$", "%1,%2", 1);
566 local matches;
567 567
568 while true do 568 while true do
569 formatted, matches = sgsub(formatted, "(%d)(%d%d%d),", "%1,%2,", 1); 569 formatted, matches = sgsub(formatted, "(%d)(%d%d%d),", "%1,%2,", 1);
570 570
571 if matches == 0 then 571 if matches == 0 then