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:
## Interface: 60200
## Author: lightspark
## Version: 0.1
## Title: test
## SavedVariables: TEST_CONFIG
test.lua
test.lua:
Lua Code:
local _, ns = ... -- here we get addon table
-------------
-- HELPERS --
-------------
-- copies missing fields from source table
function CopyTable(src, dest)
if type(dest) ~= "table" then
dest = {}
end
for k, v in pairs(src) do
if type(v) == "table" then
dest[k] = CopyTable(v, dest[k])
elseif type(v) ~= type(dest[k]) then
dest[k] = v
end
end
return dest
end
-- removes everything that is present in source table from another table
function DiffTable(src, dest)
if type(dest) ~= "table" then
return {}
end
if type(src) ~= "table" then
return dest
end
for k, v in pairs(dest) do
if type(v) == "table" then
if not next(DiffTable(src[k], v)) then
dest[k] = nil
end
elseif v == src[k] then
dest[k] = nil
end
end
return dest
end
local function ConvertDateToNumber(month, day, year)
month = gsub(month, "(%d)(%d?)", function(d1, d2) return d2 == "" and "0"..d1 or d1..d2 end) -- converts M to MM
day = gsub(day, "(%d)(%d?)", function(d1, d2) return d2 == "" and "0"..d1 or d1..d2 end) -- converts D to DD
return tonumber(year..month..day)
end
--------------
-- DEFAULTS --
--------------
local D = {
["money_related_stuff"] = {}
}
-----------
-- STUFF --
-----------
local function Controller_OnEvent(self, event, arg)
if event == "ADDON_LOADED" and arg == "test" then -- "test" is addon name
local CONFIG = CopyTable(D, TEST_CONFIG)
ns.CONFIG = CONFIG -- makes this table available throughout addon
local playerName, playerRealm = UnitName("player"), GetRealmName()
if not CONFIG["money_related_stuff"][playerRealm] then
CONFIG["money_related_stuff"][playerRealm] = {} -- creates a table, if it doesn't exist
end
local realmData = CONFIG["money_related_stuff"][playerRealm] -- just an alias
ns.realmData = realmData
if not realmData[playerName] then
realmData[playerName] = {} -- creates a table, if it doesn't exist
end
ns.playerData = realmData[playerName]
self:UnregisterEvent("ADDON_LOADED")
elseif event == "PLAYER_LOGIN" then
local playerData = ns.playerData
local weekday, month, day, year = CalendarGetDate()
local today = ConvertDateToNumber(month, day, year)
local updateData
if playerData.lastLoginDate then
if playerData.lastLoginDate < today then -- is true, if last time player logged in was the day before or even earlier
playerData.lastLoginDate = today
updateData = true
end
else
playerData.lastLoginDate = today
updateData = true
end
if updateData then -- daily updates
if playerData["money_on_first_weekday"] then
if weekday == 1 then -- 1 is Sunday, 2 is Monday, different countries have different first day of the week
playerData["money_on_first_weekday"] = GetMoney()
end
else
playerData["money_on_first_weekday"] = GetMoney()
end
playerData["money_on_first_login_today"] = GetMoney()
end
playerData["money_on_session_start"] = GetMoney() -- this one resets on every single login or UI reload
self:UnregisterEvent("PLAYER_LOGIN")
elseif event == "PLAYER_LOGOUT" then
TEST_CONFIG = DiffTable(D, ns.CONFIG) -- writes data into TEST_CONFIG table
end
end
local Controller = CreateFrame("Frame")
Controller:RegisterEvent("ADDON_LOADED")
Controller:RegisterEvent("PLAYER_LOGIN")
Controller:RegisterEvent("PLAYER_LOGOUT")
Controller:SetScript("OnEvent", Controller_OnEvent)
and here are saved variables I got after logging in on few characters on different PTRs:
Lua Code:
TEST_CONFIG = {
["money_related_stuff"] = {
["Brill (EU)"] = {
["Galdricus"] = {
["lastLoginDate"] = 20151021,
["money_on_session_start"] = 371265253,
["money_on_first_weekday"] = 371265253,
["money_on_first_login_today"] = 371265253,
},
["Livarra"] = {
["money_on_session_start"] = 496438400,
["money_on_first_login_today"] = 496438400,
["money_on_first_weekday"] = 496438400,
["lastLoginDate"] = 20151021,
},
["Bjordius"] = {
["money_on_first_login_today"] = 500745400,
["money_on_session_start"] = 501745400,
["lastLoginDate"] = 20151021,
["money_on_first_weekday"] = 500745400,
},
},
["Nobundo (KR)"] = {
["Phia"] = {
["money_on_session_start"] = 188362133,
["money_on_first_login_today"] = 188362133,
["money_on_first_weekday"] = 188362133,
["lastLoginDate"] = 20151021,
},
},
},
}
I hope table names are self-explanatory.