Advertisement
KiLLerBoy_001

RareShare RareShare.lua

Aug 17th, 2019
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.17 KB | None | 0 0
  1. -- Rare Share Core module v0.6.1
  2.  
  3. local AddonName, Addon = ...
  4.  
  5.  Addon.SoundID  = 12867
  6.  Addon.Cooldown = 210
  7.  Addon.PreCooldown = 15
  8.  
  9. function Addon.InitConfig()
  10.     if RareShareDB                                   == nil then RareShareDB                                    = {}            end
  11.     if RareShareDB["Config"]                         == nil then RareShareDB["Config"]                          = {}            end
  12.  
  13.     if RareShareDB["Config"]["ChatAnnounce"]         == nil then RareShareDB["Config"]["ChatAnnounce"]          = true          end
  14.  
  15.     if RareShareDB["Config"]["Sound"]                == nil then RareShareDB["Config"]["Sound"]                 = {}            end
  16.     if RareShareDB["Config"]["Sound"]["Master"]      == nil then RareShareDB["Config"]["Sound"]["Master"]       = true          end
  17.     if RareShareDB["Config"]["Sound"]["Rares"]       == nil then RareShareDB["Config"]["Sound"]["Rares"]        = true          end
  18.     if RareShareDB["Config"]["Sound"]["Duplicates"]  == nil then RareShareDB["Config"]["Sound"]["Duplicates"]   = true          end
  19.  
  20.     if RareShareDB["Config"]["TomTom"]               == nil then RareShareDB["Config"]["TomTom"]                = {}            end
  21.     if RareShareDB["Config"]["TomTom"]["Master"]     == nil then RareShareDB["Config"]["TomTom"]["Master"]      = true          end
  22.     if RareShareDB["Config"]["TomTom"]["Rares"]      == nil then RareShareDB["Config"]["TomTom"]["Rares"]       = true          end
  23.     if RareShareDB["Config"]["TomTom"]["Duplicates"] == nil then RareShareDB["Config"]["TomTom"]["Duplicates"]  = true          end
  24.  
  25.     if RareShareDB["Config"]["OnDeath"]              == nil then RareShareDB["Config"]["OnDeath"]               = false         end
  26.    
  27.     if RareShareDB["Config"]["CChannel"]             == nil then RareShareDB["Config"]["CChannel"]              = {}            end
  28.     if RareShareDB["Config"]["CChannel"]["CName"]    == nil then RareShareDB["Config"]["CChannel"]["CName"]     = tostring(nil) end
  29.     if RareShareDB["Config"]["CChannel"]["CID"]      == nil then RareShareDB["Config"]["CChannel"]["CID"]       = 0             end
  30.    
  31.     if RareShareDB["LastAnnounce"]                   == nil then RareShareDB["LastAnnounce"]                    = {}            end
  32.     if RareShareDB["LastAnnounce"]["Time"]           == nil then RareShareDB["LastAnnounce"]["Time"]            = 0             end      
  33.     if RareShareDB["LastAnnounce"]["ID"]             == nil then RareShareDB["LastAnnounce"]["ID"]              = 0             end
  34.  
  35.     if RareShareDB["Modules"]                        == nil then RareShareDB["Modules"]                         = {}            end
  36. end
  37.  
  38. function Addon:Log(MSG, Level, MapID)
  39.     local Module = self:GetModule(MapID)
  40.     local Colour = Module.Colour
  41.     local Title  = Module.Title
  42.     if (Level == 2) then
  43.         Level = "|cffff001e" --[[ Red ]]
  44.     elseif (Level == 3) then
  45.         Level = "|cffad36ff" --[[ Purple ]]
  46.     else
  47.         Level = "|cff1eff00" --[[ Green ]]
  48.     end
  49.     print(Colour..Title.." "..Addon.Loc.Title..": "..Level..MSG)
  50. end
  51.  
  52. function Addon:PlaySound()
  53.     PlaySound(self.SoundID)
  54. end
  55.    
  56. function Addon:CheckZone()
  57.     local CurrMapID, ParentMapID = self:GetMapID()
  58.     if self.Modules[CurrMapID] then
  59.         return CurrMapID
  60.     elseif self.Modules[ParentMapID] then
  61.         return ParentMapID
  62.     else
  63.         return nil
  64.     end
  65. end
  66.  
  67. function Addon:InitChat()
  68.     if (self:GetChannelID() == 0) then
  69.         C_Timer.After(1, function() self:InitChat() end)
  70.     else
  71.         RareShareDB["Config"]["CChannel"]["CID"]    = self:GetChannelID()
  72.         RareShareDB["Config"]["CChannel"]["CName"]  = self:GetChannelText()
  73.     end
  74. end
  75.  
  76. function Addon:GetMapID()
  77.     local   MapID       = C_Map.GetBestMapForUnit("player");
  78.     if      MapID       == nil then return nil end
  79.     local   mapInfo     = C_Map.GetMapInfo(MapID)
  80.     local   parentMapID = mapInfo["parentMapID"];
  81.     return  MapID, parentMapID
  82. end
  83.  
  84. function Addon:GetChannelID()
  85.     return GetChannelName(tostring(self:GetChannelText()))
  86. end
  87.  
  88. function Addon:GetChannelText()
  89.     local General = EnumerateServerChannels();
  90.     local Zone = GetZoneText();
  91.     if (General == nil or Zone == nil) then return nil end
  92.     local Conn = " - "; if (GetLocale() == "ruRU") then Conn = ": " end
  93.     return General..Conn..Zone
  94. end
  95.  
  96. function Addon:GetNPCID(GUID)
  97.     if GUID == nil then return GUID end
  98.     local UnitType, _, _, _, _, UnitID = strsplit("-", GUID);
  99.     if (UnitType == "Creature" or UnitType == "Vehicle") then
  100.         return tonumber(UnitID);
  101.     end
  102.     return nil;
  103. end
  104.  
  105. function Addon:GetHealthPercent(Curr, Max)
  106.     return self:Round((Curr / Max * 100), 2)
  107. end
  108.  
  109. function Addon:Round(number, decimals)
  110.     return (("%%.%df"):format(decimals)):format(number)
  111. end
  112.  
  113. function Addon:GetReadablePlayerPosition(MapID)
  114.     local pos = C_Map.GetPlayerMapPosition(MapID,"player");
  115.     return math.ceil(pos.x*10000)/100, math.ceil(pos.y*10000)/100
  116. end
  117.  
  118. -- function Addon:CheckVignettes()
  119. --     local Module = self:GetModule(self.LastMap)
  120. --     local Vignettes = C_VignetteInfo.GetVignettes()
  121. --     for i, v in pairs(Vignettes) do
  122. --         local VignetteInfo = C_VignetteInfo.GetVignetteInfo(v)
  123. --         local ID = self:GetNPCID(VignetteInfo.objectGUID)
  124. --         if (Module.Rares[ID] ~= nil) then
  125. --             if Module.Rares[ID][2] < time() - RareShare.Cooldown then
  126. --                 Module.Rares[ID][3] = true
  127. --                 local x,y = (C_VignetteInfo.GetVignettePosition(v, self.LastMap)):GetXY()
  128. --                 local Msg = Module.Rares[ID][1].." ".."?% ~("..self:Round(x * 100, 2)..", "..self:Round(y * 100, 2)..")"
  129. --                 SendChatMessage(Msg ,"CHANNEL", nil, RareShareDB["Config"]["CChannel"]["CID"])
  130. --                 self:Log("Rare found! Announcing to chat..", 1, self.LastMap)
  131. --                 RareShareDB["LastAnnounce"]["ID"]   = ID
  132. --                 RareShareDB["LastAnnounce"]["Time"] = time()
  133. --                 Module.Rares[ID][2] = time()
  134. --                 Module.Rares[ID][4] = false
  135. --                 if Module.Duplicates then Module.Duplicates(ID) end
  136. --                 if (RareShareDB["Config"]["Sound"]["Master"] == true) and (RareShareDB["Config"]["Sound"]["Rares"] == true) then
  137. --                     Addon:PlaySound()
  138. --                 end
  139. --                 if (Addon.TomTom == true) and (RareShareDB["Config"]["TomTom"]["Master"] == true) and (RareShareDB["Config"]["TomTom"]["Rares"] == true) then
  140. --                     Addon:CreateTomTomWaypoint(x, y, Module.Rares[ID][1]);
  141. --                 end
  142. --             end
  143. --         end
  144. --     end
  145. -- end
  146.  
  147. function Addon:GetRarePos(ID)
  148.     local Vignettes = C_VignetteInfo.GetVignettes()
  149.     local Returned = false
  150.     for i, v in pairs(Vignettes) do
  151.         local VignetteInfo = C_VignetteInfo.GetVignetteInfo(v)
  152.         local ID2 = self:GetNPCID(VignetteInfo.objectGUID)
  153.         if ID == ID2 then
  154.             local X, Y      = (C_VignetteInfo.GetVignettePosition(VignetteInfo.vignetteGUID, Addon.LastMap)):GetXY()
  155.             Returned = true
  156.             return RareShare:Round(X * 100, 2), RareShare:Round(Y * 100, 2)
  157.         end
  158.     end
  159.     if not Returned then
  160.         return Addon:GetReadablePlayerPosition(Addon.LastMap)
  161.     end
  162. end
  163.  
  164. function Addon:AnnounceRare(ID, Name, HP, HPMax, X, Y)
  165.     if (RareShareDB["LastAnnounce"]["ID"] == ID and RareShareDB["LastAnnounce"]["Time"] >= time() - Addon.Cooldown) then return end
  166.     local HealthPercent = self:GetHealthPercent(HP, HPMax)
  167.     local Faction = UnitFactionGroup("targettarget")
  168.     local FactionStr = "Empty"
  169.     if Faction == nil then
  170.         FactionStr = "Untagged"
  171.     else
  172.         FactionStr = Faction.. " tagged"
  173.     end
  174.     local Msg =FactionStr..": "..Name.." "..HealthPercent.."% ~("..X..", "..Y..")"
  175.     SendChatMessage(Msg ,"CHANNEL", nil, RareShareDB["Config"]["CChannel"]["CID"])
  176.     self:Log(Addon.Loc.RareFound, 1, self.LastMap)
  177.     RareShareDB["LastAnnounce"]["ID"]   = ID
  178.     RareShareDB["LastAnnounce"]["Time"] = time()
  179.     local Module = self:GetModule(self.LastMap)
  180.     Module.Rares[ID][2] = time()
  181.     Module.Rares[ID][4] = false
  182.     if Module.Duplicates then Module:Duplicates(ID) end
  183.     if (RareShareDB["Config"]["Sound"]["Master"] == true) and (RareShareDB["Config"]["Sound"]["Rares"] == true) then
  184.         Addon:PlaySound()
  185.     end
  186. end
  187.  
  188. function Addon:PreAnnounceRare(ID, Name, X, Y)
  189.     if (RareShareDB["LastAnnounce"]["ID"] == ID and RareShareDB["LastAnnounce"]["Time"] >= time() - Addon.Cooldown) then return end
  190.     local Msg = Name.." Sighted ~("..X..", "..Y..")"
  191.     SendChatMessage(Msg ,"CHANNEL", nil, RareShareDB["Config"]["CChannel"]["CID"])
  192.     self:Log("Rare found!", 1, self.LastMap)
  193.     RareShareDB["LastAnnounce"]["ID"]   = ID
  194.     RareShareDB["LastAnnounce"]["Time"] = (time() - Addon.Cooldown+Addon.PreCooldown) --Verry short CD cause we want health + tagged info asap
  195.     self:Log(Msg,1,self.LastMap)
  196.     local Module = self:GetModule(self.LastMap)
  197.     Module.Rares[ID][2] = (time() - Addon.Cooldown+Addon.PreCooldown) --Verry short CD cause we want health + tagged info asap
  198.     Module.Rares[ID][4] = false
  199.     if Module.Duplicates then Module:Duplicates(ID) end
  200.     if (RareShareDB["Config"]["Sound"]["Master"] == true) and (RareShareDB["Config"]["Sound"]["Rares"] == true) then
  201.         Addon:PlaySound()
  202.     end
  203.     if (Addon.TomTom == true) and (RareShareDB["Config"]["TomTom"]["Master"] == true) and (RareShareDB["Config"]["TomTom"]["Rares"] == true) then
  204.         Addon:CreateTomTomWaypoint(X / 100, Y / 100, Name);
  205.     end
  206. end
  207.  
  208. local TomTomLastWaypoint
  209. local TomTomExpireTimer
  210. function Addon:CreateTomTomWaypoint(X, Y, Name)
  211.     if TomTomLastWaypoint ~= nil then
  212.         TomTom:RemoveWaypoint(TomTomLastWaypoint)
  213.     end
  214.  
  215.     local X, Y = tonumber(strtrim(X)), tonumber(strtrim(Y))
  216.     TomTomLastWaypoint = TomTom:AddWaypoint(RareShare.LastMap, X, Y, {
  217.         title = Name,
  218.         persistent = false,
  219.         minimap = true,
  220.         world = true
  221.     });
  222.  
  223.     if TomTomExpireTimer ~= nil then TomTomExpireTimer:Cancel() end;
  224.     TomTomExpireTimer = C_Timer.NewTimer(RareShare.Cooldown / 2, function()
  225.         if TomTomLastWaypoint ~= nil then
  226.             TomTom:RemoveWaypoint(TomTomLastWaypoint)
  227.         end
  228.     end)
  229. end
  230.  
  231. RareShare = Addon
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement