Module:Test

From Elwiki
Revision as of 15:08, 26 August 2022 by Ritsu (talk | contribs)

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

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

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

    function dump(o)
        if type(o) == 'table' then
            local s = '{ '
            for k, v in pairs(o) do
                if type(k) ~= 'number' then
                    k = '"' .. k .. '"'
                end
                s = s .. '[' .. k .. '] = ' .. dump(v) .. ','
            end
            return s .. '} '
        else
            return tostring(o)
        end
    end

    function CreateClass(...)
        -- "cls" is the new class
        local cls, bases = {}, {...}
        -- copy base class contents into the new class
        for i, base in ipairs(bases) do
            for k, v in pairs(base) do
                cls[k] = v
            end
        end
        -- set the class's __index, and start filling an "is_a" table that contains this class and all of its bases
        -- so you can do an "instance of" check using my_instance.is_a[MyClass]
        cls.__index, cls.is_a = cls, {
            [cls] = true
        }
        for i, base in ipairs(bases) do
            for c in pairs(base.is_a) do
                cls.is_a[c] = true
            end
            cls.is_a[base] = true
        end
        -- the class's __call metamethod
        setmetatable(cls, {
            __call = function(c, ...)
                local instance = setmetatable({}, c)
                -- run the init method if it's there
                local init = instance._init
                if init then
                    init(instance, ...)
                end
                return instance
            end
        })
        -- return the new class table, that's ready to fill with methods
        return cls
    end

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

    -- Define the class blueprint for traits.
    Trait = {
        name = "",
        values = {
            mp_multiplier = 100,
            cd_multiplier = 100,
            duration_multiplier = 100
        },
        details = {
            mp_cost = {0, 0},
            cooldown = {0, 0},
            duration = {0, 0}
        }
    }

    function Trait:new(t)
        t = t or {}
        setmetatable(t, self)
        self.__index = self

        for k, v in pairs(t.values) do
            for i = 1, 2, 1 do
                v = v or self.values[k]
            end
        end

        for k, v in pairs(t.details) do
            for i = 1, 2, 1 do
                v[i] = v[i] or {0, 0}
            end
        end
        
        return t
    end

    function Trait:math()
        for i = 1, 2, 1 do
            self.details.mp_cost[i] = self.details.mp_cost[i] * self.values.mp_multiplier
            self.details.cooldown[i] = self.details.cooldown[i] * self.values.cd_multiplier
            self.details.duration[i] = self.details.duration[i] * self.values.duration_multiplier
        end
        return self
    end

    -- Define the mother object of traits.
    local reversed = Trait:new({
        name = "Reversed",
        values = {
            mp_multiplier = 60,
            cd_multiplier = 150,
            duration_multiplier = 100
        },
        details = {
            mp_cost = {300, 300},
            cooldown = {22, 22},
            duration = {0, 0}
        }
    })

    -- Output point
    -- return tostring(trait_table);
    return dump(reversed:math());

end

return p