Jump to content

Module:GetEffect: Difference between revisions

From Untitled Monster Game
imported>Ziebster
No edit summary
imported>Ziebster
No edit summary
Line 11: Line 11:
      
      
     local content = page:getContent()
     local content = page:getContent()
     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)
      
      
Line 38: Line 38:
             end
             end
         end
         end
         -- If not, try range pattern: number to <number stat (optional (paren))
         -- If not, try range pattern: +number stat to <number (optional (paren))
         local range, stat, paren = part:match('^([+-]?[%d%.]+%s*to%s*<?[%d%.]+)%s+([%w%.]+)%s*(%b())?$')
         local num1, stat, less_than, num2, paren = part:match('^([+-]?[%d%.]+)%s+([%w%.]+)%s+to%s*(<)?([%d%.]+)%s*(%b())?$')
         if range and stat then
         if num1 and stat and num2 then
             local normStat = stat:lower()
             local normStat = stat:lower()
             if normStat == normalizedStat then
             if normStat == normalizedStat then
                 -- Strip leading '+' if present in range
                 -- Strip leading '+' from num1 if present
                 if range:sub(1,1) == '+' then
                 if num1:sub(1,1) == '+' then
                     range = range:sub(2)
                     num1 = num1:sub(2)
                 end
                 end
                -- Reconstruct the range: "num1 to <num2" (less_than is optional)
                local range = num1 .. ' to ' .. (less_than or '') .. num2
                 return mw.text.trim(range)
                 return mw.text.trim(range)
             end
             end

Revision as of 19:45, 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 stat to <number (optional (paren))
        local num1, stat, less_than, num2, paren = part:match('^([+-]?[%d%.]+)%s+([%w%.]+)%s+to%s*(<)?([%d%.]+)%s*(%b())?$')
        if num1 and stat and num2 then
            local normStat = stat:lower()
            if normStat == normalizedStat then
                -- Strip leading '+' from num1 if present
                if num1:sub(1,1) == '+' then
                    num1 = num1:sub(2)
                end
                -- Reconstruct the range: "num1 to <num2" (less_than is optional)
                local range = num1 .. ' to ' .. (less_than or '') .. num2
                return mw.text.trim(range)
            end
        end
    end
    
    -- If not found, return empty string
    return ''
end

return p