Jump to content

Module:GetEffect: Difference between revisions

From Untitled Monster Game
imported>Ziebster
No edit summary
imported>Ziebster
No edit summary
 
(21 intermediate revisions by the same user not shown)
Line 3: Line 3:
function p.getEffect(frame)
function p.getEffect(frame)
     local pageName = frame.args[1] or mw.title.getCurrentTitle().text
     local pageName = frame.args[1] or mw.title.getCurrentTitle().text
     local statName = frame.args[2]
     local param = frame.args[2] or '1'  -- default to all-stats from effects
      
      
     local page = mw.title.new(pageName)
     local page = mw.title.new(pageName)
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 ''
   
    -- Determine which effect param to use
    local effectParam = 'effects'
    if param == '2' then
        effectParam = 'effects2'
    elseif param == '3' then
        effectParam = 'effects3'
    end
   
    -- Extract the chosen effect
     local effect = content:match('|%s*' .. effectParam .. '%s*=%s*(.-)\n%s*|') or content:match('|%s*' .. effectParam .. '%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 param is '0' or empty, return full raw string
        -- Return full effect if no stat specified
     if param == '' or param:lower() == '0' then
         return effect
         return effect
     end
     end
      
      
     -- Normalize statName for case-insensitive matching
     -- Parse stats into a map (same logic as before)
    local normalizedStat = statName:lower()
     local statsMap = {}
   
     local debugOutput = 'Debug Info:\n'
    debugOutput = debugOutput .. 'Extracted effect: ' .. effect .. '\n'
    debugOutput = debugOutput .. 'Normalized stat: ' .. normalizedStat .. '\n'
   
    -- Split the effect into comma-separated parts
     local parts = mw.text.split(effect, ',')
     local parts = mw.text.split(effect, ',')
     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())?$')
        if num1 and stat and num2 then
            debugOutput = debugOutput .. 'Range match: num1=' .. num1 .. ', stat=' .. stat .. ', num2=' .. num2 .. ', paren=' .. (paren or '') .. '\n'
            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
        else
            debugOutput = debugOutput .. 'Range match: no match\n'
        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 num:sub(1,1) == '+' then
                -- Strip leading '+' if present
                 num = num:sub(2)
                 if num:sub(1,1) == '+' then
            end
                    num = num:sub(2)
            local value = num .. percent
                 end
           
                 return num
            -- Handle range/conditional in key
            local range_stat, range_continuation = key:match('^([%w%.]+)%s+(to%s*[%w%.<>]+.*)$')
            if range_stat then
                local normRangeStat = range_stat:lower()
                 range_continuation = range_continuation:gsub('%s*%b()$', ''):gsub('%s*%b[]$', ''):match('^(.-)%s*$')
                 value = num .. percent .. ' ' .. range_continuation
                statsMap[normRangeStat] = mw.text.trim(value)
            else
                statsMap[normKey] = mw.text.trim(value)
             end
             end
        else
            debugOutput = debugOutput .. 'Standard match: no match\n'
         end
         end
     end
     end
      
      
     -- If not found, return debug output instead of empty string
     -- Always return all 6 stats separated by || (for '1', '2', '3', or any other non-'0')
     return debugOutput
    local allStats = {'w.pwr', 'def', 'crit', 'speed', 'gather speed', 'durability'}
    local results = {}
    for _, s in ipairs(allStats) do
        local val = statsMap[s] or ''
        table.insert(results, val)
    end
     return table.concat(results, '||')
end
end


return p
return p

Latest revision as of 22:32, 9 February 2026

Example usage here:


local p = {}

function p.getEffect(frame)
    local pageName = frame.args[1] or mw.title.getCurrentTitle().text
    local param = frame.args[2] or '1'  -- default to all-stats from effects
    
    local page = mw.title.new(pageName)
    if not page then
        return 'Page not found'
    end
    
    local content = page:getContent()
    
    -- Determine which effect param to use
    local effectParam = 'effects'
    if param == '2' then
        effectParam = 'effects2'
    elseif param == '3' then
        effectParam = 'effects3'
    end
    
    -- Extract the chosen effect
    local effect = content:match('|%s*' .. effectParam .. '%s*=%s*(.-)\n%s*|') or content:match('|%s*' .. effectParam .. '%s*=%s*(.-)%s*}}') or ''
    effect = mw.text.trim(effect)
    effect = effect:gsub("&lt;", "<")
    
    -- If param is '0' or empty, return full raw string
    if param == '' or param:lower() == '0' then
        return effect
    end
    
    -- Parse stats into a map (same logic as before)
    local statsMap = {}
    local parts = mw.text.split(effect, ',')
    for _, part in ipairs(parts) do
        part = mw.text.trim(part)
        local num, percent, key = part:match('^([+-]?[%d%.]+)(%%?)%s+(.*)$')
        if num and key then
            local normKey = mw.text.trim(key):lower()
            if num:sub(1,1) == '+' then
                num = num:sub(2)
            end
            local value = num .. percent
            
            -- Handle range/conditional in key
            local range_stat, range_continuation = key:match('^([%w%.]+)%s+(to%s*[%w%.<>]+.*)$')
            if range_stat then
                local normRangeStat = range_stat:lower()
                range_continuation = range_continuation:gsub('%s*%b()$', ''):gsub('%s*%b[]$', ''):match('^(.-)%s*$')
                value = num .. percent .. ' ' .. range_continuation
                statsMap[normRangeStat] = mw.text.trim(value)
            else
                statsMap[normKey] = mw.text.trim(value)
            end
        end
    end
    
    -- Always return all 6 stats separated by || (for '1', '2', '3', or any other non-'0')
    local allStats = {'w.pwr', 'def', 'crit', 'speed', 'gather speed', 'durability'}
    local results = {}
    for _, s in ipairs(allStats) do
        local val = statsMap[s] or ''
        table.insert(results, val)
    end
    return table.concat(results, '||')
end

return p