aggixx

Untitled

Aug 14th, 2016
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.63 KB | None | 0 0
  1. local A = FALoot;
  2. A.util = {};
  3. local U = A.util;
  4. local SD = A.sData;
  5. local PD = A.pData;
  6. local E = A.events;
  7.  
  8. SD.debugData = {};
  9. PD.debugOn = 0;
  10.  
  11. --[[ =======================================================
  12. Utility / Helper Functions
  13. ======================================================= --]]
  14.  
  15. U.formatDebugData = function()
  16. local s = ""
  17. for i=1,#debugData do
  18. if i > 1 then
  19. s = s .. "\n";
  20. end
  21. s = s .. "[" .. date("%c", debugData[i].time) .. "]<" .. debugData[i].threshold .. "> " .. debugData[i].msg;
  22. end
  23. return s;
  24. end;
  25.  
  26. U.debug = function(msg, verbosity)
  27. local output;
  28. if type(msg) == "string" or type(msg) == "number" or type(msg) == "nil" then
  29. output = msg or "nil";
  30. elseif type(msg) == "boolean" then
  31. output = (msg and "true") or "false";
  32. elseif type(msg) == "table" then
  33. if DevTools_Dump then
  34. if not verbosity or PD.debugOn >= verbosity then
  35. DevTools_Dump(msg);
  36. end
  37. return;
  38. else
  39. output = "DevTools not found.";
  40. end
  41. else
  42. return;
  43. end
  44. table.insert(SD.debugData, {
  45. ["msg"] = output,
  46. ["time"] = time(),
  47. ["threshold"] = verbosity or 0,
  48. });
  49. if not verbosity or PD.debugOn >= verbosity then
  50. print(A.CHAT_HEADER..output);
  51. end
  52. end
  53.  
  54. U.deepCopy = function(orig)
  55. local orig_type = type(orig)
  56. local copy
  57. if orig_type == 'table' then
  58. copy = {}
  59. for orig_key, orig_value in next, orig, nil do
  60. copy[U.deepCopy(orig_key)] = U.deepCopy(orig_value)
  61. end
  62. setmetatable(copy, U.deepCopy(getmetatable(orig)))
  63. else -- number, string, boolean, etc
  64. copy = orig
  65. end
  66. return copy
  67. end
  68.  
  69. U.deepCompare = function(t1, t2, ignore_mt)
  70. local ty1 = type(t1)
  71. local ty2 = type(t2)
  72. if ty1 ~= ty2 then return false end
  73. -- non-table types can be directly compared
  74. if ty1 ~= 'table' and ty2 ~= 'table' then return t1 == t2 end
  75. -- as well as tables which have the metamethod __eq
  76. local mt = getmetatable(t1)
  77. if not ignore_mt and mt and mt.__eq then return t1 == t2 end
  78. for k1,v1 in pairs(t1) do
  79. local v2 = t2[k1]
  80. if v2 == nil or not U.deepCompare(v1,v2) then return false end
  81. end
  82. for k2,v2 in pairs(t2) do
  83. local v1 = t1[k2]
  84. if v1 == nil or not U.deepCompare(v1,v2) then return false end
  85. end
  86. return true
  87. end
  88.  
  89. -- custom UnitName
  90. U.UnitName = function(unit, showServer)
  91. local name = UnitName(unit, showServer);
  92. if showServer and name and not string.match(name, "-") then
  93. name = name .. "-" .. SD.PLAYER_REALM;
  94. end
  95. return name;
  96. end
  97.  
  98. -- custom GetRaidRosterInfo
  99. U.GetRaidRosterInfo = function(index)
  100. local name, rank, subgroup, level, class, fileName, zone, online, isDead, role, isML = GetRaidRosterInfo(index);
  101. return (U.UnitName("raid"..index, true) or name), rank, subgroup, level, class, fileName, zone, online, isDead, role, isML;
  102. end
  103.  
  104. -- Get current server timestamp
  105. U.GetCurrentServerTime = function()
  106. local _, hours, minutes = GameTime_GetGameTime(true);
  107. local _, month, day, year = CalendarGetDate();
  108. local currentServerTime = time({
  109. ["hour"] = hours,
  110. ["min"] = minutes,
  111. ["month"] = month,
  112. ["day"] = day,
  113. ["year"] = year,
  114. });
  115. return currentServerTime;
  116. end`
  117.  
  118. -- As of 6.0.2:
  119. -- itemID:enchant:gem1:gem2:gem3:gem4:suffixID:uniqueID:level:specId:upgradeId:instanceDifficultyID:numBonusIDs:bonusID1:bonusID2...:upgradeValue
  120.  
  121. U.ItemLinkStrip = function(itemLink)
  122. if not itemLink then
  123. U.debug("util.ItemLinkStrip was passed a nil value!", 1);
  124. return;
  125. elseif type(itemLink) ~= "string" then
  126. U.debug("util.ItemLinkStrip was passed a non-string value!", 1);
  127. return;
  128. end
  129.  
  130. local itemString = string.match(itemLink, "|c%x+|Hitem:(.-)|h%[.-]|h|r");
  131. local out = "";
  132. local i = 1;
  133. local numBonuses;
  134.  
  135. for id in string.gmatch(itemString, "(%-?%d*):?") do
  136. id = tonumber(id);
  137.  
  138. if i == 13 then
  139. numBonuses = id or 0;
  140. elseif i == 1 -- itemID
  141. or i == 7 -- suffixID
  142. or i == 11 -- upgradeID
  143. or i > 13 and i <= 13 + numBonuses -- bonusIDs
  144. then
  145. if i ~= 1 then
  146. out = out .. ":";
  147. end
  148. if i == 7 and id and id > 60000 then -- ugly hack to account for suffix system
  149. id = id - 65536;
  150. end
  151. out = out .. ( id or 0 );
  152. end
  153.  
  154. i = i + 1;
  155. end
  156.  
  157. return out;
  158. end
  159.  
  160. U.ItemLinkAssemble = function(itemString)
  161. local i = 1;
  162. local j = 0;
  163. local s = "item:";
  164. local bonusIDs = "";
  165. for id in string.gmatch(itemString, "(%-?%d+):?") do
  166. if i == 1 then -- itemID
  167. s = s .. id;
  168. elseif i == 2 then -- suffixID
  169. s = s .. "::::::" .. id;
  170. elseif i == 3 then -- upgradeID
  171. s = s .. "::::" .. id;
  172. else -- bonusIDs
  173. j = j + 1;
  174. bonusIDs = bonusIDs .. ":".. id;
  175. end
  176.  
  177. i = i + 1;
  178. end
  179.  
  180. if j > 0 then
  181. s = s .. "::" .. j .. bonusIDs;
  182. end
  183.  
  184. local _, link = GetItemInfo(s);
  185. if not link then
  186. return;
  187. end
  188.  
  189. return link;
  190. end
  191.  
  192. U.isNameInGuild = function(name)
  193. local showOffline = GetGuildRosterShowOffline();
  194. SetGuildRosterShowOffline(false);
  195. local _, onlineguildies = GetNumGuildMembers();
  196. for j=1,onlineguildies do
  197. local jname = GetGuildRosterInfo(j);
  198. if jname == name then
  199. return true;
  200. end
  201. end
  202. SetGuildRosterShowOffline(showOffline);
  203. end
  204.  
  205. U.isMainRaid = function()
  206. GuildRoster()
  207. local groupType
  208. if IsInRaid() then
  209. groupType = "raid"
  210. else
  211. groupType = "party"
  212. end
  213. local aspects, drakes = 0, 0;
  214.  
  215. -- save show offline state to restore later
  216. local showOffline = GetGuildRosterShowOffline();
  217. SetGuildRosterShowOffline(false);
  218.  
  219. --[[ create table of guild member data so we don't have to
  220. call GetGuildRosterInfo a zillion times. --]]
  221. local guild = {};
  222. for i=1,select(2, GetNumGuildMembers()) do
  223. local name, rank = GetGuildRosterInfo(i);
  224. guild[i] = {};
  225. guild[i].name = string.match(name, "^[^-]+");
  226. guild[i].rank = rank;
  227. end
  228.  
  229. -- compare our table of data with the people in the raid
  230. for i=1,40 do
  231. if UnitExists(groupType..i) then
  232. local rName = GetRaidRosterInfo(i), "^[^-]+";
  233. for j=1,#guild do
  234. if rName == guild[j].name then
  235. if string.match(guild[j].rank, "Aspect") then
  236. aspects = aspects + 1
  237. elseif guild[j].rank == "Drake" then
  238. drakes = drakes + 1
  239. end
  240. break;
  241. end
  242. end
  243. end
  244. end
  245.  
  246. -- restore show offline state
  247. SetGuildRosterShowOffline(showOffline);
  248.  
  249. if aspects >= 2 and drakes >= 5 then
  250. return true
  251. else
  252. return false, aspects, drakes
  253. end
  254. end
  255.  
  256. do
  257. local cache;
  258.  
  259. -- Create function to be called when isEnabled's result may have changed.
  260. local function wipeCache()
  261. cache = nil;
  262. U.debug("Addon enabled state wiped.", 2);
  263. end
  264.  
  265. -- Only get a new result if we don't have a cached one (or the old cached value has been cleared).
  266. A.isEnabled = function(overrideDebug)
  267. if not overrideDebug and PD.debugOn > 0 then
  268. return true;
  269. end
  270.  
  271. if cache then
  272. return cache;
  273. end
  274.  
  275. -- do efficient methods first
  276. if select(2, IsInInstance()) ~= "raid" then
  277. cache = false;
  278. return cache, "wrong instance type";
  279. end
  280.  
  281. local n = GetNumGroupMembers();
  282.  
  283. if n < 10 then
  284. cache = false;
  285. return cache, "not enough group members";
  286. end
  287.  
  288. local d = GetRaidDifficultyID();
  289.  
  290. if not (d == 15 or d == 16) then
  291. cache = false;
  292. return cache, "wrong instance difficulty";
  293. elseif select(2, InGuildParty())/n < 0.75 then
  294. cache = false;
  295. return cache, "not guild group";
  296. --[[elseif not U.isMainRaid() then
  297. cache = false;
  298. return cache, "not enough officers";]]
  299. end
  300.  
  301. cache = true;
  302. return cache;
  303. end
  304.  
  305. -- Set events for cache to be cleared
  306.  
  307. local eventFrame = CreateFrame("Frame");
  308. eventFrame:SetScript("OnEvent", wipeCache);
  309. eventFrame:RegisterEvent("PLAYER_DIFFICULTY_CHANGED");
  310.  
  311. E.Register("GROUP_ROSTER_UPDATE", wipeCache);
  312. E.Register("ZONE_CHANGED_NEW_AREA", wipeCache);
  313. end
  314.  
  315. U.checkFilters = function(itemString, checkItemLevel)
  316. -- itemString must be a string!
  317. if type(itemString) ~= "string" then
  318. U.debug("checkFilters was passed a non-string value!", 1);
  319. return;
  320. end
  321.  
  322. --this is the function that determines if an item should or shouldn't be added to the window and/or announced
  323. local itemLink = U.ItemLinkAssemble(itemString);
  324.  
  325. if not itemLink then
  326. U.debug("checkFilters: Unable to retrieve itemLink! itemString = "..itemString..", itemLink = "..(itemLink or ""), 1);
  327. return false;
  328. end
  329.  
  330. if PD.debugOn > 0 then
  331. return true
  332. end
  333.  
  334. -- check properties of item
  335. local _, _, quality, ilevel, _, class, subClass = GetItemInfo(itemLink)
  336.  
  337. -- check if the quality of the item is high enough
  338. if quality ~= 4 then -- TODO: Add customizable quality filters
  339. U.debug("Quality of "..itemLink.." is too low.", 1);
  340. return false
  341. end
  342.  
  343. -- check if the class of the item is appropriate
  344. if class == "Miscellaneous" and subClass == "Junk" then
  345. return true;
  346. end
  347.  
  348. if not (class == "Armor" or class == "Weapon") then
  349. U.debug("Class of "..itemLink.." is incorrect.", 1)
  350. return false
  351. end
  352.  
  353. -- check if the item level of the item is high enough
  354. if checkItemLevel or class then
  355. local playerTotal = GetAverageItemLevel()
  356. if playerTotal - ilevel > 60 then -- if the item is more than 60 levels below the player
  357. U.debug("Item Level of "..itemLink.." is too low.", 1);
  358. return false
  359. end
  360. end
  361.  
  362. return true
  363. end
Advertisement
Add Comment
Please, Sign In to add comment