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

Module:NanaminShipInfo

From Kancolle Wiki
Revision as of 07:29, 19 April 2015 by Nanamin (talk | contribs)
Jump to navigation Jump to search

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

local getArgs = require('Module:GetArgs')

local Ship = require('Module:Ship')
local Formatting = require('Module:Formatting')
local format = require('Module:StringInterpolation').format

local NanaminShipInfo = {
    _sorted_entries = {},
    _rows = {},
    
    _table_start = [[{| style="width: 100%; text-align: center;"]],
    _row_starter = "|-",
    _table_end = [[|}]],
    _ship_card_template = [[| style="width: 40%;" |<div class="mw-customtoggle-${class}">${ship_card}</div>]],
    _level_template = [[| style="width: 10%;" |${level}]],
    _embedded_start = [[| colspan="2" |]],
    _embedded_table = [[{| class="mw-collapsible mw-collapsed" id="mw-customcollapsible-${name}"
    | <b>Class</b>: ${class}
    |-
    | <b>Extra Leveling</b>: ${extra_levels}
    |-
    | <b>Notes</b>: ${notes}
    |}]],
}

function NanaminShipInfo:sort_arguments(args)
    --Support two methods of entry:
    ---Each value on a separate line
    ---Each value on the same line, separated by slashes
    --Name/Remodel/Level/Extra Leveling
    local entry = {}
    for _, item in ipairs(args) do
        if item == "-" then
            local ship = Ship(entry[1], entry[2])
            local types = {
                [2] = "Destroyer",
                [3] = "Light Cruiser",
                [4] = "Light Cruiser",
                [5] = "Heavy Cruiser",
                [6] = "Heavy Cruiser",
                [7] = "Carrier",
                [8] = "Battleship",
                [9] = "Battleship",
                [10] = "Battleship",
                [11] = "Carrier",
                [12] = "Battleship",
                [13] = "Submarine",
                [14] = "Submarine",
                [18] = "Carrier",
            }
            if ship:type() then
                local type = types[ship:type()] or "Other"
                if not self._sorted_entries[type] then
                    self._sorted_entries[type] = {}
                end
                table.insert(self._sorted_entries[type], { [1] = ship, [2] = tonumber(entry[3]), [3] = entry[4], [4] = entry[5] })
            end
            
            entry = {}
        else
            while mw.ustring.find(item, '/') do
                local split = mw.ustring.find(item, '/')
                table.insert(entry, mw.ustring.sub(item, 1, split - 1))
                item = mw.ustring.sub(item, split + 1)
            end
            table.insert(entry, item)
        end
    end
end

function NanaminShipInfo:embed_table(entry)
if entry[3] == "" then
    entry[3] = "N/A"
end
table.insert(self._rows, self._embedded_start)
table.insert(self._rows, format{self._embedded_table,
    name = entry[1]:name():gsub(" ", ""),
    class = entry[1]:class():name(),
    extra_levels = entry[3] or "N/A",
    notes = entry[4] or "None",
})
end

function NanaminShipInfo:build_table(class)
    if self._sorted_entries[class] ~= nil then
        table.insert(self._rows, "===" .. class .. "s===")
        table.insert(self._rows, self._table_start)
        
        local entries = self._sorted_entries[class]
        table.sort(entries, function(a, b) return a[2] < b[2] end)
        local count = 0
        for a = #entries, 1, -1 do
            count = count + 1
            
            table.insert(self._rows, format{self._ship_card_template,
                class = entries[a][1]:name():gsub(" ", ""),
                ship_card = Formatting:format_image{entries[a][1]:battle_card(), align = "center"},
            })
            table.insert(self._rows, format{self._level_template,
                level = entries[a][2],
            })
            
            if count % 2 == 0 then
                table.insert(self._rows, self._row_starter)
                self:embed_table(entries[a + 1])
                self:embed_table(entries[a])
                table.insert(self._rows, self._row_starter)
            elseif a == 1 then
                self:embed_table(entries[a])
            end
        end
        table.insert(self._rows, self._table_end)
    end
end

function NanaminShipInfo:Main(args)
    self:sort_arguments(args)
    --mw.log(self._sorted_entries["Light Cruiser"]["111"][1][1])
    self:build_table("Battleship")
    self:build_table("Carrier")
    self:build_table("Heavy Cruiser")
    self:build_table("Light Cruiser")
    self:build_table("Submarine")
    return table.concat(self._rows, "\n")
end

function NanaminShipInfo.Begin(frame)
	local args = getArgs{frame = frame:getParent()}
	return NanaminShipInfo:Main(args)
end

return NanaminShipInfo