View Single Post
10-21-15, 08:49 AM   #7
lightspark
A Rage Talon Dragon Guard
 
lightspark's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2012
Posts: 341
Yo! That's me again... Here's a small addon I wrote to demonstrate how saved variables work. It stores info about character's gold on first day of the week, on first login this day and when session starts. Session begins, when player either logs in or reloads UI.

This data can be used for gold gain calculations.

test.toc:
Lua Code:
  1. ## Interface: 60200
  2. ## Author: lightspark
  3. ## Version: 0.1
  4. ## Title: test
  5. ## SavedVariables: TEST_CONFIG
  6.  
  7. test.lua

test.lua:
Lua Code:
  1. local _, ns = ... -- here we get addon table
  2. -------------
  3. -- HELPERS --
  4. -------------
  5.  
  6. -- copies missing fields from source table
  7. function CopyTable(src, dest)
  8.     if type(dest) ~= "table" then
  9.         dest = {}
  10.     end
  11.  
  12.     for k, v in pairs(src) do
  13.         if type(v) == "table" then
  14.             dest[k] = CopyTable(v, dest[k])
  15.         elseif type(v) ~= type(dest[k]) then
  16.             dest[k] = v
  17.         end
  18.     end
  19.  
  20.     return dest
  21. end
  22.  
  23. -- removes everything that is present in source table from another table
  24. function DiffTable(src, dest)
  25.     if type(dest) ~= "table" then
  26.         return {}
  27.     end
  28.  
  29.     if type(src) ~= "table" then
  30.         return dest
  31.     end
  32.  
  33.     for k, v in pairs(dest) do
  34.         if type(v) == "table" then
  35.             if not next(DiffTable(src[k], v)) then
  36.                 dest[k] = nil
  37.             end
  38.         elseif v == src[k] then
  39.             dest[k] = nil
  40.         end
  41.     end
  42.  
  43.     return dest
  44. end
  45.  
  46. local function ConvertDateToNumber(month, day, year)
  47.     month = gsub(month, "(%d)(%d?)", function(d1, d2) return d2 == "" and "0"..d1 or d1..d2 end) -- converts M to MM
  48.     day = gsub(day, "(%d)(%d?)", function(d1, d2) return d2 == "" and "0"..d1 or d1..d2 end) -- converts D to DD
  49.  
  50.     return tonumber(year..month..day)
  51. end
  52.  
  53. --------------
  54. -- DEFAULTS --
  55. --------------
  56.  
  57. local D = {
  58.     ["money_related_stuff"] = {}
  59. }
  60.  
  61. -----------
  62. -- STUFF --
  63. -----------
  64.  
  65. local function Controller_OnEvent(self, event, arg)
  66.     if event == "ADDON_LOADED" and arg == "test" then -- "test" is addon name
  67.         local CONFIG = CopyTable(D, TEST_CONFIG)
  68.         ns.CONFIG = CONFIG -- makes this table available throughout addon
  69.  
  70.         local playerName, playerRealm = UnitName("player"), GetRealmName()
  71.  
  72.         if not CONFIG["money_related_stuff"][playerRealm] then
  73.             CONFIG["money_related_stuff"][playerRealm] = {} -- creates a table, if it doesn't exist
  74.         end
  75.  
  76.         local realmData = CONFIG["money_related_stuff"][playerRealm] -- just an alias
  77.         ns.realmData = realmData
  78.  
  79.         if not realmData[playerName] then
  80.             realmData[playerName] = {} -- creates a table, if it doesn't exist
  81.         end
  82.  
  83.         ns.playerData = realmData[playerName]
  84.  
  85.         self:UnregisterEvent("ADDON_LOADED")
  86.     elseif event == "PLAYER_LOGIN" then
  87.         local playerData = ns.playerData
  88.  
  89.         local weekday, month, day, year = CalendarGetDate()
  90.         local today = ConvertDateToNumber(month, day, year)
  91.         local updateData
  92.  
  93.         if playerData.lastLoginDate then
  94.             if playerData.lastLoginDate < today then -- is true, if last time player logged in was the day before or even earlier
  95.                 playerData.lastLoginDate = today
  96.                 updateData = true
  97.             end
  98.         else
  99.             playerData.lastLoginDate = today
  100.             updateData = true
  101.         end
  102.  
  103.         if updateData then -- daily updates
  104.             if playerData["money_on_first_weekday"] then
  105.                 if weekday == 1 then -- 1 is Sunday, 2 is Monday, different countries have different first day of the week
  106.                     playerData["money_on_first_weekday"] = GetMoney()
  107.                 end
  108.             else
  109.                 playerData["money_on_first_weekday"] = GetMoney()
  110.             end
  111.  
  112.             playerData["money_on_first_login_today"] = GetMoney()
  113.         end
  114.  
  115.         playerData["money_on_session_start"] = GetMoney() -- this one resets on every single login or UI reload
  116.  
  117.         self:UnregisterEvent("PLAYER_LOGIN")
  118.     elseif event == "PLAYER_LOGOUT" then
  119.         TEST_CONFIG = DiffTable(D, ns.CONFIG) -- writes data into TEST_CONFIG table
  120.     end
  121. end
  122.  
  123. local Controller = CreateFrame("Frame")
  124. Controller:RegisterEvent("ADDON_LOADED")
  125. Controller:RegisterEvent("PLAYER_LOGIN")
  126. Controller:RegisterEvent("PLAYER_LOGOUT")
  127. Controller:SetScript("OnEvent", Controller_OnEvent)

and here are saved variables I got after logging in on few characters on different PTRs:
Lua Code:
  1. TEST_CONFIG = {
  2.     ["money_related_stuff"] = {
  3.         ["Brill (EU)"] = {
  4.             ["Galdricus"] = {
  5.                 ["lastLoginDate"] = 20151021,
  6.                 ["money_on_session_start"] = 371265253,
  7.                 ["money_on_first_weekday"] = 371265253,
  8.                 ["money_on_first_login_today"] = 371265253,
  9.             },
  10.             ["Livarra"] = {
  11.                 ["money_on_session_start"] = 496438400,
  12.                 ["money_on_first_login_today"] = 496438400,
  13.                 ["money_on_first_weekday"] = 496438400,
  14.                 ["lastLoginDate"] = 20151021,
  15.             },
  16.             ["Bjordius"] = {
  17.                 ["money_on_first_login_today"] = 500745400,
  18.                 ["money_on_session_start"] = 501745400,
  19.                 ["lastLoginDate"] = 20151021,
  20.                 ["money_on_first_weekday"] = 500745400,
  21.             },
  22.         },
  23.         ["Nobundo (KR)"] = {
  24.             ["Phia"] = {
  25.                 ["money_on_session_start"] = 188362133,
  26.                 ["money_on_first_login_today"] = 188362133,
  27.                 ["money_on_first_weekday"] = 188362133,
  28.                 ["lastLoginDate"] = 20151021,
  29.             },
  30.         },
  31.     },
  32. }

I hope table names are self-explanatory.
__________________

Last edited by lightspark : 10-21-15 at 11:03 AM. Reason: updated few comments
  Reply With Quote