View Single Post
10-21-15, 01:28 PM   #12
saxitoxin
A Theradrim Guardian
 
saxitoxin's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 60
whole modules\gold.lua
Lua Code:
  1. local addon, ns = ...
  2. local cfg = ns.cfg
  3. local unpack = unpack
  4. --------------------------------------------------------------
  5. --if not cfg.show.gold then return end
  6.  
  7. -------------
  8. -- HELPERS --
  9. -------------
  10.  
  11. -- copies missing parts of source table to another table
  12. function CopyTable(src, dest)
  13.     if type(dest) ~= "table" then
  14.         dest = {}
  15.     end
  16.  
  17.     for k, v in pairs(src) do
  18.         if type(v) == "table" then
  19.             dest[k] = CopyTable(v, dest[k])
  20.         elseif type(v) ~= type(dest[k]) then
  21.             dest[k] = v
  22.         end
  23.     end
  24.  
  25.     return dest
  26. end
  27.  
  28. -- removes everything that is present in source table from another table
  29. function DiffTable(src, dest)
  30.     if type(dest) ~= "table" then
  31.         return {}
  32.     end
  33.  
  34.     if type(src) ~= "table" then
  35.         return dest
  36.     end
  37.  
  38.     for k, v in pairs(dest) do
  39.         if type(v) == "table" then
  40.             if not next(DiffTable(src[k], v)) then
  41.                 dest[k] = nil
  42.             end
  43.         elseif v == src[k] then
  44.             dest[k] = nil
  45.         end
  46.     end
  47.  
  48.     return dest
  49. end
  50.  
  51. local function ConvertDateToNumber(month, day, year)
  52.     month = gsub(month, "(%d)(%d?)", function(d1, d2) return d2 == "" and "0"..d1 or d1..d2 end) -- converts M to MM
  53.     day = gsub(day, "(%d)(%d?)", function(d1, d2) return d2 == "" and "0"..d1 or d1..d2 end) -- converts D to DD
  54.  
  55.     return tonumber(year..month..day)
  56. end
  57.  
  58. --------------
  59. -- DEFAULTS --
  60. --------------
  61.  
  62. local D = {
  63.     ["money_related_stuff"] = {}
  64. }
  65.  
  66. -----------
  67. -- STUFF --
  68. -----------
  69.  
  70. local function Controller_OnEvent(self, event, arg)
  71.     if event == "ADDON_LOADED" and arg == "SXUI_Bottombar" then -- "test" is addon name
  72.         local CONFIG = CopyTable(D, TEST_CONFIG)
  73.         ns.CONFIG = CONFIG -- makes this table available throughout addon
  74.  
  75.         local playerRealm, playerFaction, playerName = GetRealmName(), UnitFactionGroup("player"), UnitName("player")
  76.  
  77.         if not CONFIG["money_related_stuff"][playerRealm] then
  78.             CONFIG["money_related_stuff"][playerRealm] = {} -- creates a table, if it doesn't exist
  79.         end
  80.  
  81.         local realmData = CONFIG["money_related_stuff"][playerRealm] -- just an alias
  82.         ns.realmData = realmData
  83.  
  84.         if not realmData[playerFaction[playerName]] then
  85.             realmData[playerFaction[playerName]] = {} -- creates a table, if it doesn't exist
  86.         end
  87.  
  88.         ns.playerData = realmData[playerFaction[playerName]]
  89.  
  90.         self:UnregisterEvent("ADDON_LOADED")
  91.     elseif event == "PLAYER_LOGIN" then
  92.         local playerData = ns.playerData
  93.  
  94.         local weekday, month, day, year = CalendarGetDate()
  95.         local today = ConvertDateToNumber(month, day, year)
  96.         local updateData
  97.  
  98.         if playerData.lastLoginDate then
  99.             if playerData.lastLoginDate < today then -- is true, if last time player logged in was the day before or even earlier
  100.                 playerData.lastLoginDate = today
  101.                 updateData = true
  102.             end
  103.         else
  104.             playerData.lastLoginDate = today
  105.             updateData = true
  106.         end
  107.  
  108.         if updateData then -- daily updates
  109.             if playerData["money_on_first_weekday"] then
  110.                 if weekday == cfg.core.firstWeekday then -- 1 is Sunday, 2 is Monday, different countries have different first day of the week
  111.                     playerData["money_on_first_weekday"] = GetMoney()
  112.                 end
  113.             else
  114.                 playerData["money_on_first_weekday"] = GetMoney()
  115.             end
  116.  
  117.             playerData["money_on_first_login_today"] = GetMoney()
  118.         end
  119.  
  120.         playerData["money_on_session_start"] = GetMoney() -- this one resets on every single login or UI reload
  121.  
  122.         self:UnregisterEvent("PLAYER_LOGIN")
  123.     elseif event == "PLAYER_LOGOUT" then
  124.         TEST_CONFIG = DiffTable(D, ns.CONFIG) -- writes data into TEST_CONFIG table
  125.     end
  126. end
  127.  
  128. local Controller = CreateFrame("Frame")
  129. Controller:RegisterEvent("ADDON_LOADED")
  130. Controller:RegisterEvent("PLAYER_LOGIN")
  131. Controller:RegisterEvent("PLAYER_LOGOUT")
  132. Controller:SetScript("OnEvent", Controller_OnEvent)
  133.  
  134. local goldConverter = function(money)
  135. if money == 1 then
  136. end
  137. local g, s, c = abs(money/10000), abs(mod(money/100, 100)), abs(mod(money, 100))
  138. end
  139.  
  140. local goldFrame = CreateFrame("BUTTON",nil, cfg.SXUIframe)
  141. goldFrame:SetPoint("RIGHT",-210,0)
  142. goldFrame:SetSize(16, 16)
  143. goldFrame:EnableMouse(true)
  144. goldFrame:RegisterForClicks("AnyUp")
  145.  
  146. local goldIcon = goldFrame:CreateTexture(nil,"OVERLAY",nil,7)
  147. goldIcon:SetPoint("LEFT")
  148. goldIcon:SetTexture(cfg.mediaFolder.."datatexts\\gold")
  149. goldIcon:SetVertexColor(unpack(cfg.color.normal))
  150.  
  151. local goldText = goldFrame:CreateFontString(nil, "OVERLAY")
  152. goldText:SetFont(cfg.font, cfg.core.normalFontSize)
  153. goldText:SetPoint("RIGHT", goldFrame,2,0)
  154. goldText:SetTextColor(unpack(cfg.color.normal))
  155.  
  156. goldFrame:SetScript("OnEnter", function()
  157.     if InCombatLockdown() then return end
  158.     goldIcon:SetVertexColor(unpack(cfg.color.hover))
  159.     GameTooltip:SetOwner(goldFrame, cfg.tooltipPos)
  160.     GameTooltip:AddLine("[|cff6699FFGold|r]")
  161.     GameTooltip:AddLine(" ")
  162.     ---------------------------------------------------
  163.  
  164.     local sessionGold = playerData["money_on_session_start"]
  165.     local dailyGold = playerData["money_on_first_login_today"]
  166.     local weekGold = playerData["money_on_first_weekday"]
  167.    
  168.     local money = GetMoney()
  169.     local moneyIcon = GetCoinIcon(money)
  170.     local g, s, c = abs(money/10000), abs(mod(money/100, 100)), abs(mod(money, 100))
  171.     if ( g < 1 ) then g = "" else g = string.format("|cffffffff%d|cffffd700g|r ", g) end
  172.     if ( s < 1 ) then s = "" else s = string.format("|cffffffff%d|cffc7c7cfs|r ", s) end
  173.     c = string.format("|cffffffff%d|cffeda55fc|r ", c)
  174.    
  175.     GameTooltip:AddDoubleLine("Current Gold",("%s%s%s"):format(g,s,c))
  176.     GameTooltip:AddDoubleLine("Session Gold",sessionGold)
  177.     GameTooltip:AddDoubleLine("Daily Gold",dailyGold)
  178.     GameTooltip:AddDoubleLine("Week Gold",weekGold)
  179.  
  180.    
  181.     GameTooltip:Show()
  182. end)
  183.  
  184. goldFrame:SetScript("OnLeave", function() if ( GameTooltip:IsShown() ) then GameTooltip:Hide() end goldIcon:SetVertexColor(unpack(cfg.color.normal)) end)
  185.  
  186. goldFrame:SetScript("OnClick", function(self, button, down)
  187.     if InCombatLockdown() then return end
  188.     if button == "LeftButton" then
  189.         OpenAllBags()
  190.     elseif button == "RightButton" then
  191.         CloseAllBags()
  192.     end
  193. end)
  194.  
  195. local eventframe = CreateFrame("Frame")
  196. eventframe:RegisterEvent("PLAYER_LOGIN")
  197. eventframe:RegisterEvent("PLAYER_ENTERING_WORLD")
  198. eventframe:RegisterEvent("PLAYER_MONEY")
  199. eventframe:RegisterEvent("SEND_MAIL_MONEY_CHANGED")
  200. eventframe:RegisterEvent("SEND_MAIL_COD_CHANGED")
  201. eventframe:RegisterEvent("PLAYER_TRADE_MONEY")
  202. eventframe:RegisterEvent("TRADE_MONEY_CHANGED")
  203. eventframe:RegisterEvent("TRADE_LOGOUT")
  204.  
  205. eventframe:SetScript("OnEvent", function(self,event, ...)
  206. if event == ("PLAYER_LOGIN") then
  207. local sessionGold = 0
  208. end
  209.  
  210.     local money = GetMoney()
  211.        
  212.     local g, s, c = abs(money/10000), abs(mod(money/100, 100)), abs(mod(money, 100))
  213.  
  214.     if g > 1 then
  215.         goldText:SetText(floor(g).."g")
  216.     elseif s > 1 then
  217.         goldText:SetText(floor(s).."s")
  218.     else
  219.         goldText:SetText(floor(c).."c")
  220.     end
  221.     goldFrame:SetSize(goldText:GetStringWidth()+18, 16)
  222. end)
  Reply With Quote