Module:Damage: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 8: Line 8:
function p.main(frame)
function p.main(frame)
     local args = getArgs(frame)
     local args = getArgs(frame)
    local out


     function inArgs(key)
     function inArgs(key)
Line 14: Line 15:
         end
         end
     end
     end
    local modes = { 'PvE', 'PvP' }


     -- Define the schema for the table
     -- Define the schema for the table
     local tableSchema = {
     local tableSchema = {}
         PvE = {},
    for _, mode in ipairs(modes) do
         PvP = {}
         tableSchema[mode] = {}
     }
    end
 
    function forEach(func)
        for _, mode in ipairs(modes) do
            func(mode)
         end
     end


     -- Function to create a new table with the desired schema
     -- Function to create a new table with the desired schema
Line 116: Line 125:
             |passive1=... |passive2=... -> { passive1, passive2 }
             |passive1=... |passive2=... -> { passive1, passive2 }
             --]]
             --]]
            local display_name = v
            local is_custom = string.find(k, '_define') ~= nil
             local passive_index = string.match(k, "%d")
             local passive_index = string.match(k, "%d")
             local passive_values = split(frame:preprocess('{{:' .. v .. '}}{{#arrayprint:' .. v .. '}}'));
             local passive_values = split(is_custom and v or frame:preprocess('{{:' .. display_name .. '}}{{#arrayprint:' .. display_name .. '}}'));
           
            if is_custom then
                display_name = passive_values[#passive_values]
                passive_values[#passive_values] = nil
            end
 
             PASSIVES[tonumber(passive_index)] = {
             PASSIVES[tonumber(passive_index)] = {
                 name = v,
                 name = display_name,
                 value = passive_values[1],
                 value = passive_values[1],
                 value_pvp = passive_values[2],
                 value_pvp = passive_values[2],
Line 155: Line 172:
     -- Store a configuration that will tell the main function how to behave given different inputs.
     -- Store a configuration that will tell the main function how to behave given different inputs.
     -- It will always take the first value if available. If not, fall back to the other (recursively).
     -- It will always take the first value if available. If not, fall back to the other (recursively).
     local DAMAGE_CONFIG = {
     local BASE_DAMAGE_CONFIG = {
         total_damage = {
         total_damage = {
             damage_numbers = { 'dmg' },
             damage_numbers = { 'dmg' },
Line 198: Line 215:
         },
         },
     }
     }
    local DAMAGE_CONFIG = {}
    function handleCancel()
        local processed_keys = {}
        for config_key, config_value in pairs(BASE_DAMAGE_CONFIG) do
            if not config_key:match('cancel_') then
                local new_config_value = {}
                for arg_table_key, arg_table in pairs(config_value) do
                    local new_arg_table = {}
                    for _, arg in ipairs(arg_table) do
                        table.insert(new_arg_table, 'cancel_' .. arg)
                    end
                    new_config_value[arg_table_key] = new_arg_table
                end
                local new_key = 'cancel_' .. config_key
                DAMAGE_CONFIG[new_key] = new_config_value
                processed_keys[new_key] = true
            end
        end
        return processed_keys
    end
    if args.cancel_dmg then
        handleCancel()
        DAMAGE_CONFIG = table.fuse(BASE_DAMAGE_CONFIG, DAMAGE_CONFIG)
    else
        DAMAGE_CONFIG = BASE_DAMAGE_CONFIG
    end
     -- Inherits values from args if not provided, but usage suggests that they're meant to be generated.
     -- Inherits values from args if not provided, but usage suggests that they're meant to be generated.
     function inherit(mode)
     function inherit(mode)
Line 217: Line 263:
                             Alternatively, it can contain an "i" to template the value to inherit.
                             Alternatively, it can contain an "i" to template the value to inherit.
                         ]]
                         ]]
                         while i <= #(args.dmg) do
                        local cancel_dmg_len = args.cancel_dmg and #(args.cancel_dmg) or 0
                         while i <= (#(args.dmg) + cancel_dmg_len) do
                             local main_arg_value = main_arg_values[i]
                             local main_arg_value = main_arg_values[i]


Line 224: Line 271:
                                 -- No inheritance from itself.
                                 -- No inheritance from itself.
                                 if inherit_arg and inherit_arg[i] and inherit_arg[i] ~= '' and ix ~= 1 then
                                 if inherit_arg and inherit_arg[i] and inherit_arg[i] ~= '' and ix ~= 1 then
                                     -- Only inherit if empty or nil
                                     -- Only inherit if empty
                                     if main_arg_value == '' then
                                     if main_arg_value == '' then
                                         args[main_key_prefixed][i] = inherit_arg[i]
                                         args[main_key_prefixed][i] = inherit_arg[i]
Line 243: Line 290:
     end
     end


     inherit('PvE')
     forEach(inherit)
    inherit('PvP')


     local DAMAGE_PARSED = createDamageDataTable()
     local DAMAGE_PARSED = createDamageDataTable()
Line 267: Line 313:
                         if k == 'provided' then
                         if k == 'provided' then
                             output_value = false
                             output_value = false
                        else
                            output_value = {}
                         end
                         end
                     end
                     end
Line 278: Line 326:
     end
     end


     parseConfig('PvE')
     forEach(parseConfig)
    parseConfig('PvP')


     -- Detected "count", for skills like Clementine, Enough Mineral, etc.
     -- Detected "count", for skills like Clementine, Enough Mineral, etc.
     function doEachDamage()
     function doEachDamage()
        local WITH_EACH = table.deep_copy(DAMAGE_PARSED)
         for mode, mode_content in pairs(DAMAGE_PARSED) do
         for mode, mode_content in pairs(DAMAGE_PARSED) do
             for damage_key, damage_value in pairs(mode_content) do
             for damage_key, damage_value in pairs(mode_content) do
Line 294: Line 342:
                     end
                     end


                     DAMAGE_PARSED[mode][damage_key:gsub("total_", "each_")] = damage_value
                     WITH_EACH[mode][damage_key:gsub("total_", "each_")] = damage_value
                     DAMAGE_PARSED[mode][damage_key] = new_value
                     WITH_EACH[mode][damage_key] = new_value
                 end
                 end
             end
             end
         end
         end
        return WITH_EACH
     end
     end


     if args.count then
     if args.count then
         doEachDamage()
         DAMAGE_PARSED = doEachDamage()
     end
     end


Line 327: Line 376:


     doBasicDamage()
     doBasicDamage()
    -- Adding missing cancel part damage to full, so that repetition wouldn't be a problem.
    function addCancelDamage()
        for mode, mode_content in pairs(BASIC_DAMAGE) do
            for damage_key, damage_value in pairs(mode_content) do
                local cancel_candidate = BASIC_DAMAGE[mode]['cancel_' .. damage_key]
                if string.find(damage_key, 'total_') and cancel_candidate then
                    BASIC_DAMAGE[mode][damage_key] = damage_value + cancel_candidate
                end
            end
        end
    end
    if args.cancel_dmg then
        addCancelDamage()
    end


     local WITH_TRAITS = createDamageDataTable()
     local WITH_TRAITS = createDamageDataTable()
Line 551: Line 616:
             }),
             }),
             is_visible = checkTraits()
             is_visible = checkTraits()
        },
        {
            type = 'cancel',
            text = {
                'Cancel', 'Full'
            },
            keywords = { 'cancel' },
            keyword_first = true,
            is_visible = inArgs('cancel_dmg')
         },
         },
         {
         {
Line 602: Line 676:
                             if current_row.keyword_next_to_main_key then
                             if current_row.keyword_next_to_main_key then
                                 new_key = prev_key:gsub(main_key, main_key .. '_' .. keyword)
                                 new_key = prev_key:gsub(main_key, main_key .. '_' .. keyword)
                            elseif current_row.keyword_first then
                                new_key = keyword .. '_' .. prev_key
                             end
                             end
                             table.insert(new_list, new_key)
                             table.insert(new_list, new_key)
Line 613: Line 689:
                 end
                 end
             end
             end
        end
        -- Sort the list once more, in order to swap the order of cancel & full.
        if inArgs('cancel_dmg') then
            local new_list = {}
            local cancel_counter = 1
            local full_counter = 2
            for i, damage_key in ipairs(all_list) do
                local regex = "^(%w+_)"
                local prefix = 'cancel_'
                local match = string.match(damage_key, regex)
                if (match == prefix) then
                    new_list[i] = damage_key:gsub(prefix, "")
                else
                    new_list[i] = prefix .. damage_key
                end
            end
            all_list = new_list
         end
         end


Line 702: Line 796:
     function doTable()
     function doTable()
         doHeaders()
         doHeaders()
         doContentByMode('PvE')
         forEach(doContentByMode)
        doContentByMode('PvP')
     end
     end


Line 727: Line 820:
     -- Transform into variables
     -- Transform into variables
     local variables = doVariables(frame, FINAL_DAMAGE, OPTIONS.prefix)
     local variables = doVariables(frame, FINAL_DAMAGE, OPTIONS.prefix)
    if out ~= nil then
        return inspect_dump(frame, out)
    end


     return variables .. bug .. (OPTIONS.do_table and tostring(TABLE) or '')
     return variables .. bug .. (OPTIONS.do_table and tostring(TABLE) or '')