Module:Traits: Difference between revisions

From Elwiki
No edit summary
m (add i18n)
 
(41 intermediate revisions by 2 users not shown)
Line 1: Line 1:
-- pystart
-- pystart
require('Module:CommonFunctions')
require('Module:CommonFunctions')
local i18n = require('Module:I18n')
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs
local getTranslations = i18n.getTranslations
local p = {}
local p = {}


-- Main process
-- Main process
function p.main(frame)
function p.main(frame)
     local args = getArgs(frame);
     local args = getArgs(frame)
    local tr = getTranslations(frame, 'Template:Traits', args.lang, true)
    local is_rose = args[1] == 'Rose' and args.ecp == 'true'
 
    function translate(key)
        return i18n.translate(tr, key)
    end


     -- Argument init
     -- Argument init
Line 21: Line 29:


     -- Dictionary for headers
     -- Dictionary for headers
     local prop = {'MP Usage', 'Cooldown', 'Duration', 'MP Recovery', 'Max Hits'}
     local prop = {  
        is_rose and translate('ECP Usage') or translate('MP Usage'),
        translate('Cooldown'),
        translate('Duration'),
        is_rose and translate('ECP Recovery') or translate('MP Recovery'),
        translate('Max Hits'),
        translate('Effects'),
    }
 
    local prop_short = { 'mp', 'cd', 'duration', 'mp_recovery', 'hits', 'effects', 'chance' }


     local prop_short = {'mp', 'cd', 'duration', 'mp_recovery', 'hits', 'chance'}
     local OPTIONS = {
        lang_suffix = args.lang and ('/' .. args.lang) or '',
        lang_append = args.lang ~= nil and args.lang ~= ''
    }


     local STR = {
     local STR = {
Line 38: Line 58:
         POWERFUL = 'Powerful',
         POWERFUL = 'Powerful',
         USEFUL = 'Useful',
         USEFUL = 'Useful',
         SEC = ' Seconds',
         SEC = 'Seconds',
         MP = ' MP'
         MP = 'MP',
        ECP = 'ECP',
        PERSISTENT = 'Persistent',
        PERSISTENT2 = 'Persistent2'
     }
     }


     local details = {{STR.LIGHT, STR.CRITICAL, STR.REVERSED},
     local details = {
                    {STR.HEAVY, STR.HASTE, STR.REGEN2, STR.RUTHLESS, STR.POWERFUL, STR.REVERSED}, {STR.KB1},
        { STR.LIGHT, STR.CRITICAL, STR.REVERSED },
                    {STR.REGEN1}, {STR.USEFUL}}
        { STR.HEAVY, STR.HASTE,   STR.REGEN2, STR.RUTHLESS, STR.POWERFUL, STR.REVERSED },
        { STR.KB1 },
        { STR.REGEN1 },
        { STR.USEFUL, STR.PERSISTENT2 },
        { STR.KB2 }
    }


     -- Default values
     -- Default values
Line 55: Line 83:
         },
         },
         [STR.CRITICAL] = {
         [STR.CRITICAL] = {
             mp = 120
             mp = (args['def_ignore1'] ~= nil or args['def_ignore2'] ~= nil) and 100 or 120
         },
         },
         [STR.HASTE] = {
         [STR.HASTE] = {
Line 68: Line 96:
         [STR.REGEN1] = {
         [STR.REGEN1] = {
             chance = 50,
             chance = 50,
             mp_recovery = 50
             mp = 50
         },
         },
         [STR.REGEN2] = {
         [STR.REGEN2] = {
Line 80: Line 108:
         [STR.USEFUL] = {
         [STR.USEFUL] = {
             dmg = 80
             dmg = 80
        },
        [STR.PERSISTENT2] = {
            dmg = 100
         }
         }
     }
     }
Line 105: Line 136:
     local tr2 = new_row()
     local tr2 = new_row()
     local tr3 = new_row()
     local tr3 = new_row()
    local has_pvp_values = false
    for k, v in pairs(args) do
        if string.find(k, '_pvp') then
            has_pvp_values = true
            break
        end
    end
    local tr4
    -- If pvp values exist, we need the row to place them.
    if has_pvp_values and not tr4 then
        tr4 = new_row()
        -- Add indicator headers
        tr1:tag('th'):wikitext(translate('Mode')):attr('rowspan', 2)
        tr3:tag('td'):wikitext(frame:expandTemplate {
            title = translate('PvE')
        })
        tr4:tag('td'):wikitext(frame:expandTemplate {
            title = translate('PvP')
        })
    end


     -- Loop through 2 input traits.
     -- Loop through 2 input traits.
     for trait_count, trait_name in ipairs(traits) do
     for trait_count, trait_name in ipairs(traits) do
         local th = tr1:tag('th'):wikitext(trait_name .. ' ' .. skill);
         local th = tr1:tag('th'):wikitext(trait_name:gsub(translate(STR.PERSISTENT) .. 2, translate(STR.PERSISTENT)) .. ' ' .. skill);
         local th_effect;
         local th_effect;
         local th_skilltext;
         local th_skilltext;
        local detail_list = {}
         local default_value = DEFAULT[trait_name] or {};
         local default_value = DEFAULT[trait_name] or {};


Line 117: Line 169:
         for detail_key, detail in ipairs(details) do
         for detail_key, detail in ipairs(details) do
             if not th_effect then
             if not th_effect then
                 th_effect = tr2:tag('th'):wikitext('Attribute Effect');
                 th_effect = tr2:tag('th'):wikitext(translate('Attribute Effect'));
             end
             end
             if indexOf(trait_name, detail) then
             if indexOf(trait_name, detail) then
                 local th_detail = tr2:tag('th'):wikitext(prop[detail_key]);
                 local th_detail = tr2:tag('th'):wikitext(translate(prop[detail_key]));
                 th:attr('colspan', tonumber(th:getAttr('colspan') or 1) + 1)
                 th:attr('colspan', tonumber(th:getAttr('colspan') or 1) + 1)
             end
             end
Line 127: Line 179:
         -- Unnamed argument.
         -- Unnamed argument.
         local unnamed = split(args[3 + trait_count]);
         local unnamed = split(args[3 + trait_count]);
        local MP_ARG = args['mp' .. trait_count] or args['mp_recovery' .. trait_count] or default_value['mp_recovery'] or
            default_value['mp'];
        local ECP_ARG = nil
        if is_rose then
            unnamed[2] = unnamed[1] or args['mp' .. trait_count]
            ECP_ARG = unnamed[1] or args['mp' .. trait_count]
            MP_ARG = nil
        end


         -- Append contents.
         -- Append contents.
         for detail_key, detail in ipairs(details) do
         for detail_key, detail in ipairs(details) do
             if not th_skilltext then
             if not th_skilltext then
                 th_skilltext = tr3:tag('td'):wikitext(frame:expandTemplate{
                 th_skilltext = tr3:tag('td'):attr('rowspan', has_pvp_values and 2 or 1):wikitext(frame:expandTemplate {
                     title = 'SkillText',
                     title = translate('SkillText'),
                     args = {
                     args = {
                         trait_name,
                         trait_name,
                         unnamed[1],
                         unnamed[1],
                         unnamed[2],
                         unnamed[2],
                         MP = args['mp' .. trait_count] or args['mp_recovery' .. trait_count] or default_value['mp'] or
                         MP = args['def_ignore' .. trait_count] ~= nil and 'Energy' or MP_ARG,
                            default_value['mp_recovery'],
                        ECP = ECP_ARG,
                         CD = args['cd' .. trait_count] or default_value['cd'],
                         CD = args['cd' .. trait_count] or default_value['cd'],
                         DURATION = args['duration' .. trait_count],
                         DURATION = args['duration' .. trait_count],
                         CHANCE = args['chance' .. trait_count] or default_value['chance'],
                         CHANCE = args['chance' .. trait_count] or default_value['chance'],
                         DAMAGE = args['dmg' .. trait_count] or default_value['dmg']
                         DAMAGE = args['dmg' .. trait_count] or default_value['dmg'],
                        DEF_IGNORE = args['def_ignore' .. trait_count],
                        DEF_IGNORE_PVP = args['pvp_def_ignore' .. trait_count]
                     }
                     }
                 });
                 });
             end
             end
            if indexOf(trait_name, detail) then
                local short_detail = prop_short[detail_key]
                local short_detail_improved = short_detail


                 -- Fix Regen 1.
            function doDetail()
                if short_detail == 'mp_recovery' then
                 for _, modeSuffix in ipairs({ '', '_pvp' }) do
                    short_detail_improved = 'mp'
                    if indexOf(trait_name, detail) then
                end
                        local short_detail = prop_short[detail_key]
                        local short_detail_improved = short_detail
                        local is_pvp = modeSuffix == '_pvp'
 
                        -- Fix Regen 1.
                        if short_detail == 'mp_recovery' then
                            short_detail_improved = 'mp'
                        end
 
                        local detail_content = args[short_detail_improved .. trait_count]
                        local multiplier = detail_content or default_value[short_detail_improved] or 100
                        local suffix = ''
                        if short_detail_improved == 'mp' then
                            suffix = STR.MP
                            if is_rose then
                                suffix = STR.ECP
                            end
                        elseif short_detail == 'cd' or short_detail == 'duration' then
                            suffix = STR.SEC
                        end
                        suffix = suffix and (' ' .. suffix) or ''
 
                        local base_detail = args[short_detail_improved .. modeSuffix]


                local detail_content = args[short_detail_improved .. trait_count]
                        local function calcEndValue(base)
                local multiplier = detail_content or default_value[short_detail_improved] or 100
                            local end_value;
                local suffix = ''
                            if base and tonumber(base) == nil then
                if short_detail_improved == 'mp' then
                                end_value = base
                    suffix = STR.MP
                            else
                elseif short_detail == 'cd' or short_detail == 'duration' then
                                end_value = (tonumber(multiplier) / 100) * tonumber(base or -1)
                    suffix = STR.SEC
                            end
                end
                            if tonumber(end_value) ~= nil and tonumber(end_value) < 0 then
                                end_value = '-'
                            end
                            return end_value .. suffix
                        end


                local base_detail = args[short_detail_improved]
                        local end_value = calcEndValue(base_detail)


                local function calcEndValue(base)
                        local enhanced_detail = args[short_detail_improved .. modeSuffix .. '_enhanced']
                    local end_value;
                         if enhanced_detail ~= nil then
                    if short_detail == 'hits' then
                            end_value = end_value .. "<br/>'''" .. frame:expandTemplate {
                         end_value = base or 0
                                title = 'Tt',
                    else
                                args = { calcEndValue(enhanced_detail) .. "'''",
                        end_value = (tonumber(multiplier) / 100) * tonumber(base or 0)
                                    translate("Final Enhanced Skill") }
                    end
                            }
                    if tonumber(end_value) ~= nil and tonumber(end_value) <= 0 then
                        end
                        end_value = '-'
                    end
                    return end_value .. suffix
                end


                local end_value = calcEndValue(base_detail)
                        if (is_pvp and base_detail) or not is_pvp then
                            local detail_text = end_value
                            if end_value == '-' and args['detail' .. trait_count] then
                                detail_text = args
                                    ['detail' .. trait_count]
                            end


                if args[short_detail_improved .. '_enhanced'] ~= nil then
                            local detail_cell = (is_pvp and tr4 or tr3):tag('td'):wikitext(detail_text):attr('rowspan', has_pvp_values and (not args[short_detail_improved .. '_pvp']) and 2 or 1);
                    end_value = end_value .. "<br/>'''" .. frame:expandTemplate{
                        end
                        title = 'Tt',
                     end
                        args = {calcEndValue(args[short_detail_improved .. '_enhanced']) .. "'''",
                                "Final Enhanced Skill"}
                     }
                 end
                 end
            end


                local detail_cell = tr3:tag('td'):wikitext(end_value);
            doDetail()
            end
         end
         end
     end
     end


     return tostring(trait_table);
     return tostring(trait_table);
end
end


return p
return p
-- pyend
-- pyend

Latest revision as of 23:43, 14 March 2024

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

-- pystart
require('Module:CommonFunctions')
local i18n = require('Module:I18n')
local getArgs = require('Module:Arguments').getArgs
local getTranslations = i18n.getTranslations
local p = {}

-- Main process
function p.main(frame)
    local args = getArgs(frame)
    local tr = getTranslations(frame, 'Template:Traits', args.lang, true)
    local is_rose = args[1] == 'Rose' and args.ecp == 'true'

    function translate(key)
        return i18n.translate(tr, key)
    end

    -- Argument init
    local traits = args[3] or args.traits
    if (traits == nil) then
        traits = '-, -'
    end
    traits = split(traits);
    for k, v in ipairs(traits) do
        traits[k] = trim(v)
    end
    local skill = args[2] or args.skill
    local default_multiplier = 100

    -- Dictionary for headers
    local prop = { 
        is_rose and translate('ECP Usage') or translate('MP Usage'),
        translate('Cooldown'),
        translate('Duration'),
        is_rose and translate('ECP Recovery') or translate('MP Recovery'),
        translate('Max Hits'),
        translate('Effects'),
    }

    local prop_short = { 'mp', 'cd', 'duration', 'mp_recovery', 'hits', 'effects', 'chance' }

    local OPTIONS = {
        lang_suffix = args.lang and ('/' .. args.lang) or '',
        lang_append = args.lang ~= nil and args.lang ~= ''
    }

    local STR = {
        LIGHT = 'Light',
        CRITICAL = 'Critical',
        REVERSED = 'Reversed',
        HEAVY = 'Heavy',
        HASTE = 'Haste',
        REGEN1 = 'Regenerating (1)',
        REGEN2 = 'Regenerating (2)',
        KB1 = 'Killing Blow (1)',
        KB2 = 'Killing Blow (2)',
        RUTHLESS = 'Ruthless',
        POWERFUL = 'Powerful',
        USEFUL = 'Useful',
        SEC = 'Seconds',
        MP = 'MP',
        ECP = 'ECP',
        PERSISTENT = 'Persistent',
        PERSISTENT2 = 'Persistent2'
    }

    local details = {
        { STR.LIGHT, STR.CRITICAL, STR.REVERSED },
        { STR.HEAVY, STR.HASTE,    STR.REGEN2,  STR.RUTHLESS, STR.POWERFUL, STR.REVERSED },
        { STR.KB1 },
        { STR.REGEN1 },
        { STR.USEFUL, STR.PERSISTENT2 },
        { STR.KB2 }
    }

    -- Default values
    local DEFAULT = {
        [STR.HEAVY] = {
            cd = 120
        },
        [STR.LIGHT] = {
            mp = 80
        },
        [STR.CRITICAL] = {
            mp = (args['def_ignore1'] ~= nil or args['def_ignore2'] ~= nil) and 100 or 120
        },
        [STR.HASTE] = {
            cd = 80
        },
        [STR.RUTHLESS] = {
            cd = 200
        },
        [STR.POWERFUL] = {
            cd = 150
        },
        [STR.REGEN1] = {
            chance = 50,
            mp = 50
        },
        [STR.REGEN2] = {
            chance = 50,
            cd = 50
        },
        [STR.REVERSED] = {
            mp = 60,
            cd = 150
        },
        [STR.USEFUL] = {
            dmg = 80
        },
        [STR.PERSISTENT2] = {
            dmg = 100
        }
    }

    local function color(char)
        char = char or args[1] or args.char or 'Elsword'
        return char:gsub('/', '')
    end

    local trait_table = mw.html.create('div'):attr('class', 'content-table'):tag('table'):attr({
        cellpadding = '5',
        border = '1',
        class = 'colortable-' .. color()
    }):css({
        ['border-collapse'] = 'collapse',
        ['text-align'] = 'center'
    })

    function new_row()
        return trait_table:tag('tr')
    end

    -- headers
    local tr1 = new_row()
    local tr2 = new_row()
    local tr3 = new_row()

    local has_pvp_values = false
    for k, v in pairs(args) do
        if string.find(k, '_pvp') then
            has_pvp_values = true
            break
        end
    end

    local tr4
    -- If pvp values exist, we need the row to place them.
    if has_pvp_values and not tr4 then
        tr4 = new_row()
        -- Add indicator headers
        tr1:tag('th'):wikitext(translate('Mode')):attr('rowspan', 2)
        tr3:tag('td'):wikitext(frame:expandTemplate {
            title = translate('PvE')
        })
        tr4:tag('td'):wikitext(frame:expandTemplate {
            title = translate('PvP')
        })
    end

    -- Loop through 2 input traits.
    for trait_count, trait_name in ipairs(traits) do
        local th = tr1:tag('th'):wikitext(trait_name:gsub(translate(STR.PERSISTENT) .. 2, translate(STR.PERSISTENT)) .. ' ' .. skill);
        local th_effect;
        local th_skilltext;
        local default_value = DEFAULT[trait_name] or {};

        -- Check if detail fields are required and which.
        for detail_key, detail in ipairs(details) do
            if not th_effect then
                th_effect = tr2:tag('th'):wikitext(translate('Attribute Effect'));
            end
            if indexOf(trait_name, detail) then
                local th_detail = tr2:tag('th'):wikitext(translate(prop[detail_key]));
                th:attr('colspan', tonumber(th:getAttr('colspan') or 1) + 1)
            end
        end

        -- Unnamed argument.
        local unnamed = split(args[3 + trait_count]);
        local MP_ARG = args['mp' .. trait_count] or args['mp_recovery' .. trait_count] or default_value['mp_recovery'] or
            default_value['mp'];
        local ECP_ARG = nil

        if is_rose then
            unnamed[2] = unnamed[1] or args['mp' .. trait_count]
            ECP_ARG = unnamed[1] or args['mp' .. trait_count]
            MP_ARG = nil
        end

        -- Append contents.
        for detail_key, detail in ipairs(details) do
            if not th_skilltext then
                th_skilltext = tr3:tag('td'):attr('rowspan', has_pvp_values and 2 or 1):wikitext(frame:expandTemplate {
                    title = translate('SkillText'),
                    args = {
                        trait_name,
                        unnamed[1],
                        unnamed[2],
                        MP = args['def_ignore' .. trait_count] ~= nil and 'Energy' or MP_ARG,
                        ECP = ECP_ARG,
                        CD = args['cd' .. trait_count] or default_value['cd'],
                        DURATION = args['duration' .. trait_count],
                        CHANCE = args['chance' .. trait_count] or default_value['chance'],
                        DAMAGE = args['dmg' .. trait_count] or default_value['dmg'],
                        DEF_IGNORE = args['def_ignore' .. trait_count],
                        DEF_IGNORE_PVP = args['pvp_def_ignore' .. trait_count]
                    }
                });
            end

            function doDetail()
                for _, modeSuffix in ipairs({ '', '_pvp' }) do
                    if indexOf(trait_name, detail) then
                        local short_detail = prop_short[detail_key]
                        local short_detail_improved = short_detail
                        local is_pvp = modeSuffix == '_pvp'

                        -- Fix Regen 1.
                        if short_detail == 'mp_recovery' then
                            short_detail_improved = 'mp'
                        end

                        local detail_content = args[short_detail_improved .. trait_count]
                        local multiplier = detail_content or default_value[short_detail_improved] or 100
                        local suffix = ''
                        if short_detail_improved == 'mp' then
                            suffix = STR.MP
                            if is_rose then
                                suffix = STR.ECP
                            end
                        elseif short_detail == 'cd' or short_detail == 'duration' then
                            suffix = STR.SEC
                        end
                        suffix = suffix and (' ' .. suffix) or ''

                        local base_detail = args[short_detail_improved .. modeSuffix]

                        local function calcEndValue(base)
                            local end_value;
                            if base and tonumber(base) == nil then
                                end_value = base
                            else
                                end_value = (tonumber(multiplier) / 100) * tonumber(base or -1)
                            end
                            if tonumber(end_value) ~= nil and tonumber(end_value) < 0 then
                                end_value = '-'
                            end
                            return end_value .. suffix
                        end

                        local end_value = calcEndValue(base_detail)

                        local enhanced_detail = args[short_detail_improved .. modeSuffix .. '_enhanced']
                        if enhanced_detail ~= nil then
                            end_value = end_value .. "<br/>'''" .. frame:expandTemplate {
                                title = 'Tt',
                                args = { calcEndValue(enhanced_detail) .. "'''",
                                    translate("Final Enhanced Skill") }
                            }
                        end

                        if (is_pvp and base_detail) or not is_pvp then
                            local detail_text = end_value
                            if end_value == '-' and args['detail' .. trait_count] then
                                detail_text = args
                                    ['detail' .. trait_count]
                            end

                            local detail_cell = (is_pvp and tr4 or tr3):tag('td'):wikitext(detail_text):attr('rowspan', has_pvp_values and (not args[short_detail_improved .. '_pvp']) and 2 or 1);
                        end
                    end
                end
            end

            doDetail()
        end
    end

    return tostring(trait_table);
end

return p
-- pyend