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

Difference between revisions of "Module:ShipPage"

From Kancolle Wiki
Jump to navigation Jump to search
m
Line 129: Line 129:
 
end
 
end
  
function ShipPage:format_header(ship_name, basepagename)
+
function ShipPage:format_header(ship_name, basepagename, args)
 
     self._vars = {
 
     self._vars = {
 
         ship_name = ship_name,
 
         ship_name = ship_name,
Line 136: Line 136:
 
     self:format_ship_meta()
 
     self:format_ship_meta()
 
     self:format_ship_infoboxes(basepagename)
 
     self:format_ship_infoboxes(basepagename)
     self:format_ship_categories(basepagename)
+
     if not args.no_categories then
 +
        self:format_ship_categories(basepagename)
 +
    end
 
     self:format_notice(basepagename)
 
     self:format_notice(basepagename)
 
     return format(self._header, self._vars)
 
     return format(self._header, self._vars)
Line 145: Line 147:
 
     local basepagename = args.implicit.basepagename or ""
 
     local basepagename = args.implicit.basepagename or ""
 
     local ship_name = args.explicit.ship or args.implicit.pagename or ""
 
     local ship_name = args.explicit.ship or args.implicit.pagename or ""
     return ShipPage:format_header(ship_name, basepagename)
+
     return ShipPage:format_header(ship_name, basepagename, args.explicit)
 
end
 
end
  

Revision as of 11:05, 16 September 2017

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")
local ShipGraphicKai = require('Module:ShipGraphicKai')

-- [[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:remodel_to_level() normally for some reason
local reversible_forms = {}

function ship_remodel_forms(ship_name, seasonal_forms, 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]
    if seasonal_forms then
        for suffix, data in pairs(ship_data) do
            if data._seasonal == true and data._name and data._suffix then
                table.insert(seasonal_forms, data._name .. "/" .. data._suffix)
            end
        end
    end
    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, seasonal_forms, 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 = "${notice}${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",
        "Fourth Upgrade",
        "Fifth Upgrade"
    },

    _gallery_link = [=[*[[${fullpagename}/Gallery|View ${ship_name} CG]]]=],
    _ship_type_link = [=[*[[${ship_code}|List of ${ship_type}s]]]=],
    _wikipedia_link = [=[*[[wikipedia:${wikipedia}|Wikipedia entry on ${ship_type} ${ship_name}]]]=],

    _sandbox_notice = [=[<div style="width:90%; border:2px solid #aa5; background:#ffc; text-align:center; margin:15px auto 20px; padding:5px;">This is a testing ship page, it does not correspond to an actual ship.</div>
]=],

    _gallery_tabber = [[<div class="ship-page-gallery-tab">
{{#tag:tabber|
{{!}}-{{!}}Normal=${image}
{{!}}-{{!}}Damaged=${image_damaged}
}}
</div>]],

    _gallery_normal = [[<div class="ship-page-gallery-tab">
{{#tag:tabber|
{{!}}-{{!}}Normal=${image}
}}
</div>]],

    _gallery_damaged = [[<div class="ship-page-gallery-tab">
{{#tag:tabber|
{{!}}-{{!}}Damaged=${image}
}}
</div>]],

    _footer_sections = [=[<div class="ship-page-gallery">
==[[${name}/Gallery|CG]]==
${gallery}
</div>
==See Also==
${links}]=],

}

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

function ShipPage:format_ship_infoboxes(basepagename)
    local forms = ship_remodel_forms(self._vars.ship_name)
    local infoboxes = {}
    for i, form in ipairs(forms) do
        local override
        if basepagename == "Sandbox" then
            override = { _page = "Sandbox/" .. self._vars.ship_name }
        end
        table.insert(infoboxes, format{
            self._ship_infobox,
            title = (self._ship_infobox_titles[i] or "?") .. (reversible_forms[form] and " (Reversible)" or ""),
            infobox = ShipInfoKai:Infobox({ form, override = override })
        })
    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_name })
end

function ShipPage:format_notice(basepagename)
    self._vars.notice = basepagename == "Sandbox" and self._sandbox_notice or ""
end

function ShipPage:format_header(ship_name, basepagename, args)
    self._vars = {
        ship_name = ship_name,
        clear = Formatting:clear(),
    }
    self:format_ship_meta()
    self:format_ship_infoboxes(basepagename)
    if not args.no_categories then
        self:format_ship_categories(basepagename)
    end
    self:format_notice(basepagename)
    return format(self._header, self._vars)
end

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

function ShipPage.FooterLinks(frame, args)
    args = args or Utils.getTemplateArgs(frame)
    local basepagename = args.implicit.basepagename or ""
    local ship_name = args.explicit.ship or args.implicit.pagename or ""
    local ship = Ship(ship_name)
    local ship_type = ship:type()
    local ship_name_ = ship:name()
    local ship_type_ = Formatting:format_ship_type(ship_type)
    local ship_code = Formatting:format_ship_code(ship_type)
    local wikipedia = ship:wikipedia()
    local fullpagename = basepagename == "Sandbox" and ("Sandbox/" .. ship_name) or ship_name
    local links = {
        format{
            ShipPage._gallery_link,
            fullpagename = fullpagename,
            ship_name = ship_name_,
        }
    }
    if ship_type then
        table.insert(links, format{
            ShipPage._ship_type_link,
            ship_code = ship_code,
            ship_type = ship_type_,
        })
    end
    if wikipedia then
        table.insert(links, format{
            ShipPage._wikipedia_link,
            ship_name = ship_name_,
            ship_type = ship_type_,
            wikipedia = wikipedia,
        })
    end
    if args.explicit.links then
        links = Utils.concat(links, args.explicit.links)
    end
    return table.concat(links, "\n")
end

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

function ship_cgs(ship_name, ship_suffix, j)
    j = j or 1
    ship_suffix = ship_suffix or ""
    local forms = { { name = ship_name .. '/' .. ship_suffix, normal = true, damaged = true } }
    local success, ship_data = Utils.requireModule(ship_name)
    if not success then
        return forms
    end
    local form_data = ship_data[ship_suffix]
    local seasonal_forms = {}
    for suffix, data in pairs(ship_data) do
        if data._seasonal == true and data._name and data._suffix then
            local normal = data._cg ~= false
            local damaged = data._cg_damaged ~= false
            table.insert(seasonal_forms, { name = data._name .. "/" .. data._suffix, seasonal = true, normal = normal, damaged = damaged })
        end
    end
    local i = 1
    while form_data and form_data._remodel_to and not Utils.find(forms, form_data._remodel_to, 'name') and i <= 15 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, Utils.concat(seasonal_forms, ship_cgs(ship_name_next, ship_suffix_next, j + 1)))
        else
            local name = form_data._remodel_to
            form_data = ship_data[ship_suffix_next]
            local normal = not (form_data._cg_reference and true or false)
            local damaged = not (form_data._cg_damaged_reference and true or false)
            table.insert(forms, { name = name, normal = normal, damaged = damaged })
        end
        i = i + 1
    end
    return Utils.concat(forms, seasonal_forms)
end

-- [[Category:Todo]]:
-- Ignore forms with same base and/or damaged CG (requires updating for data modules)
-- Order/years for seasonal forms?
-- Improve performance by not using frame:preprocess?
function ShipPage.GalleryTabber(frame, args)
    args = args or Utils.getTemplateArgs(frame)
    local ship_name = args.explicit.ship or args.implicit.pagename or ""
    local forms = ship_cgs(ship_name)
    local result = { "{{#tag:tabber|" }
    for _, form in ipairs(forms) do
        if form.normal or form.damaged then
            local name, suffix = Ship:process_ship_key(form.name)
            suffix = suffix or ""
            local title = form.seasonal and suffix or suffix == "" and name or suffix
            table.insert(result, "{{!}}-{{!}}" .. title .. "=")
            if form.normal and form.damaged then
                local image = ShipGraphicKai:Asset({ form.name, link = "nil", size = "300px" })
                local image_damaged = ShipGraphicKai:Asset({ form.name, link = "nil", damaged = true, size = "300px" })
                table.insert(result, format{
                    ShipPage._gallery_tabber,
                    image = image,
                    image_damaged = image_damaged
                })
            elseif form.normal then
                local image = ShipGraphicKai:Asset({ form.name, link = "nil", size = "300px" })
                table.insert(result, format{
                    ShipPage._gallery_normal,
                    image = image,
                })
            elseif form.damaged then
                local image_damaged = ShipGraphicKai:Asset({ form.name, link = "nil", damaged = true, size = "300px" })
                table.insert(result, format{
                    ShipPage._gallery_damaged,
                    image = image_damaged,
                })
            end
        end
    end
    table.insert(result, "}}")
    return table.concat(result, "\n")
end

function ShipPage.TestGallery(ship)
    return ShipPage.GalleryTabber(nil, {
        explicit = { ship = ship },
        implicit = {},
    })
end
-- print(p.TestGallery("Taigei"))
-- print(p.TestGallery("Ayanami"))
-- print(p.TestGallery("Iowa"))
-- print(p.TestGallery("Kasuga Maru"))
-- print(p.TestGallery("Murakumo"))

function ShipPage.FooterSections(frame, args)
    args = args or Utils.getTemplateArgs(frame)
    local ship_name = args.explicit.ship or args.implicit.pagename or ""
    return format{
        ShipPage._footer_sections,
        name = ship_name,
        gallery = frame:preprocess(ShipPage.GalleryTabber(frame, args)),
        links = ShipPage.FooterLinks(frame, args)
    }
end

local ShipIterator = require('Module:ShipIterator')

function ShipPage.TestGalleryAll(frame)
    local result = {}
    for _, ship in ShipIterator.base() do
        table.insert(result, '===[[' .. ship .. '/Gallery|' .. ship .. ']]===')
        table.insert(result, ShipPage.TestGallery(ship))
    end
    return frame:preprocess(table.concat(result, '\n'))
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