Advertisement
Guest User

Artifact.lua [7.2]

a guest
Mar 30th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.68 KB | None | 0 0
  1. ------------------------------------------------------------
  2. -- Experiencer by Sonaza
  3. -- All rights reserved
  4. -- http://sonaza.com
  5. ------------------------------------------------------------
  6.  
  7. local ADDON_NAME, Addon = ...;
  8.  
  9. local module = Addon:RegisterModule("artifact", {
  10.     label       = "Artifact",
  11.     order       = 3,
  12.     savedvars   = {
  13.         global = {
  14.             ShowRemaining = true,
  15.             ShowUnspentPoints = true,
  16.             ShowTotalArtifactPower = false,
  17.             UnspentInChatMessage = false,
  18.             ShowBagArtifactPower = true,
  19.             VisualizeBagArtifactPower = true,
  20.         },
  21.     },
  22. });
  23.  
  24. module.levelUpRequiresAction = true;
  25.  
  26. function module:Initialize()
  27.     self:RegisterEvent("ARTIFACT_XP_UPDATE");
  28.     self:RegisterEvent("UNIT_INVENTORY_CHANGED");
  29. end
  30.  
  31. function module:IsDisabled()
  32.     -- If player doesn't have Legion on their account or hasn't completed first quest of artifact chain
  33.     return GetExpansionLevel() < 6 or not module:HasCompletedArtifactIntro();
  34. end
  35.  
  36. function module:Update(elapsed)
  37.    
  38. end
  39.  
  40. function module:CanLevelUp()
  41.     if(not HasArtifactEquipped()) then return false end
  42.    
  43.     local _, _, _, _, totalXP, pointsSpent, _, _, _, _, _, _, artifactTier = C_ArtifactUI.GetEquippedArtifactInfo();
  44.     local numPoints, xp, xpForNextPoint = MainMenuBar_GetNumArtifactTraitsPurchasableFromXP(pointsSpent, totalXP, artifactTier);
  45.     return numPoints > 0;
  46. end
  47.  
  48. function module:HasCompletedArtifactIntro()
  49.     local quests = {
  50.         40408, -- Paladin
  51.         40579, -- Warrior
  52.         40618, -- Hunter
  53.         40636, -- Monk
  54.         40646, -- Druid
  55.         40684, -- Warlock
  56.         40706, -- Priest
  57.         40715, -- Death Knight
  58.         40814, -- Demon Hunter
  59.         40840, -- Rogue
  60.         41085, -- Mage
  61.         41335, -- Shaman
  62.     };
  63.    
  64.     for _, questID in ipairs(quests) do
  65.         if(IsQuestFlaggedCompleted(questID)) then return true end
  66.     end
  67.    
  68.     return false;
  69. end
  70.  
  71. function module:CalculateTotalArtifactPower()
  72.     if(not HasArtifactEquipped()) then return 0 end
  73.    
  74.     local _, _, _, _, currentXP, pointsSpent = C_ArtifactUI.GetEquippedArtifactInfo();
  75.    
  76.     local totalXP = 0;
  77.    
  78.     for i=0, pointsSpent-1 do
  79.         local numPoints, artifactXP, xpForNextPoint = MainMenuBar_GetNumArtifactTraitsPurchasableFromXP(i, 0);
  80.         totalXP = totalXP + xpForNextPoint;
  81.     end
  82.    
  83.     return totalXP + currentXP;
  84. end
  85.  
  86. function module:GetText()
  87.     if(not HasArtifactEquipped()) then
  88.         return "No artifact equipped";
  89.     end
  90.    
  91.     local outputText = {};
  92.    
  93.     local itemID, altItemID, name, icon, totalXP, pointsSpent, _, _, _, _, _, _, artifactTier = C_ArtifactUI.GetEquippedArtifactInfo();
  94.     local numPoints, artifactXP, xpForNextPoint = MainMenuBar_GetNumArtifactTraitsPurchasableFromXP(pointsSpent, totalXP, artifactTier);
  95.    
  96.     local remaining         = xpForNextPoint - artifactXP;
  97.    
  98.     local progress          = artifactXP / (xpForNextPoint > 0 and xpForNextPoint or 1);
  99.     local progressColor     = Addon:GetProgressColor(progress);
  100.    
  101.     tinsert(outputText,
  102.         ("|cffffecB3%s|r (Rank %d):"):format(name, pointsSpent + numPoints)
  103.     );
  104.    
  105.     if(self.db.global.ShowRemaining) then
  106.         tinsert(outputText,
  107.             ("%s%s|r (%s%.1f|r%%)"):format(progressColor, BreakUpLargeNumbers(remaining), progressColor, 100 - progress * 100)
  108.         );
  109.     else
  110.         tinsert(outputText,
  111.             ("%s%s|r / %s (%s%.1f|r%%)"):format(progressColor, BreakUpLargeNumbers(artifactXP), BreakUpLargeNumbers(xpForNextPoint), progressColor, progress * 100)
  112.         );
  113.     end
  114.    
  115.     if(self.db.global.ShowTotalArtifactPower) then
  116.         tinsert(outputText,
  117.             ("%s |cffffdd00total artifact power|r"):format(BreakUpLargeNumbers(module:CalculateTotalArtifactPower()))
  118.         );
  119.     end
  120.    
  121.     if(self.db.global.ShowBagArtifactPower) then
  122.         local totalPower = module:FindPowerItemsInInventory();
  123.         if(totalPower and totalPower > 0) then
  124.             tinsert(outputText,
  125.                 ("%s |cffa8ff00artifact power in bags|r"):format(BreakUpLargeNumbers(totalPower))
  126.             );
  127.         end
  128.     end
  129.    
  130.     if(self.db.global.ShowUnspentPoints and numPoints > 0) then
  131.         tinsert(outputText,
  132.             ("|cff86ff33%d unspent point%s|r"):format(numPoints, numPoints == 1 and "" or "s")
  133.         );
  134.     end
  135.    
  136.     return table.concat(outputText, "  ");
  137. end
  138.  
  139. function module:HasChatMessage()
  140.     return HasArtifactEquipped(), "No artifact equipped.";
  141. end
  142.  
  143. function module:GetChatMessage()
  144.     local outputText = {};
  145.    
  146.     local itemID, altItemID, name, icon, totalXP, pointsSpent, _, _, _, _, _, _, artifactTier = C_ArtifactUI.GetEquippedArtifactInfo();
  147.     local numPoints, artifactXP, xpForNextPoint = MainMenuBar_GetNumArtifactTraitsPurchasableFromXP(pointsSpent, totalXP, artifactTier);
  148.    
  149.     local remaining = xpForNextPoint - artifactXP;
  150.     local progress  = artifactXP / (xpForNextPoint > 0 and xpForNextPoint or 1);
  151.    
  152.     tinsert(outputText, ("%s is currently rank %s"):format(
  153.         name,
  154.         pointsSpent + numPoints
  155.     ));
  156.    
  157.     if(pointsSpent > 0) then
  158.         tinsert(outputText, ("at %s/%s power (%.1f%%) with %d to go"):format(
  159.             BreakUpLargeNumbers(artifactXP),   
  160.             BreakUpLargeNumbers(xpForNextPoint),
  161.             progress * 100,
  162.             remaining
  163.         ));
  164.     end
  165.    
  166.     if(self.db.global.UnspentInChatMessage and numPoints > 0) then
  167.         tinsert(outputText,
  168.             (" (%d unspent point%s)"):format(numPoints, numPoints == 1 and "" or "s")
  169.         );
  170.     end
  171.    
  172.     return table.concat(outputText, " ");
  173. end
  174.  
  175. function module:GetBarData()
  176.     local data    = {};
  177.     data.level    = 0;
  178.     data.min      = 0;
  179.     data.max      = 1;
  180.     data.current  = 0;
  181.     data.rested   = nil;
  182.     data.visual   = nil;
  183.    
  184.     if(HasArtifactEquipped()) then
  185.         local itemID, altItemID, name, icon, totalXP, pointsSpent, _, _, _, _, _, _, artifactTier = C_ArtifactUI.GetEquippedArtifactInfo();
  186.         local numPoints, artifactXP, xpForNextPoint = MainMenuBar_GetNumArtifactTraitsPurchasableFromXP(pointsSpent, totalXP, artifactTier);
  187.        
  188.         data.level    = pointsSpent + numPoints or 0;
  189.         data.max      = xpForNextPoint;
  190.         data.current  = artifactXP;
  191.        
  192.         if(self.db.global.VisualizeBagArtifactPower) then
  193.             local totalPower = module:FindPowerItemsInInventory();
  194.             data.visual = totalPower;
  195.         end
  196.     end
  197.    
  198.     return data;
  199. end
  200.  
  201. function module:GetOptionsMenu()
  202.     local menudata = {
  203.         {
  204.             text = "Artifact Options",
  205.             isTitle = true,
  206.             notCheckable = true,
  207.         },
  208.         {
  209.             text = "Show remaining artifact power",
  210.             func = function() self.db.global.ShowRemaining = true; module:RefreshText(); end,
  211.             checked = function() return self.db.global.ShowRemaining == true; end,
  212.         },
  213.         {
  214.             text = "Show current and max artifact power",
  215.             func = function() self.db.global.ShowRemaining = false; module:RefreshText(); end,
  216.             checked = function() return self.db.global.ShowRemaining == false; end,
  217.         },
  218.         {
  219.             text = " ", isTitle = true, notCheckable = true,
  220.         },
  221.         {
  222.             text = "Show total artifact power",
  223.             func = function() self.db.global.ShowTotalArtifactPower = not self.db.global.ShowTotalArtifactPower; module:RefreshText(); end,
  224.             checked = function() return self.db.global.ShowTotalArtifactPower; end,
  225.             isNotRadio = true,
  226.         },
  227.         {
  228.             text = "Show unspent points",
  229.             func = function() self.db.global.ShowUnspentPoints = not self.db.global.ShowUnspentPoints; module:RefreshText(); end,
  230.             checked = function() return self.db.global.ShowUnspentPoints; end,
  231.             isNotRadio = true,
  232.         },
  233.         {
  234.             text = "Include unspent points in chat message",
  235.             func = function() self.db.global.UnspentInChatMessage = not self.db.global.UnspentInChatMessage; module:RefreshText(); end,
  236.             checked = function() return self.db.global.UnspentInChatMessage; end,
  237.             isNotRadio = true,
  238.         },
  239.         {
  240.             text = "Show unspent artifact power in bags",
  241.             func = function() self.db.global.ShowBagArtifactPower = not self.db.global.ShowBagArtifactPower; module:RefreshText(); end,
  242.             checked = function() return self.db.global.ShowBagArtifactPower; end,
  243.             isNotRadio = true,
  244.         },
  245.         {
  246.             text = "Visualize unspent artifact power in bags",
  247.             func = function() self.db.global.VisualizeBagArtifactPower = not self.db.global.VisualizeBagArtifactPower; module:RefreshText(); end,
  248.             checked = function() return self.db.global.VisualizeBagArtifactPower; end,
  249.             isNotRadio = true,
  250.         },
  251.     };
  252.    
  253.     return menudata;
  254. end
  255.  
  256. ------------------------------------------
  257.  
  258. function module:ARTIFACT_XP_UPDATE()
  259.     module:Refresh();
  260. end
  261.  
  262. function module:UNIT_INVENTORY_CHANGED(event, unit)
  263.     if(unit ~= "player") then return end
  264.     if(self:IsDisabled()) then
  265.         Addon:CheckDisabledStatus();
  266.     else
  267.         module:Refresh(true);
  268.     end
  269. end
  270.  
  271. local EMPOWERING_SPELL_ID = 227907;
  272.  
  273. local ExperiencerAPScannerTooltip = CreateFrame("GameTooltip", "ExperiencerAPScannerTooltip", nil, "GameTooltipTemplate");
  274. function module:FindPowerItemsInInventory()
  275.     local powers = {};
  276.     local totalPower = 0;
  277.    
  278.     local spellName = GetSpellInfo(EMPOWERING_SPELL_ID);
  279.    
  280.     for container = 0, NUM_BAG_SLOTS do
  281.         local numSlots = GetContainerNumSlots(container);
  282.        
  283.         for slot = 1, numSlots do
  284.             local link = GetContainerItemLink(container, slot);
  285.             if(link and GetItemSpell(link) == spellName) then
  286.                 ExperiencerAPScannerTooltip:SetOwner(UIParent, "ANCHOR_NONE");
  287.                 ExperiencerAPScannerTooltip:SetHyperlink(link);
  288.                
  289.                 local tooltipText = ExperiencerAPScannerTooltipTextLeft4:GetText();
  290.                 if(tooltipText) then
  291.                     local power = tonumber(tooltipText:gsub("[,%.]", ""):match("%d.-%s"));
  292.                     if(power) then
  293.                         totalPower = totalPower + power;
  294.                         tinsert(powers, {
  295.                             link = link,
  296.                             power = power,
  297.                         });
  298.                     end
  299.                 end
  300.             end
  301.         end
  302.     end
  303.    
  304.     return totalPower, powers;
  305. end
  306.  
  307. function module:GetItemArtifactPower(link)
  308.     if(not link) then return nil end
  309.    
  310.     ExperiencerAPScannerTooltip:SetOwner(UIParent, "ANCHOR_NONE");
  311.     ExperiencerAPScannerTooltip:SetHyperlink(link);
  312.    
  313.     local tooltipText = ExperiencerAPScannerTooltipTextLeft4:GetText();
  314.     if(not tooltipText) then return nil end
  315.    
  316.     local power = tooltipText:gsub("[,%.]", ""):match("%d.-%s");
  317.     if(power) then
  318.         return tonumber(power);
  319.     end
  320.    
  321.     return nil;
  322. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement