Module:ShipPage

Revision as of 11:33, 6 June 2017 by がか (talk | contribs)

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

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

-- [[Category:Todo]]: support passing Ship object in those modules, use Ship object, redo and move ship_remodel_forms function
local ShipMetaKai = require("Module:ShipMetaKai")
local ShipInfoKai = require("Module:ShipInfoKai")
local ShipCategoriesKai = require("Module:ShipCategoriesKai")

-- can't use Ship:reversible() normally for some reason
local reversible_forms = {}

function ship_remodel_forms(ship_name, ship_suffix, j)
    j = j or 1
    ship_suffix = ship_suffix or ""
    local forms = { ship_name .. "/" .. ship_suffix }
    local success, ship_data = Utils.requireModule(ship_name)
    if not success then
        return forms
    end
    local form_data = ship_data[ship_suffix]
    local i = 0
    while form_data and form_data._remodel_to and not Utils.find(forms, form_data._remodel_to) and i < 10 and j <= 3 do
        local ship_name_next, ship_suffix_next = Ship:process_ship_key(form_data._remodel_to)
        if ship_name_next ~= ship_name then
            return Utils.concat(forms, ship_remodel_forms(ship_name_next, ship_suffix_next, j + 1))
        else
            table.insert(forms, form_data._remodel_to)
            form_data = ship_data[ship_suffix_next]
            if form_data._remodel_to_level then
                 reversible_forms[form_data._name .. "/" .. (form_data._suffix or "")] = true
            end
        end
        i = i + 1
    end
    return forms
end

local ShipPage = {

    _header = "${ship_meta}__TOC__${clear}${ship_infoboxes}${ship_categories}",

    _ship_infobox = [[<div style="display:inline-block;">
===${title}===
${infobox}
</div>]],

    _ship_infobox_titles = {
        "Basic",
        "Upgrade",
        "Second Upgrade",
        "Third Upgrade"
    },

    _footer_links = [=[*[[${ship_key}Gallery|View ${ship_name} CG]]
*[[${ship_code}|List of ${ship_type}s]]
*[[wikipedia:${wikipedia}|Wikipedia entry on ${ship_type} ${ship_name}]]${extra_links}]=],

}

function ShipPage:format_ship_meta()
    self._vars.ship_meta = ShipMetaKai:Infobox({ self._vars.ship_key })
end

function ShipPage:format_ship_infoboxes()
    local forms = ship_remodel_forms(self._vars.ship_name)
    local infoboxes = {}
    for i, form in ipairs(forms) do
        table.insert(infoboxes, format{
            self._ship_infobox,
            title = (self._ship_infobox_titles[i] or "?") .. (reversible_forms[form] and " (Reversible)" or ""),
            infobox = ShipInfoKai:Infobox({ form })
        })
    end
    self._vars.ship_infoboxes = table.concat(infoboxes, "\n")
end

function ShipPage:format_ship_categories(basepagename)
    self._vars.ship_categories = basepagename == "Sandbox" and "" or ShipCategoriesKai:Categories({ self._vars.ship_key })
end

function ShipPage:format_header(ship_key, ship_name, basepagename)
    self._vars = {
        ship_key = ship_key,
        ship_name = ship_name,
        clear = Formatting:clear(),
    }
    self:format_ship_meta()
    self:format_ship_infoboxes()
    self:format_ship_categories(basepagename)
    return format(self._header, self._vars)
end

function ShipPage.Header(frame, args)
    args = args or Utils.getTemplateArgs(frame)
    local ship_name = args.explicit.ship or args.implicit.pagename or ""
    ship_key = ship_name .. "/"
    return ShipPage:format_header(ship_key, ship_name, args.implicit.basepagename)
end

function ShipPage.FooterLinks(frame, args)
    args = args or Utils.getTemplateArgs(frame)
    local ship_key = args.explicit.ship or args.implicit.pagename or ""
    ship_key = ship_key .. "/"
    local ship = Ship(ship_key)
    local ship_type = ship:type()
    return format{
        ShipPage._footer_links,
        ship_key = ship_key,
        ship_name = ship:name(),
        ship_code = Formatting:format_ship_code(ship_type),
        ship_type = Formatting:format_ship_type(ship_type),
        wikipedia = ship:wikipedia(),
        extra_links = args.explicit.links and ("\n" .. args.explicit.links) or ""
    }
end

function ShipPage.ClassTemplate(frame, args)
    args = args or Utils.getTemplateArgs(frame)
    local ship_key = args.explicit.ship or args.implicit.pagename or ""
    ship_key = ship_key .. "/"
    local ship = Ship(ship_key)
    return ship:class() and ship:class():name_override() or "Unknown Class"
end

--[[
ShipPage.t1 = ShipPage.Header(nil, {
    explicit = { ship = "Ayanami" },
    implicit = {},
})
-- print(p.t1)

ShipPage.t2 = ShipPage.Header(nil, {
    explicit = { ship = "Hibiki" },
    implicit = {},
})
-- print(p.t2)

ShipPage.t3 = ShipPage.Header(nil, {
    explicit = { ship = "Zuikaku" },
    implicit = {},
})
-- print(p.t3)

ShipPage.t4 = ShipPage.Header(nil, {
    explicit = {},
    implicit = {},
})
-- print(p.t4)

ShipPage.t5 = ShipPage.FooterLinks(nil, {
    explicit = { ship = "Ayanami" },
    implicit = {},
})
-- print(p.t5)

ShipPage.t6 = ShipPage.ClassTemplate(nil, {
    explicit = { ship = "Matsukaze" },
    implicit = {},
})
-- print(p.t6)
]]--
--[[
function ShipPage.test()
    function print_forms(ship_name)
        for _, v in ipairs(ship_remodel_forms(ship_name)) do
            mw.log(v)
        end
    end
    print_forms("Ayanami")
    print_forms("Hibiki")
    print_forms("Zuikaku")
    print_forms("Matsukaze")
    print_forms("Bismarck")
    print_forms("Taigei")
    print_forms("U-511")
    print_forms("Kasuga Maru")
    print_forms("Gangut")
end
]]--

return ShipPage