Module:SS Medals

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 21:59, 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)
    local data = mw.loadJsonData('JoJo Wiki:SS Medals')
    local id = frame.args[1]
    local param = frame.args[2]
	
	-- Check if data for the given ID exists
    if not data[id] then
        return "N/A"
    end

    -- Handle image file name generation
    if param == "frameImage" then
        local rarity = data[id]["rarity"] or "None"
        local class = "None"  -- Default class to "None"
        if data[id]["class"] then
            if type(data[id]["class"]) == "table" and #data[id]["class"] == 1 then
                class = data[id]["class"][1]
            elseif type(data[id]["class"]) == "string" then
                class = data[id]["class"]
            end
        end
        return string.format("Unit_Frame_%s_%s.png", rarity, class)
    elseif param == "coinImage" then
        return string.format("Unit_Coin_%s.png", data[id]["rarity"] or "None")
    elseif param == "bgImage" then
        return string.format("Unit_bg_%s.png", data[id]["rarity"] or "None")
    elseif param == "class" then
        if type(data[id][param]) == "table" then
            if #data[id][param] == 1 then
                return data[id][param][1]
            else
                local result = {}
                for _, v in ipairs(data[id][param]) do
                    table.insert(result, "{{SSIcon|" .. v .. "}}")
                end
                return frame:preprocess(table.concat(result))
            end
        elseif type(data[id][param]) == "string" then
            return frame:preprocess("{{SSIcon|" .. data[id][param] .. "}}")
        else 
            return "None"
        end
    elseif param:match("^character%d+$") then
        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
        if type(data[id]["character"]) == "table" then
            if #data[id]["character"] == 1 then
                return data[id]["character"][1]
            else
                return table.concat(data[id]["character"], ", ")
            end
        elseif type(data[id]["character"]) == "string" then
            return data[id]["character"]
        else
            return "None"
        end
    else
        local value = data[id][param]
        return value and (type(value) == "table" and table.concat(value, ", ") or tostring(value)) or "None"
    end
end

return p