Module:SkillTable

From Elwiki
Revision as of 13:23, 10 August 2022 by Ritsu (talk | contribs)

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

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

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

    -- Main block
    local skill_table = mw.html.create('div'):addClass('related-skills shown skill-table');

    local iterations = args['i'];
    if iterations == nil then
        iterations = 4
    end

    -- Split into two tables for easier maintenance of the code.
    local skills = args.skills;
    if (skills == nil) then
        skills = args[1]
    end
    local skills = split(skills, ';')
    local image_name_table = {}
    local skill_name_table = {}
    local index = 1
    for k, v in ipairs(skills) do
        v = trim(v);
        if (index % 2 == 0) then
            -- Skill names
            table.insert(skill_name_table, v);
        else
            -- Image names
            table.insert(image_name_table, v);
        end
        index = index + 1
    end

    -- Iterate through rows
    local i = 1;
    -- Spawn a table row.
    local row = skill_table:tag('div'):attr('data-type', 'table-row');
    while i <= #skill_name_table do
        local image = image_name_table[i];
        local name = skill_name_table[i];
        local lock = ''
        local mod_subpage = '';

        -- Locked skills
        if (string.find(name, ' Locked-')) then
            local lock_arg = name:gsub('.*%sLocked%-', '');
            name = name:gsub('%sLocked%-.*', '');
            lock = frame:expandTemplate{
                title = 'Locked Skill',
                args = {lock_arg}
            }
        end

        -- Mod skills
        if (string.find(name, '/Mod')) then
            name = name:gsub('%/Mod', '');
            mod_subpage = '/ModA';
        end

        -- Check if the user wants to render a header or not.
        local is_header = false;
        if (string.starts(image, '--')) then
            is_header = true;
            table.remove(image_name_table, i);
            table.remove(skill_name_table, i);
        end

        -- Make chunks of skill boxes based on user preference (default 4).
        if ((i-1) % iterations == 0 or image_name_table[i-1] == nil) then
            row = skill_table:tag('div'):attr('data-type', 'table-row');
        end

        -- Spawn regular cells
        if not (is_header) then
            local image_cell = row:tag('div'):attr('data-type', 'table-cell'):attr('style', 'width:54px');
            if (mod_subpage == '') then
                image_cell:wikitext('[[File:' .. image .. '|link=' .. name .. ']]')
            else
                image_cell:wikitext(frame:expandTemplate{
                    title = 'Force',
                    args = {image, link = name}
                });
            end
            local name_cell = row:tag('div'):attr('data-type', 'table-cell'):attr('style', 'width:150px');
            local skill_wrap = name_cell:tag('div'):addClass('skill-wrap');
            skill_wrap:wikitext('[[' .. name .. mod_subpage .. '|.]]');

            local display_name = name:gsub("%s%([%w%s%/]+%)", "");
            if not (mod_subpage == '') then
                display_name = '[Mod] ' .. display_name;
            end
            
            skill_wrap:tag('div'):addClass('skill-wrap-text'):wikitext('[[' .. name .. mod_subpage .. '|' .. display_name .. ']] ' .. lock);
            
            -- If a header is detected, it will not interrupt chunking.
            i = i + 1
        else
            -- Spawn a header
            local color = image:gsub('%-%-', '');
            -- Set color, either hardcoded or from the ColorSel template.
            if not (string.find(image, "#") or string.find(image, "rgb(") or string.find(image, "rgba(")) then
                color = frame:expandTemplate{
                    title = 'ColorSel',
                    args = {'Character', color}
                }
            end
            if (color == '&#35;') then
                color = frame:expandTemplate{
                    title = 'ColorSel',
                    args = {'Misc'}
                }
            end
            row = skill_table:tag('div'):attr('data-type', 'table-row'):attr('style', 'background-color: ' .. color):attr('class', 'textfloat table-head');
            row:tag('div'):attr('data-type', 'table-head'):wikitext(name);
        end
    end

    -- Output point
    return tostring(skill_table);

end

return p