-- 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") -- Lifetime total label local lifetimeLabel = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall") lifetimeLabel:SetPoint("TOPLEFT", timeLabel, "BOTTOMLEFT", 0, -4) lifetimeLabel:SetText("Lifetime: 0g 0s 0c") -- 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) -- Lifetime total lifetimeLabel:SetText("Lifetime: " .. FormatCopper(lifetimeTotal)) 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) -- --------------------------------------------------------------------------- -- Lifetime total (Step 8) -- --------------------------------------------------------------------------- local lifetimeTotal = SessionGoldTrackerDB.lifetimeTotal or 0 SessionGoldTrackerDB.lifetimeTotal = lifetimeTotal -- --------------------------------------------------------------------------- -- 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 lifetimeTotal = lifetimeTotal + delta SessionGoldTrackerDB.lifetimeTotal = lifetimeTotal end sessionNet = sessionNet + delta lastMoney = currentMoney print(("[SessionGoldTracker] Money change: %s (net: %s, gross: %s)"):format( GetMoneyString(delta), GetMoneyString(sessionNet), GetMoneyString(sessionGross) )) end end) -- --------------------------------------------------------------------------- -- Reset Button (Step 6) -- --------------------------------------------------------------------------- local resetButton = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate") resetButton:SetSize(60, 22) resetButton:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -8, 8) resetButton:SetText("Reset") local function DoReset() sessionGross = 0 sessionNet = 0 sessionStart = GetTime() sessionGoldStart = GetMoney() lastMoney = GetMoney() UpdateDisplay() print("[SessionGoldTracker] Session reset.") end resetButton:SetScript("OnClick", DoReset) -- --------------------------------------------------------------------------- -- Slash Command (Step 7) -- --------------------------------------------------------------------------- SlashCmdList["SESSIONGOLDTRACKER"] = function(msg) msg = msg and msg:lower() or "" if msg == "reset" then DoReset() else if frame:IsShown() then frame:Hide() else frame:Show() end end end SLASH_SESSIONGOLDTRACKER1 = "/gt"