• 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:Fleet"

From Kancolle Wiki
Jump to navigation Jump to search
m
Line 27: Line 27:
  
 
local function fromModule(name)
 
local function fromModule(name)
    local data = loadData('Fleet/' .. name)
 
 
     local result = ''
 
     local result = ''
    --if success then
+
 
        data = JSON.decode(data.api)
 
        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
 
     return result
 
end
 
end
Line 60: Line 40:
 
![[File:Icon Torpedo.png]]
 
![[File:Icon Torpedo.png]]
 
![[File:Icon AA.png]]
 
![[File:Icon AA.png]]
![[File:Icon Armor.png]]
+
![[File:Icon Armor.png]]]=]
]=]
 
  
 
local tableHeaderSimple = [=[{|class="wikitable" style="text-align:center"
 
local tableHeaderSimple = [=[{|class="wikitable" style="text-align:center"
Line 67: Line 46:
 
!colspan="4"|Equipment
 
!colspan="4"|Equipment
 
]=]
 
]=]
 +
 +
-- can count for scoring
 +
-- can store fleet types in ship data modules
 +
local friendTypes = {
 +
    ['DesDiv 31'] = {
 +
        'Kishinami',
 +
        'Naganami',
 +
    },
 +
    ['Europe Fleet'] = {
 +
        'Gotland',
 +
        'Italia',
 +
        'Littorio',
 +
        'Nelson',
 +
        'Pola',
 +
        'Roma',
 +
        'Warspite',
 +
        'Zara',
 +
    },
 +
    ['US Fleet'] = {
 +
        'Intrepid',
 +
        'Saratoga'
 +
    }
 +
}
 +
 +
local function getFleetType(fleet)
 +
    for k, v in pairs(friendTypes) do
 +
        for i, ship in ipairs(v) do
 +
            if _.ifindBy(fleet, function (e) return _.startsWith(e.ship, ship) end) then
 +
                return k
 +
            end
 +
        end
 +
    end
 +
    return 'Unassorted'
 +
end
 +
 +
local function formatHp(hps, maxHp)
 +
    return string.format(
 +
        '<span class="explain" title="%s">~/%s</span>',
 +
        _.ijoin(hps, ', '),
 +
        maxHp
 +
    )
 +
end
 +
 +
local function renderFriend(args, frame)
 +
    local data = loadData('Fleet/Friend/' .. args.friend)
 +
    local groups = {}
 +
    for _, e in pairs(data) do
 +
        local fleetType = getFleetType(e.fleet)
 +
        groups[fleetType] = groups[fleetType] or {}
 +
        table.insert(groups[fleetType], e)
 +
    end
 +
    local result = {}
 +
    table.insert(result, '{{#tag:tabber|')
 +
    for fleetType, fleets in pairs(groups) do
 +
        table.sort(fleets, function(a, b)
 +
            return #(a.fleet) < #(b.fleet)
 +
        end)
 +
        table.insert(result, string.format('{{!}}-{{!}} %s =', fleetType))
 +
        table.insert(result, '<tabber>')
 +
        for i, fleet in ipairs(fleets) do
 +
            table.insert(result, string.format('|-| %s =\n' .. tableHeaderFriend, 'Variant ' .. i))
 +
            for j, ship in ipairs(fleet.fleet) do
 +
                table.insert(result, string.format(
 +
                    '|-\n|%s||%s||%s||%s||%s||%s||%s||%s||%s||%s||%s',
 +
                    getShipCard(ship.ship),
 +
                    ship.level,
 +
                    ship.equipment[1] and getEquipmentCard(ship.equipment[1]) or 'style="background:grey;width:100px"| ',
 +
                    ship.equipment[2] and getEquipmentCard(ship.equipment[2]) or 'style="background:grey;width:100px"| ',
 +
                    ship.equipment[3] and getEquipmentCard(ship.equipment[3]) or 'style="background:grey;width:100px"| ',
 +
                    ship.equipment[4] and getEquipmentCard(ship.equipment[4]) or 'style="background:grey;width:100px"| ',
 +
                    type(ship.hp) == 'table' and formatHp(ship.hp, ship.maxHp) or ship.hp ~= ship.maxHp and string.format('%s/%s', ship.hp, ship.maxHp) or ship.maxHp,
 +
                    ship.firepower,
 +
                    ship.torpedo,
 +
                    ship.aa,
 +
                    ship.armor
 +
                ))
 +
            end
 +
            table.insert(result, '|}')
 +
        end
 +
        table.insert(result, '</tabber>')
 +
    end
 +
    table.insert(result, '}}')
 +
    return table.concat(result, '\n')
 +
end
  
 
local function render(args, frame)
 
local function render(args, frame)
     local tableHeader = args.friend and tableHeaderFriend or tableHeaderSimple
+
     if args.friend then
 +
        return renderFriend(args, frame)
 +
    end
 
     local result = {}
 
     local result = {}
 
     local firstCell = true
 
     local firstCell = true
Line 77: Line 142:
 
         if arg:sub(0, 2) == '!!' then
 
         if arg:sub(0, 2) == '!!' then
 
             if openSubtab then
 
             if openSubtab then
                 table.insert(result, string.format('|}\n|-| %s =\n' .. tableHeader, arg:sub(3)))
+
                 table.insert(result, string.format('|}\n|-| %s =\n' .. tableHeaderSimple, arg:sub(3)))
 
             else
 
             else
                 table.insert(result, string.format('<tabber>\n|-| %s =\n' .. tableHeader, arg:sub(3)))
+
                 table.insert(result, string.format('<tabber>\n|-| %s =\n' .. tableHeaderSimple, arg:sub(3)))
 
                 openSubtab = true
 
                 openSubtab = true
 
             end
 
             end
Line 90: Line 155:
 
                 table.insert(result, string.format('{{!}}-{{!}} %s =', arg:sub(2)))
 
                 table.insert(result, string.format('{{!}}-{{!}} %s =', arg:sub(2)))
 
             else
 
             else
                 table.insert(result, string.format('{{#tag:tabber|\n{{!}}-{{!}} %s =', arg:sub(2)))  
+
                 table.insert(result, string.format('{{#tag:tabber|\n{{!}}-{{!}} %s =', arg:sub(2)))
 
                 openTab = true
 
                 openTab = true
 
             end
 
             end
Line 138: Line 203:
 
         '-', 'Shigure/Kai Ni', 'Type 3 Active Sonar', 'Type 3 Active Sonar',
 
         '-', 'Shigure/Kai Ni', 'Type 3 Active Sonar', 'Type 3 Active Sonar',
 
     }),
 
     }),
     test2 = render({
+
     test2 = function()
        friend = true,
+
        return render({
         '!Europe Fleet',
+
            friend = 'Early Fall 2018 Event',
         '!!Variant 1',
+
         })
         '~Europe Fleet 1',
+
    end,
 +
    test3 = render({
 +
        friend = 'Early Fall 2018 Event',
 +
         map = 'E-5',
 +
    }),
 +
    test4 = getFleetType({
 +
         { ship = 'Littorio' }
 
     }),
 
     }),
 
}
 
}
 
-- print(p.test1)
 
-- print(p.test1)
 
-- print(p.test2)
 
-- print(p.test2)
 +
-- print(p.test3)

Revision as of 12:54, 27 September 2018

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 loadData = require('Module:Data').load

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, size = '160px' })
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 result = ''

    return result
end

local tableHeaderFriend = [=[ {|class="wikitable" style="text-align:center"
!Ship
!Lv.
!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
]=]

-- can count for scoring
-- can store fleet types in ship data modules
local friendTypes = {
    ['DesDiv 31'] = {
        'Kishinami',
        'Naganami',
    },
    ['Europe Fleet'] = {
        'Gotland',
        'Italia',
        'Littorio',
        'Nelson',
        'Pola',
        'Roma',
        'Warspite',
        'Zara',
    },
    ['US Fleet'] = {
        'Intrepid',
        'Saratoga'
    }
}

local function getFleetType(fleet)
    for k, v in pairs(friendTypes) do
        for i, ship in ipairs(v) do
            if _.ifindBy(fleet, function (e) return _.startsWith(e.ship, ship) end) then
                return k
            end
        end
    end
    return 'Unassorted'
end

local function formatHp(hps, maxHp)
    return string.format(
        '<span class="explain" title="%s">~/%s</span>',
        _.ijoin(hps, ', '),
        maxHp
    )
end

local function renderFriend(args, frame)
    local data = loadData('Fleet/Friend/' .. args.friend)
    local groups = {}
    for _, e in pairs(data) do
        local fleetType = getFleetType(e.fleet)
        groups[fleetType] = groups[fleetType] or {}
        table.insert(groups[fleetType], e)
    end
    local result = {}
    table.insert(result, '{{#tag:tabber|')
    for fleetType, fleets in pairs(groups) do
        table.sort(fleets, function(a, b)
            return #(a.fleet) < #(b.fleet)
        end)
        table.insert(result, string.format('{{!}}-{{!}} %s =', fleetType))
        table.insert(result, '<tabber>')
        for i, fleet in ipairs(fleets) do
            table.insert(result, string.format('|-| %s =\n' .. tableHeaderFriend, 'Variant ' .. i))
            for j, ship in ipairs(fleet.fleet) do
                 table.insert(result, string.format(
                    '|-\n|%s||%s||%s||%s||%s||%s||%s||%s||%s||%s||%s',
                    getShipCard(ship.ship),
                    ship.level,
                    ship.equipment[1] and getEquipmentCard(ship.equipment[1]) or 'style="background:grey;width:100px"| ',
                    ship.equipment[2] and getEquipmentCard(ship.equipment[2]) or 'style="background:grey;width:100px"| ',
                    ship.equipment[3] and getEquipmentCard(ship.equipment[3]) or 'style="background:grey;width:100px"| ',
                    ship.equipment[4] and getEquipmentCard(ship.equipment[4]) or 'style="background:grey;width:100px"| ',
                    type(ship.hp) == 'table' and formatHp(ship.hp, ship.maxHp) or ship.hp ~= ship.maxHp and string.format('%s/%s', ship.hp, ship.maxHp) or ship.maxHp,
                    ship.firepower,
                    ship.torpedo,
                    ship.aa,
                    ship.armor
                ))
            end
            table.insert(result, '|}')
        end
        table.insert(result, '</tabber>')
    end
    table.insert(result, '}}')
    return table.concat(result, '\n')
end

local function render(args, frame)
    if args.friend then
        return renderFriend(args, frame)
    end
    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' .. tableHeaderSimple, arg:sub(3)))
            else
                table.insert(result, string.format('<tabber>\n|-| %s =\n' .. tableHeaderSimple, 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 = function()
        return render({
            friend = 'Early Fall 2018 Event',
        })
    end,
    test3 = render({
        friend = 'Early Fall 2018 Event',
        map = 'E-5',
    }),
    test4 = getFleetType({
        { ship = 'Littorio' }
    }),
}
-- print(p.test1)
-- print(p.test2)
-- print(p.test3)