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

Module:Fleet

From Kancolle Wiki
Revision as of 13:15, 19 September 2018 by がか (talk | contribs)
Jump to navigation Jump to search

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

-- Example how to not write modules.

local _ = require('Module:Core')
local JSON = require('Module:Core/JSON')

local ShipBattleCardKai = require('Module:ShipBattleCardKai')
local EquipmentCardKai = require('Module:EquipmentCardKai')
local ShipsByApiId = require('Module:Collection/ShipsByApiId')
local EquipmentByApiId = require('Module:Collection/EquipmentByApiId')

local function getShipCard(ship)
    local id = tonumber(ship)
    if id then
        ship = ShipsByApiId[id]
    end
    return ShipBattleCardKai:Asset({ ship, hd = true })
end

local function getEquipmentCard(eq)
    local id = tonumber(eq)
    if id then
        eq = EquipmentByApiId[id]
    end
    return EquipmentCardKai:Asset({ eq, size = '100px' })
end

local function fromModule(name)
    local success, data = _.requireModule('Data/Fleet/Raw/' .. name)
    local result = ''
    if success then
        data = JSON.decode(data)
        --return _.js(data)
        for i, id in ipairs(data.api_ship_id) do
          result = result .. string.format(
              '|-\n|%s||%s|%s|%s|%s|%s||%s||%s||%s||%s||%s\n',
              getShipCard(id),
              data.api_ship_lv[i],
              data.api_Slot[i][1] > 0 and ('|' .. getEquipmentCard(data.api_Slot[i][1])) or '|style="background:grey;width:100px"|',
              data.api_Slot[i][2] > 0 and ('|' .. getEquipmentCard(data.api_Slot[i][2])) or '|style="background:grey;width:100px"|',
              data.api_Slot[i][3] > 0 and ('|' .. getEquipmentCard(data.api_Slot[i][3])) or '|style="background:grey;width:100px"|',
              data.api_Slot[i][4] > 0 and ('|' .. getEquipmentCard(data.api_Slot[i][4])) or '|style="background:grey;width:100px"|',
              -- data.api_nowhps[i],
              data.api_maxhps[i],
              data.api_Param[i][1],
              data.api_Param[i][2],
              data.api_Param[i][3],
              data.api_Param[i][4]
          )
        end
    end
    return result
end

local tableHeaderFriend = [=[ {|class="wikitable" style="text-align:center"
!Ship
!Level
!colspan="4"|Equipment
![[File:Icon HP.png]]
![[File:Icon Gun.png]]
![[File:Icon Torpedo.png]]
![[File:Icon AA.png]]
![[File:Icon Armor.png]]
]=]

local tableHeaderSimple = [=[{|class="wikitable" style="text-align:center"
!Ship
!colspan="4"|Equipment
]=]

local function render(args, frame)
    local tableHeader = args.friend and tableHeaderFriend or tableHeaderSimple
    local result = {}
    local firstCell = true
    local openTab = false
    local openSubtab = false
    for _, arg in ipairs(args) do
        if arg:sub(0, 2) == '!!' then
            if openSubtab then
                table.insert(result, string.format('|}\n|-| %s =\n' .. tableHeader, arg:sub(3)))
            else
                table.insert(result, string.format('<tabber>\n|-| %s =\n' .. tableHeader, arg:sub(3)))
                openSubtab = true
            end
        elseif arg:sub(0, 1) == '!' then
            if openSubtab then
                table.insert(result, '|}\n</tabber>')
                openSubtab = false
            end
            if openTab then
                table.insert(result, string.format('{{!}}-{{!}} %s =', arg:sub(2)))
            else
                table.insert(result, string.format('{{#tag:tabber|\n{{!}}-{{!}} %s =', arg:sub(2))) 
                openTab = true
            end
        elseif arg:sub(0, 1) == '~' then
            table.insert(result, fromModule(arg:sub(2)))
        elseif arg == '-' then
            table.insert(result, '|-')
            firstCell = true
        else
            if firstCell then
                table.insert(result, '|' .. getShipCard(arg))
                firstCell = false
            else
                table.insert(result, '|' .. getEquipmentCard(arg))
            end
        end
    end
    if openSubtab then
        table.insert(result, '|}\n</tabber>')
        openSubtab = false
    end
    if openTab then
        table.insert(result, '}}')
        openTab = false
    end
    return table.concat(result, '\n')
end

local getArgs = require('Module:GetArgs')

return {
    render = function(frame)
    	return frame:preprocess(render(getArgs{ frame = frame:getParent() }))
    end,
    test1 = render({
        '!1 DD',
        '!!Example 1',
        '-', '1', 'Type 3 Active Sonar',
        '!!Example 2',
        '-', 'Yuudachi/Kai Ni', '282', '282',
        '!2 DD',
        '!!Example 1',
        '-', 'Yuudachi/Kai Ni', 'Type 3 Active Sonar',
        '-', 'Shigure/Kai Ni', 'Type 3 Active Sonar',
        '!!Example 2',
        '-', 'Yuudachi/Kai Ni', 'Type 3 Active Sonar', 'Type 3 Active Sonar',
        '-', 'Shigure/Kai Ni', 'Type 3 Active Sonar', 'Type 3 Active Sonar',
    }),
    test2 = render({
        friend = true,
        '!Europe Fleet',
        '!!Variant 1',
        '~Europe Fleet 1',
    }),
}
-- print(p.test1)
-- print(p.test2)