Module:ItemInfoParse

From Dune: Awakening Community Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:ItemInfoParse/doc

local p = {}

-- Grab params from an ItemInfo
-- (bit fragile, but works for now, replace with data from cargo etc when available)
function p.getItemInfoParams(args)
    local pageName
    if type(args) == "string" then
        pageName = args
    elseif type(args) == "table" then
        pageName = args.args[1]
    end
    mw.log("getting info from " .. pageName)
    local title = mw.title.new(pageName)
    if not title then return nil end

    local content = title:getContent()
    if not content then return nil end

    local lines = mw.text.split(content, '\n')
    local inside = false
    local params = {}

    for _, line in ipairs(lines) do
        if not inside then
            -- Look for start of ItemInfo
            if line:match('{{%s*[Uu]ser:[^/]+/Template:Item[Ii]nfo') or line:match('{{%s*Item[Ii]nfo') then
                inside = true
            end
        else
            -- Check for closing
            if line:match('^}}') then break end

            -- Extract parameter
            local rawKey, val = line:match('|%s*([^=]+)%s*=%s*(.*)')
            local key = mw.text.trim(rawKey)
            if key and val then
                params[key] = val
            end
        end
    end

    return params
end

function p.getFileName(args)
    local content
    if type(args) == "string" then
        content = args
    elseif type(args) == "table" then
        content = args.args[1]
    end
    local filename = content and content:match("%[%[%s*File:(.-)[|%]]")
    return filename
end

function p.renderArmorSet(frame)
    local args = frame.args -- frame:getParent().args or 
    -- ordered the way the items would be worn in ui
    local slots = { "head", "torso", "hands", "legs", "feet" }
    local stats = {
        "armor_value",
        "light_dart_mitigation",
        "blade_mitigation",
        "heavy_dart_mitigation",
        "concussive_mitigation",
        "heat_protection",
        "catchpocket_size"
    }

    local out = {}
    local total = {}
    local list = {}

    for _, stat in ipairs(stats) do
        total[stat] = 0
    end
    -- go through every slot/item in order they would be worn in ui
    for _, slot in ipairs(slots) do
        local item = args[slot]
        if item and item ~= "" then
            mw.log("checking slot " .. slot .. ": " .. item)
            local data = p.getItemInfoParams(item)
            if data then
                mw.log("got data")
                local file = p.getFileName(data.image)
                local icon = file and ('[[File:' .. file .. '|frameless|32x32px]]') or ''
                -- conditional link
                table.insert(list,
                    '* ' .. icon ..
                    '{{#ifeq:{{FULLPAGENAME}}|' .. item .. '|' .. item .. '|[[' .. item .. ']]}}'
                )

                for _, stat in ipairs(stats) do
                    local val = tonumber(data[stat]) or 0
                    total[stat] = total[stat] + val
                    mw.log("got stat " .. stat .. "val: " .. val)
                end
            end
        end
    end

    table.insert(out, '<table class="wikitable">')
    for _, stat in ipairs(stats) do
        if total[stat] > 0 then
            -- make stat look nice
            local label = stat:gsub("_", " "):gsub("^%l", string.upper)
            table.insert(out, '<tr><th>' .. label .. '</th><td>' .. string.format("%.2f", total[stat]) .. '</td></tr>')
        end
    end
    table.insert(out, '</table>\n')

    table.insert(out, '<ul>\n' .. table.concat(list, '\n') .. '</ul>')

    return frame:preprocess(table.concat(out, '\n'))
end

return p