Jump to content

Module:GetEffect

From Untitled Monster Game
Revision as of 22:23, 9 February 2026 by imported>Ziebster

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 ''
    
    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 param == '' or param:lower() == '0' then
        return effect  -- full string
    end
    
    local normalizedParam = param:lower()
    
    -- Parse all 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
            
            -- 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()
                -- Trim off any trailing (condition) or [condition]
                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
    
    -- If param is '1' (or truthy), return all 6 stats with ||
    if normalizedParam == '1' or normalizedParam == 'true' then
        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
    
    -- Otherwise, return the single stat value
    return statsMap[normalizedParam] or ''
end

return p