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 param = frame.args[2] or '' | ||
local page = mw.title.new(pageName) | local page = mw.title.new(pageName) | ||
| Line 15: | Line 15: | ||
effect = effect:gsub("<", "<") | effect = effect:gsub("<", "<") | ||
if | if param == '' or param:lower() == '0' then | ||
-- | return effect -- full string | ||
end | end | ||
-- Parse stats into a map | local normalizedParam = param:lower() | ||
-- Parse all stats into a map | |||
local statsMap = {} | 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 | ||
part = mw.text.trim(part) | part = mw.text.trim(part) | ||
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 | ||
| Line 34: | Line 34: | ||
local value = num .. percent | local value = num .. percent | ||
-- | -- Handle range/conditional in key | ||
local range_stat, range_continuation = key:match('^([%w%.]+)%s+(to%s*[%w%.<>]+.*)$') | local range_stat, range_continuation = key:match('^([%w%.]+)%s+(to%s*[%w%.<>]+.*)$') | ||
if range_stat then | if range_stat then | ||
| Line 46: | Line 46: | ||
end | end | ||
-- | -- If param is '1' (or truthy), return all 6 stats with || | ||
local allStats = {'w.pwr', 'def', 'crit', 'durability', 'gather speed', 'speed'} | if normalizedParam == '1' or normalizedParam == 'true' then | ||
local allStats = {'w.pwr', 'def', 'crit', 'durability', 'gather speed', 'speed'} | |||
local results = {} | |||
for _, s in ipairs(allStats) do | |||
table. | table.insert(results, statsMap[s] or '') | ||
end | |||
return table.concat(results, '||') | |||
end | end | ||
return | |||
-- Otherwise, assume it's a single stat name and return that value | |||
return statsMap[normalizedParam] or '' | |||
end | end | ||
return p | return p | ||
Revision as of 22:22, 9 February 2026
local p = {}
function p.getEffect(frame)
local pageName = frame.args[1] or mw.title.getCurrentTitle().text
local param = frame.args[2] or ''
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("<", "<")
if param == '' or param:lower() == '0' then
return effect -- full string
end
local normalizedParam = param:lower()
-- Parse all stats into a map
local statsMap = {}
local parts = mw.text.split(effect, ',')
for _, part in ipairs(parts) do
part = mw.text.trim(part)
local num, percent, key = part:match('^([+-]?[%d%.]+)(%%?)%s+(.*)$')
if num and key then
local normKey = mw.text.trim(key):lower()
if num:sub(1,1) == '+' then
num = num:sub(2)
end
local value = num .. percent
-- Handle range/conditional in key
local range_stat, range_continuation = key:match('^([%w%.]+)%s+(to%s*[%w%.<>]+.*)$')
if range_stat then
local normRangeStat = range_stat:lower()
value = num .. percent .. ' ' .. range_continuation
statsMap[normRangeStat] = mw.text.trim(value)
else
statsMap[normKey] = mw.text.trim(value)
end
end
end
-- If param is '1' (or truthy), return all 6 stats with ||
if normalizedParam == '1' or normalizedParam == 'true' then
local allStats = {'w.pwr', 'def', 'crit', 'durability', 'gather speed', 'speed'}
local results = {}
for _, s in ipairs(allStats) do
table.insert(results, statsMap[s] or '')
end
return table.concat(results, '||')
end
-- Otherwise, assume it's a single stat name and return that value
return statsMap[normalizedParam] or ''
end
return p