• 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 16:40, 29 September 2017 by がか (talk | contribs)
Jump to navigation Jump to search

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

local Utils = require("Module:Utils")
local format = require("Module:StringOperations").format
local Formatting = require("Module:Formatting")
local Ship = require("Module:Ship")
local ShipIterator = require("Module:ShipIterator")

local args = nil
local ship = nil

local enumerating_functions = {
    
    base_names = function()
        return ShipIterator.baseForms
    end,

    all_names = function()
        return ShipIterator.allForms
    end,
    
    enemy = function()
        return ShipIterator.enemyForms
    end,

}

-- TODO: have a syntax for calling Module:Formatting functions?
local formatting_functions = {

    night_battle_power = function(ship)
        return (ship:firepower_max() or 0) + (ship:torpedo_max() or 0)
    end,
    
    code = function(ship)
        return Formatting:format_ship_code(ship:type())
    end,

    -- overrides ShipData:type
    type = function(ship)
        return Formatting:format_ship_type(ship:type())
    end,

    -- overrides ShipData:link
    link = function(ship)
        return Formatting:format_link(ship:link())
    end,

    hp_mod1 = function(ship)
        return ship:hp_max() and ship:hp() and math.min(2, ship:hp_max() - ship:hp()) or "??"
    end,

    hp_mod2 = function(ship)
        return ship:hp_max() and ship:hp_married() and math.min(2, ship:hp_max() - ship:hp_married()) or "??"
    end,

    -- overrides ShipData:implementation_date
    implementation_date = function(ship)
        local date = ship:implementation_date()
        return not date and "??" or format{
            "${year}/${month}/${day}",
            year = date[1],
            month = date[2] < 10 and "0" .. date[2] or date[2],
            day = date[3] < 10 and "0" .. date[3] or date[3],
        }
    end,

}

function format_lua(lua)
    if type(lua) == "table" then
        return tostring(table.concat(lua, args.concat_value or ", "))
    else
        return tostring(lua)
    end
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 format_lua(lua(ship))
        else
            return format_lua(lua)
        end
    end
end

local sequence = nil
local sequence_position = nil

function interpret_arg(arg)
    local prefix = string.sub(arg, 1, 1)
    if prefix == "@" then
        local enumerator = string.sub(arg, 2)
        local enumerating_function = enumerating_functions[enumerator]
        if enumerating_function and not sequence then
            sequence = enumerating_function()
            sequence_position = 1
        end
    elseif arg == "!@" then
        local ship_key = sequence[sequence_position]
        sequence_position = sequence_position + 1
        ship = Ship(ship_key)
    elseif 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 = {}
    repeat
        for _, arg in ipairs(args_) do
            local value = interpret_arg(arg)
            if value then
                table.insert(values, value)
            end
        end
    until not sequence or sequence_position > #sequence
    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

function Calc.test()
    mw.log(Calc.format(nil, {
        format = "table",
        "@base_names",
        "#", "!@", "?id", "?link", "?implementation_date"
    }))
end

return Calc