• 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 1: Line 1:
 
-- Example how to not write modules.
 
-- Example how to not write modules.
 +
 +
local _ = require('Module:Core')
 +
local JSON = require('Module:Core/JSON')
  
 
local ShipBattleCardKai = require('Module:ShipBattleCardKai')
 
local ShipBattleCardKai = require('Module:ShipBattleCardKai')
 
local EquipmentCardKai = require('Module:EquipmentCardKai')
 
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 ' ',
 +
              data.api_Slot[i][2] > 0 and getEquipmentCard(data.api_Slot[i][2]) or ' ',
 +
              data.api_Slot[i][3] > 0 and getEquipmentCard(data.api_Slot[i][3]) or ' ',
 +
              data.api_Slot[i][4] > 0 and getEquipmentCard(data.api_Slot[i][4]) or ' ',
 +
              -- 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 tableHeader = [=[{|class="wikitable" style="text-align:center"\n!Ship\n!colspan="4"|Equipment]=]
  
 
local function render(args, frame)
 
local function render(args, frame)
 +
    local tableHeader = args.friend and tableHeaderFriend or tableHeader
 
     local result = {}
 
     local result = {}
 
     local firstCell = true
 
     local firstCell = true
Line 12: Line 73:
 
         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{|class="wikitable" style="text-align:center"\n!Ship\n!colspan="4"|Equipment', arg:sub(3)))
+
                 table.insert(result, string.format('|}\n|-| %s =\n' .. tableHeader, arg:sub(3)))
 
             else
 
             else
                 table.insert(result, string.format('<tabber>\n|-| %s =\n{|class="wikitable" style="text-align:center"\n!Ship\n!colspan="4"|Equipment', arg:sub(3)))
+
                 table.insert(result, string.format('<tabber>\n|-| %s =\n' .. tableHeader, arg:sub(3)))
 
                 openSubtab = true
 
                 openSubtab = true
 
             end
 
             end
Line 28: Line 89:
 
                 openTab = true
 
                 openTab = true
 
             end
 
             end
 +
        elseif arg:sub(0, 1) == '~' then
 +
            table.insert(result, fromModule(arg:sub(2)))
 
         elseif arg == '-' then
 
         elseif arg == '-' then
 
             table.insert(result, '|-')
 
             table.insert(result, '|-')
Line 33: Line 96:
 
         else
 
         else
 
             if firstCell then
 
             if firstCell then
                 table.insert(result, '|' .. ShipBattleCardKai:Asset({ arg, hd = true }))
+
                 table.insert(result, '|' .. getShipCard(arg))
 
                 firstCell = false
 
                 firstCell = false
 
             else
 
             else
                 table.insert(result, '|' .. EquipmentCardKai:Asset({ arg, size = '100px' }))
+
                 table.insert(result, '|' .. getEquipmentCard(arg))
 
             end
 
             end
 
         end
 
         end
Line 57: Line 120:
 
     return frame:preprocess(render(getArgs{ frame = frame:getParent() }))
 
     return frame:preprocess(render(getArgs{ frame = frame:getParent() }))
 
     end,
 
     end,
     test = render({
+
     test1 = render({
 
         '!1 DD',
 
         '!1 DD',
 
         '!!Example 1',
 
         '!!Example 1',
         '-', 'Yuudachi/Kai Ni', 'Type 3 Active Sonar',
+
         '-', '1', 'Type 3 Active Sonar',
 
         '!!Example 2',
 
         '!!Example 2',
         '-', 'Yuudachi/Kai Ni', 'Type 3 Active Sonar', 'Type 3 Active Sonar',
+
         '-', 'Yuudachi/Kai Ni', '282', '282',
 
         '!2 DD',
 
         '!2 DD',
 
         '!!Example 1',
 
         '!!Example 1',
Line 70: Line 133:
 
         '-', 'Yuudachi/Kai Ni', 'Type 3 Active Sonar', 'Type 3 Active Sonar',
 
         '-', 'Yuudachi/Kai Ni', 'Type 3 Active Sonar', 'Type 3 Active Sonar',
 
         '-', 'Shigure/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)

Revision as of 13:02, 19 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 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 ' ',
              data.api_Slot[i][2] > 0 and getEquipmentCard(data.api_Slot[i][2]) or ' ',
              data.api_Slot[i][3] > 0 and getEquipmentCard(data.api_Slot[i][3]) or ' ',
              data.api_Slot[i][4] > 0 and getEquipmentCard(data.api_Slot[i][4]) or ' ',
              -- 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 tableHeader = [=[{|class="wikitable" style="text-align:center"\n!Ship\n!colspan="4"|Equipment]=]

local function render(args, frame)
    local tableHeader = args.friend and tableHeaderFriend or tableHeader
    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)