Module:Calc

From Elwiki
Revision as of 11:59, 25 September 2022 by Ritsu (talk | contribs)

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

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

function p.main(frame)
    local args = getArgs(frame);
    args.level = args.level or args[3]
    args.defense = args.defense or args[2]
    local divider = 258.65 + 39.35 * args.level

    function toPercentDef(defense)
        local sign = '%'
        if (args.sign == 'false') then
            sign = ''
        end

        defense = defense or args.defense
        local def_percentage = math.round(10000 * defense / (divider + defense)) / 100;
        return def_percentage .. sign
    end

    function toRawDef()
        if (tonumber(args.defense) >= 100) then
            args.defense = 99.99
        end
        local raw_def = (-divider * args.defense / (args.defense - 100) * 100) / 100
        return math.round(raw_def)
    end

    if args[1] == 'to_percent_def' then
        return toPercentDef()
    end

    if args[1] == 'to_raw_def' then
        return toRawDef()
    end

    if args[1] == 'get_def' then
        local multiplier = 67.215 + 12.554 * args.level
        args.mode = args.mode or 1
        local defense = math.round((args.defense * divider * multiplier / (divider - (args.defense - 1) * multiplier)) * args.mode)

        return (args.to_percent or args[4]) and toPercentDef(defense) or not (args.to_percent and args[4]) and defense
    end

end

return p