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


return p
return p

Revision as of 21:47, 9 February 2026

Example usage here:


local p = {}

function p.getEffect(frame)
    local pageName = frame.args[1] or mw.title.getCurrentTitle().text
    local booleanParam = 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 booleanParam == '1' then
        -- 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)
            -- 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
                -- Strip leading '+' if present
                if num:sub(1,1) == '+' then
                    num = num:sub(2)
                end
                local value = num .. percent
                local normKey = mw.text.trim(key):lower()
                -- Check if it's a range format in the key: stat to <number (optional (paren or condition))
                local range_stat, range_part, condition = key:match('^([%w%.]+)%s+(to%s*<[%d%.]+)%s*(%b()|[%w%.]+)?$')
                if range_stat then
                    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
        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
    
    -- If invalid boolean, return empty string
    return ''
end

return p