• Welcome to the Kancolle Wiki!
  • If you have any questions regarding site content, account registration, etc., please visit the KanColle Wiki Discord

Module:Calc

From Kancolle Wiki
Revision as of 20:15, 3 February 2017 by がか (talk | contribs) (Created page with "local Utils = require("Module:Utils") local Formatting = require("Module:Formatting") local Ship = require("Module:Ship") local args = nil local ship = nil local formatting_...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

local Utils = require("Module:Utils")
local Formatting = require("Module:Formatting")
local Ship = require("Module:Ship")

local args = nil
local ship = nil

local formatting_functions = {
    -- overrides ShipData:link
    link = function(ship) return Formatting:format_link(ship:link()) end,
}

function format_value(key)
    local formatting_function = formatting_functions[key]
    if formatting_function then
        return formatting_function(ship)
    else
        local lua = ship[key]
        if type(lua) == "function" then
            return tostring(lua(ship))
        elseif type(lua) == "table" then
            return tostring(table.concat(lua, args.concat_value or ", "))
        else
            return tostring(lua)
        end
    end
end

function interpret_arg(arg)
    local prefix = string.sub(arg, 1, 1)
    if prefix == "!" then
        local ship_key = string.sub(arg, 2)
        ship = Ship(ship_key)
    elseif prefix == "#" and args.format == "table" then
        return "|-\n"
    elseif prefix == "?" then
        local key = string.sub(arg, 2)
        local value = format_value(key)
        if args.format == "table" then
            return "|" .. value .. "\n"
        else
            return value
        end
    else
        return arg
    end
end

function interpret_args(args_)
    args = args_
    local values = {}
    for _, arg in ipairs(args) do
        local value = interpret_arg(arg)
        if value then
            table.insert(values, value)
        end
    end
    return table.concat(values, args.concat or "")
end

local Calc = {}

function Calc.format(frame, args)
    return interpret_args(args or Utils.getTemplateArgs(frame).explicit)
end

return Calc