Repository navigation
Expand file tree
/
Copy pathSession.lua
More file actions
375 lines (336 loc) · 11.6 KB
/
Copy pathSession.lua
File metadata and controls
375 lines (336 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
-- Wick's Ledger
-- Session.lua: session lifecycle, loot tracking, gold delta, persist/restore, history
local ADDON, ns = ...
if not WickCore then return end -- said once in Core.lua
local WL = WicksLedger
WL.Session = WL.Session or {}
local S = WL.Session
local MAX_HISTORY = 5
-- ============================================================
-- STATE
-- ============================================================
S.active = false
S.startTime = nil
S.startMoney = nil
S.goldDelta = 0
S.loot = {} -- { [itemLink] = { itemID, name, icon, count, copper, source } }
S.totalCopper = 0 -- goldDelta + loot value
S.zoneName = nil
-- XP
S.startXP = nil
S.xpDelta = 0
S.maxLevel = false
-- Rep: { [factionID] = { name, delta } }
S.rep = {}
-- ============================================================
-- HELPERS
-- ============================================================
local function ItemIDFromLink(link)
local id = link and link:match("item:(%d+)")
return id and tonumber(id)
end
local function RecalcTotal()
local total = S.goldDelta
for _, entry in pairs(S.loot) do
if entry.isJunk then
-- Junk bucket: copper is already the running total, not per-item
total = total + (entry.copper or 0)
else
total = total + (entry.copper or 0) * (entry.count or 1)
end
end
S.totalCopper = total
end
-- ============================================================
-- PERSIST / RESTORE
-- ============================================================
local function PersistActive()
if not WL.charDB then return end
if not S.active then
WL.charDB.activeSession = nil
return
end
-- Serialize loot (keyed by itemID string) and rep
WL.charDB.activeSession = {
startTime = S.startTime,
startMoney = S.startMoney,
goldDelta = S.goldDelta,
totalCopper= S.totalCopper,
zoneName = S.zoneName,
startXP = S.startXP,
xpDelta = S.xpDelta,
maxLevel = S.maxLevel,
loot = S.loot,
rep = S.rep,
}
end
local function RestoreActive()
if not WL.charDB then return end
local saved = WL.charDB.activeSession
if not saved or not saved.startTime then return end
-- Stale-session guard: discard if older than 8 hours.
-- Covers crashes, force-quits, and forgotten sessions.
if (time() - saved.startTime) > 28800 then
WL.charDB.activeSession = nil
return
end
S.active = true
S.startTime = saved.startTime
S.startMoney = saved.startMoney
S.goldDelta = saved.goldDelta or 0
S.totalCopper = saved.totalCopper or 0
S.zoneName = saved.zoneName
S.startXP = saved.startXP
S.xpDelta = saved.xpDelta or 0
S.maxLevel = saved.maxLevel or false
S.loot = saved.loot or {}
S.rep = saved.rep or {}
end
-- ============================================================
-- HISTORY
-- ============================================================
local function CommitToHistory()
if not WL.charDB then return end
-- Discard sessions with nothing earned
if (S.totalCopper or 0) <= 0 then return end
local entry = {
startTime = S.startTime,
elapsed = S:Elapsed(),
goldDelta = S.goldDelta,
totalCopper = S.totalCopper,
zoneName = S.zoneName,
xpDelta = S.xpDelta,
loot = S.loot,
rep = S.rep,
}
local history = WL.charDB.history
table.insert(history, 1, entry) -- newest first
while #history > MAX_HISTORY do
table.remove(history)
end
end
-- ============================================================
-- LOOT PARSING
-- ============================================================
local LOOT_PATTERNS = {
"You receive loot: (.+)$",
"You loot (.+)$",
}
local function ParseLootMessage(msg)
local body
for _, pat in ipairs(LOOT_PATTERNS) do
body = msg:match(pat)
if body then break end
end
if not body then return nil, nil, nil end
body = body:gsub("%.$", "")
local count = 1
local suffix = body:match(" x(%d+)$")
if suffix then
count = tonumber(suffix)
body = body:gsub(" x%d+$", "")
end
local link = body:match("|H(item:[^|]+)|h%[.-%]|h")
if link then link = "|H" .. link .. "|h[" .. (body:match("|h%[(.-)%]|h") or "?") .. "]|h" end
if not link then link = body end
local itemID = ItemIDFromLink(link)
return itemID, link, count
end
-- ============================================================
-- PRICE OVERRIDE
-- ============================================================
function S:SetItemVendorPrice(key)
local entry = S.loot[key]
if not entry or entry.isJunk then return end
local _, _, _, _, _, _, _, _, _, _, vendorPrice = GetItemInfo(entry.itemID)
if not vendorPrice or vendorPrice <= 0 then return end
entry.copper = vendorPrice
entry.source = "vendor"
RecalcTotal()
PersistActive()
if WL.UI and WL.UI.OnUpdate then WL.UI:OnUpdate() end
end
-- ============================================================
-- SESSION CONTROL
-- ============================================================
local function SnapshotXP()
local level = UnitLevel("player")
local maxLvl = 70
if level >= maxLvl then return nil, true end
return UnitXP("player"), false
end
function S:Start()
if S.active then return end
S.active = true
S.startTime = time()
S.startMoney = GetMoney()
S.goldDelta = 0
S.loot = {}
S.totalCopper = 0
S.rep = {}
S.zoneName = GetRealZoneText()
S.startXP, S.maxLevel = SnapshotXP()
S.xpDelta = 0
PersistActive()
if WL.UI and WL.UI.OnSessionStart then WL.UI:OnSessionStart() end
WL.A:Print(string.format("session started in %s", S.zoneName or "?"))
end
function S:Stop()
if not S.active then return end
S.active = false
S.goldDelta = GetMoney() - (S.startMoney or GetMoney())
if not S.maxLevel and S.startXP then
S.xpDelta = UnitXP("player") - S.startXP
if S.xpDelta < 0 then S.xpDelta = S.xpDelta + UnitXPMax("player") end
end
RecalcTotal()
CommitToHistory()
PersistActive() -- clears activeSession
if WL.UI and WL.UI.OnSessionStop then WL.UI:OnSessionStop() end
WL.A:Print(string.format("session ended -- %s earned",
WL.Prices and WL.Prices:FormatCopper(S.totalCopper) or "?"))
end
function S:Reset()
S.active = false
S.startTime = nil
S.startMoney = nil
S.goldDelta = 0
S.loot = {}
S.totalCopper = 0
S.rep = {}
S.startXP = nil
S.xpDelta = 0
S.maxLevel = false
PersistActive() -- clears activeSession
if WL.UI and WL.UI.OnSessionReset then WL.UI:OnSessionReset() end
end
function S:Elapsed()
if not S.startTime then return 0 end
return time() - S.startTime
end
-- ============================================================
-- EVENT HANDLERS
-- ============================================================
WL:On("LOGIN", function()
RestoreActive()
if S.active then
WL.A:Print(string.format("session resumed in %s (%s elapsed)",
S.zoneName or "?",
(function()
local e = S:Elapsed()
local h = math.floor(e/3600)
local m = math.floor((e%3600)/60)
if h > 0 then return string.format("%dh %dm", h, m) end
return string.format("%dm", m)
end)()))
if WL.UI and WL.UI.OnSessionStart then WL.UI:OnSessionStart() end
end
end)
WL:On("ZONE", function(isInstance, instanceType)
if not WL.db or not WL.db.autoMode then return end
if isInstance and not S.active then
if WL.db.hardLock and WL.charDB and WL.charDB.activeSession then
-- Hard-lock resume: restore the paused session instead of starting fresh.
-- startMoney is adjusted so goldDelta stays accurate across the zone gap.
RestoreActive()
S.startMoney = GetMoney() - S.goldDelta
PersistActive()
if WL.UI and WL.UI.OnSessionStart then WL.UI:OnSessionStart() end
else
S:Start()
end
elseif not isInstance and S.active then
-- Ghost after a wipe: player released to graveyard outside the instance.
-- Don't stop -- they'll run back in.
if UnitIsGhost("player") then return end
if WL.db.hardLock then
-- Hard-lock: pause instead of stop. Save state, go idle, resume on re-entry.
S.goldDelta = GetMoney() - (S.startMoney or GetMoney())
if not S.maxLevel and S.startXP then
local cur = UnitXP("player")
local delta = cur - S.startXP
if delta < 0 then delta = delta + UnitXPMax("player") end
S.xpDelta = delta
end
RecalcTotal()
S.active = false
PersistActive()
if WL.UI and WL.UI.OnSessionStop then WL.UI:OnSessionStop() end
WL.A:Print("session paused (hard lock -- re-enter to resume)")
else
S:Stop()
end
end
end)
WL:On("MONEY", function()
if not S.active then return end
local current = GetMoney()
S.goldDelta = current - (S.startMoney or current)
RecalcTotal()
PersistActive()
if WL.UI and WL.UI.OnUpdate then WL.UI:OnUpdate() end
end)
WL:On("XP", function()
if not S.active or S.maxLevel or not S.startXP then return end
local cur = UnitXP("player")
local delta = cur - S.startXP
if delta < 0 then delta = delta + UnitXPMax("player") end
S.xpDelta = delta
PersistActive()
if WL.UI and WL.UI.OnUpdate then WL.UI:OnUpdate() end
end)
WL:On("REP", function(factionName, amount)
if not S.active then return end
if not S.rep[factionName] then
S.rep[factionName] = { name = factionName, delta = 0 }
end
S.rep[factionName].delta = S.rep[factionName].delta + amount
PersistActive()
if WL.UI and WL.UI.OnUpdate then WL.UI:OnUpdate() end
end)
WL:On("LOOT_MSG", function(msg)
if not S.active then return end
local itemID, link, count = ParseLootMessage(msg)
if not itemID then return end
local copper, source
local _, _, quality, _, _, _, _, _, _, icon, vendorPrice = GetItemInfo(itemID)
-- Grey (Poor quality) items: collapse into a single "Junk" bucket using vendor price.
if quality == 0 then
local vendorCopper = (vendorPrice or 0) * count
if S.loot["junk"] then
S.loot["junk"].count = S.loot["junk"].count + count
S.loot["junk"].copper = S.loot["junk"].copper + vendorCopper
else
S.loot["junk"] = {
itemID = 0,
link = nil,
icon = "Interface\\Icons\\INV_Misc_Bag_07",
count = count,
copper = vendorCopper,
source = "vendor",
isJunk = true,
}
end
RecalcTotal()
PersistActive()
if WL.UI and WL.UI.OnUpdate then WL.UI:OnUpdate() end
return
end
if WL.Prices then copper, source = WL.Prices:GetPrice(itemID) end
local key = tostring(itemID)
if S.loot[key] then
S.loot[key].count = S.loot[key].count + count
else
S.loot[key] = {
itemID = itemID,
link = link,
icon = icon,
count = count,
copper = copper or 0,
source = source or "unknown",
}
end
RecalcTotal()
PersistActive()
if WL.UI and WL.UI.OnUpdate then WL.UI:OnUpdate() end
end)