Skip to content

Commit 89d99f2

Browse files
committed
move common calc format code between CompareTab and CalcSectionControl to shared module
1 parent d8a169c commit 89d99f2

4 files changed

Lines changed: 81 additions & 131 deletions

File tree

src/Classes/CalcSectionControl.lua

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -157,64 +157,6 @@ function CalcSectionClass:UpdatePos()
157157
end
158158
end
159159

160-
function CalcSectionClass:FormatVal(val, p)
161-
return formatNumSep(tostring(round(val, p)))
162-
end
163-
164-
function CalcSectionClass:FormatStr(str, actor, colData)
165-
str = str:gsub("{output:([%a%.:]+)}", function(c)
166-
local ns, var = c:match("^(%a+)%.(%a+)$")
167-
if ns then
168-
return actor.output[ns] and actor.output[ns][var] or ""
169-
else
170-
return actor.output[c] or ""
171-
end
172-
end)
173-
str = str:gsub("{(%d+):output:([%a%.:]+)}", function(p, c)
174-
local ns, var = c:match("^(%a+)%.(%a+)$")
175-
if ns then
176-
return self:FormatVal(actor.output[ns] and actor.output[ns][var] or 0, tonumber(p))
177-
else
178-
return self:FormatVal(actor.output[c] or 0, tonumber(p))
179-
end
180-
end)
181-
str = str:gsub("{(%d+):mod:([%d,]+)}", function(p, n)
182-
local numList = { }
183-
for num in n:gmatch("%d+") do
184-
t_insert(numList, tonumber(num))
185-
end
186-
local modType = colData[numList[1]].modType
187-
local modTotal = modType == "MORE" and 1 or 0
188-
for _, num in ipairs(numList) do
189-
local sectionData = colData[num]
190-
local modCfg = (sectionData.cfg and actor.mainSkill[sectionData.cfg.."Cfg"]) or { }
191-
if sectionData.modSource then
192-
modCfg.source = sectionData.modSource
193-
end
194-
if sectionData.actor then
195-
modCfg.actor = sectionData.actor
196-
end
197-
local modVal
198-
local modStore = (sectionData.enemy and actor.enemy.modDB) or (sectionData.cfg and actor.mainSkill.skillModList) or actor.modDB
199-
if type(sectionData.modName) == "table" then
200-
modVal = modStore:Combine(sectionData.modType, modCfg, unpack(sectionData.modName))
201-
else
202-
modVal = modStore:Combine(sectionData.modType, modCfg, sectionData.modName)
203-
end
204-
if modType == "MORE" then
205-
modTotal = modTotal * modVal
206-
else
207-
modTotal = modTotal + modVal
208-
end
209-
end
210-
if modType == "MORE" then
211-
modTotal = (modTotal - 1) * 100
212-
end
213-
return self:FormatVal(modTotal, tonumber(p))
214-
end)
215-
return str
216-
end
217-
218160
function CalcSectionClass:Draw(viewPort, noTooltip)
219161
local x, y = self:GetPos()
220162
local width, height = self:GetSize()
@@ -245,7 +187,7 @@ function CalcSectionClass:Draw(viewPort, noTooltip)
245187
DrawString(x + 3, lineY + 3, "LEFT", 16, "VAR BOLD", textColor..subSec.label..":")
246188
if subSec.data.extra then
247189
local x = x + 3 + DrawStringWidth(16, "VAR BOLD", subSec.label) + 10
248-
DrawString(x, lineY + 3, "LEFT", 16, "VAR", "^7"..self:FormatStr(subSec.data.extra, actor))
190+
DrawString(x, lineY + 3, "LEFT", 16, "VAR", "^7"..formatCalcStr(subSec.data.extra, actor))
249191
end
250192
end
251193
-- Draw line below label
@@ -300,7 +242,7 @@ function CalcSectionClass:Draw(viewPort, noTooltip)
300242
end
301243
local textSize = rowData.textSize or 14
302244
SetViewport(colData.x + 3, colData.y, colData.width - 4, colData.height)
303-
DrawString(1, 9 - textSize/2, "LEFT", textSize, "VAR", "^7"..self:FormatStr(colData.format, actor, colData))
245+
DrawString(1, 9 - textSize/2, "LEFT", textSize, "VAR", "^7"..formatCalcStr(colData.format, actor, colData))
304246
SetViewport()
305247
end
306248
end

src/Classes/CompareTab.lua

Lines changed: 4 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,73 +1030,6 @@ function CompareTabClass:GetShortBuildName(fullName)
10301030
return fullName
10311031
end
10321032

1033-
-- Format a numeric value with separator and rounding
1034-
function CompareTabClass:FormatVal(val, p)
1035-
return formatNumSep(tostring(round(val, p)))
1036-
end
1037-
1038-
-- Resolve format strings against an actor's output/modDB
1039-
-- Handles: {output:Key}, {p:output:Key}, {p:mod:indices}
1040-
function CompareTabClass:FormatStr(str, actor, colData)
1041-
if not actor then return "" end
1042-
str = str:gsub("{output:([%a%.:]+)}", function(c)
1043-
local ns, var = c:match("^(%a+)%.(%a+)$")
1044-
if ns then
1045-
return actor.output[ns] and actor.output[ns][var] or ""
1046-
else
1047-
return actor.output[c] or ""
1048-
end
1049-
end)
1050-
str = str:gsub("{(%d+):output:([%a%.:]+)}", function(p, c)
1051-
local ns, var = c:match("^(%a+)%.(%a+)$")
1052-
if ns then
1053-
return self:FormatVal(actor.output[ns] and actor.output[ns][var] or 0, tonumber(p))
1054-
else
1055-
return self:FormatVal(actor.output[c] or 0, tonumber(p))
1056-
end
1057-
end)
1058-
str = str:gsub("{(%d+):mod:([%d,]+)}", function(p, n)
1059-
local numList = { }
1060-
for num in n:gmatch("%d+") do
1061-
t_insert(numList, tonumber(num))
1062-
end
1063-
if not colData[numList[1]] or not colData[numList[1]].modType then
1064-
return "?"
1065-
end
1066-
local modType = colData[numList[1]].modType
1067-
local modTotal = modType == "MORE" and 1 or 0
1068-
for _, num in ipairs(numList) do
1069-
local sectionData = colData[num]
1070-
if not sectionData then break end
1071-
local modCfg = (sectionData.cfg and actor.mainSkill and actor.mainSkill[sectionData.cfg.."Cfg"]) or { }
1072-
if sectionData.modSource then
1073-
modCfg.source = sectionData.modSource
1074-
end
1075-
if sectionData.actor then
1076-
modCfg.actor = sectionData.actor
1077-
end
1078-
local modVal
1079-
local modStore = (sectionData.enemy and actor.enemy and actor.enemy.modDB) or (sectionData.cfg and actor.mainSkill and actor.mainSkill.skillModList) or actor.modDB
1080-
if not modStore then break end
1081-
if type(sectionData.modName) == "table" then
1082-
modVal = modStore:Combine(sectionData.modType, modCfg, unpack(sectionData.modName))
1083-
else
1084-
modVal = modStore:Combine(sectionData.modType, modCfg, sectionData.modName)
1085-
end
1086-
if modType == "MORE" then
1087-
modTotal = modTotal * modVal
1088-
else
1089-
modTotal = modTotal + modVal
1090-
end
1091-
end
1092-
if modType == "MORE" then
1093-
modTotal = (modTotal - 1) * 100
1094-
end
1095-
return self:FormatVal(modTotal, tonumber(p))
1096-
end)
1097-
return str
1098-
end
1099-
11001033
-- Populate a set-selector dropdown from a tab's ordered set list.
11011034
-- tab: the tab object (e.g. itemsTab, skillsTab, configTab)
11021035
-- orderListField/setsField/activeIdField: string keys on tab
@@ -4399,8 +4332,8 @@ function CompareTabClass:DrawCalcs(vp, compareEntry)
43994332
if subSec.data and subSec.data.extra then
44004333
local extraTextW = DrawStringWidth(16, "VAR BOLD", subSec.label .. ":")
44014334
local extraX = x + 3 + extraTextW + 8
4402-
local ok1, pExtra = pcall(self.FormatStr, self, subSec.data.extra, primaryActor)
4403-
local ok2, cExtra = pcall(self.FormatStr, self, subSec.data.extra, compareActor)
4335+
local ok1, pExtra = pcall(formatCalcStr, subSec.data.extra, primaryActor)
4336+
local ok2, cExtra = pcall(formatCalcStr, subSec.data.extra, compareActor)
44044337
if ok1 and ok2 then
44054338
DrawString(extraX, lineY + 3, "LEFT", 16, "VAR",
44064339
colorCodes.POSITIVE .. pExtra .. " ^8| " .. colorCodes.WARNING .. cExtra)
@@ -4452,7 +4385,7 @@ function CompareTabClass:DrawCalcs(vp, compareEntry)
44524385
DrawImage(nil, x + valCol1X, lineY, valColWidth, 18)
44534386
end
44544387
if colData and colData.format then
4455-
local ok, str = pcall(self.FormatStr, self, colData.format, primaryActor, colData)
4388+
local ok, str = pcall(formatCalcStr, colData.format, primaryActor, colData)
44564389
if ok and str then
44574390
DrawString(x + valCol1X + 2, lineY + 9 - textSize / 2, "LEFT", textSize, "VAR", "^7" .. str)
44584391
end
@@ -4466,7 +4399,7 @@ function CompareTabClass:DrawCalcs(vp, compareEntry)
44664399
DrawImage(nil, x + valCol2X, lineY, valColWidth, 18)
44674400
end
44684401
if colData and colData.format then
4469-
local ok, str = pcall(self.FormatStr, self, colData.format, compareActor, colData)
4402+
local ok, str = pcall(formatCalcStr, colData.format, compareActor, colData)
44704403
if ok and str then
44714404
DrawString(x + valCol2X + 2, lineY + 9 - textSize / 2, "LEFT", textSize, "VAR", "^7" .. str)
44724405
end

src/Modules/CalcFormat.lua

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
-- Path of Building
2+
--
3+
-- Module: CalcFormat
4+
-- Format helpers for calc section cells/labels. Resolves a small placeholder
5+
-- language against an actor's output/modDB:
6+
-- {output:Key}, {ns.var} variant -> actor.output value
7+
-- {p:output:Key} -> rounded + thousand-separated
8+
-- {p:mod:indices} -> combined mod total (INC/MORE/...)
9+
--
10+
local t_insert = table.insert
11+
12+
function formatCalcVal(val, p)
13+
return formatNumSep(tostring(round(val, p)))
14+
end
15+
16+
function formatCalcStr(str, actor, colData)
17+
if not actor then return "" end
18+
str = str:gsub("{output:([%a%.:]+)}", function(c)
19+
local ns, var = c:match("^(%a+)%.(%a+)$")
20+
if ns then
21+
return actor.output[ns] and actor.output[ns][var] or ""
22+
else
23+
return actor.output[c] or ""
24+
end
25+
end)
26+
str = str:gsub("{(%d+):output:([%a%.:]+)}", function(p, c)
27+
local ns, var = c:match("^(%a+)%.(%a+)$")
28+
if ns then
29+
return formatCalcVal(actor.output[ns] and actor.output[ns][var] or 0, tonumber(p))
30+
else
31+
return formatCalcVal(actor.output[c] or 0, tonumber(p))
32+
end
33+
end)
34+
str = str:gsub("{(%d+):mod:([%d,]+)}", function(p, n)
35+
local numList = { }
36+
for num in n:gmatch("%d+") do
37+
t_insert(numList, tonumber(num))
38+
end
39+
if not colData[numList[1]] or not colData[numList[1]].modType then
40+
return "?"
41+
end
42+
local modType = colData[numList[1]].modType
43+
local modTotal = modType == "MORE" and 1 or 0
44+
for _, num in ipairs(numList) do
45+
local sectionData = colData[num]
46+
if not sectionData then break end
47+
local modCfg = (sectionData.cfg and actor.mainSkill and actor.mainSkill[sectionData.cfg.."Cfg"]) or { }
48+
if sectionData.modSource then
49+
modCfg.source = sectionData.modSource
50+
end
51+
if sectionData.actor then
52+
modCfg.actor = sectionData.actor
53+
end
54+
local modVal
55+
local modStore = (sectionData.enemy and actor.enemy and actor.enemy.modDB) or (sectionData.cfg and actor.mainSkill and actor.mainSkill.skillModList) or actor.modDB
56+
if not modStore then break end
57+
if type(sectionData.modName) == "table" then
58+
modVal = modStore:Combine(sectionData.modType, modCfg, unpack(sectionData.modName))
59+
else
60+
modVal = modStore:Combine(sectionData.modType, modCfg, sectionData.modName)
61+
end
62+
if modType == "MORE" then
63+
modTotal = modTotal * modVal
64+
else
65+
modTotal = modTotal + modVal
66+
end
67+
end
68+
if modType == "MORE" then
69+
modTotal = (modTotal - 1) * 100
70+
end
71+
return formatCalcVal(modTotal, tonumber(p))
72+
end)
73+
return str
74+
end

src/Modules/Main.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ local m_pi = math.pi
1616

1717
LoadModule("GameVersions")
1818
LoadModule("Modules/Common")
19+
LoadModule("Modules/CalcFormat")
1920
LoadModule("Modules/Data")
2021
LoadModule("Modules/ModTools")
2122
LoadModule("Modules/ItemTools")

0 commit comments

Comments
 (0)