Jump to content

Module:GetEffect: Difference between revisions

From Untitled Monster Game
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 booleanParam = frame.args[2]
     local statName = frame.args[2]
      
      
     local page = mw.title.new(pageName)
     local page = mw.title.new(pageName)
Line 15: Line 15:
     effect = effect:gsub("&lt;", "<")
     effect = effect:gsub("&lt;", "<")
      
      
     if booleanParam == '1' then
     if not statName or statName == '' then
         -- Parse all stats into a map
         -- Return full effect if no stat specified
         local statsMap = {}
         return effect
        local parts = mw.text.split(effect, ',')
    end
        for _, part in ipairs(parts) do
   
            part = mw.text.trim(part)
    -- Normalize statName for case-insensitive matching
            -- Try standard pattern: +number%? stat (e.g., +15 W.PWR, +15% CRIT, +1.5 Speed)
    local normalizedStat = statName:lower()
            local num, percent, key = part:match('^([+-]?[%d%.]+)(%%?)%s+(.*)$')
   
            if num and key then
    -- Split the effect into comma-separated parts
    local parts = mw.text.split(effect, ',')
    for _, part in ipairs(parts) do
        part = mw.text.trim(part)
        -- Try standard pattern: +number%? stat (e.g., +15 W.PWR, +15% CRIT, +1.5 Speed)
        local num, percent, key = part:match('^([+-]?[%d%.]+)(%%?)%s+(.*)$')
        if num and key then
            local normKey = mw.text.trim(key):lower()
            if normKey == normalizedStat then
                 -- Strip leading '+' if present
                 -- Strip leading '+' if present
                 if num:sub(1,1) == '+' then
                 if num:sub(1,1) == '+' then
                     num = num:sub(2)
                     num = num:sub(2)
                 end
                 end
                 local value = num .. percent
                 -- Append % if it existed
                 local normKey = mw.text.trim(key):lower()
                if percent ~= '' then
                 -- Check if it's a range format in the key: stat to <number (optional (paren or condition))
                    num = num .. percent
                local range_stat, range_part, condition = key:match('^([%w%.]+)%s+(to%s*<[%d%.]+)%s*(%b()|[%w%.]+)?$')
                 end
                if range_stat then
                 return num
                    local normRangeStat = range_stat:lower()
            end
                     value = num .. percent .. ' ' .. range_part
            -- Check if it's a range format in the key: stat to ( < )second (optional condition)
                    statsMap[normRangeStat] = mw.text.trim(value)
            local range_stat, range_part, less_than, second, condition = key:match('^([%w%.]+)%s+(to)%s*(<)?%s*([%w%.]+)%s*(%b()|[%w%.]+)?$')
                else
            if range_stat and range_part then
                     statsMap[normKey] = mw.text.trim(value)
                local normRangeStat = range_stat:lower()
                if normRangeStat == normalizedStat then
                     -- Strip leading '+' if present
                    if num:sub(1,1) == '+' then
                        num = num:sub(2)
                    end
                    local range = num .. percent .. ' ' .. range_part .. ' ' .. (less_than or '') .. second
                     return mw.text.trim(range)
                 end
                 end
             end
             end
         end
         end
       
        -- Output the 6 stats separated by double ||
        local results = {}
        local allStats = {'w.pwr', 'def', 'crit', 'durability', 'gather speed', 'speed'}
        for _, s in ipairs(allStats) do
            local value = statsMap[s] or ''
            table.insert(results, value)
        end
        return table.concat(results, '||')
    elseif booleanParam == '0' then
        -- Return full effect
        return effect
     end
     end
      
      
     -- If invalid boolean, return empty string
     -- If not found, return empty string
     return ''
     return ''
end
end


return p
return p

Revision as of 21:50, 9 February 2026

Example usage here:


local p = {}

function p.getEffect(frame)
    local pageName = frame.args[1] or mw.title.getCurrentTitle().text
    local statName = frame.args[2]
    
    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("&lt;", "<")
    
    if not statName or statName == '' then
        -- Return full effect if no stat specified
        return effect
    end
    
    -- Normalize statName for case-insensitive matching
    local normalizedStat = statName:lower()
    
    -- Split the effect into comma-separated parts
    local parts = mw.text.split(effect, ',')
    for _, part in ipairs(parts) do
        part = mw.text.trim(part)
        -- Try standard pattern: +number%? stat (e.g., +15 W.PWR, +15% CRIT, +1.5 Speed)
        local num, percent, key = part:match('^([+-]?[%d%.]+)(%%?)%s+(.*)$')
        if num and key then
            local normKey = mw.text.trim(key):lower()
            if normKey == normalizedStat then
                -- Strip leading '+' if present
                if num:sub(1,1) == '+' then
                    num = num:sub(2)
                end
                -- Append % if it existed
                if percent ~= '' then
                    num = num .. percent
                end
                return num
            end
            -- Check if it's a range format in the key: stat to ( < )second (optional condition)
            local range_stat, range_part, less_than, second, condition = key:match('^([%w%.]+)%s+(to)%s*(<)?%s*([%w%.]+)%s*(%b()|[%w%.]+)?$')
            if range_stat and range_part then
                local normRangeStat = range_stat:lower()
                if normRangeStat == normalizedStat then
                    -- Strip leading '+' if present
                    if num:sub(1,1) == '+' then
                        num = num:sub(2)
                    end
                    local range = num .. percent .. ' ' .. range_part .. ' ' .. (less_than or '') .. second
                    return mw.text.trim(range)
                end
            end
        end
    end
    
    -- If not found, return empty string
    return ''
end

return p