@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,412 @@
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
+ -- Dump of character state and the registry of its sections.
5
+ --
6
+ -- Two kinds of sections:
7
+ -- * collected on every dump (Snapshot.addSection), in registration order,
8
+ -- i.e. in file order in WowMcp.toc;
9
+ -- * collected when a window opens (Snapshot.addWindowSection): saved
10
+ -- immediately in the current dump, and carried over from the previous one on a plain dump.
11
+ -- Data recorded continuously (deaths, note) is carried over the same way.
12
+ -- A new section = a new file in Sections/ with one registration call.
13
+ --
14
+ -- The player can disable a section (/mcp off): such a section is not collected and disappears from the dump.
15
+
16
+ local _, ns = ...
17
+ local Store, Events, plain = ns.Store, ns.Events, ns.Compat.plain
18
+
19
+ local Snapshot = {}
20
+ ns.Snapshot = Snapshot
21
+
22
+ local ALWAYS_ON = "character" -- without it the dump does not say whose it is
23
+
24
+ local dumpSections = {} -- { name, collect } in dump order
25
+ local windowSections = {} -- names of window sections (and continuously collected data)
26
+ local windowCollectors = {} -- name -> collect (when the section has one simple read)
27
+ local quietSections = {} -- sections that do not remind about saving to disk
28
+ local capturedSinceSave = {} -- name -> true: window data that is not on disk yet
29
+ local histories = {} -- section name -> { name, key, max } (e.g. vendor -> vendors)
30
+ local dumpRefreshers = {} -- { name, refresh }: window sections that a dump refreshes with a cheap read
31
+ local changeListeners = {}
32
+ local unsaved = false -- dump from /mcp without /reload: in memory, but not on disk yet
33
+
34
+ function Snapshot.addSection(name, collect)
35
+ dumpSections[#dumpSections + 1] = { name = name, collect = collect }
36
+ end
37
+
38
+ -- Window section. With collect and events: each of these events stores the result of collect.
39
+ -- Without them the section handles its own events (e.g. recipes, auctions).
40
+ -- options.quiet: the data needs no reminder to save (probe, deaths, note).
41
+ -- options.history = { name, key, max }: a second section (e.g. vendors) with a result[key] -> result map
42
+ -- for the last few windows (e.g. vendors); the earliest visited fall off beyond max.
43
+ function Snapshot.addWindowSection(name, collect, events, options)
44
+ windowSections[#windowSections + 1] = name
45
+ windowCollectors[name] = collect
46
+ quietSections[name] = options and options.quiet or nil
47
+ local history = options and options.history
48
+ if history then
49
+ windowSections[#windowSections + 1] = history.name
50
+ histories[name] = history
51
+ end
52
+ for _, event in ipairs(events or {}) do
53
+ Events.on(event, function() Snapshot.capture(name) end)
54
+ end
55
+ end
56
+
57
+ -- Window section that every dump refreshes with a cheap read of client memory (e.g. whether the character is
58
+ -- in a guild): refresh(previous value) returns the new one. Never asks the game server for data.
59
+ function Snapshot.refreshOnDump(name, refresh)
60
+ dumpRefreshers[#dumpRefreshers + 1] = { name = name, refresh = refresh }
61
+ end
62
+
63
+ ---------------------------------------------------------------------------
64
+ -- Section names and disabling
65
+ ---------------------------------------------------------------------------
66
+
67
+ -- All section names that can reach the dump, alphabetically.
68
+ function Snapshot.sectionNames()
69
+ local names = {}
70
+ for _, section in ipairs(dumpSections) do names[#names + 1] = section.name end
71
+ for _, name in ipairs(windowSections) do names[#names + 1] = name end
72
+ table.sort(names)
73
+ return names
74
+ end
75
+
76
+ -- Canonical section name for player input (case-insensitive), or nil.
77
+ function Snapshot.resolveName(text)
78
+ local wanted = tostring(text or ""):lower()
79
+ for _, name in ipairs(Snapshot.sectionNames()) do
80
+ if name:lower() == wanted then return name end
81
+ end
82
+ return nil
83
+ end
84
+
85
+ function Snapshot.canDisable(name)
86
+ return name ~= ALWAYS_ON
87
+ end
88
+
89
+ function Snapshot.isDisabled(name)
90
+ return Snapshot.canDisable(name) and Store.isSectionDisabled(name)
91
+ end
92
+
93
+ -- Enables or disables a section. A disabled one also disappears from the current in-memory dump.
94
+ function Snapshot.setEnabled(name, enabled)
95
+ Store.setSectionDisabled(name, not enabled)
96
+ if not enabled then
97
+ Snapshot.current()[name] = nil
98
+ capturedSinceSave[name] = nil
99
+ end
100
+ end
101
+
102
+ -- Names of disabled sections (only existing ones), alphabetically.
103
+ function Snapshot.disabledNames()
104
+ local names = {}
105
+ for _, name in ipairs(Snapshot.sectionNames()) do
106
+ if Snapshot.isDisabled(name) then names[#names + 1] = name end
107
+ end
108
+ return names
109
+ end
110
+
111
+ -- Wraps a window section's event handler: a disabled section reads nothing.
112
+ function Snapshot.whenEnabled(name, handler)
113
+ return function(...)
114
+ if Snapshot.isDisabled(name) then return end
115
+ return handler(...)
116
+ end
117
+ end
118
+
119
+ ---------------------------------------------------------------------------
120
+ -- Window data and the reminder to save
121
+ ---------------------------------------------------------------------------
122
+
123
+ -- Dump of the character being played (a place for window section results).
124
+ Snapshot.current = Store.currentCharacter
125
+
126
+ -- The panel (and any other view) registers here to refresh after new window data.
127
+ function Snapshot.onWindowCaptured(listener)
128
+ changeListeners[#changeListeners + 1] = listener
129
+ end
130
+
131
+ -- Sections filled by windows that are not on disk yet (only the player saves).
132
+ function Snapshot.pendingWindowSections()
133
+ local names = {}
134
+ for name in pairs(capturedSinceSave) do names[#names + 1] = name end
135
+ table.sort(names)
136
+ return names
137
+ end
138
+
139
+ -- A window section has just stored data in the current dump.
140
+ function Snapshot.windowCaptured(name)
141
+ if quietSections[name] then return end
142
+ capturedSinceSave[name] = true
143
+ for _, listener in ipairs(changeListeners) do pcall(listener) end
144
+ end
145
+
146
+ -- Keeps at most max entries in the map; those with the oldest capturedAt fall off
147
+ -- (on equal times the key decides, so the result is reproducible).
148
+ function Snapshot.keepNewest(map, max)
149
+ local keys = {}
150
+ for key in pairs(map) do keys[#keys + 1] = key end
151
+ if #keys <= max then return end
152
+ table.sort(keys, function(a, b)
153
+ local ta, tb = map[a].capturedAt or 0, map[b].capturedAt or 0
154
+ if ta ~= tb then return ta > tb end
155
+ return tostring(a) < tostring(b)
156
+ end)
157
+ for i = max + 1, #keys do map[keys[i]] = nil end
158
+ end
159
+
160
+ -- Appends a window result to a section with history (e.g. vendors), unless that one is disabled.
161
+ -- The key (NPC name) goes through plain: a client "secret" cannot be a table key
162
+ -- nor be compared, so without a plain value no history entry is created.
163
+ local function rememberInHistory(name, result)
164
+ local history = histories[name]
165
+ if not history or Snapshot.isDisabled(history.name) then return end
166
+ local key = plain(result[history.key])
167
+ if type(key) ~= "string" or key == "" then return end
168
+ local current = Snapshot.current()
169
+ if type(current[history.name]) ~= "table" then current[history.name] = {} end
170
+ current[history.name][key] = result
171
+ Snapshot.keepNewest(current[history.name], history.max)
172
+ -- The reminder to save is already handled by the main section (vendor, trainer).
173
+ end
174
+
175
+ -- Stores a window section's result in the character's current dump.
176
+ function Snapshot.capture(name)
177
+ if Snapshot.isDisabled(name) or not windowCollectors[name] then return end
178
+ local ok, result = pcall(windowCollectors[name])
179
+ if ok and result then
180
+ Snapshot.current()[name] = result
181
+ Snapshot.windowCaptured(name)
182
+ pcall(rememberInHistory, name, result)
183
+ end
184
+ end
185
+
186
+ ---------------------------------------------------------------------------
187
+ -- Periods of continuously collected data (loot, XP)
188
+ ---------------------------------------------------------------------------
189
+
190
+ -- Whether the saved dump ends with a skipped logout dump (the client already had empty data):
191
+ -- then the disk holds an old dump, and the loot and XP period continues from it.
192
+ function Snapshot.continuesPeriod(saved)
193
+ return type(saved) == "table" and type(saved.lastSkippedLogoutAt) == "number"
194
+ and type(saved.dumpedAt) == "number" and saved.lastSkippedLogoutAt >= saved.dumpedAt
195
+ end
196
+
197
+ -- Closed periods (addon 0.12.0). The addon never assumes the server read a dump: the game may save twice
198
+ -- (/mcp reload, then logout) before the server looks at the file, and the second save would otherwise
199
+ -- replace the first period's loot and XP. So when a login closes a period that has content, the period
200
+ -- (its fields plus until = the dumpedAt of the save that closed it) is appended to the section's
201
+ -- "periods" list, oldest first, and carried on. The list is bounded by count and by age; empty periods
202
+ -- are not stored.
203
+ Snapshot.MAX_PERIODS = 20
204
+ Snapshot.MAX_PERIOD_AGE = 7 * 24 * 3600
205
+
206
+ -- The carried periods that are still within the bounds at time now: tables with a numeric until not older
207
+ -- than MAX_PERIOD_AGE, at most MAX_PERIODS of the newest. Returns a new list (possibly empty).
208
+ function Snapshot.keptPeriods(periods, now)
209
+ local kept = {}
210
+ if type(periods) ~= "table" then return kept end
211
+ for _, period in ipairs(periods) do
212
+ local ended = type(period) == "table" and period["until"]
213
+ if type(ended) == "number" and now - ended <= Snapshot.MAX_PERIOD_AGE then kept[#kept + 1] = period end
214
+ end
215
+ while #kept > Snapshot.MAX_PERIODS do table.remove(kept, 1) end
216
+ return kept
217
+ end
218
+
219
+ -- Closes the saved section's period at time ended: returns the carried list with the closed period appended
220
+ -- when hasContent(section) says it holds anything, bounded by keptPeriods; nil when the list is empty.
221
+ function Snapshot.closePeriod(section, ended, hasContent, now)
222
+ if type(section) ~= "table" then return nil end
223
+ local periods = section.periods
224
+ section.periods = nil
225
+ if type(section.since) == "number" and hasContent(section) then
226
+ section["until"] = ended
227
+ periods = type(periods) == "table" and periods or {}
228
+ periods[#periods + 1] = section
229
+ end
230
+ local kept = Snapshot.keptPeriods(periods, now)
231
+ if #kept == 0 then return nil end
232
+ return kept
233
+ end
234
+
235
+ -- After login (and after /reload) starts a new period of section name: the previous dump is already
236
+ -- on disk, so the section starts from zero with since = its dumpedAt. fresh(since) creates an empty section,
237
+ -- and the closed period goes to the section's periods list when hasContent(section) is true.
238
+ -- When the previous logout dump was skipped, the period continues (with its periods). Returns since or nil.
239
+ function Snapshot.startPeriod(name, fresh, hasContent)
240
+ local saved = Store.savedCurrentCharacter()
241
+ local previous = saved and type(saved[name]) == "table" and saved[name] or nil
242
+ if Snapshot.continuesPeriod(saved) and previous and type(previous.since) == "number" then
243
+ local kept = Snapshot.keptPeriods(previous.periods, time())
244
+ previous.periods = #kept > 0 and kept or nil
245
+ return previous.since
246
+ end
247
+ local since = (saved and type(saved.dumpedAt) == "number" and saved.dumpedAt) or time()
248
+ if Snapshot.isDisabled(name) then
249
+ if saved then saved[name] = nil end
250
+ return since
251
+ end
252
+ local section = fresh(since)
253
+ section.periods = Snapshot.closePeriod(previous, since, hasContent, time())
254
+ Snapshot.current()[name] = section
255
+ return since
256
+ end
257
+
258
+ ---------------------------------------------------------------------------
259
+ -- Dump
260
+ ---------------------------------------------------------------------------
261
+
262
+ local function collectSections(snapshot, recorder)
263
+ for _, section in ipairs(dumpSections) do
264
+ if not Snapshot.isDisabled(section.name) then
265
+ local ok, result = recorder.measure(section.name, section.collect)
266
+ if ok then
267
+ snapshot[section.name] = result
268
+ else
269
+ snapshot.errors[#snapshot.errors + 1] = section.name .. ": " .. tostring(result)
270
+ end
271
+ end
272
+ end
273
+ end
274
+
275
+ local function keepWindowSections(snapshot, previous)
276
+ for _, name in ipairs(windowSections) do
277
+ if not Snapshot.isDisabled(name) then
278
+ snapshot[name] = previous and previous[name] or nil
279
+ end
280
+ end
281
+ for _, r in ipairs(dumpRefreshers) do
282
+ if not Snapshot.isDisabled(r.name) then
283
+ local ok, value = pcall(r.refresh, snapshot[r.name])
284
+ if ok then
285
+ snapshot[r.name] = value
286
+ else
287
+ snapshot.errors[#snapshot.errors + 1] = r.name .. ": " .. tostring(value)
288
+ end
289
+ end
290
+ end
291
+ end
292
+
293
+ ---------------------------------------------------------------------------
294
+ -- Dump from a client that has already unloaded character data
295
+ ---------------------------------------------------------------------------
296
+
297
+ local function isEmptyList(value)
298
+ return type(value) == "table" and next(value) == nil
299
+ end
300
+
301
+ local function hasNoBagSpace(bags)
302
+ if type(bags) ~= "table" then return false end
303
+ for _, bag in ipairs(bags) do
304
+ if type(bag) == "table" and type(bag.size) == "number" and bag.size > 0 then return false end
305
+ end
306
+ return true
307
+ end
308
+
309
+ -- The four sections whose emptiness says the client has unloaded the character. The guard reads them
310
+ -- even when the player turned them off with /mcp off (see guardView), so it never depends on that choice.
311
+ local GUARD_SECTIONS = { "stats", "spells", "equipment", "bags" }
312
+
313
+ -- How many empty-client signs the dump has.
314
+ function Snapshot.unloadedSignals(snapshot)
315
+ local signals = 0
316
+ if type(snapshot.stats) == "table" and not (type(snapshot.stats.healthMax) == "number" and snapshot.stats.healthMax > 0) then
317
+ signals = signals + 1
318
+ end
319
+ if isEmptyList(snapshot.spells) then signals = signals + 1 end
320
+ if isEmptyList(snapshot.equipment) then signals = signals + 1 end
321
+ if hasNoBagSpace(snapshot.bags) then signals = signals + 1 end
322
+ return signals
323
+ end
324
+
325
+ local function collectorOf(name)
326
+ for _, section in ipairs(dumpSections) do
327
+ if section.name == name then return section.collect end
328
+ end
329
+ return nil
330
+ end
331
+
332
+ -- The sections the guard looks at: the dump's own, and for a disabled one a fresh read that is only used for the
333
+ -- check (never stored in the dump). A section that errored simply gives no sign, as before.
334
+ local function guardView(snapshot)
335
+ local view = {}
336
+ for _, name in ipairs(GUARD_SECTIONS) do
337
+ view[name] = snapshot[name]
338
+ local collect = collectorOf(name)
339
+ if view[name] == nil and collect and Snapshot.isDisabled(name) then
340
+ local ok, result = pcall(collect)
341
+ if ok then view[name] = result end
342
+ end
343
+ end
344
+ return view
345
+ end
346
+
347
+ -- On a real exit from the game (and logout to the menu) PLAYER_LOGOUT arrives once the client has
348
+ -- already unloaded the character: zero health, no spells, equipment or bags. Two signs are enough;
349
+ -- a real character has none (it always has a backpack, health and Attack in the spellbook).
350
+ -- Independent of /mcp off: a disabled sign section is read for the check only.
351
+ function Snapshot.looksUnloaded(snapshot)
352
+ return Snapshot.unloadedSignals(guardView(snapshot)) >= 2
353
+ end
354
+
355
+ -- Last pass before the game writes SavedVariables: drops from the character's saved dump anything that is not a
356
+ -- plain value (Compat.sanitizeTree). Runs at the end of every dump and once more at logout, after window data
357
+ -- captured since the last dump.
358
+ function Snapshot.sanitizeCurrent()
359
+ local saved = Store.savedCurrentCharacter()
360
+ if type(saved) == "table" then ns.Compat.sanitizeTree(saved) end
361
+ end
362
+
363
+ local function appendEventErrors(snapshot)
364
+ for _, text in ipairs(Events.errors()) do
365
+ snapshot.errors[#snapshot.errors + 1] = text
366
+ end
367
+ end
368
+
369
+ -- Collects all enabled sections and saves the character's dump in WowMcpDB.
370
+ -- A logout dump from an empty client does not overwrite the previous one: that one stays (with window data,
371
+ -- loot and XP collected since then) and only gets the lastSkippedLogoutAt marker.
372
+ -- Returns the saved dump and true when the new one was skipped.
373
+ function Snapshot.dump(reason)
374
+ local key = Store.characterKey()
375
+ local previous = Store.openCharacters()[key]
376
+ local snapshot = { errors = {} }
377
+ local recorder = ns.Timing.newRecorder()
378
+ collectSections(snapshot, recorder)
379
+ snapshot.timings = recorder.report()
380
+
381
+ if reason == "logout" and Snapshot.looksUnloaded(snapshot) then
382
+ if type(previous) == "table" and type(previous.dumpedAt) == "number" then
383
+ previous.lastSkippedLogoutAt = time()
384
+ Store.saveCharacter(key, previous)
385
+ pcall(Snapshot.sanitizeCurrent)
386
+ return previous, true
387
+ end
388
+ snapshot.clientUnloaded = true -- nothing to keep: we save, but with a warning
389
+ end
390
+
391
+ appendEventErrors(snapshot)
392
+ keepWindowSections(snapshot, previous)
393
+
394
+ local disabled = Snapshot.disabledNames()
395
+ snapshot.disabledSections = #disabled > 0 and disabled or nil
396
+ snapshot.dumpedAt = time()
397
+ snapshot.dumpedAtText = date("%Y-%m-%d %H:%M:%S")
398
+ snapshot.reason = reason
399
+ snapshot.addonVersion = ns.ADDON_VERSION
400
+
401
+ Store.saveCharacter(key, snapshot)
402
+ pcall(Snapshot.sanitizeCurrent)
403
+ return snapshot, false
404
+ end
405
+
406
+ function Snapshot.markUnsaved()
407
+ unsaved = true
408
+ end
409
+
410
+ function Snapshot.isUnsaved()
411
+ return unsaved
412
+ end
@@ -0,0 +1,142 @@
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
+ -- SavedVariables (WowMcpDB): character dumps and settings.
5
+ -- The game replaces WowMcpDB after the addon files load, so every
6
+ -- function reads the global at call time and never caches it.
7
+
8
+ local _, ns = ...
9
+
10
+ WowMcpDB = WowMcpDB or {}
11
+
12
+ local Store = {}
13
+ ns.Store = Store
14
+
15
+ local sessionKey
16
+
17
+ local function realmName()
18
+ for _, name in ipairs({ "GetRealmName", "GetNormalizedRealmName" }) do
19
+ local read = _G[name]
20
+ if type(read) == "function" then
21
+ local ok, realm = pcall(read)
22
+ if ok and type(realm) == "string" and realm ~= "" then return realm end
23
+ end
24
+ end
25
+ return nil
26
+ end
27
+
28
+ local function playerTokens()
29
+ local _, classToken = UnitClass("player")
30
+ local _, raceToken = UnitRace("player")
31
+ return classToken, raceToken
32
+ end
33
+
34
+ -- Some client builds return no realm name (seen in Forever build 71037). The character must then keep the
35
+ -- key it already has, or its history splits and a character with the same name on another realm overwrites
36
+ -- it. Returns the one saved "Name-Realm" key whose dump has the player's class and race, or nil when there is
37
+ -- none or more than one.
38
+ local function savedRealmKey(name)
39
+ local characters = WowMcpDB.characters
40
+ if type(characters) ~= "table" then return nil end
41
+ local classToken, raceToken = playerTokens()
42
+ if not classToken then return nil end
43
+ local prefix, found = name .. "-", nil
44
+ for key, dump in pairs(characters) do
45
+ local character = type(dump) == "table" and dump.character
46
+ if type(key) == "string" and key:sub(1, #prefix) == prefix and type(character) == "table"
47
+ and character.classToken == classToken and character.raceToken == raceToken then
48
+ if found then return nil end
49
+ found = key
50
+ end
51
+ end
52
+ return found
53
+ end
54
+
55
+ -- Character key in WowMcpDB.characters: "Name-Realm"; when the client gives no realm name, the saved
56
+ -- key of this character (savedRealmKey), and only then just the name.
57
+ function Store.characterKey()
58
+ if sessionKey then return sessionKey end
59
+ local name = UnitName("player") or "unknown"
60
+ local realm = realmName()
61
+ local key = realm and (name .. "-" .. realm) or savedRealmKey(name)
62
+ -- Remembered for the session: at logout the client may no longer tell the class, and the key must not change.
63
+ sessionKey = key
64
+ return key or name
65
+ end
66
+
67
+ -- Dumps saved under the bare name while the client gave no realm name belong to the "Name-Realm" key: the
68
+ -- newer of the two dumps stays under that key and the bare entry is removed. Does nothing for another
69
+ -- character with the same name (class or race differs). Returns the key that received the dump, or nil.
70
+ function Store.adoptRealmlessDump()
71
+ local characters = WowMcpDB.characters
72
+ local name = UnitName("player")
73
+ if type(characters) ~= "table" or type(name) ~= "string" then return nil end
74
+ local key, bare = Store.characterKey(), characters[name]
75
+ if key == name or type(bare) ~= "table" then return nil end
76
+ local classToken, raceToken = playerTokens()
77
+ local character = bare.character
78
+ if type(character) ~= "table" or character.classToken ~= classToken or character.raceToken ~= raceToken then
79
+ return nil
80
+ end
81
+ local kept = characters[key]
82
+ local bareAt = type(bare.dumpedAt) == "number" and bare.dumpedAt or 0
83
+ local keptAt = type(kept) == "table" and type(kept.dumpedAt) == "number" and kept.dumpedAt or 0
84
+ if type(kept) ~= "table" or bareAt > keptAt then characters[key] = bare end
85
+ characters[name] = nil
86
+ if WowMcpDB.last == name then WowMcpDB.last = key end
87
+ return key
88
+ end
89
+
90
+ -- Dump of the character being played; creates an empty one if none exists yet.
91
+ function Store.currentCharacter()
92
+ local key = Store.characterKey()
93
+ WowMcpDB.characters = WowMcpDB.characters or {}
94
+ WowMcpDB.characters[key] = WowMcpDB.characters[key] or {}
95
+ return WowMcpDB.characters[key]
96
+ end
97
+
98
+ -- Prepares WowMcpDB for a new dump; returns the character key -> dump map.
99
+ function Store.openCharacters()
100
+ WowMcpDB.schema = ns.SCHEMA_VERSION
101
+ WowMcpDB.characters = WowMcpDB.characters or {}
102
+ return WowMcpDB.characters
103
+ end
104
+
105
+ function Store.saveCharacter(key, snapshot)
106
+ WowMcpDB.characters[key] = snapshot
107
+ WowMcpDB.last = key
108
+ end
109
+
110
+ -- Saved dump of the character being played, or nil; creates nothing.
111
+ function Store.savedCurrentCharacter()
112
+ return WowMcpDB.characters and WowMcpDB.characters[Store.characterKey()]
113
+ end
114
+
115
+ -- Key and dump of the character from the last dump on this account.
116
+ function Store.lastSaved()
117
+ local key = WowMcpDB.last
118
+ return key, key and WowMcpDB.characters and WowMcpDB.characters[key]
119
+ end
120
+
121
+ -- Settings for the panel, played time and disabled sections (panelHidden, panelPoint, played, disabledSections).
122
+ function Store.settings()
123
+ if type(WowMcpDB.settings) ~= "table" then WowMcpDB.settings = {} end
124
+ return WowMcpDB.settings
125
+ end
126
+
127
+ -- Whether the section is disabled by /mcp off. Creates nothing in settings.
128
+ function Store.isSectionDisabled(name)
129
+ local disabled = Store.settings().disabledSections
130
+ return type(disabled) == "table" and disabled[name] == true
131
+ end
132
+
133
+ function Store.setSectionDisabled(name, disabled)
134
+ local settings = Store.settings()
135
+ if disabled then
136
+ if type(settings.disabledSections) ~= "table" then settings.disabledSections = {} end
137
+ settings.disabledSections[name] = true
138
+ elseif type(settings.disabledSections) == "table" then
139
+ settings.disabledSections[name] = nil
140
+ if next(settings.disabledSections) == nil then settings.disabledSections = nil end
141
+ end
142
+ end