Advertisement
bredar

Bredar's goldtrack luafile

Jun 22nd, 2016
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.88 KB | None | 0 0
  1. local ADDON_NAME = ...
  2. local ADDON_VERSION = GetAddOnMetadata(ADDON_NAME, "Version")
  3. local GoldTrack = {}
  4. GoldTrack.__index = GoldTrack
  5.  
  6. local SESSION_BALANCE = 1
  7. local REALM_BALANCE = 2
  8. local CHARACTER_BALANCE = 3
  9.  
  10. SLASH_GOLDTRACK1 = "/goldtrack"
  11. SLASH_GOLDTRACK2 = "/gt"
  12.  
  13. function SlashCmdList.GOLDTRACK(msg, edit)
  14. if msg == "debug" then
  15. GoldTrack.global.debug = not GoldTrack.global.debug
  16. if GoldTrack.global.debug then
  17. GoldTrack:Print("Debug mode enabled!")
  18. else
  19. GoldTrack:Print("Debug mode disabled!")
  20. end
  21. end
  22. GoldTrack:Show()
  23. end
  24.  
  25. local backdrop = {
  26. bgFile = "Interface\\FrameGeneral\\UI-Background-Marble",
  27. edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Gold-Border",
  28. tile = false,
  29. tileSize = 128,
  30. edgeSize = 16,
  31. insets = {
  32. left = 4,
  33. right = 4,
  34. top = 4,
  35. bottom = 4
  36. }
  37. }
  38.  
  39. function GoldTrack:Initialize()
  40. self:CreateMainFrame()
  41. self:CreateTooltipFrame()
  42. self:CreateMenuFrame()
  43. self:RegisterEvents()
  44. end
  45.  
  46. function GoldTrack:CreateMainFrame()
  47. -- Create the frame and set backdrop
  48. self.Frame = CreateFrame("Button", "GoldTrack_Frame", UIParent)
  49. self.Frame:Hide()
  50. self.Frame:SetBackdrop(backdrop)
  51. self.Frame:SetFrameStrata("HIGH")
  52. self.Frame:SetAlpha(0.75)
  53.  
  54. -- Set frame size
  55. self.Frame:SetWidth(340)
  56. self.Frame:SetHeight(48)
  57.  
  58. -- Create frame text
  59.  
  60. self.Frame.Text = self.Frame:CreateFontString(nil, "HIGH", "BossEmoteNormalHuge")
  61. self.Frame.Text:SetAllPoints()
  62. self.Frame.Text:SetPoint("CENTER", 0, 0)
  63.  
  64. -- Make the frame movable
  65. self.Frame:SetMovable(true)
  66. self.Frame:EnableMouse(true)
  67. self.Frame:RegisterForDrag("LeftButton")
  68. self.Frame:SetScript("OnDragStart", self.Frame.StartMoving)
  69. self.Frame:SetScript("OnDragStop", function()
  70. self.char.pos_x, self.char.pos_y = self.Frame:GetCenter()
  71. self.Frame:StopMovingOrSizing()
  72. end)
  73.  
  74. self.Frame:RegisterForClicks("RightButtonUp")
  75. self.Frame:SetScript("OnClick", function(frame, button, down) if button == "RightButton" then ToggleDropDownMenu(1, nil, self.Frame.Menu, "GoldTrack_Frame", 0, 0) end end)
  76. end
  77.  
  78. function GoldTrack:CreateTooltipFrame()
  79. self.Frame.Tooltip = CreateFrame("GameTooltip", "GoldTrack_Tooltip", nil, "GameTooltipTemplate")
  80. self.Frame.Tooltip:SetScale(0.75)
  81. end
  82.  
  83. function GoldTrack:CreateMenuFrame()
  84. self.Frame.Menu = CreateFrame("Frame", "GoldTrack_Menu")
  85. self.Frame.Menu.displayMode = "MENU"
  86. self.Frame.Menu.initialize = function(frame, level, menuList) self:InitializeMenu(level, menuList) end
  87. end
  88.  
  89. function GoldTrack:InitializeMenu(level, menuList)
  90. local info = UIDropDownMenu_CreateInfo()
  91. if not level then return end
  92.  
  93. if level == 1 then
  94. info.isTitle = true
  95. info.text = "Reset balance"
  96. info.notCheckable = true
  97. UIDropDownMenu_AddButton(info)
  98.  
  99. info.isTitle = nil
  100. info.notCheckable = true
  101. info.disabled = false
  102. info.text = "Session"
  103. info.func = function() self:ResetSessionBalance() end
  104. UIDropDownMenu_AddButton(info)
  105.  
  106. info.text = "Realm"
  107. info.func = function() self:ResetRealmFactionBalance() end
  108. UIDropDownMenu_AddButton(info)
  109.  
  110. info.text = "Character"
  111. info.func = nil
  112. info.menuList = 1
  113. info.hasArrow = true
  114. UIDropDownMenu_AddButton(info)
  115.  
  116. info.isTitle = true
  117. info.menuList = nil
  118. info.hasArrow = false
  119. info.disabled = true
  120. info.text = nil
  121. UIDropDownMenu_AddButton(info)
  122.  
  123. info.disabled = false
  124. info.isTitle = false
  125. info.text = "Show"
  126. info.menuList = 2
  127. info.hasArrow = true
  128. info.func = nil
  129. UIDropDownMenu_AddButton(info)
  130.  
  131. info.text = "Hide"
  132. info.func = function() self:Hide() end
  133. info.hasArrow = false
  134. info.menuList = nil
  135. UIDropDownMenu_AddButton(info)
  136.  
  137. elseif level == 2 and menuList == 1 then
  138. info.isTitle = false
  139. info.disabled = false
  140. info.notCheckable = true
  141. info.func = function(frame, name) self:ResetCharacterBalance(name) end
  142.  
  143. for _,data in next,self:GetRealmFactionChars(),nil do
  144. if self.char.faction == data.faction and data.session_money ~= 0 then
  145. if self.char.realm == data.realm then
  146. info.text = data.name
  147. else
  148. info.text = data.name .. "-" .. data.realm
  149. end
  150. info.arg1 = data
  151. UIDropDownMenu_AddButton(info, level)
  152. end
  153. end
  154. elseif level == 2 and menuList == 2 then
  155. info.isTitle = false
  156. info.disabled = false
  157. info.notCheckable = false
  158. info.func = function(frame, value) self.char.main_balance = value self:UpdateMainFrame() end
  159.  
  160. info.text = "Session"
  161. info.arg1 = SESSION_BALANCE
  162. info.checked = self.char.main_balance == SESSION_BALANCE
  163. UIDropDownMenu_AddButton(info, level)
  164.  
  165. info.text = "Realm"
  166. info.arg1 = REALM_BALANCE
  167. info.checked = self.char.main_balance == REALM_BALANCE
  168. UIDropDownMenu_AddButton(info, level)
  169.  
  170. info.text = "Character"
  171. info.arg1 = CHARACTER_BALANCE
  172. info.checked = self.char.main_balance == CHARACTER_BALANCE
  173. UIDropDownMenu_AddButton(info, level)
  174. end
  175. end
  176.  
  177. function GoldTrack:RegisterEvents()
  178. self.Frame:RegisterEvent("ADDON_LOADED")
  179. self.Frame:RegisterEvent("PLAYER_LOGOUT")
  180. self.Frame:RegisterEvent("PLAYER_ENTERING_WORLD")
  181. self.Frame:RegisterEvent("PLAYER_MONEY")
  182.  
  183. self.Frame:SetScript("OnEvent", function(frame, event, ...) self[event](self, ...) end)
  184. self.Frame:SetScript("OnEnter", function() self:ShowTooltip() end)
  185. self.Frame:SetScript("OnLeave", function() self:HideTooltip() end)
  186. end
  187.  
  188. function GoldTrack:UpdateMainFrame()
  189. local balance = 0
  190. local balance_str = nil
  191.  
  192. if self.char.main_balance == SESSION_BALANCE then
  193. if self.login_money then
  194. balance = GetMoney() - self.login_money
  195. end
  196. elseif self.char.main_balance == REALM_BALANCE then
  197. balance = self:GetRealmFactionBalance()
  198. else
  199. balance = self.char.session_money
  200. end
  201.  
  202. if balance < 0 then
  203. balance_str = "|cffff0000" .. GetCoinTextureString(abs(balance)) .. "|r"
  204. elseif balance > 0 then
  205. balance_str = "|cff00ff00" .. GetCoinTextureString(balance) .. "|r"
  206. else
  207. balance_str = "|cffffffff" .. GetCoinTextureString(balance) .. "|r"
  208. end
  209.  
  210. self.Frame.Text:SetText(balance_str)
  211. end
  212.  
  213. function GoldTrack:Show()
  214. self.char.show = true
  215. self.Frame:Show()
  216. end
  217.  
  218. function GoldTrack:Hide()
  219. self.char.show = false
  220. self:Print(ADDON_NAME .. " is now hidden. Use " .. SLASH_GOLDTRACK1 .. " or " .. SLASH_GOLDTRACK2 .. " to show it again!")
  221. self.Frame:Hide()
  222. end
  223.  
  224. function GoldTrack:ShowTooltip()
  225. self.Frame.Tooltip:Hide()
  226. self.Frame.Tooltip:ClearLines()
  227. self.Frame.Tooltip:SetOwner(self.Frame, "ANCHOR_RIGHT")
  228.  
  229. self.Frame.Tooltip:AddLine(ADDON_NAME .. " " .. ADDON_VERSION, 1, 1, 0)
  230. self.Frame.Tooltip:AddLine(" ")
  231.  
  232. -- Earnings --
  233.  
  234. local realm_balance = self:GetRealmFactionBalance()
  235. if realm_balance < 0 then
  236. self.Frame.Tooltip:AddDoubleLine(self:GetFactionImage(self.char.faction) .. self.realm.name .. ":", GetCoinTextureString(abs(realm_balance)), 1, 1, 1, 1, 0, 0)
  237. elseif realm_balance > 0 then
  238. self.Frame.Tooltip:AddDoubleLine(self:GetFactionImage(self.char.faction) .. self.realm.name .. ":", GetCoinTextureString(realm_balance), 1, 1, 1, 0.2, 0.8, 0.2)
  239. else
  240. self.Frame.Tooltip:AddDoubleLine(self:GetFactionImage(self.char.faction) .. self.realm.name .. ":", GetCoinTextureString(realm_balance), 1, 1, 1, 1, 1, 1)
  241. end
  242.  
  243. self.Frame.Tooltip:AddLine(" ")
  244.  
  245. self.Frame.Tooltip:AddLine("Character balance:", 1, 1, 1)
  246. for _, data in next,self:GetRealmFactionChars(), nil do
  247. local class_color = RAID_CLASS_COLORS[data.class]
  248. local name_str = data.name
  249. if self.char.realm ~= data.realm then
  250. name_str = name_str .. "-" .. data.realm
  251. end
  252. if data.session_money < 0 then
  253. self.Frame.Tooltip:AddDoubleLine(" " .. name_str .. ":", GetCoinTextureString(abs(data.session_money)), class_color.r, class_color.g, class_color.b, 1, 0, 0)
  254. elseif data.session_money > 0 then
  255. self.Frame.Tooltip:AddDoubleLine(" " .. name_str .. ":", GetCoinTextureString(data.session_money), class_color.r, class_color.g, class_color.b, 0.2, 0.8, 0.2)
  256. end
  257. end
  258.  
  259. self.Frame.Tooltip:AddLine(" ")
  260.  
  261. -- Wealth --
  262. self.Frame.Tooltip:AddLine("Wealth:", 1, 1, 1)
  263. for _, data in next,self:GetRealmFactionChars(), nil do
  264. local class_color = RAID_CLASS_COLORS[data.class]
  265. if self.char.realm == data.realm then
  266. self.Frame.Tooltip:AddDoubleLine(" " .. data.name .. ":", GetCoinTextureString(data.money), class_color.r, class_color.g, class_color.b, 1, 1, 1)
  267. else
  268. self.Frame.Tooltip:AddDoubleLine(" " .. data.name .. "-" .. data.realm .. ":", GetCoinTextureString(data.money), class_color.r, class_color.g, class_color.b, 1, 1, 1)
  269. end
  270. end
  271. self.Frame.Tooltip:AddLine(" ")
  272. self.Frame.Tooltip:AddDoubleLine(" Total:", GetCoinTextureString(self:GetRealmFactionWealth()), 1, 1, 1, 1, 1, 1)
  273.  
  274. self.Frame.Tooltip:Show()
  275. end
  276.  
  277. function GoldTrack:HideTooltip()
  278. self.Frame.Tooltip:Hide()
  279. end
  280.  
  281. function GoldTrack:ADDON_LOADED(addon)
  282.  
  283. -- Only handle if our addon
  284. if addon ~= ADDON_NAME then
  285. return
  286. end
  287.  
  288. -- Because this is alpha, there are often changes to saved variables
  289. -- To prevent errors, delete the old saved variables when updating
  290. if not GoldTrack_DB or GoldTrack_DB.version ~= ADDON_VERSION then
  291. GoldTrack_DB = { version = ADDON_VERSION, debug = false }
  292. end
  293. if not GoldTrack_DB then GoldTrack_DB = { version = ADDON_VERSION, debug = false } end
  294.  
  295. local name, realm = UnitName("player")
  296. local realm = GetRealmName():gsub("%s+", "")
  297. local _, class = UnitClass("player")
  298.  
  299. self.realms = GetAutoCompleteRealms()
  300.  
  301. if not self.realms then
  302. self.realms = { realm }
  303. end
  304.  
  305. if not GoldTrack_DB[realm] then
  306. GoldTrack_DB[realm] = {
  307. name = realm,
  308. Horde = {
  309. session_money = 0
  310. },
  311. Alliance = {
  312. session_money = 0
  313. },
  314. chars = {
  315. }
  316. }
  317. end
  318.  
  319. if not GoldTrack_DB[realm].chars[name] then
  320. GoldTrack_DB[realm].chars[name] = {
  321. name = name,
  322. realm = realm,
  323. show = true,
  324. money = 0,
  325. session_money = 0,
  326. faction = UnitFactionGroup("player"),
  327. class = class,
  328. main_balance = REALM_BALANCE
  329. }
  330. end
  331.  
  332. self.char = GoldTrack_DB[realm].chars[name]
  333. self.realm = GoldTrack_DB[realm]
  334. self.global = GoldTrack_DB
  335.  
  336. if self.global.debug == nil then
  337. self.global.debug = false
  338. end
  339.  
  340. for _, realm in next,self.realms,nil do
  341. self:DebugPrint(realm)
  342. end
  343.  
  344. self:PrintTable(self.global)
  345.  
  346. self.Frame:SetPoint("CENTER", 0, 0)
  347.  
  348. if self.char.show then
  349. self:Show()
  350. end
  351.  
  352. self:UpdateMainFrame()
  353. end
  354.  
  355. function GoldTrack:PLAYER_ENTERING_WORLD()
  356. self.char.money = GetMoney()
  357.  
  358. if not self.login_money then
  359. self.login_money = GetMoney()
  360. end
  361. end
  362.  
  363. function GoldTrack:PLAYER_MONEY()
  364. local money = GetMoney()
  365. local diff = money - self.char.money
  366.  
  367. self:OnMoneyChange(diff)
  368. self.char.money = money
  369. end
  370.  
  371. function GoldTrack:ResetSessionBalance()
  372. self.login_money = GetMoney()
  373. self:UpdateMainFrame()
  374. end
  375.  
  376. function GoldTrack:ResetRealmFactionBalance()
  377. for _, realm in next,self.realms,nil do
  378. if self.global[realm] then
  379. self.global[realm][self.char.faction].session_money = 0
  380. end
  381. end
  382. end
  383.  
  384. function GoldTrack:ResetCharacterBalance(data)
  385. data.session_money = 0
  386. end
  387.  
  388. function GoldTrack:OnMoneyChange(diff)
  389. self.char.session_money = self.char.session_money + diff
  390. self.realm[self.char.faction].session_money = self.realm[self.char.faction].session_money + diff
  391.  
  392. self:UpdateMainFrame()
  393. end
  394.  
  395. -- Get all the characters that are on current realm or on realm connected
  396. -- to the current realm
  397. function GoldTrack:GetRealmFactionChars()
  398. local chars = {}
  399.  
  400. for _, realm in next,self.realms,nil do
  401. if self.global[realm] then
  402. for _, c in next,self.global[realm].chars,nil do
  403. if self.char.faction == c.faction then
  404. self:DebugPrint("Match")
  405. table.insert(chars, c)
  406. end
  407. end
  408. end
  409. end
  410.  
  411. return chars
  412. end
  413.  
  414. -- Get total wealth of characters on current realm and connected realms
  415. function GoldTrack:GetRealmFactionWealth()
  416. local wealth = 0
  417. for _, data in next,self:GetRealmFactionChars(),nil do
  418. if data.faction == self.char.faction then
  419. wealth = wealth + data.money
  420. end
  421. end
  422.  
  423. return wealth
  424. end
  425.  
  426. function GoldTrack:GetRealmFactionBalance()
  427. local balance = 0
  428. for _, realm in next, self.realms,nil do
  429. if self.global[realm] then
  430. balance = balance + self.global[realm][self.char.faction].session_money
  431. end
  432. end
  433.  
  434. return balance
  435. end
  436.  
  437. function GoldTrack:GetFactionImage(faction)
  438. if faction == "Horde" then
  439. return "|TInterface\\FriendsFrame\\PlusManz-Horde:20:20:0:0|t"
  440. else
  441. return "|TInterface\\FriendsFrame\\PlusManz-Alliance:20:20:0:0|t"
  442. end
  443. end
  444.  
  445. function GoldTrack:DebugPrint(msg)
  446. if self.global.debug then
  447. self:Print("(DEBUG) " .. msg)
  448. end
  449. end
  450.  
  451. function GoldTrack:PrintTable(tbl)
  452. for k,v in next,tbl,nil do
  453. if type(v) == "table" then self:PrintTable(v)
  454. else self:DebugPrint(k .. ": " .. tostring(v)) end
  455. end
  456. end
  457.  
  458. function GoldTrack:Print(msg)
  459. ChatFrame1:AddMessage("|cff00ffcc" .. ADDON_NAME .. "|r: " .. msg)
  460. end
  461.  
  462. GoldTrack:Initialize()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement