Module:Traits

From Elwiki
Revision as of 17:51, 21 August 2022 by Ritsu (talk | contribs)

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

require('Module:CommonFunctions')
local getArgs = require('Module:Arguments').getArgs
local p = {}

-- Main process
function p.main(frame)
    local args = getArgs(frame);

    -- Argument init
    local traits = args[3]
    if (traits == nil) then
        traits = '-, -'
    end
    traits = split(traits);
    local skill = args[2]

    -- Attribute effect
    local effects = {
        [1] = {},
        [2] = {}
    }
    local e1 = args.effect1;
    local e2 = args.effect2;
    if args.effect1 == nil then
        e1 = args[4]
    end
    if args.effect2 == nil then
        e2 = args[5]
    end
    if e1 ~= nil then
        effects[1] = split(e1)
    end
    if e2 ~= nil then
        effects[2] = split(e2)
    end

    local desc = {
        [1] = {},
        [2] = {}
    }

    -- Extra effects
    local counts = {}
    local desc_arg = nil;
    local header_data = {
        [1] = {},
        [2] = {}
    }
    for i = 1, 2, 1 do
        if (i == 1) then
            desc_arg = args[6]
        else
            desc_arg = args[7]
        end

        if desc_arg == nil then
            desc_arg = args['desc' .. i]
        end

        if desc_arg ~= nil then
            local headers = split(desc_arg, ';')[1];
            local data = split(desc_arg, ';')[2];
            headers = split(headers);

            for k, v in pairs(headers) do
                table.insert(header_data[i], v)
            end

            data = split(data);
            counts[i] = 1 + #headers;

            local index = 1
            while index <= #headers do
                desc[i][headers[index]] = data[index]
                index = index + 1
            end
        end

    end

    -- Main block
    local trait_table = mw.html.create('table'):attr({
        ['cellpadding'] = '5',
        ['border'] = '1'
    }):css({
        ['border-collapse'] = 'collapse',
        ['text-align'] = 'center'
    })

    -- Get table head color
    local function color(char)
        char = char or args[1]
        return frame:expandTemplate{
            title = 'ColorSel',
            args = {'CharLight', char}
        }:gsub("&#35;", "#")
    end

    Tr = {}
    function Tr:new()
        return trait_table:tag('tr'):css('background-color', color())
    end

    -- Spawn headers
    local thead = Tr:new()
    thead:tag('th'):attr('colspan', counts[1]):wikitext(traits[1] .. ' ' .. skill)
    thead:tag('th'):attr('colspan', counts[2]):wikitext(traits[2] .. ' ' .. skill)

    local tr_2 = Tr:new();
    for i = 1, 2, 1 do
        tr_2:tag('th'):wikitext('Attribute Effect')
        if (args['effects' .. i] ~= nil) then
            for k, v in pairs(header_data[i]) do
                v = frame:preprocess(v);
                tr_2:tag('th'):wikitext(v)
            end
        end
    end

    -- Spawn information
    local tr = trait_table:tag('tr')
    for i = 1, 2, 1 do
        tr:tag('td'):wikitext(frame:expandTemplate{
            title = 'SkillText',
            args = {traits[i], effects[i][1], effects[i][2]}
        })
        for k, v in pairs(desc[i]) do
            tr:tag('td'):wikitext(frame:preprocess(v))
        end
    end

    -- Output point
    return tostring(trait_table);

end

return p