Module:GetEffect: Difference between revisions
Appearance
imported>Ziebster No edit summary |
imported>Ziebster No edit summary |
||
| Line 3: | Line 3: | ||
function p.getEffect(frame) | function p.getEffect(frame) | ||
local pageName = frame.args[1] or mw.title.getCurrentTitle().text | local pageName = frame.args[1] or mw.title.getCurrentTitle().text | ||
local | local booleanParam = frame.args[2] or '0' -- default to full effect | ||
local page = mw.title.new(pageName) | local page = mw.title.new(pageName) | ||
| Line 13: | Line 13: | ||
local effect = content:match('|%s*effects%s*=%s*(.-)\n%s*|') or content:match('|%s*effects%s*=%s*(.-)%s*}}') or '' | local effect = content:match('|%s*effects%s*=%s*(.-)\n%s*|') or content:match('|%s*effects%s*=%s*(.-)%s*}}') or '' | ||
effect = mw.text.trim(effect) | effect = mw.text.trim(effect) | ||
effect = effect:gsub("<", "<") | |||
if | local debugOutput = 'Debug Info:\n' | ||
-- Return full effect if | debugOutput = debugOutput .. 'Extracted effect: ' .. effect .. '\n' | ||
if booleanParam ~= '1' then | |||
-- Return full effect string if boolean is not '1' | |||
return effect | return effect | ||
end | end | ||
-- | -- Parse stats into a map | ||
local | local statsMap = {} | ||
local parts = mw.text.split(effect, ',') | local parts = mw.text.split(effect, ',') | ||
for _, part in ipairs(parts) do | for _, part in ipairs(parts) do | ||
| Line 32: | Line 30: | ||
debugOutput = debugOutput .. 'Part: ' .. part .. '\n' | debugOutput = debugOutput .. 'Part: ' .. part .. '\n' | ||
local num, percent, key = part:match('^([+-]?[%d%.]+)(%%?)%s+(.*)$') | local num, percent, key = part:match('^([+-]?[%d%.]+)(%%?)%s+(.*)$') | ||
if num and key then | if num and key then | ||
debugOutput = debugOutput .. 'Standard match: num=' .. num .. ', percent=' .. (percent or '') .. ', key=' .. key .. '\n' | debugOutput = debugOutput .. 'Standard match: num=' .. num .. ', percent=' .. (percent or '') .. ', key=' .. key .. '\n' | ||
local normKey = mw.text.trim(key):lower() | local normKey = mw.text.trim(key):lower() | ||
local value = num .. percent | |||
if num:sub(1,1) == '+' then | |||
value = value:sub(2) | |||
end | end | ||
-- | -- Handle range formats in key | ||
local range_stat, | local range_stat, range_part, condition = key:match('^([%w%.]+)%s+(to%s*(<)?%s*[%w%.]+)%s*(%b()|[%w%.]+)?$') | ||
if range_stat | if range_stat then | ||
debugOutput = debugOutput .. 'Range in key | debugOutput = debugOutput .. 'Range match in key: range_stat=' .. range_stat .. ', range_part=' .. range_part .. ', condition=' .. (condition or '') .. '\n' | ||
local normRangeStat = range_stat:lower() | local normRangeStat = range_stat:lower() | ||
value = num .. percent .. ' ' .. range_part | |||
statsMap[normRangeStat] = mw.text.trim(value) | |||
else | |||
statsMap[normKey] = mw.text.trim(value) | |||
end | end | ||
else | else | ||
| Line 63: | Line 53: | ||
end | end | ||
-- If | -- Output all 6 stats separated by || | ||
return | local allStats = {'w.pwr', 'def', 'crit', 'durability', 'gather speed', 'speed'} | ||
local results = {} | |||
for _, s in ipairs(allStats) do | |||
local val = statsMap[s] or '' | |||
table.insert(results, val) | |||
end | |||
local output = table.concat(results, '||') | |||
-- If no values were found for any stat, return debug to diagnose | |||
if output == '|||||' then | |||
return debugOutput .. '\nNo stats matched - full output would be empty' | |||
end | |||
return output | |||
end | end | ||
return p | return p | ||
Revision as of 22:09, 9 February 2026
local p = {}
function p.getEffect(frame)
local pageName = frame.args[1] or mw.title.getCurrentTitle().text
local booleanParam = frame.args[2] or '0' -- default to full effect
local page = mw.title.new(pageName)
if not page then
return 'Page not found'
end
local content = page:getContent()
local effect = content:match('|%s*effects%s*=%s*(.-)\n%s*|') or content:match('|%s*effects%s*=%s*(.-)%s*}}') or ''
effect = mw.text.trim(effect)
effect = effect:gsub("<", "<")
local debugOutput = 'Debug Info:\n'
debugOutput = debugOutput .. 'Extracted effect: ' .. effect .. '\n'
if booleanParam ~= '1' then
-- Return full effect string if boolean is not '1'
return effect
end
-- Parse stats into a map
local statsMap = {}
local parts = mw.text.split(effect, ',')
for _, part in ipairs(parts) do
part = mw.text.trim(part)
debugOutput = debugOutput .. 'Part: ' .. part .. '\n'
local num, percent, key = part:match('^([+-]?[%d%.]+)(%%?)%s+(.*)$')
if num and key then
debugOutput = debugOutput .. 'Standard match: num=' .. num .. ', percent=' .. (percent or '') .. ', key=' .. key .. '\n'
local normKey = mw.text.trim(key):lower()
local value = num .. percent
if num:sub(1,1) == '+' then
value = value:sub(2)
end
-- Handle range formats in key
local range_stat, range_part, condition = key:match('^([%w%.]+)%s+(to%s*(<)?%s*[%w%.]+)%s*(%b()|[%w%.]+)?$')
if range_stat then
debugOutput = debugOutput .. 'Range match in key: range_stat=' .. range_stat .. ', range_part=' .. range_part .. ', condition=' .. (condition or '') .. '\n'
local normRangeStat = range_stat:lower()
value = num .. percent .. ' ' .. range_part
statsMap[normRangeStat] = mw.text.trim(value)
else
statsMap[normKey] = mw.text.trim(value)
end
else
debugOutput = debugOutput .. 'Standard match: no match\n'
end
end
-- Output all 6 stats separated by ||
local allStats = {'w.pwr', 'def', 'crit', 'durability', 'gather speed', 'speed'}
local results = {}
for _, s in ipairs(allStats) do
local val = statsMap[s] or ''
table.insert(results, val)
end
local output = table.concat(results, '||')
-- If no values were found for any stat, return debug to diagnose
if output == '|||||' then
return debugOutput .. '\nNo stats matched - full output would be empty'
end
return output
end
return p