Module:SS Medals

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 19:21, 7 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 = {}

-- Helper function to determine the correct class value, handling single, multiple, or no classes appropriately.
-- Accepts frame for preprocessing SSIcon templates when forDisplay is true.
local function getClass(data, id, frame, forDisplay)
    local class = data[id]["class"]
    if type(class) == "string" then
        -- If the class is a string and forDisplay is true, process it with SSIcon template.
        if forDisplay then
            return frame:preprocess("{{SSIcon|" .. class .. "}}")
        else
            return class
        end
    elseif type(class) == "table" then
        -- If the class is a table, check for single or multiple entries.
        if #class == 1 then
            -- For a single class, return it directly or as an SSIcon.
            if forDisplay then
                return frame:preprocess("{{SSIcon|" .. class[1] .. "}}")
            else
                return class[1]
            end
        elseif forDisplay then
            -- For multiple classes, compile all into SSIcon strings.
            local result = {}
            for _, v in ipairs(class) do
                table.insert(result, frame:preprocess("{{SSIcon|" .. v .. "}}"))
            end
            return table.concat(result)
        end
    end
    -- Return "None" if no appropriate class information is found.
    return "None"
end

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 and specific class-based requests
    if param == "frameImage" then
        local rarity = data[id]["rarity"] or "None"
        local class = getClass(data, id, frame, false)
        -- Construct the frame image filename
        return string.format("Unit_Frame_%s_%s.png", rarity, class)
    elseif param == "ability" then
        local class = getClass(data, id, frame, false)
        -- Construct the ability image filename
        return string.format("Unit_Ability_%s.png", class)
    elseif param == "coinImage" then
        -- Generate the coin image filename based on rarity
        return string.format("Unit_Coin_%s.png", data[id]["rarity"] or "None")
    elseif param == "bgImage" then
        -- Generate the background image filename based on rarity
        return string.format("Unit_bg_%s.png", data[id]["rarity"] or "None")
    elseif param == "class" then
        -- Process and return the class, potentially formatted with SSIcon
        return getClass(data, id, frame, true)

    -- Handling specific requests for 'character' parameters
    elseif param.match("^character%d+$") then
        local index = tonumber(param:match("%d+"))
        local characters = data[id]["character"]
        -- Retrieve specific character based on index if available
        if characters and type(characters) == "table" and characters[index] then
            return characters[index]
        else
            return "None"
        end
    elseif param == "character" then
        -- Process and return character information, handling single or multiple entries
        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
        -- Standard parameter handling, returning direct values or concatenated lists
        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