Module:Traits: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 8: Line 8:


     -- Argument init
     -- Argument init
     local traits = args[3]
     local traits = args[3] or args.traits
     if (traits == nil) then
     if (traits == nil) then
         traits = '-, -'
         traits = '-, -'
     end
     end
     traits = split(traits);
     traits = split(traits);
     local skill = args[2]
    for k, v in ipairs(traits) do
        traits[k] = trim(v)
    end
     local skill = args[2] or args.skill
 
    -- Dictionary for headers
    local headers = {'MP Usage', 'Cooldown', 'Duration', 'MP Recovery', 'Max Hits'}
    local header_dict = {
        [1] = {'Light', 'Critical', 'Reversed'},
        [2] = {'Heavy', 'Haste', 'Regenerating (2)', 'Ruthless', 'Powerful', 'Reversed'},
        [3] = {'Killing Blow (1)'},
        [4] = {'Regenerating (1)'},
        [5] = {'Useful'}
    }


     -- Attribute effect
     -- Default values
     local effects = {
     local default_trait_values = {
         [1] = {},
         Heavy = {
         [2] = {}
            multi_cooldown = 120
        },
        Light = {
            multi_mp_cost = 80
        },
        Critical = {
            multi_mp_cost = 120
        },
        Haste = {
            multi_cooldown = 80
        },
        Ruthless = {
            multi_cooldown = 200
        },
         Powerful = {
            multi_cooldown = 120
        }
     }
     }
    args.effects1 = args.effects1 or args[4]
    args.effects2 = args.effects2 or args[5]
    if args.effects1 ~= nil then
        effects[1] = split(args.effects1)
    end
    if args.effects2 ~= nil then
        effects[2] = split(args.effects2)
    end


     local desc = {}
     -- Define the class blueprint for traits.
    Trait = {}


     -- Extra effects
     function Trait:new(t)
    local counts = {}
         t = t or {}
    local desc_arg = nil;
    local header_data = {}
    local headers={};
    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
         t.details_mp_cost = {args.mp, args.mp_pvp, args.mp_enhanced, args.mp_enhanced_pvp}
            desc_arg = args['desc' .. i]
        t.details_cooldown = {args.cooldown, args.cooldown_pvp, args.cooldown_enhanced, args.cooldown_enhanced_pvp}
         end
         t.details_duration = {args.duration, args.duration_pvp, args.duration_enhanced, args.duration_enhanced_pvp}


         if desc_arg ~= nil then
         t.colspan = 1
            traits[i] = trim(traits[i]);
        t.colspan = t.colspan + table.matches(header_dict, t.name)
            local data_split = split(desc_arg, ';');
            --local headers;
            local data;


            if (#data_split >= 2) then
        t.multi_mp_cost = args['mp' .. t.index] or 100
                headers = split(data_split[1]);
        t.multi_cooldown = args['cd' .. t.index] or 100
                data = data_split[2];
        t.multi_duration = args['duration' .. t.index] or 100
            else
                data = data_split[1];


                -- Generate headers based on trait.
        -- Set defaults for generic trait values.
                if (traits[i] == 'Light' or traits[i] == 'Critical') then
        local default = default_trait_values[t.name]
                    headers = {
        if (default ~= nil) then
                        [1] = 'MP Usage'
            for k, v in pairs(default) do
                    }
                 if (t[k] == 100) then
                elseif (traits[i] == 'Heavy' or traits[i] == 'Haste' or traits[i] == 'Regenerating (2)' or traits[i] ==
                     t[k] = v
                    'Ruthless' or traits[i] == 'Powerful') then
                    headers = {
                        [1] = 'Cooldown'
                    }
                elseif (traits[i] == 'Reversed') then
                    headers = {
                        [1] = 'MP Usage',
                        [2] = 'Cooldown'
                    }
                elseif (traits[i] == 'Regenerating (1)') then
                    headers = {
                        [1] = 'MP Recovery'
                    }
                 elseif (traits[i] == 'Killing Blow (1)') then
                     headers = {
                        [1] = 'Duration'
                    }
                else
                    headers = {}
                 end
                 end
             end
             end
        end


            if (header_data[i] == nil) then
        t.chance = args['chance' .. t.index]
                header_data[i] = {}
            end


            for k, v in spairs(headers) do
        t.unnamed_2 = args['desc1_trait' .. t.index]
                table.insert(header_data[i], trim(v))
        t.unnamed_3 = args['desc2_trait' .. t.index]
            end


            data = split(data);
        self.__index = self
            counts[i] = 1 + #headers;
        setmetatable(t, self)


            if (desc[i] == nil) then
        return t
                desc[i] = {}
    end
            end


             local index = 1
    -- Calculation method
             while index <= #headers do
    function Trait:calc()
                desc[i][index] = {}
        for k, v in pairs(self) do
                desc[i][index][headers[index]] = trim(data[index])
             local mp_done = false
                 index = index + 1
             if (string.find(k, 'details_') and next(v)) then
                for k2, v2 in ipairs(v) do
                    local current_data = k:gsub('details_', '')
                    self[k][k2] = self['multi_' .. current_data] / 100 * v2
                    if current_data == 'mp_cost' or current_data == 'mp_recovery' then
                        self[k][k2] = self[k][k2] .. ' MP'
                    else
                        self[k][k2] = self[k][k2] .. ' Seconds'
                    end
                 end
             end
             end
         end
         end
        return self
    end


     end
     local trait_left = Trait:new({
        name = traits[1],
        index = 1
    })


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


Line 130: Line 125:
         }:gsub("&#35;", "#")
         }:gsub("&#35;", "#")
     end
     end
    -- Main block
    local trait_table = mw.html.create('table'):attr({
        ['cellpadding'] = '5',
        ['border'] = '1'
    }):css({
        ['border-collapse'] = 'collapse',
        ['text-align'] = 'center'
    })


     function newtr()
     function newtr()
Line 135: Line 139:
     end
     end


    -- Spawn headers
     local thead = newtr()
     local thead = newtr()
     local no_desc;
     local tr = newtr()
     local tr_2 = newtr();
     local tr_2 = newtr()


     for i = 1, 2, 1 do
     -- Header spawning method
         thead:tag('th'):attr('colspan', counts[i]):wikitext(traits[i] .. ' ' .. skill)
    function Trait:do_headers()
         no_desc = args['desc' .. i] == nil and args[6] == nil and args[7] == nil;
        local function getCustomHeader(spawn)
            local i = 1
            while true do
                local custom_header = args['header' .. i .. '_trait' .. self.index]
                if custom_header ~= nil then
                    if spawn then
                        tr_2:tag('th'):wikitext(custom_header)
                    end
                    i = i + 1
                else
                    break
                end
            end
            return i-1
        end
 
         thead:tag('th'):attr('colspan', self.colspan + getCustomHeader()):wikitext(self.name .. ' ' .. skill)
         local has_details = table.matches(header_dict, self.name);


         tr_2:tag('th'):wikitext('Attribute Effect')
         tr_2:tag('th'):wikitext('Attribute Effect')
         if not (no_desc or header_data[i] == nil) then
         if has_details then
             for k, v in ipairs(header_data[i]) do
            if self.multi_mp_cost ~= 100 then
                 v = frame:preprocess(v);
                if self.name == 'Regenerating (1)' then
                 tr_2:tag('th'):wikitext(v)
                    tr_2:tag('th'):wikitext('MP Recovery')
                else
                    tr_2:tag('th'):wikitext('MP Usage')
                end
            end
             if self.multi_cooldown ~= 100 then
                tr_2:tag('th'):wikitext('Cooldown')
            end
            if self.multi_duration ~= 100 then
                 tr_2:tag('th'):wikitext('Duration')
            end
            if self.name == 'Useful' then
                 tr_2:tag('th'):wikitext('Max Hits')
             end
             end
         end
         end
        getCustomHeader(true)
     end
     end


     -- Spawn information
     trait_left:do_headers()
    trait_right:do_headers()
 
    trait_left:calc()
    trait_right:calc()
 
     local tr = trait_table:tag('tr')
     local tr = trait_table:tag('tr')
     for i = 1, 2, 1 do
 
        no_desc = args['desc' .. i] == nil and args[6] == nil and args[7] == nil;
     function Trait:do_content()
         tr:tag('td'):wikitext(frame:expandTemplate{
         tr:tag('td'):wikitext(frame:expandTemplate{
             title = 'SkillText',
             title = 'SkillText',
             args = {traits[i], effects[i][1], effects[i][2]}
             args = {
                self.name,
                self.unnamed_2,
                self.unnamed_3,
                MP = self.multi_mp_cost,
                CD = self.multi_cooldown,
                DURATION = self.multi_duration,
                CHANCE = self.chance
            }
         })
         })
        if not (no_desc or desc[i] == nil) then
            for k, v in ipairs(desc[i]) do
                for k2, v2 in pairs(desc[i][k]) do
                    -- Decide the order of headers when added automatically
                    local expr_header_table = {
                        [1] = 'Cooldown',
                        [2] = 'MP Usage',
                        [3] = 'MP Recovery',
                        [4] = 'Duration'
                    }
                    if (table.containsValue(expr_header_table, k2)) then
                        if not string.find(v2, "%*") then
                            -- Solve formulas automatically
                            if (table.containsValue({'Light', 'Critical', 'Haste', 'Ruthless', 'Powerful', 'Heavy'}, traits[i]) or (traits[i] == 'Reversed' and k2 == expr_header_table[2])) then


                                -- Default multipliers from SkillText
        local function addIfMultiExists(param_tbl)
                                if (effects[i][1] == nil) then
            for k, v in ipairs(param_tbl) do
                                    if (table.containsValue({'Light', 'Haste'}, traits[i])) then
                if (self['multi_' .. v] ~= 100) then
                                        effects[i][1] = 80
                    tr:tag('td'):wikitext(self['details_' .. v][1])
                                    elseif (table.containsValue({'Critical', 'Heavy'}, traits[i])) then
                end
                                        effects[i][1] = 120
            end
                                    elseif (traits[i] == 'Ruthless') then
        end
                                        effects[i][1] = 200
 
                                    elseif (traits[i] == 'Powerful') then
        addIfMultiExists({'mp_cost', 'cooldown', 'duration'})
                                        effects[i][1] = 150
                                    end
                                end


                                v2 = effects[i][1] * (v2 / 100)
        local function getCustomContent()
                            elseif (string.find(traits[i], 'Regenerating') or (traits[i] == 'Reversed' and k2 == expr_header_table[1])) then
            local i = 1
                                v2 = effects[i][2] * (v2 / 100)
            while true do
                            end
                local custom_content = args['detail' .. i .. '_trait' .. self.index]
                        else
                if custom_content ~= nil then
                            v2 = '{{#expr:' .. v2 .. '}}'
                     tr:tag('td'):wikitext(custom_content)
                        end
                    i = i + 1
                       
                else
                        -- Automatic prefixes
                    break
                        if (k2 == expr_header_table[2] or k2 == expr_header_table[3]) then
                            v2 = v2 .. ' MP'
                        else
                            v2 = v2 .. ' Seconds'
                        end
                    end
                     tr:tag('td'):wikitext(frame:preprocess(v2))
                 end
                 end
             end
             end
         end
         end
        getCustomContent()
           
     end
     end


     -- Output point
     trait_left:do_content()
    trait_right:do_content()
 
     return tostring(trait_table);
     return tostring(trait_table);
    -- return dump(trait_left) .. '<br><br>' .. dump(trait_right);


end
end


return p
return p