Files
SessionGoldTracker/SessionGoldTracker.lua
T
2026-10-15 14:27:00 +11:00

198 lines
6.6 KiB
Lua

-- SessionGoldTracker — DB Init & Session Restore
-- SavedVariables are auto-declared by the TOC as SessionGoldTrackerDB.
SessionGoldTrackerDB = SessionGoldTrackerDB or {}
-- ---------------------------------------------------------------------------
-- Defaults
-- ---------------------------------------------------------------------------
local DEFAULTS = {
position = { point = "CENTER", x = 0, y = 0 },
lifetimeTotal = 0,
showOnLogin = false,
lastSession = {
sessionStart = 0,
sessionGoldStart = 0,
sessionGross = 0,
sessionNet = 0,
},
}
-- Fill in any missing keys
local function FillDefaults(t, defaults)
for k, v in pairs(defaults) do
if t[k] == nil then
if type(v) == "table" then
t[k] = {}
FillDefaults(t[k], v)
else
t[k] = v
end
end
end
end
FillDefaults(SessionGoldTrackerDB, DEFAULTS)
-- ---------------------------------------------------------------------------
-- Session state (not saved — restored from lastSession on login/reload)
-- ---------------------------------------------------------------------------
local sessionStart = 0
local sessionGoldStart = 0
local sessionGross = 0
local sessionNet = 0
local lastMoney = 0
-- ---------------------------------------------------------------------------
-- Constants
-- ---------------------------------------------------------------------------
local GOLD_PER_HOUR_SHOW_AFTER = 60 -- seconds before showing gph
-- ---------------------------------------------------------------------------
-- Helper: format copper as gold/silver/copper string
-- ---------------------------------------------------------------------------
local function FormatCopper(copper)
local absCopper = math.abs(copper)
local gold = floor(absCopper / 10000)
local silver = floor((absCopper % 10000) / 100)
local copper = absCopper % 100
local str = ""
if gold > 0 then
str = str .. gold .. "g "
end
if silver > 0 or gold > 0 then
str = str .. silver .. "s "
end
str = str .. copper .. "c"
-- trim trailing space (standard Lua, no str:trim())
str = str:gsub("^%s+", ""):gsub("%s+$", "")
return (copper < 0 and "-" or "") .. str
end
-- ---------------------------------------------------------------------------
-- Frame creation (Step 4: Frame)
-- ---------------------------------------------------------------------------
local frame = CreateFrame("Frame", "SessionGoldTrackerFrame", UIParent)
frame:SetSize(200, 60)
frame:SetFrameStrata("MEDIUM")
frame:SetClampedToScreen(true)
frame:Hide() -- hidden by default, toggled with /gt
-- Title label
local title = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
title:SetPoint("TOPLEFT", frame, "TOPLEFT", 8, -4)
title:SetText("Session Gold Tracker")
-- Session earned label
local earnedLabel = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
earnedLabel:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -8)
earnedLabel:SetText("Earned: 0c")
-- Gold per hour label
local gphLabel = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
gphLabel:SetPoint("TOPLEFT", earnedLabel, "BOTTOMLEFT", 0, -4)
gphLabel:SetText("Rate: ---")
-- Time elapsed label
local timeLabel = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
timeLabel:SetPoint("TOPLEFT", gphLabel, "BOTTOMLEFT", 0, -4)
timeLabel:SetText("Time: 0s")
-- Update display function
local function UpdateDisplay()
local elapsed = GetTime() - sessionStart
local hours = elapsed / 3600
-- Earned (gross)
earnedLabel:SetText("Earned: " .. FormatCopper(sessionGross))
-- Gold per hour (show "---" until threshold)
if elapsed >= GOLD_PER_HOUR_SHOW_AFTER and hours > 0 then
local gph = sessionGross / hours
gphLabel:SetText("Rate: " .. FormatCopper(floor(gph)) .. "/hr")
else
gphLabel:SetText("Rate: ---")
end
-- Time elapsed
local timeStr = SecondsToTime(elapsed, false, false, 1)
timeLabel:SetText("Time: " .. timeStr)
end
-- ---------------------------------------------------------------------------
-- PLAYER_LOGIN handler
-- ---------------------------------------------------------------------------
local loginFrame = CreateFrame("Frame")
loginFrame:RegisterEvent("PLAYER_LOGIN")
loginFrame:SetScript("OnEvent", function(self, event)
self:UnregisterEvent(event)
-- Merge any missing DB keys
FillDefaults(SessionGoldTrackerDB, DEFAULTS)
-- Restore session state if we have a previous session (e.g. after /reload)
if SessionGoldTrackerDB.lastSession and SessionGoldTrackerDB.lastSession.sessionStart > 0 then
sessionStart = SessionGoldTrackerDB.lastSession.sessionStart
sessionGoldStart = SessionGoldTrackerDB.lastSession.sessionGoldStart
sessionGross = SessionGoldTrackerDB.lastSession.sessionGross or 0
sessionNet = SessionGoldTrackerDB.lastSession.sessionNet or 0
else
-- Fresh session
sessionStart = GetTime()
sessionGoldStart = GetMoney()
sessionGross = 0
sessionNet = 0
end
lastMoney = GetMoney()
print(("[SessionGoldTracker] Session started. Gold: %s"):format(GetMoneyString(sessionGoldStart)))
-- Restore position
if SessionGoldTrackerDB.position then
local pos = SessionGoldTrackerDB.position
frame:ClearAllPoints()
frame:SetPoint(pos.point, UIParent, pos.point, pos.x, pos.y)
else
frame:ClearAllPoints()
frame:SetPoint("CENTER", UIParent, "CENTER", 0, 0)
end
-- Show frame if showOnLogin is true
if SessionGoldTrackerDB.showOnLogin then
frame:Show()
end
-- Start update ticker
frame:SetScript("OnUpdate", function(self, elapsed)
UpdateDisplay()
end)
end)
-- ---------------------------------------------------------------------------
-- Money tracking (PLAYER_MONEY event)
-- ---------------------------------------------------------------------------
local moneyFrame = CreateFrame("Frame")
moneyFrame:RegisterEvent("PLAYER_MONEY")
moneyFrame:SetScript("OnEvent", function(self, event)
if event == "PLAYER_MONEY" then
local currentMoney = GetMoney()
local delta = currentMoney - lastMoney
if delta > 0 then
sessionGross = sessionGross + delta
end
sessionNet = sessionNet + delta
lastMoney = currentMoney
print(("[SessionGoldTracker] Money change: %s (net: %s, gross: %s)"):format(
GetMoneyString(delta),
GetMoneyString(sessionNet),
GetMoneyString(sessionGross)
))
end
end)