Module:SS Coin

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Jump to navigation Jump to search

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

local p = {}

function p.getCharacterIDs(frame)
    local characterName = frame.args[1]  -- The character name to search for
    if not characterName or characterName == '' then
        return "No character name provided."  -- Return a message if no character name is given
    end

    local data = mw.loadJsonData('JoJo Wiki:SS Medals')
    local matches = {}
    
    -- Collect all entries that match the character name along with their rarity
    for id, entry in pairs(data) do
        local characters = entry.character
        if type(characters) == "table" then
            for _, name in ipairs(characters) do
                if name == characterName then
                    table.insert(matches, {id = id, rarity = entry.rarity})
                    break
                end
            end
        elseif type(characters) == "string" and characters == characterName then
            table.insert(matches, {id = id, rarity = entry.rarity})
        end
    end

    -- Define rarity order for sorting
    local rarityOrder = {
        SSR = 1,  -- Most valuable
        SR = 2,
        R = 3,
        N = 4   -- Least valuable
    }

    -- Sorting function to sort by rarity first and then by ID within each rarity
    table.sort(matches, function(a, b)
        local rarityA = rarityOrder[a.rarity] or 5  -- Default to lowest if undefined
        local rarityB = rarityOrder[b.rarity] or 5
        if rarityA == rarityB then
            local idA = tonumber(a.id:match("%d+"))
            local idB = tonumber(b.id:match("%d+"))
            return idA < idB
        else
            return rarityA < rarityB
        end
    end)

    if #matches == 0 then
        return "No matches found for " .. characterName -- Return a message if no matches are found
    end

    -- Format the matches into template calls
    local output = {}
    for _, match in ipairs(matches) do
        table.insert(output, frame:preprocess('{{CoinS2|' .. match.id .. '}}'))
    end
    return table.concat(output, "\n")
end

return p