Module:Material Usage/Draft
Cached copy of the original page · revision 1644351 · refresh
This is the module draft page for Module:Material Usage (diff).
| Main | Sandbox | Usage |
The documentation for this module is missing. Click here to create it.
local p = {}
local function escapePattern(s)
return (s:gsub("[%^%$%(%)%%%.%[%]%*%+%-%?]", "%%%1"))
end
-- Find balanced {{TemplateName ...}} blocks in wikitext
local function findTemplateBlocks(content, templateName)
local blocks = {}
local pat = "{{%s*" .. escapePattern(templateName) .. "%s*[|}]"
local searchFrom = 1
while true do
local s = content:find(pat, searchFrom)
if not s then
break
end
local depth, i, len, e = 0, s, #content, nil
while i <= len do
local two = content:sub(i, i + 1)
if two == "{{" then
depth = depth + 1
i = i + 2
elseif two == "}}" then
depth = depth - 1
i = i + 2
if depth == 0 then
e = i - 1
break
end
else
i = i + 1
end
end
if not e then
break
end
table.insert(blocks, { text = content:sub(s, e), startPos = s })
searchFrom = e + 1
end
return blocks
end
-- Find {{<item>|<amount>}} uses and the "|XY =" key each belongs to
local function findItemUses(blockText, itemPatterns)
local uses = {}
for _, ip in ipairs(itemPatterns) do
local from = 1
while true do
local s, e, amount = blockText:find(
"{{%s*" .. ip .. "%s*|%s*([%d,]+)%s*[|}]", from)
if not s then
break
end
local prefix = blockText:sub(1, s - 1)
local key = prefix:match(".*|%s*(%w+)%s*=%s*$")
or prefix:match(".*|%s*(%w+)%s*=[^|]*$")
if key then
table.insert(uses, {
key = key,
amount = tonumber((amount:gsub(",", ""))) or 0,
})
end
from = e + 1
end
end
return uses
end
-- Ascension block: tiers 1-4 -> ascension total, tiers 5+ -> costumes
local function aggregateAscension(block, itemPatterns)
local asc = 0
local costumes = {}
local tierCos = {}
for tier, cosN in block:gmatch(
"|%s*(%d)icon%s*=%s*{{[^{}]-stage%s*=%s*cos(%d+)") do
tierCos[tonumber(tier)] = tonumber(cosN)
end
for _, u in ipairs(findItemUses(block, itemPatterns)) do
if u.key:match("^%d%d$") then
local tier = tonumber(u.key:sub(1, 1))
if tier <= 4 then
asc = asc + u.amount
else
local cosN = tierCos[tier] or (tier - 4)
costumes[cosN] = (costumes[cosN] or 0) + u.amount
end
end
end
return asc, costumes
end
-- Sum all "|XY =" uses in one Skillreinforcement block (= cost per skill)
local function aggregateSkill(block, itemPatterns)
local total = 0
for _, u in ipairs(findItemUses(block, itemPatterns)) do
if u.key:match("^%d%d$") then
total = total + u.amount
end
end
return total
end
-- Whether a Skillreinforcement block is the Active or Append tab
local function skillTabLabel(content, startPos, index)
local prefix = content:sub(math.max(1, startPos - 80), startPos - 1)
if prefix:match("Append[^{}]*|%s*$") then
return "append"
end
if prefix:match("Active[^{}]*|%s*$") then
return "active"
end
return index == 1 and "active" or "append"
end
local function analyzeServant(title, itemPatterns)
local t = mw.title.new(title)
if not t then
return nil
end
local content = t:getContent()
if not content then
return nil
end
local result = { asc = 0, skill = 0, append = 0, costumes = {} }
for _, b in ipairs(findTemplateBlocks(content, "Ascension")) do
local asc, costumes = aggregateAscension(b.text, itemPatterns)
result.asc = result.asc + asc
for n, v in pairs(costumes) do
result.costumes[n] = (result.costumes[n] or 0) + v
end
end
local skillBlocks = findTemplateBlocks(content, "Skillreinforcement")
for i, b in ipairs(skillBlocks) do
local label = skillTabLabel(content, b.startPos, i)
local total = aggregateSkill(b.text, itemPatterns)
if label == "active" then
result.skill = result.skill + total
else
result.append = result.append + total
end
end
return result
end
local function fetchUserTitles(frame, item)
local result = frame:callParserFunction("#dpl",
"namespace=",
"category=Servant",
"uses=Template:" .. item,
"distinct=true",
"mode=userformat",
"format=,%TITLE%\\n,,"
)
local titles, seen = {}, {}
for line in mw.text.gsplit(result or "", "\n") do
local title = mw.text.trim(line)
if title ~= "" and not seen[title] then
seen[title] = true
table.insert(titles, title)
end
end
return titles
end
-- Computed usage map, same filtering as Module:Material Usage
local function computeUsage(frame, item)
local servantData = mw.loadData("Module:Servant Icon/data")
local map = {}
for _, title in ipairs(fetchUserTitles(frame, item)) do
local sd = servantData[title]
if sd and (sd.id or 0) ~= 9999 then
local r = analyzeServant(title, { escapePattern(item) })
if r and (r.asc > 0 or r.skill > 0 or r.append > 0
or next(r.costumes)) then
map[title] = r
end
end
end
return map
end
-- Debug entry point:
-- {{#invoke:Material Usage/sandbox|analyze|Odysseus|Meteoric Horseshoe}}
function p.analyze(frame)
local servant = frame.args[1]
local item = frame.args[2]
local r = analyzeServant(servant, { escapePattern(item) })
if not r then
return "NO CONTENT for " .. tostring(servant)
end
local cos = {}
for n, v in pairs(r.costumes) do
table.insert(cos, "cos" .. n .. "=" .. v)
end
table.sort(cos)
return string.format("%s: asc=%d skill=%d append=%d %s",
servant, r.asc, r.skill, r.append, table.concat(cos, " "))
end
-- Compact per-servant dump (no id=9999 filter, shows NODATA lines).
-- {{#invoke:Material Usage/sandbox|dump|Meteoric Horseshoe}}
function p.dump(frame)
local item = frame.args[1]
local servantData = mw.loadData("Module:Servant Icon/data")
local lines = {}
for _, title in ipairs(fetchUserTitles(frame, item)) do
local sd = servantData[title]
if not sd then
table.insert(lines, "NODATA|" .. title)
else
local r = analyzeServant(title, { escapePattern(item) })
if r and (r.asc > 0 or r.skill > 0 or r.append > 0
or next(r.costumes)) then
local cos = {}
for n, v in pairs(r.costumes) do
table.insert(cos, "cos" .. n .. "=" .. v)
end
table.sort(cos)
table.insert(lines, string.format(
"%s|%d|%s|%d|%d|%d|%s",
sd.class, sd.id or 0, title,
r.asc, r.skill, r.append,
table.concat(cos, ";")))
end
end
end
table.sort(lines)
return table.concat(lines, "\n")
end
-- Parse the existing manual {{ReqItem}} entries on a material page
local function parseManualTable(item)
local t = mw.title.new(item)
local content = t and t:getContent()
if not content then
return nil
end
local main, costume = {}, {}
for args in content:gmatch("{{%s*ReqItem%s*|([^{}]*)}}") do
local pos, named = {}, {}
for part in mw.text.gsplit(args, "|") do
local k, v = part:match("^%s*([%w]+)%s*=%s*(.-)%s*$")
if k then
named[k] = v
else
table.insert(pos, mw.text.trim(part))
end
end
local name = pos[1]
if name then
if named.stage then
local n = tonumber(named.stage:match("cos(%d+)") or "")
if n then
costume[name .. "#cos" .. n] =
tonumber(named.total or "") or -1
end
else
main[name] = {
asc = tonumber(pos[2] or "") or -1,
skill = tonumber(pos[3] or "") or -1,
append = tonumber(pos[4] or "") or -1,
}
end
end
end
return main, costume
end
-- Diff the manual table on the material page against computed values.
-- Prints differences only.
-- {{#invoke:Material Usage/sandbox|compare|Meteoric Horseshoe}}
function p.compare(frame)
local item = frame.args[1] or mw.title.getCurrentTitle().text
local manualMain, manualCos = parseManualTable(item)
if not manualMain then
return "NO PAGE CONTENT for " .. item
end
local auto = computeUsage(frame, item)
local lines = {}
local matches = 0
-- main rows
local autoMainSeen = {}
for name, m in pairs(manualMain) do
local a = auto[name]
if not a or (a.asc == 0 and a.skill == 0 and a.append == 0) then
table.insert(lines, string.format(
"ONLY_MANUAL|%s|%d|%d|%d", name, m.asc, m.skill, m.append))
else
autoMainSeen[name] = true
if a.asc ~= m.asc or a.skill ~= m.skill
or a.append ~= m.append then
table.insert(lines, string.format(
"DIFF|%s|manual=%d/%d/%d|auto=%d/%d/%d",
name, m.asc, m.skill, m.append,
a.asc, a.skill, a.append))
else
matches = matches + 1
end
end
end
for name, a in pairs(auto) do
if (a.asc > 0 or a.skill > 0 or a.append > 0)
and not manualMain[name] then
table.insert(lines, string.format(
"ONLY_AUTO|%s|%d|%d|%d", name, a.asc, a.skill, a.append))
end
end
-- costume rows
local autoCos = {}
for name, a in pairs(auto) do
for n, v in pairs(a.costumes) do
autoCos[name .. "#cos" .. n] = v
end
end
for key, v in pairs(manualCos) do
if autoCos[key] == nil then
table.insert(lines, "ONLY_MANUAL_COS|" .. key .. "|" .. v)
elseif autoCos[key] ~= v then
table.insert(lines, string.format(
"DIFF_COS|%s|manual=%d|auto=%d", key, v, autoCos[key]))
else
matches = matches + 1
end
end
for key, v in pairs(autoCos) do
if manualCos[key] == nil then
table.insert(lines, "ONLY_AUTO_COS|" .. key .. "|" .. v)
end
end
table.sort(lines)
table.insert(lines, 1, "MATCH=" .. matches)
return table.concat(lines, "\n")
end
p._analyzeServant = analyzeServant
p._escapePattern = escapePattern
return p