Quantcast
How could this be written neater? - WoWInterface
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
05-07-17, 09:41 AM   #1
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
How could this be written neater?

Hi all,

I've tried to make an aura tracker addon and..... it is working fine, but the code looks really disgusting.

Lua Code:
  1. -- Upvalue variables
  2. local _G = getfenv(0);
  3.  
  4. local GetSpecialization = _G.GetSpecialization;
  5. local GetSpellInfo = _G.GetSpellInfo;
  6. local ReloadUI = _G.ReloadUI;
  7.  
  8. -- Local variables
  9. local size = 40;
  10. local point = {"CENTER", UIParent, "CENTER", 0, -110};
  11. local gap = 2;
  12. local max = 6;
  13.  
  14. local buffIDList = {
  15.     ["WARLOCK"] = {
  16.         [3] = { -- Destruction
  17.             235156,
  18.             215165,
  19.             119899,
  20.         },
  21.     },
  22. };
  23.  
  24. -- Local functions
  25. local InitObjects;
  26.  
  27. -- AuraTrack
  28. local AuraTrack = CreateFrame("Frame");
  29. AuraTrack:RegisterEvent("ADDON_LOADED");
  30. AuraTrack:RegisterEvent("PLAYER_ENTERING_WORLD");
  31. AuraTrack:SetScript("OnEvent", function(self, event, ...)
  32.     self[event](self, ...);
  33. end);
  34.  
  35. function AuraTrack:ADDON_LOADED(...)
  36.     if ... == "MyAuraTracker" then
  37.         InitObjects();
  38.  
  39.         self:UnregisterEvent("ADDON_LOADED");
  40.     end
  41. end
  42.  
  43. function AuraTrack:PLAYER_ENTERING_WORLD()
  44.     local holder = self.holder;
  45.  
  46.     holder:RegisterUnitEvent("UNIT_AURA", "player");
  47.     holder:SetScript("OnEvent", function(self, event, ...)
  48.         AuraTrack[event](AuraTrack, ...);
  49.     end);
  50.  
  51.     self:UnregisterEvent("PLAYER_ENTERING_WORLD");
  52. end
  53.  
  54. function AuraTrack:UNIT_AURA(...)
  55.     local holder = self.holder;
  56.  
  57.     -- Get player's class and spec to access buffIDList table
  58.     local _, class = UnitClass(...);
  59.     local spec = GetSpecialization();
  60.  
  61.     -- For each button, check if they are already assigned with aura
  62.     for i = 1, #holder.button do
  63.         local button = holder.button[i];
  64.  
  65.         -- If the button is assigned with aura and that aura is on-going, update its duration and so on
  66.         -- Otherwise, remove id from button and hide
  67.         if button.spellID then
  68.             local spellName = GetSpellInfo(button.spellID);
  69.  
  70.             local _, _, icon, count, _, duration, expires = UnitAura(..., spellName);
  71.  
  72.             if duration and duration > 0 then
  73.                 button.cd:SetCooldown(expires - duration, duration);
  74.  
  75.                 button.icon:SetTexture(icon);
  76.  
  77.                 button.count:SetText((count and count > 0) and count or "");
  78.  
  79.                 button:Show();
  80.             else
  81.                 button.spellID = nil;
  82.  
  83.                 button:Hide();
  84.             end
  85.         end
  86.     end
  87.  
  88.     -- For each buff, check if they are activated
  89.     for i = 1, #buffIDList[class][spec] do
  90.         local spellName = GetSpellInfo(buffIDList[class][spec][i]);
  91.  
  92.         local _, _, icon, count, _, duration, expires = UnitAura(..., spellName);
  93.  
  94.         -- If the buff is activated, see if that buff is already assigned to a button
  95.         if duration and duration > 0 then
  96.             local assigned = false;
  97.  
  98.             for j = 1, #holder.button do
  99.                 if holder.button[j].spellID == buffIDList[class][spec][i] then
  100.                     assigned = true;
  101.                 end
  102.             end
  103.  
  104.             -- If buff is not assigned to any of buttons,
  105.             -- assign the id and update its duration and so on
  106.             if not assigned then
  107.                 for j = 1, #holder.button do
  108.                     local button = holder.button[j];
  109.  
  110.                     if not button.spellID then
  111.                         button.spellID = buffIDList[class][spec][i];
  112.  
  113.                         button.cd:SetCooldown(expires - duration, duration);
  114.  
  115.                         button.icon:SetTexture(icon);
  116.  
  117.                         button.count:SetText((count and count > 0) and count or "");
  118.  
  119.                         button:Show();
  120.                        
  121.                         break;
  122.                     end
  123.                 end
  124.             end
  125.         end
  126.     end
  127. end
  128.  
  129. function InitObjects()
  130.     local holder = CreateFrame("Frame", "AuraTrackerHolder", UIParent);
  131.     holder:SetPoint(unpack(point));
  132.     holder:SetSize((size + gap) * max - gap, size);
  133.  
  134.     holder.button = {};
  135.  
  136.     for i = 1, max do
  137.         local button = CreateFrame("Frame", "$parentButton" .. i, holder);
  138.         button:Hide();
  139.  
  140.         if i == 1 then
  141.             button:SetPoint("LEFT");
  142.         else
  143.             button:SetPoint("LEFT", holder.button[i - 1], "RIGHT", gap, 0);
  144.         end
  145.  
  146.         button:SetSize(size, size);
  147.  
  148.         local cd = CreateFrame("Cooldown", "$parentCooldown", button, "CooldownFrameTemplate");
  149.         cd:SetAllPoints(button);
  150.  
  151.         local icon = button:CreateTexture(nil, "BORDER");
  152.         icon:SetAllPoints(button);
  153.  
  154.         local count = button:CreateFontString(nil, "OVERLAY");
  155.         count:SetFontObject(NumberFontNormal);
  156.         count:SetPoint("TOP", button, "BOTTOM", 0, -2);
  157.  
  158.         holder.button[i] = button;
  159.  
  160.         button.cd = cd;
  161.         button.icon = icon;
  162.         button.count = count;
  163.     end
  164.  
  165.     AuraTrack.holder = holder;
  166. end

I am pretty happy with the rest of parts, but :UNIT_AURA() function is... meh.......

Even if I've left some comments there, pretty sure I would forget what I have done there with all those for loops.

Could I ask some helps?!

Thank you.

Last edited by Layback_ : 05-07-17 at 10:03 AM.
  Reply With Quote
 

WoWInterface » Developer Discussions » Lua/XML Help » How could this be written neater?

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off