-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSynStatExport.lua
More file actions
341 lines (293 loc) · 9.4 KB
/
SynStatExport.lua
File metadata and controls
341 lines (293 loc) · 9.4 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
local addonName = ...
SynStatExportDB = SynStatExportDB or {
lastExport = nil,
history = {},
}
local EXPORT_HEADER = "!PESTATS!"
local MAX_HISTORY = 10
local ADDON_TITLE = "Synergy Loadout Master"
local CHAT_PREFIX = "|cff66d9efSLM Stat Export:|r "
local popupFrame = nil
local popupEditBox = nil
local function ensureDb()
SynStatExportDB = SynStatExportDB or {}
SynStatExportDB.lastExport = SynStatExportDB.lastExport or nil
SynStatExportDB.history = SynStatExportDB.history or {}
end
local function chat(message)
if DEFAULT_CHAT_FRAME and DEFAULT_CHAT_FRAME.AddMessage then
DEFAULT_CHAT_FRAME:AddMessage(CHAT_PREFIX .. tostring(message))
end
end
local function ensurePopup()
if popupFrame then
return popupFrame, popupEditBox
end
local frame = CreateFrame("Frame", "SynStatExportPopup", UIParent)
frame:SetWidth(720)
frame:SetHeight(220)
frame:SetPoint("CENTER")
frame:SetFrameStrata("DIALOG")
frame:EnableMouse(true)
frame:SetMovable(true)
frame:RegisterForDrag("LeftButton")
frame:SetScript("OnDragStart", function(self)
self:StartMoving()
end)
frame:SetScript("OnDragStop", function(self)
self:StopMovingOrSizing()
end)
frame:SetBackdrop({
bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
tile = true,
tileSize = 32,
edgeSize = 32,
insets = { left = 8, right = 8, top = 8, bottom = 8 },
})
frame:SetBackdropColor(0, 0, 0, 1)
frame:Hide()
local title = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
title:SetPoint("TOP", 0, -16)
title:SetText(ADDON_TITLE .. " - Stat Export")
local subtitle = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
subtitle:SetPoint("TOPLEFT", 20, -42)
subtitle:SetPoint("TOPRIGHT", -20, -42)
subtitle:SetJustifyH("LEFT")
subtitle:SetText("The export string is selected automatically. Click inside the field and press Ctrl+C if needed.")
local scrollFrame = CreateFrame("ScrollFrame", "SynStatExportPopupScroll", frame, "UIPanelScrollFrameTemplate")
scrollFrame:SetPoint("TOPLEFT", 20, -66)
scrollFrame:SetPoint("BOTTOMRIGHT", -44, 52)
local editBox = CreateFrame("EditBox", "SynStatExportPopupEditBox", scrollFrame)
editBox:SetMultiLine(true)
editBox:SetFontObject(ChatFontNormal)
editBox:SetWidth(620)
editBox:SetAutoFocus(false)
editBox:EnableMouse(true)
editBox:SetScript("OnEscapePressed", function()
frame:Hide()
end)
editBox:SetScript("OnTextChanged", function(self)
scrollFrame:UpdateScrollChildRect()
end)
editBox:SetScript("OnEditFocusGained", function(self)
self:HighlightText()
end)
scrollFrame:SetScrollChild(editBox)
local closeButton = CreateFrame("Button", nil, frame, "UIPanelCloseButton")
closeButton:SetPoint("TOPRIGHT", -6, -6)
local selectButton = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
selectButton:SetWidth(110)
selectButton:SetHeight(24)
selectButton:SetPoint("BOTTOMRIGHT", -20, 18)
selectButton:SetText("Select Text")
selectButton:SetScript("OnClick", function()
editBox:SetFocus()
editBox:HighlightText()
end)
local hint = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
hint:SetPoint("BOTTOMLEFT", 20, 24)
hint:SetText("Use /slmstats to export or /slmshow to reopen the latest string.")
popupFrame = frame
popupEditBox = editBox
return popupFrame, popupEditBox
end
local function showSharePopup(shareString)
local frame, editBox = ensurePopup()
frame:Show()
editBox:SetText(tostring(shareString or ""))
editBox:SetFocus()
editBox:HighlightText()
end
local function clampNumber(value, fallback)
local numberValue = tonumber(value)
if numberValue == nil then
return fallback or 0
end
return numberValue
end
local function round(value, precision)
local numberValue = clampNumber(value, 0)
local factor = 10 ^ (precision or 0)
return math.floor(numberValue * factor + 0.5) / factor
end
local function escapeJsonString(value)
local input = tostring(value or "")
input = input:gsub("\\", "\\\\")
input = input:gsub("\"", "\\\"")
input = input:gsub("\n", "\\n")
input = input:gsub("\r", "\\r")
input = input:gsub("\t", "\\t")
return input
end
local function encodeJsonValue(value)
local valueType = type(value)
if valueType == "nil" then
return "null"
end
if valueType == "boolean" then
return value and "true" or "false"
end
if valueType == "number" then
if value ~= value or value == math.huge or value == -math.huge then
return "0"
end
return tostring(value)
end
if valueType == "string" then
return "\"" .. escapeJsonString(value) .. "\""
end
if valueType ~= "table" then
return "null"
end
local parts = {}
for key, nestedValue in pairs(value) do
parts[#parts + 1] = {
key = tostring(key),
value = encodeJsonValue(nestedValue),
}
end
table.sort(parts, function(left, right)
return left.key < right.key
end)
local encoded = {}
for index = 1, #parts do
encoded[#encoded + 1] = "\"" .. escapeJsonString(parts[index].key) .. "\":" .. parts[index].value
end
return "{" .. table.concat(encoded, ",") .. "}"
end
local function getUnitStatValue(unit, index)
local base = select(1, UnitStat(unit, index))
return clampNumber(base, 0)
end
local function getAttackPower()
local base, positiveBuff, negativeBuff = UnitAttackPower("player")
return clampNumber(base, 0) + clampNumber(positiveBuff, 0) + clampNumber(negativeBuff, 0)
end
local function getSpellPower()
if type(GetSpellBonusHealing) == "function" then
local healing = clampNumber(GetSpellBonusHealing(), 0)
if healing > 0 then
return healing
end
end
if type(GetSpellBonusDamage) == "function" then
local highest = 0
for school = 1, 7 do
highest = math.max(highest, clampNumber(GetSpellBonusDamage(school), 0))
end
return highest
end
return 0
end
local function getArmor()
local _, effectiveArmor = UnitArmor("player")
return clampNumber(effectiveArmor, 0)
end
local function getCritChance()
if type(GetCritChance) == "function" then
return round(GetCritChance(), 2)
end
return 0
end
local function getHaste()
if type(GetHaste) == "function" then
return round(GetHaste(), 2)
end
return 0
end
local function getHitChance()
if type(GetHitModifier) == "function" then
return round(GetHitModifier(), 2)
end
return 0
end
local function getResilience()
if type(GetCombatRatingBonus) == "function" and type(CR_CRIT_TAKEN_MELEE) == "number" then
return round(GetCombatRatingBonus(CR_CRIT_TAKEN_MELEE), 2)
end
return 0
end
local function collectStats()
return {
level = clampNumber(UnitLevel("player"), 1),
sp = round(getSpellPower(), 2),
ap = round(getAttackPower(), 2),
armor = round(getArmor(), 2),
stamina = clampNumber(getUnitStatValue("player", 3), 0),
str = clampNumber(getUnitStatValue("player", 1), 0),
agi = clampNumber(getUnitStatValue("player", 2), 0),
sta = clampNumber(getUnitStatValue("player", 3), 0),
int = clampNumber(getUnitStatValue("player", 4), 0),
spi = clampNumber(getUnitStatValue("player", 5), 0),
crit = getCritChance(),
haste = getHaste(),
hit = getHitChance(),
resil = getResilience(),
}
end
local function saveExport(stats)
ensureDb()
local payload = {
version = 1,
exportedAt = date("%Y-%m-%d %H:%M:%S"),
character = UnitName("player") or "",
realm = GetRealmName and GetRealmName() or "",
class = select(2, UnitClass("player")) or "",
stats = stats,
}
payload.share = EXPORT_HEADER .. encodeJsonValue(payload)
SynStatExportDB.lastExport = payload
table.insert(SynStatExportDB.history, 1, payload)
while #SynStatExportDB.history > MAX_HISTORY do
table.remove(SynStatExportDB.history)
end
return payload
end
local function exportStats()
local payload = saveExport(collectStats())
chat("Stats exported.")
chat("Share string saved to SavedVariables under SynStatExportDB.lastExport.share")
chat(string.format("Level %d | SP %s | AP %s | Crit %s | Haste %s", payload.stats.level, payload.stats.sp, payload.stats.ap, payload.stats.crit, payload.stats.haste))
showSharePopup(payload.share)
end
local function showLastExport()
ensureDb()
if not SynStatExportDB.lastExport then
chat("No export available yet. Use /slmstats first.")
return
end
local exportData = SynStatExportDB.lastExport
chat("Last export: " .. tostring(exportData.exportedAt or "unknown"))
chat("Character: " .. tostring(exportData.character or "") .. " - " .. tostring(exportData.realm or ""))
showSharePopup(exportData.share)
end
local function printHelp()
chat("Commands:")
chat("/slmstats - Collect current character stats and open the export popup")
chat("/slmshow - Reopen the latest export popup")
chat("/slmstats help - Show this help")
end
SLASH_SLMSTATEXPORT1 = "/slmstats"
SlashCmdList["SLMSTATEXPORT"] = function(message)
local command = string.lower((message or ""):match("^%s*(.-)%s*$") or "")
if command == "" or command == "export" then
exportStats()
return
end
if command == "help" then
printHelp()
return
end
chat("Unknown command: " .. tostring(command))
printHelp()
end
SLASH_SLMSHOW1 = "/slmshow"
SlashCmdList["SLMSHOW"] = function()
ensureDb()
if not SynStatExportDB.lastExport or not SynStatExportDB.lastExport.share then
chat("No export available yet. Use /slmstats first.")
return
end
showLastExport()
end