@piekasz/wow-mcp 0.0.0-stage → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/LICENSE +674 -0
  2. package/NOTICE +37 -0
  3. package/README.md +41 -2
  4. package/addon/WowMcp/AuctionTooltip.lua +106 -0
  5. package/addon/WowMcp/Commands.lua +259 -0
  6. package/addon/WowMcp/Compat.lua +167 -0
  7. package/addon/WowMcp/Core.lua +50 -0
  8. package/addon/WowMcp/Events.lua +62 -0
  9. package/addon/WowMcp/Items.lua +314 -0
  10. package/addon/WowMcp/Maps.lua +66 -0
  11. package/addon/WowMcp/MinimapButton.lua +114 -0
  12. package/addon/WowMcp/Options.lua +208 -0
  13. package/addon/WowMcp/Panel.lua +294 -0
  14. package/addon/WowMcp/QuestDb.lua +303 -0
  15. package/addon/WowMcp/QuestRewards.lua +137 -0
  16. package/addon/WowMcp/Sections/ActionBars.lua +54 -0
  17. package/addon/WowMcp/Sections/Auctions.lua +305 -0
  18. package/addon/WowMcp/Sections/Bank.lua +48 -0
  19. package/addon/WowMcp/Sections/Buyback.lua +47 -0
  20. package/addon/WowMcp/Sections/Character.lua +177 -0
  21. package/addon/WowMcp/Sections/Cooldowns.lua +76 -0
  22. package/addon/WowMcp/Sections/Currencies.lua +47 -0
  23. package/addon/WowMcp/Sections/Deaths.lua +43 -0
  24. package/addon/WowMcp/Sections/Dungeons.lua +117 -0
  25. package/addon/WowMcp/Sections/EquipmentSets.lua +83 -0
  26. package/addon/WowMcp/Sections/FlightPaths.lua +145 -0
  27. package/addon/WowMcp/Sections/Friends.lua +70 -0
  28. package/addon/WowMcp/Sections/Group.lua +101 -0
  29. package/addon/WowMcp/Sections/Guide.lua +317 -0
  30. package/addon/WowMcp/Sections/Guild.lua +131 -0
  31. package/addon/WowMcp/Sections/Inspect.lua +100 -0
  32. package/addon/WowMcp/Sections/Inventory.lua +64 -0
  33. package/addon/WowMcp/Sections/Legacy.lua +375 -0
  34. package/addon/WowMcp/Sections/Loot.lua +170 -0
  35. package/addon/WowMcp/Sections/Macros.lua +26 -0
  36. package/addon/WowMcp/Sections/Mail.lua +70 -0
  37. package/addon/WowMcp/Sections/Note.lua +38 -0
  38. package/addon/WowMcp/Sections/Pet.lua +127 -0
  39. package/addon/WowMcp/Sections/Played.lua +52 -0
  40. package/addon/WowMcp/Sections/Probe.lua +79 -0
  41. package/addon/WowMcp/Sections/Professions.lua +129 -0
  42. package/addon/WowMcp/Sections/Pvp.lua +45 -0
  43. package/addon/WowMcp/Sections/QuestDb.lua +106 -0
  44. package/addon/WowMcp/Sections/QuestGiver.lua +45 -0
  45. package/addon/WowMcp/Sections/Quests.lua +146 -0
  46. package/addon/WowMcp/Sections/Recipes.lua +195 -0
  47. package/addon/WowMcp/Sections/Reputation.lua +56 -0
  48. package/addon/WowMcp/Sections/Spells.lua +87 -0
  49. package/addon/WowMcp/Sections/Stable.lua +90 -0
  50. package/addon/WowMcp/Sections/Talents.lua +137 -0
  51. package/addon/WowMcp/Sections/Trainer.lua +58 -0
  52. package/addon/WowMcp/Sections/Vendor.lua +72 -0
  53. package/addon/WowMcp/Sections/XpGains.lua +258 -0
  54. package/addon/WowMcp/Snapshot.lua +412 -0
  55. package/addon/WowMcp/Store.lua +142 -0
  56. package/addon/WowMcp/TalentGrid.lua +171 -0
  57. package/addon/WowMcp/Timing.lua +61 -0
  58. package/addon/WowMcp/TraitTree.lua +158 -0
  59. package/addon/WowMcp/WowMcp.lua +24 -0
  60. package/addon/WowMcp/WowMcp.toc +72 -0
  61. package/bin/wow-mcp.js +248 -0
  62. package/package.json +41 -4
@@ -0,0 +1,137 @@
1
+ -- SPDX-License-Identifier: GPL-3.0-or-later
2
+ -- Copyright (C) 2026 Piekasz (GitHub: Piekario). Author attribution must be kept: see NOTICE.
3
+
4
+ -- Section talents: spent points (points) and full trees with rank-0 nodes (nodes), so that it is possible to
5
+ -- advise where to spend further points. Data comes solely from the client API,
6
+ -- because trees in Forever differ from the classic ones.
7
+
8
+ local _, ns = ...
9
+ local Compat = ns.Compat
10
+ local plain, firstValue = Compat.plain, Compat.firstValue
11
+ local TraitTree = ns.TraitTree
12
+
13
+ local function spentPoint(tree, name, rank, maxRank)
14
+ return { tree = tree, name = name, rank = rank, maxRank = maxRank }
15
+ end
16
+
17
+ ---------------------------------------------------------------------------
18
+ -- Classic trees (GetTalentInfo)
19
+ ---------------------------------------------------------------------------
20
+
21
+ local function classicTabName(tab)
22
+ if not GetTalentTabInfo then return nil end
23
+ local a, b = GetTalentTabInfo(tab)
24
+ -- The order of returned values differs between versions: the name is the first string.
25
+ return (type(a) == "string" and a) or (type(b) == "string" and b) or tostring(tab)
26
+ end
27
+
28
+ -- Adds the talents of one tab; returns the number of points spent in it.
29
+ local function addClassicTab(talents, tab)
30
+ local tabName = classicTabName(tab)
31
+ local inTree = 0
32
+ for index = 1, GetNumTalents(tab) or 0 do
33
+ -- name, icon, tier, column, rank, maxRank, isExceptional, meetsPrereq
34
+ local name, _, tier, column, rank, maxRank, _, meetsPrereq = GetTalentInfo(tab, index)
35
+ if name then
36
+ rank = plain(rank) or 0
37
+ local node = {
38
+ tree = tabName, name = name, rank = rank, maxRank = plain(maxRank),
39
+ row = plain(tier), column = plain(column),
40
+ }
41
+ if meetsPrereq ~= nil then node.prereqMet = meetsPrereq and true or false end
42
+ talents.nodes[#talents.nodes + 1] = node
43
+ if rank > 0 then
44
+ inTree = inTree + rank
45
+ talents.points[#talents.points + 1] = spentPoint(tabName, name, rank, plain(maxRank))
46
+ end
47
+ end
48
+ end
49
+ talents.trees[#talents.trees + 1] = { name = tabName, points = inTree }
50
+ return inTree
51
+ end
52
+
53
+ local function addClassicTalents(talents)
54
+ talents.source = "classic"
55
+ talents.trees = {}
56
+ local spent = 0
57
+ for tab = 1, GetNumTalentTabs() or 0 do
58
+ spent = spent + addClassicTab(talents, tab)
59
+ end
60
+ talents.spent = spent
61
+ talents.unspent = firstValue({ "UnitCharacterPoints", "GetUnspentTalentPoints" }, "player")
62
+ end
63
+
64
+ ---------------------------------------------------------------------------
65
+ -- New API (C_Traits). In Forever the classic trees sit on the traits system:
66
+ -- one config with one tree per class, and three tabs (e.g. Beast
67
+ -- Mastery, Marksmanship, Survival) are node groups with their own name.
68
+ ---------------------------------------------------------------------------
69
+
70
+ -- Sum of spent and free points in the whole tree.
71
+ local function addTreeCurrency(talents, configID, treeID)
72
+ local currency = TraitTree.currencies(configID, treeID)[1]
73
+ if currency then
74
+ talents.spent = (talents.spent or 0) + (currency.spent or 0)
75
+ talents.unspent = (talents.unspent or 0) + (currency.available or 0)
76
+ end
77
+ end
78
+
79
+ local function addTraitTree(talents, configID, treeID)
80
+ local groupNames, groups = TraitTree.tabs(treeID)
81
+ local pointsByTree = {}
82
+ addTreeCurrency(talents, configID, treeID)
83
+
84
+ for _, nodeID in ipairs(C_Traits.GetTreeNodes(treeID) or {}) do
85
+ local node = C_Traits.GetNodeInfo(configID, nodeID)
86
+ -- Hidden nodes (e.g. from another class in the same tree) are skipped.
87
+ if node and node.isVisible ~= false and (node.maxRanks or 0) > 0 then
88
+ local rank = TraitTree.rankOf(node)
89
+ local treeName = TraitTree.tabOf(node, groupNames)
90
+ if treeName and rank > 0 then
91
+ pointsByTree[treeName] = (pointsByTree[treeName] or 0) + rank
92
+ end
93
+
94
+ -- Nodes without a named tab (when tabs are known) are usually nodes of another class.
95
+ local keep = treeName ~= nil or next(groupNames) == nil or rank > 0
96
+ local entry = TraitTree.nodeEntry(configID, nodeID, node, treeName, keep)
97
+ if keep then
98
+ TraitTree.addDescription(talents, entry, nil, rank)
99
+ talents.nodes[#talents.nodes + 1] = entry
100
+ end
101
+ if rank > 0 then
102
+ talents.points[#talents.points + 1] = spentPoint(treeName, entry.name, rank, plain(node.maxRanks))
103
+ end
104
+ end
105
+ end
106
+
107
+ for _, group in ipairs(groups) do
108
+ talents.trees[#talents.trees + 1] = {
109
+ name = group.displayName, points = pointsByTree[group.displayName] or 0,
110
+ }
111
+ end
112
+ end
113
+
114
+ local function addTraitTalents(talents)
115
+ talents.source = "traits"
116
+ talents.trees = {}
117
+ local configID = C_ClassTalents.GetActiveConfigID()
118
+ if not configID then return end
119
+ local config = C_Traits.GetConfigInfo(configID)
120
+ for _, treeID in ipairs((config and config.treeIDs) or {}) do
121
+ addTraitTree(talents, configID, treeID)
122
+ end
123
+ ns.TalentGrid.place(talents.nodes)
124
+ end
125
+
126
+ local function collectTalents()
127
+ local talents = { points = {}, nodes = {} }
128
+ -- Classic trees are the simplest source, if the client has them.
129
+ if GetNumTalentTabs and GetNumTalents and GetTalentInfo then
130
+ addClassicTalents(talents)
131
+ elseif C_ClassTalents and C_ClassTalents.GetActiveConfigID and C_Traits and C_Traits.GetConfigInfo then
132
+ addTraitTalents(talents)
133
+ end
134
+ return talents
135
+ end
136
+
137
+ ns.Snapshot.addSection("talents", collectTalents)
@@ -0,0 +1,58 @@
1
+ -- SPDX-License-Identifier: GPL-3.0-or-later
2
+ -- Copyright (C) 2026 Piekasz (GitHub: Piekario). Author attribution must be kept: see NOTICE.
3
+
4
+ -- Section trainer (from the window): the trainer's offer (also pet and profession trainers).
5
+
6
+ local _, ns = ...
7
+ local plain = ns.Compat.plain
8
+
9
+ local KNOWN_CATEGORIES = { available = true, unavailable = true, used = true, header = true }
10
+
11
+ -- The order of values differs between client versions, so we look for the category and rank by content.
12
+ -- Strings with a path (slash) are icons, not ranks.
13
+ local function serviceCategoryAndRank(returns)
14
+ local category, rank
15
+ for j = 2, 6 do
16
+ local v = plain(returns[j])
17
+ if type(v) == "string" then
18
+ if KNOWN_CATEGORIES[v] then
19
+ category = category or v
20
+ elseif v ~= "" and not rank and not v:find("[\\/]") then
21
+ rank = v
22
+ end
23
+ end
24
+ end
25
+ return category, rank
26
+ end
27
+
28
+ local function trainerService(i, name, rank, category)
29
+ local service = { name = name, rank = rank, category = category }
30
+ if GetTrainerServiceCost then
31
+ service.costCopper = plain((GetTrainerServiceCost(i)))
32
+ end
33
+ if GetTrainerServiceLevelReq then
34
+ service.levelRequired = plain((GetTrainerServiceLevelReq(i)))
35
+ end
36
+ return service
37
+ end
38
+
39
+ local function collectTrainer()
40
+ if not GetNumTrainerServices or not GetTrainerServiceInfo then return nil end
41
+ local count = plain(GetNumTrainerServices())
42
+ if type(count) ~= "number" or count <= 0 then return nil end
43
+
44
+ local trainer = { npc = plain(UnitName("npc")), capturedAt = time(), services = {} }
45
+ for i = 1, count do
46
+ local returns = { GetTrainerServiceInfo(i) }
47
+ local name = plain(returns[1])
48
+ local category, rank = serviceCategoryAndRank(returns)
49
+ if name and category ~= "header" then
50
+ trainer.services[#trainer.services + 1] = trainerService(i, name, rank, category)
51
+ end
52
+ end
53
+ return trainer
54
+ end
55
+
56
+ -- trainer is the last trainer, trainers: several latest (NPC name -> the same as trainer).
57
+ ns.Snapshot.addWindowSection("trainer", collectTrainer, { "TRAINER_SHOW", "TRAINER_UPDATE" },
58
+ { history = { name = "trainers", key = "npc", max = 10 } })
@@ -0,0 +1,72 @@
1
+ -- SPDX-License-Identifier: GPL-3.0-or-later
2
+ -- Copyright (C) 2026 Piekasz (GitHub: Piekario). Author attribution must be kept: see NOTICE.
3
+
4
+ -- Section vendor (from the window): the vendor's offer, prices and whether they repair gear.
5
+
6
+ local _, ns = ...
7
+ local plain, callFirst, firstValue = ns.Compat.plain, ns.Compat.callFirst, ns.Compat.firstValue
8
+ local Items = ns.Items
9
+
10
+ -- A negative quantity means unlimited stock.
11
+ local function limitedStock(available)
12
+ if type(available) == "number" and available >= 0 then return available end
13
+ return nil
14
+ end
15
+
16
+ local function readMerchantItem(item, index)
17
+ local info = callFirst({ "C_MerchantFrame.GetItemInfo" }, index)
18
+ if info and type(info[1]) == "table" then
19
+ local data = info[1]
20
+ item.name = plain(data.name)
21
+ item.priceCopper = plain(data.price)
22
+ item.stack = plain(data.stackCount)
23
+ item.limitedStock = limitedStock(plain(data.numAvailable))
24
+ if data.isUsable == false then item.usable = false end
25
+ return
26
+ end
27
+ local legacy = callFirst({ "GetMerchantItemInfo" }, index)
28
+ if legacy then
29
+ item.name = plain(legacy[1])
30
+ item.priceCopper = plain(legacy[3])
31
+ item.stack = plain(legacy[4])
32
+ item.limitedStock = limitedStock(plain(legacy[5]))
33
+ end
34
+ end
35
+
36
+ local function merchantItem(index)
37
+ local item = { index = index }
38
+ readMerchantItem(item, index)
39
+ local link = firstValue({ "GetMerchantItemLink" }, index)
40
+ if link then
41
+ item.id = Items.idFromLink(link)
42
+ item.name = item.name or Items.nameFromLink(link)
43
+ pcall(Items.addDetails, item, link)
44
+ if item.equipLoc then
45
+ Items.attachTooltip(item, ns.Compat.tooltipFor("GetMerchantItem", index))
46
+ end
47
+ end
48
+ return item
49
+ end
50
+
51
+ local function collectVendor()
52
+ if not GetMerchantNumItems then return nil end
53
+ local count = plain(GetMerchantNumItems())
54
+ if type(count) ~= "number" or count <= 0 then return nil end
55
+
56
+ local vendor = { npc = plain(UnitName("npc")), capturedAt = time(), items = {} }
57
+ vendor.canRepair = firstValue({ "CanMerchantRepair" }) and true or false
58
+ if vendor.canRepair then
59
+ local cost = firstValue({ "GetRepairAllCost" })
60
+ if type(cost) == "number" then vendor.repairCostCopper = cost end
61
+ end
62
+
63
+ for index = 1, count do
64
+ local item = merchantItem(index)
65
+ if item.name then vendor.items[#vendor.items + 1] = item end
66
+ end
67
+ return vendor
68
+ end
69
+
70
+ -- vendor is the last vendor, vendors: several latest (NPC name -> the same as vendor).
71
+ ns.Snapshot.addWindowSection("vendor", collectVendor, { "MERCHANT_SHOW", "MERCHANT_UPDATE" },
72
+ { history = { name = "vendors", key = "npc", max = 10 } })
@@ -0,0 +1,258 @@
1
+ -- SPDX-License-Identifier: GPL-3.0-or-later
2
+ -- Copyright (C) 2026 Piekasz (GitHub: Piekario). Author attribution must be kept: see NOTICE.
3
+
4
+ -- Section xpGains: the player's own experience since the previous dump saved to disk, broken down
5
+ -- into kills, quests and the rest (exploration, discoveries etc.), with event counts.
6
+ -- Reads only CHAT_MSG_COMBAT_XP_GAIN messages, which the client shows in chat anyway
7
+ -- (own only), and the quest turn-in event. Lifecycle as in the loot section.
8
+ -- Diagnostics: messagesSeen, messagesParsed, messagesSecret (content hidden by the client) and barXp,
9
+ -- i.e. XP computed independently from the experience bar (UnitXP on PLAYER_XP_UPDATE). When barXp
10
+ -- grows while total stands still, messages do not match the patterns or are hidden.
11
+ -- Closed periods with XP are carried in xpGains.periods (oldest first, bounded; see Snapshot.closePeriod).
12
+
13
+ local _, ns = ...
14
+ local plain = ns.Compat.plain
15
+
16
+ local XpGains = {}
17
+ ns.XpGains = XpGains
18
+
19
+ -- Window (in seconds) in which a quest XP message and the quest turn-in event are treated as the same thing.
20
+ XpGains.MATCH_WINDOW = 3
21
+
22
+ -- Game strings: with a creature name ("%s dies, you gain %d experience.") it is a kill, without a name
23
+ -- ("You gain %d experience.") it is a quest or something else. Many variants (bonus, group, raid).
24
+ local KILL_GLOBALS = {
25
+ "COMBATLOG_XPGAIN_EXHAUSTION1", "COMBATLOG_XPGAIN_EXHAUSTION1_GROUP", "COMBATLOG_XPGAIN_EXHAUSTION1_RAID",
26
+ "COMBATLOG_XPGAIN_EXHAUSTION2", "COMBATLOG_XPGAIN_EXHAUSTION2_GROUP", "COMBATLOG_XPGAIN_EXHAUSTION2_RAID",
27
+ "COMBATLOG_XPGAIN_EXHAUSTION4", "COMBATLOG_XPGAIN_EXHAUSTION4_GROUP", "COMBATLOG_XPGAIN_EXHAUSTION4_RAID",
28
+ "COMBATLOG_XPGAIN_EXHAUSTION5", "COMBATLOG_XPGAIN_EXHAUSTION5_GROUP", "COMBATLOG_XPGAIN_EXHAUSTION5_RAID",
29
+ "COMBATLOG_XPGAIN_FIRSTPERSON", "COMBATLOG_XPGAIN_FIRSTPERSON_GROUP", "COMBATLOG_XPGAIN_FIRSTPERSON_RAID",
30
+ }
31
+ local OTHER_GLOBALS = {
32
+ "COMBATLOG_XPGAIN_EXHAUSTION1_UNNAMED", "COMBATLOG_XPGAIN_EXHAUSTION2_UNNAMED",
33
+ "COMBATLOG_XPGAIN_EXHAUSTION4_UNNAMED", "COMBATLOG_XPGAIN_EXHAUSTION5_UNNAMED",
34
+ "COMBATLOG_XPGAIN_FIRSTPERSON_UNNAMED", "COMBATLOG_XPGAIN_FIRSTPERSON_UNNAMED_GROUP",
35
+ "COMBATLOG_XPGAIN_FIRSTPERSON_UNNAMED_RAID", "COMBATLOG_XPGAIN_QUEST",
36
+ }
37
+
38
+ -- When the client lacks these strings: English forms.
39
+ local ENGLISH_KILL = { "%s dies, you gain %d experience." }
40
+ local ENGLISH_OTHER = { "You gain %d experience." }
41
+
42
+ local xpSince -- time of the dump that last reached the disk (or of login)
43
+ local recentOther -- { xp, at }: the latest "nameless" XP that may turn out to be quest XP
44
+ local pendingQuest -- { xp, at }: quest turned in, and the XP message has not arrived yet
45
+ local bar -- { level, xp, xpMax }: last experience bar read
46
+
47
+ ---------------------------------------------------------------------------
48
+ -- Message recognition (pure functions)
49
+ ---------------------------------------------------------------------------
50
+
51
+ local function compile(globals, english, kind)
52
+ local patterns = {}
53
+ for _, global in ipairs(globals) do
54
+ local format = _G[global]
55
+ if type(format) == "string" and format ~= "" then
56
+ local pattern, order = ns.Loot.patternFrom(format)
57
+ patterns[#patterns + 1] = { pattern = pattern, order = order, kind = kind }
58
+ end
59
+ end
60
+ return patterns, english
61
+ end
62
+
63
+ local function xpPatterns()
64
+ local list = {}
65
+ local kills, killEnglish = compile(KILL_GLOBALS, ENGLISH_KILL, "kill")
66
+ local others, otherEnglish = compile(OTHER_GLOBALS, ENGLISH_OTHER, "other")
67
+ for _, p in ipairs(kills) do list[#list + 1] = p end
68
+ for _, p in ipairs(others) do list[#list + 1] = p end
69
+ -- The English forms only when the client has no XP strings at all: on a localized client its own
70
+ -- strings are complete, and an English pattern could only misread a message.
71
+ if #list > 0 then return list end
72
+ if #kills == 0 then
73
+ for _, format in ipairs(killEnglish) do
74
+ local pattern, order = ns.Loot.patternFrom(format)
75
+ list[#list + 1] = { pattern = pattern, order = order, kind = "kill" }
76
+ end
77
+ end
78
+ if #others == 0 then
79
+ for _, format in ipairs(otherEnglish) do
80
+ local pattern, order = ns.Loot.patternFrom(format)
81
+ list[#list + 1] = { pattern = pattern, order = order, kind = "other" }
82
+ end
83
+ end
84
+ return list
85
+ end
86
+
87
+ -- Whether the client has its own XP format strings; then they are the only patterns used (in the client's
88
+ -- language), and the English fallbacks below never apply.
89
+ function XpGains.usingGameFormats()
90
+ for _, list in ipairs({ KILL_GLOBALS, OTHER_GLOBALS }) do
91
+ for _, global in ipairs(list) do
92
+ if type(_G[global]) == "string" and _G[global] ~= "" then return true end
93
+ end
94
+ end
95
+ return false
96
+ end
97
+
98
+ -- Last resort only when the client has no XP format strings at all (another version): English
99
+ -- message beginnings, without the bonus tail.
100
+ local LOOSE_PATTERNS = {
101
+ { pattern = "^.+ dies, you gain (%d+) experience", kind = "kill" },
102
+ { pattern = "^You gain (%d+) experience", kind = "other" },
103
+ }
104
+
105
+ -- Kind ("kill" or "other") and number of points from the player's own XP message; nil for other texts.
106
+ function XpGains.parse(message, patterns)
107
+ if type(message) ~= "string" then return nil end
108
+ for _, p in ipairs(patterns or xpPatterns()) do
109
+ local captures = { message:match(p.pattern) }
110
+ if #captures > 0 then
111
+ for i, field in ipairs(p.order) do
112
+ if field == "count" then
113
+ local xp = tonumber(captures[i])
114
+ if xp then return p.kind, xp end
115
+ end
116
+ end
117
+ end
118
+ end
119
+ if XpGains.usingGameFormats() then return nil end
120
+ for _, p in ipairs(LOOSE_PATTERNS) do
121
+ local xp = tonumber(message:match(p.pattern))
122
+ if xp then return p.kind, xp end
123
+ end
124
+ return nil
125
+ end
126
+
127
+ ---------------------------------------------------------------------------
128
+ -- Recording in the dump
129
+ ---------------------------------------------------------------------------
130
+
131
+ local function newSection(since)
132
+ return {
133
+ since = since, total = 0,
134
+ kills = { count = 0, xp = 0 }, quests = { count = 0, xp = 0 }, other = { count = 0, xp = 0 },
135
+ messagesSeen = 0, messagesParsed = 0, messagesSecret = 0, barXp = 0,
136
+ }
137
+ end
138
+
139
+ local function xpSection()
140
+ local current = ns.Snapshot.current()
141
+ if type(current.xpGains) ~= "table" then current.xpGains = newSection(xpSince or time()) end
142
+ return current.xpGains
143
+ end
144
+
145
+ -- Diagnostic counter (older dumps may not have it).
146
+ local function bump(field, amount)
147
+ local section = xpSection()
148
+ section[field] = (type(section[field]) == "number" and section[field] or 0) + (amount or 1)
149
+ end
150
+
151
+ local function add(bucket, xp)
152
+ local section = xpSection()
153
+ section[bucket].count = section[bucket].count + 1
154
+ section[bucket].xp = section[bucket].xp + xp
155
+ section.total = section.total + xp
156
+ section.lastAt = time()
157
+ end
158
+
159
+ -- Reverts an earlier crediting to "other" (the message arrived before the turn-in event).
160
+ local function removeFromOther(xp)
161
+ local section = xpSection()
162
+ local other = section.other
163
+ if other.count > 0 then
164
+ other.count = other.count - 1
165
+ other.xp = math.max(other.xp - xp, 0)
166
+ section.total = math.max(section.total - xp, 0)
167
+ end
168
+ end
169
+
170
+ function XpGains.record(kind, xp)
171
+ local now = time()
172
+ if kind == "kill" then
173
+ add("kills", xp)
174
+ return
175
+ end
176
+ -- "Nameless" XP right after a quest turn-in is quest XP that the turn-in event has already counted.
177
+ if pendingQuest and pendingQuest.xp == xp and now - pendingQuest.at <= XpGains.MATCH_WINDOW then
178
+ pendingQuest = nil
179
+ return
180
+ end
181
+ add("other", xp)
182
+ recentOther = { xp = xp, at = now }
183
+ end
184
+
185
+ function XpGains.recordQuest(xp)
186
+ if type(xp) ~= "number" or xp <= 0 then return end
187
+ local now = time()
188
+ if recentOther and recentOther.xp == xp and now - recentOther.at <= XpGains.MATCH_WINDOW then
189
+ removeFromOther(xp)
190
+ recentOther = nil
191
+ else
192
+ pendingQuest = { xp = xp, at = now }
193
+ end
194
+ add("quests", xp)
195
+ end
196
+
197
+ local function onXpMessage(message)
198
+ bump("messagesSeen")
199
+ local text = plain(message)
200
+ if message ~= nil and text == nil then
201
+ bump("messagesSecret") -- the client hid the content ("secret"), it cannot be read
202
+ return
203
+ end
204
+ local kind, xp = XpGains.parse(text)
205
+ if kind then
206
+ bump("messagesParsed")
207
+ XpGains.record(kind, xp)
208
+ end
209
+ end
210
+
211
+ ---------------------------------------------------------------------------
212
+ -- Experience bar (independent of messages)
213
+ ---------------------------------------------------------------------------
214
+
215
+ local function readBar()
216
+ local level, xp, xpMax = plain(UnitLevel("player")), plain(UnitXP("player")), plain(UnitXPMax("player"))
217
+ if type(level) ~= "number" or type(xp) ~= "number" or type(xpMax) ~= "number" then return nil end
218
+ return { level = level, xp = xp, xpMax = xpMax }
219
+ end
220
+
221
+ -- XP between two bar reads; a one-level level-up adds the remainder of the previous bar.
222
+ function XpGains.barDelta(before, after)
223
+ if not (before and after) then return 0 end
224
+ if after.level == before.level then return math.max(after.xp - before.xp, 0) end
225
+ if after.level == before.level + 1 then return math.max(before.xpMax - before.xp, 0) + after.xp end
226
+ return 0 -- jump of several levels or a drop: we do not guess
227
+ end
228
+
229
+ local function onXpUpdate(unit)
230
+ if unit ~= nil and unit ~= "player" then return end
231
+ local now = readBar()
232
+ local delta = XpGains.barDelta(bar, now)
233
+ if now then bar = now end
234
+ if delta > 0 then bump("barXp", delta) end
235
+ end
236
+
237
+ -- QUEST_TURNED_IN(questID, xpReward, moneyReward)
238
+ local function onQuestTurnedIn(_, xp)
239
+ XpGains.recordQuest(plain(xp))
240
+ end
241
+
242
+ -- A closed period is worth carrying only when experience came in (from messages or the bar).
243
+ function XpGains.hasContent(section)
244
+ return (type(section.total) == "number" and section.total > 0) or (type(section.barXp) == "number" and section.barXp > 0)
245
+ end
246
+
247
+ -- After login (and after /reload) the previous dump is already on disk together with its XP; the closed period
248
+ -- is carried in xpGains.periods (Snapshot.startPeriod).
249
+ local function startXpPeriod()
250
+ xpSince = ns.Snapshot.startPeriod("xpGains", newSection, XpGains.hasContent)
251
+ bar = readBar()
252
+ end
253
+
254
+ ns.Snapshot.addWindowSection("xpGains", nil, nil, { quiet = true })
255
+ ns.Events.on("PLAYER_LOGIN", function() pcall(startXpPeriod) end)
256
+ ns.Events.on("CHAT_MSG_COMBAT_XP_GAIN", ns.Snapshot.whenEnabled("xpGains", function(message) pcall(onXpMessage, message) end))
257
+ ns.Events.on("QUEST_TURNED_IN", ns.Snapshot.whenEnabled("xpGains", function(...) pcall(onQuestTurnedIn, ...) end))
258
+ ns.Events.on("PLAYER_XP_UPDATE", ns.Snapshot.whenEnabled("xpGains", function(unit) pcall(onXpUpdate, unit) end))