Happy Early Access Launch! Please bear with us: We're doing our best to get the site updated. If we currently don't have what you're looking for, try checking our sister site, beastieball.info.

If you're looking to contribute to the site, we're happy to have you! Check out Beastiepedia:To Do List to see what needs done.

Find us in the Wishes Unlimited Discord in the "Beastiepedia: The Beastieball Wiki" thread in the Forums!

Module:TraitList

From Beastiepedia: The Beastieball Wiki

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

local p = {}
local cargo = mw.ext.cargo
local getArgs = require('Module:Arguments').getArgs

local fieldTransform = {}

function fieldTransform.Name(content)
    return "[[" .. content .. "]]"
end

function p.Main( frame )
    local names = {}
    local where = nil
    local args = getArgs(frame)
    local extraHeader = nil
    local displayFields = {"Name", "Description"}
    local fields = {"Name", "Description"}
    local occurrenceEnabled = false
    local lookingForOccurrence = false
    local occurrences = {}
    local lastTrait = nil
    for k, v in pairs(args) do
        if k == "header" then
            extraHeader = v
        elseif v == "?occurrence" then
            occurrenceEnabled = true
            table.insert(displayFields, "Occurrence")
        elseif k == "where" then
            where = v
        elseif v[1] ~= "?" then
            if lookingForOccurrence then
                occurrences[lastTrait] = v
            else
                lastTrait = v
                table.insert(names, v)
            end
            if occurrenceEnabled then
                lookingForOccurrence = lookingForOccurrence == false
            end
        end
    end
    if #names == 0 and where == nil then
        return "No traits specified"
    end
    local tables = 'Traits'
    local args = {
        where = "Traits.Name IN ('" .. table.concat(names, "','") .. "')",
        limit = 99999,
    }
    if where then
        args.where = where
        names = {}
    end
    local results = cargo.query( tables, table.concat(fields, ","), args )
    local tbl = mw.html.create('table'):addClass('wikitable')
    tbl:css( 'width', '100%' )
    if extraHeader then
        tbl:tag('tr'):tag('th'):attr("colspan", #displayFields):wikitext(extraHeader)
    end
    local header = tbl:tag('tr')
    for k = 1, #displayFields do
        header:tag('th'):wikitext(displayFields[k])
    end
    local namedResults = {}
    for r = 1, #results do
        namedResults[results[r].Name] = results[r]
        if where then
            table.insert(names, results[r].Name)
        end
    end
    for _, name in ipairs(names) do
        local row = namedResults[name]
        local tr = tbl:tag('tr')
        for k = 1, #displayFields do
            local td = tr:tag('td')
            local field = displayFields[k]
            local content = row and row[field]
            if field == "Occurrence" then
                td:wikitext(occurrences[name])
            elseif field == "Type" then
                local format = typeFormat[content]
                td:wikitext((format and format.value) or content)
                if format then
                    td:attr("bgcolor", format.bg)
                end
            else
                content = content or (field == "Name" and name) or "--"
                if fieldTransform[field] then
                    content = fieldTransform[field](content)
                end
                if field == "Pow" and row and not hasPow[row["Type"]] then
                    content = "--"
                end
                td:wikitext(frame:preprocess(content))
            end
        end
    end
    return tbl
end

return p