Jump to content

Module:GetEffect: Difference between revisions

From Untitled Monster Game
imported>Ziebster
No edit summary
imported>Ziebster
No edit summary
Line 14: Line 14:
     effect = mw.text.trim(effect)
     effect = mw.text.trim(effect)
     effect = effect:gsub("&lt;", "<")
     effect = effect:gsub("&lt;", "<")
   
    local debugOutput = 'Debug Info:\n'
    debugOutput = debugOutput .. 'Extracted effect: ' .. effect .. '\n'
      
      
     if booleanParam ~= '1' then
     if booleanParam ~= '1' then
Line 28: Line 25:
     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'
          
          
         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()
            local value = num .. percent
             if num:sub(1,1) == '+' then
             if num:sub(1,1) == '+' then
                 value = value:sub(2)
                 num = num:sub(2)
             end
             end
             -- Handle range formats in key
            local value = num .. percent
             local range_stat, range_part, condition = key:match('^([%w%.]+)%s+(to%s*(<)?%s*[%w%.]+)%s*(%b()|[%w%.]+)?$')
           
             -- Detect range in key (handles "stat to FREE", "stat to <99", etc.)
             local range_stat, range_continuation = key:match('^([%w%.]+)%s+(to%s*[%w%.<>]+.*)$')
             if range_stat then
             if range_stat then
                debugOutput = debugOutput .. 'Range match in key: range_stat=' .. range_stat .. ', range_part=' .. range_part .. ', condition=' .. (condition or '') .. '\n'
                 local normRangeStat = range_stat:lower()
                 local normRangeStat = range_stat:lower()
                 value = num .. percent .. ' ' .. range_part
                 value = num .. percent .. ' ' .. range_continuation
                 statsMap[normRangeStat] = mw.text.trim(value)
                 statsMap[normRangeStat] = mw.text.trim(value)
             else
             else
                 statsMap[normKey] = mw.text.trim(value)
                 statsMap[normKey] = mw.text.trim(value)
             end
             end
        else
            debugOutput = debugOutput .. 'Standard match: no match\n'
         end
         end
     end
     end
Line 60: Line 53:
         table.insert(results, val)
         table.insert(results, val)
     end
     end
     local output = table.concat(results, '||')
     return table.concat(results, '||')
   
    -- If no values were found for any stat, return debug to diagnose
    if output == '|||||' then
        return debugOutput .. '\nNo stats matched - full output would be empty'
    end
   
    return output
end
end


return p
return p

Revision as of 22:10, 9 February 2026

Example usage here:


local p = {}

function p.getEffect(frame)
    local pageName = frame.args[1] or mw.title.getCurrentTitle().text
    local booleanParam = frame.args[2] or '0'  -- default to full effect
    
    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)
    effect = effect:gsub("&lt;", "<")
    
    if booleanParam ~= '1' then
        -- Return full effect string if boolean is not '1'
        return effect
    end
    
    -- Parse stats into a map
    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
            
            -- Detect range in key (handles "stat to FREE", "stat to <99", etc.)
            local range_stat, range_continuation = key:match('^([%w%.]+)%s+(to%s*[%w%.<>]+.*)$')
            if range_stat then
                local normRangeStat = range_stat:lower()
                value = num .. percent .. ' ' .. range_continuation
                statsMap[normRangeStat] = mw.text.trim(value)
            else
                statsMap[normKey] = mw.text.trim(value)
            end
        end
    end
    
    -- Output all 6 stats separated by ||
    local allStats = {'w.pwr', 'def', 'crit', 'durability', 'gather speed', 'speed'}
    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