Jump to content

Module:GetEffect

From Untitled Monster Game
Revision as of 21:47, 9 February 2026 by imported>Ziebster

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