Jump to content

Module:GetEffect: Difference between revisions

From Untitled Monster Game
imported>Ziebster
No edit summary
imported>Ziebster
No edit summary
Line 26: Line 26:
     for _, part in ipairs(parts) do
     for _, part in ipairs(parts) do
         part = mw.text.trim(part)
         part = mw.text.trim(part)
         -- Match pattern: +number%? stat (e.g., +15 W.PWR, +15% CRIT, +1.5 Speed)
         local lower_part = part:lower()
         local num, percent, key = part:match('^([+-]?[%d%.]+)(%%?)%s+(.*)$')
        local start_pos, end_pos = lower_part:find(normalizedStat, 1, true)
        if num and key then
         if start_pos then
             local normKey = mw.text.trim(key):lower()
            -- Extract value before the stat name
             if normKey == normalizedStat then
            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
                 -- Strip leading '+' if present
                 if num:sub(1,1) == '+' then
                 if value:sub(1,1) == '+' then
                     num = num:sub(2)
                     value = value:sub(2)
                 end
                 end
                 return num
                -- Strip trailing '%' if present
                value = value:gsub('%%$', '')
                -- Trim again
                value = mw.text.trim(value)
                 return value
             end
             end
         end
         end

Revision as of 19:37, 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)
    
    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