Module:SS Medals

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 21:26, 2 June 2024 by Vish (talk | contribs)
Jump to navigation Jump to search

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

local p = {}

function p.getData(frame)
    -- Load JSON data directly from the page "JoJo Wiki:SS Medals"
    local data = mw.loadJsonData('JoJo Wiki:SS Medals')

    -- Retrieve the ID and parameter to return from the passed frame arguments
    local id = frame.args[1]
    local param = frame.args[2]  -- Parameter to return (e.g., title, rarity, character)

    -- Check if data for the given ID exists
    if not data[id] then
        return "N/A"
    end

    -- Handling specific requests for 'class' parameter
    if param == "class" then
        local value = data[id][param]
        if value and type(value) == "table" then
            local result = {}
            for _, v in ipairs(value) do
                table.insert(result, "{{SSIcon|" .. v .. "}}")
            end
            return frame:preprocess(table.concat(result))
        else
            return "None"
        end
    elseif param:match("^character%d+$") then
        -- Extract the index from the parameter name (e.g., "character1" -> 1)
        local index = tonumber(param:match("%d+"))
        local characters = data[id]["character"]
        if characters and type(characters) == "table" and characters[index] then
            return characters[index]
        else
            return "None"
        end
    elseif param == "character" then
        -- Handle general 'character' parameter when no specific index is provided
        local characters = data[id]["character"]
        if characters and type(characters) == "table" then
            if #characters == 1 then
                return characters[1] -- Return the single character if there's only one
            else
                return table.concat(characters, ", ") -- Concatenate with comma if multiple
            end
        else
            return "None"
        end
    else
        -- Standard parameter handling
        local value = data[id][param]
        if value then
            if type(value) == "table" then
                return table.concat(value, ", ")
            else
                return tostring(value)
            end
        else
            return "None"
        end
    end
end

return p