Jump to content

Module:GetEffect: Difference between revisions

From Untitled Monster Game
imported>Ziebster
No edit summary
imported>Ziebster
No edit summary
Line 13: Line 13:
     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)
    effect = effect:gsub("&lt;", "<")
      
      
     if not statName or statName == '' then
     if not statName or statName == '' then
Line 22: Line 21:
     -- Normalize statName for case-insensitive matching
     -- Normalize statName for case-insensitive matching
     local normalizedStat = statName:lower()
     local normalizedStat = statName:lower()
   
    local debugOutput = 'Debug Info:\n'
    debugOutput = debugOutput .. 'Extracted effect: ' .. effect .. '\n'
    debugOutput = debugOutput .. 'Normalized stat: ' .. normalizedStat .. '\n'
      
      
     -- Split the effect into comma-separated parts
     -- Split the effect into comma-separated parts
Line 31: Line 26:
     for _, part in ipairs(parts) do
     for _, part in ipairs(parts) do
         part = mw.text.trim(part)
         part = mw.text.trim(part)
         debugOutput = debugOutput .. 'Part: ' .. part .. '\n'
         -- First, try range pattern: +number stat to <number (optional (paren))
          
        local num1, stat, num2, paren = part:match('^([+-]?[%d%.]+)%s+([%w%.]+)%s+to%s*<([%d%.]+)%s*(%b())?$')
         -- Try standard pattern: +number%? stat (e.g., +15 W.PWR, +15% CRIT, +1.5 Speed)
        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"
                local range = num1 .. ' to <' .. num2
                return mw.text.trim(range)
            end
         end
         -- If not, try standard pattern: +number%? stat (e.g., +15 W.PWR, +15% CRIT, +1.5 Speed)
         local num, percent, key = part:match('^([+-]?[%d%.]+)(%%?)%s+(.*)$')
         local num, percent, key = part:match('^([+-]?[%d%.]+)(%%?)%s+(.*)$')
         if num and key then
         if num and key then
            debugOutput = debugOutput .. 'Standard match: num=' .. num .. ', percent=' .. (percent or '') .. ', key=' .. key .. '\n'
             local normKey = mw.text.trim(key):lower()
             local normKey = mw.text.trim(key):lower()
             if normKey == normalizedStat then
             if normKey == normalizedStat then
Line 42: Line 48:
                 if num:sub(1,1) == '+' then
                 if num:sub(1,1) == '+' then
                     num = num:sub(2)
                     num = num:sub(2)
                end
                -- Append % if it existed
                if percent ~= '' then
                    num = num .. percent
                 end
                 end
                 return num
                 return num
             end
             end
            -- Check if it's a range format in the key: stat to <number (paren)
            local range_stat, range_part, paren = key:match('^([%w%.]+) (to <[%d%.]+) (%b())$')
            if range_stat then
                local normRangeStat = range_stat:lower()
                if normRangeStat == normalizedStat then
                    -- Strip leading '+' from num if present
                    if num:sub(1,1) == '+' then
                        num = num:sub(2)
                    end
                    -- Reconstruct the range: "num to <num2" (ignore paren)
                    local range = num .. ' ' .. range_part
                    return mw.text.trim(range)
                end
            end
        else
            debugOutput = debugOutput .. 'Standard match: no match\n'
         end
         end
     end
     end
      
      
     -- If not found, return debug output instead of empty string
     -- If not found, return empty string
     return debugOutput
     return ''
end
end


return p
return p

Revision as of 21:03, 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 range pattern: +number stat to <number (optional (paren))
        local num1, stat, 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"
                local range = num1 .. ' to <' .. num2
                return mw.text.trim(range)
            end
        end
        -- If not, 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
                -- Append % if it existed
                if percent ~= '' then
                    num = num .. percent
                end
                return num
            end
        end
    end
    
    -- If not found, return empty string
    return ''
end

return p