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)
         local lower_part = part:lower()
         -- First, try standard pattern: +number%? stat (e.g., +15 W.PWR, +15% CRIT, +1.5 Speed)
         local start_pos, end_pos = lower_part:find(normalizedStat, 1, true)
         local num, percent, key = part:match('^([+-]?[%d%.]+)(%%?)%s+(.*)$')
         if start_pos then
         if num and key then
            -- Extract value before the stat name
             local normKey = mw.text.trim(key):lower()
             local value = mw.text.trim(part:sub(1, start_pos - 1))
             if normKey == normalizedStat then
             -- Extract rest after the stat name
                -- Strip leading '+' if present
            local rest = mw.text.trim(part:sub(end_pos + 1))
                if num:sub(1,1) == '+' then
            -- If rest starts with '(', ignore it
                    num = num:sub(2)
            if rest:match('^%(') then
                 end
                 rest = ''
                return num
             end
             end
            -- Only proceed if there's no remaining text after stat (or ignored paren)
        end
             if rest == '' then
        -- If not, try range pattern: number to <number stat (optional (paren))
                 -- Strip leading '+' if present
        local range, stat, paren = part:match('^([+-]?[%d%.]+%s*to%s*<?[%d%.]+)%s+([%w%.]+)%s*(%b())?$')
                 if value:sub(1,1) == '+' then
        if range and stat then
                     value = value:sub(2)
            local normStat = stat:lower()
             if normStat == normalizedStat then
                 -- Strip leading '+' if present in range
                 if range:sub(1,1) == '+' then
                     range = range:sub(2)
                 end
                 end
                 -- Strip trailing '%' if present
                 return mw.text.trim(range)
                value = value:gsub('%%$', '')
                -- Trim again
                value = mw.text.trim(value)
                return value
             end
             end
         end
         end

Revision as of 19:40, 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)
        -- First, 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
                return num
            end
        end
        -- If not, try range pattern: number to <number stat (optional (paren))
        local range, stat, paren = part:match('^([+-]?[%d%.]+%s*to%s*<?[%d%.]+)%s+([%w%.]+)%s*(%b())?$')
        if range and stat then
            local normStat = stat:lower()
            if normStat == normalizedStat then
                -- Strip leading '+' if present in range
                if range:sub(1,1) == '+' then
                    range = range:sub(2)
                end
                return mw.text.trim(range)
            end
        end
    end
    
    -- If not found, return empty string
    return ''
end

return p