Jump to content

Module:GetEffect

From Untitled Monster Game

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