Module:SS Medals

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 21:55, 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"] and type(data[id]["class"]) == "table" then
	        if #data[id]["class"] == 1 then
	            class = data[id]["class"][1]  -- Use the single class if there's only one
	        end
	    end
	    return string.format("Unit_Frame_%s_%s.png", rarity, class)
	elseif param == "coinImage" then
        local rarity = data[id]["rarity"] or "None"
        return string.format("Unit_Coin_%s.png", rarity)
    elseif param == "bgImage" then
        local rarity = data[id]["rarity"] or "None"
        return string.format("Unit_bg_%s.png", rarity)
	 -- Handling specific requests for 'class' parameter
	elseif param == "class" then
	    local value = data[id][param]
	    if value and type(value) == "table" then
	        if #value == 1 then
	            return value[1]  -- Return the single class directly if there's only one
	        else
	            local result = {}
	            for _, v in ipairs(value) do
	                table.insert(result, "{{SSIcon|" .. v .. "}}")
	            end
	            return frame:preprocess(table.concat(result))
	        end
	    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