Jump to content

Module:GetEffect

From Untitled Monster Game
Revision as of 19:37, 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 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)
    
    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)
        local lower_part = part:lower()
        local start_pos, end_pos = lower_part:find(normalizedStat, 1, true)
        if start_pos then
            -- Extract value before the stat name
            local value = mw.text.trim(part:sub(1, start_pos - 1))
            -- Extract rest after the stat name
            local rest = mw.text.trim(part:sub(end_pos + 1))
            -- If rest starts with '(', ignore it
            if rest:match('^%(') then
                rest = ''
            end
            -- Only proceed if there's no remaining text after stat (or ignored paren)
            if rest == '' then
                -- Strip leading '+' if present
                if value:sub(1,1) == '+' then
                    value = value:sub(2)
                end
                -- Strip trailing '%' if present
                value = value:gsub('%%$', '')
                -- Trim again
                value = mw.text.trim(value)
                return value
            end
        end
    end
    
    -- If not found, return empty string
    return ''
end

return p