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 itemId
    if type(args) == "string" then
        itemId = args
    elseif type(args) == "table" then
        itemId = args.args[1]
    end
    mw.log("getting info from " .. itemId)

    local data, errors = mw.ext.externalData.getExternalData("https://api.awakening.wiki/items/" .. itemId)
    local json = not errors and data.__json or false
    return json
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",
        "energy_mitigation",
        "explosive_mitigation",
        "fire_mitigation",
        "poison_mitigation",
        "radiation_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 = data.image
                local icon = file and ('[[File:' .. file .. '|frameless|32x32px]]') or ''
                -- conditional link
                table.insert(list,
                    '* ' .. icon ..
                    '{{#ifeq:{{FULLPAGENAME}}|' .. data.name .. '|<span class="mw-selflink selflink">[[' .. data.name .. ']]</span>|[[' .. data.name .. ']]}}'
                )

                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
            else
                return frame:preprocess("[[Category:ArmorSet with bad data]]")
            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