Module:ProcessJobs

From HorizonXI Wiki
Revision as of 13:26, 24 March 2024 by Edwardcd (talk | contribs) (Allows users to enter in job abbrvs in any order and without wiki tags or era tags and processes it automatically.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

--- Job Processor for ITEMS, uses the Scribunto module (LUA).
--  Let's users enter in jobs without tags or worring about
--  what era the specific job comes from.
--  Output will be a clean and uniform, correctly displayed,
--  job list for any equipable item, era tagged and all.
--
--  Written by edwardcd
--  last modification: 24 March 2024
--  version 0.1a (testing)
--
-- Module:ProcessJobsModule
local p = {}

function p.processJobs(frame)
    local jobsString = frame.args.jobs or ''
    
    -- Convert the jobs string into an array
    -- Removes spaces and punctuation
    local jobsArray = {}
    for job in jobsString:upper():gmatch("%w+") do
        table.insert(jobsArray, job)
    end
    
    -- Define job codes in the desired order, as per ASB
    local jobCodes = {
        "WAR", "MNK", "WHM", "BLM", "RDM", "THF", "PLD", "DRK",
        "BST", "BRD", "RNG", "SAM", "NIN", "DRG", "SMN", "BLU",
        "COR", "PUP", "DNC", "SCH", "GEO", "RUN"
    }
    
    -- Create a lookup table for jobCodes
    local jobCodesLookup = {}
    for i, v in ipairs(jobCodes) do
        jobCodesLookup[v] = i
    end
    
    -- Define era spans for specific jobs
    local specialSpans = {
        toau = {["BLU"] = true, ["COR"] = true, ["PUP"] = true},
        wotg = {["DNC"] = true, ["SCH"] = true},
        soa = {["GEO"] = true, ["RUN"] = true}
    }
    
    -- Filter out invalid codes
    -- Sort the valid codes 
    local processedJobs = {}
    for _, job in ipairs(jobsArray) do
        if jobCodesLookup[job] then
            table.insert(processedJobs, job)
        end
    end
    
    local processedJobs = {}
    for _, job in ipairs(jobsArray) do
        if jobCodesLookup[job] then
            local spanStart, spanEnd = "", ""
            if specialSpans.toau[job] then
                spanStart, spanEnd = '<span class="toau">', '</span>'
            elseif specialSpans.wotg[job] then
                spanStart, spanEnd = '<span class="wotg">', '</span>'
            elseif specialSpans.soa[job] then
                spanStart, spanEnd = '<span class="soa">', '</span>'
            end
            table.insert(processedJobs, spanStart .. "[[" .. job .. "]]" .. spanEnd)
        end
    end
    
    table.sort(processedJobs, function(a, b) return jobCodesLookup[a:match("%w+")] < jobCodesLookup[b:match("%w+")] end)
    
    local output = table.concat(processedJobs, ' / ')
    
    return output
end

return p