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

From Kancolle Wiki
Jump to navigation Jump to search
m
 
(42 intermediate revisions by 2 users not shown)
Line 1: Line 1:
-- Example how to not write modules.
+
local U = require('Module:Core')
 
+
local getArgs = require('Module:GetArgs')
local _ = require('Module:Core')
 
local JSON = require('Module:Core/JSON')
 
local loadData = require('Module:Data').load
 
 
 
 
local Ship = require('Module:Ship')
 
local Ship = require('Module:Ship')
 
local Equipment = require('Module:Equipment')
 
local Equipment = require('Module:Equipment')
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 function getShipCard(ship)
    local s = Ship(ship)
+
  local s = Ship(ship)
    local i = s:id() or s:true_id()
+
  -- local i = s:id() or s:true_id()
    local is = i and (i < 10 and '00' .. i or i < 100 and '0' .. i or i) or '?'
+
  -- local is = i and (i < 10 and '00' .. i or i < 100 and '0' .. i or i) or '?'
    return '[[File:KanMusu' .. is .. 'Banner.png]]<br>[[' .. s:base_name() .. '|' .. s:name() .. ']]'
+
  return '[[File:Ship Banner ' .. s:name() .. '.png|160px|link=' .. s:base_name() .. ']]<br>[[' .. s:base_name() .. '|' .. s:name() .. ']]'
 
end
 
end
  
 
local function getEquipmentCard(eq)
 
local function getEquipmentCard(eq)
    return '[[File:Equipment' .. (Equipment(eq):id() or '?') .. '-1.png|150px]]<!--' .. eq .. '-->'
+
  local e = Equipment(eq)
 +
  return '[[File:Equipment Card ' .. e:name():gsub("/", " ") .. '.png|100px|link=' .. e:name() .. ']]'
 +
end
 +
 
 +
local function getFleetName(fleet, event)
 +
  local names = require('Module:FleetNames')[event] or {}
 +
  if names == 'flagship' then
 +
    return Ship(fleet[1].ship):base_name() .. ' FS'
 +
  elseif names[1] then
 +
    for _, f in ipairs(names) do
 +
      for i, ship in ipairs(f.ships) do
 +
        for j, e in ipairs(fleet) do
 +
          if f.flagship then
 +
            if i == 1 and j == 1 and string.sub(e.ship, 1, string.len(ship)) == ship then
 +
              return f.name
 +
            end
 +
          else
 +
            if string.sub(e.ship, 1, string.len(ship)) == ship then
 +
              return f.name
 +
            end
 +
          end
 +
        end
 +
      end
 +
    end
 +
  else
 +
    for name, ships in pairs(names) do
 +
      for _, ship in ipairs(ships) do
 +
        for _, e in ipairs(fleet) do
 +
          if string.sub(e.ship, 1, string.len(ship)) == ship then
 +
            return name
 +
          end
 +
        end
 +
      end
 +
    end
 +
  end
 +
  return 'Unassorted'
 
end
 
end
  
local function fromModule(name)
+
local function formatHp(hps, maxHp)
     local result = ''
+
  local minHp = U.imin(hps)
 +
  if minHp / maxHp <= 0.5 then
 +
     return string.format('{{color|red|{{tooltip|~/%s|%s}}}}', maxHp, U.ijoin(hps, ', '))
 +
  elseif minHp / maxHp <= 0.75 then
 +
    return string.format('{{color|coral|{{tooltip|~/%s|%s}}}}', maxHp, U.ijoin(hps, ', '))
 +
  else
 +
    return string.format('{{tooltip|~/%s|%s}}', maxHp, U.ijoin(hps, ', '))
 +
  end
 +
end
  
     return result
+
local function formatVoice(e)
 +
  if type(e.voice) == 'number' and e.voice < 10 then
 +
     return string.format('{{audio|file=Ship Voice %s Spring 2019 Event Friend Fleet %s.mp3}}', e.ship, e.voice)
 +
  else
 +
    return string.format('{{audio|file=Ship Voice %s %s.mp3}}', e.ship, e.voice)
 +
  end
 
end
 
end
  
local tableHeaderFriend = [=[{|class="wikitable" style="text-align:center"
+
local tableStart =
 +
  [=[{|class="wikitable mw-collapsible mw-collapsed" style="text-align:center;min-width:700px"
 +
|-
 +
!colspan="11"|%s]=]
 +
 
 +
local tableHeader = [=[|-
 
!Ship
 
!Ship
 
!Lv.
 
!Lv.
!colspan="4"|Equipment
+
!Equipment
 
![[File:Icon HP.png]]
 
![[File:Icon HP.png]]
 
![[File:Icon Gun.png]]
 
![[File:Icon Gun.png]]
Line 39: Line 84:
 
![[File:Icon Armor.png]]]=]
 
![[File:Icon Armor.png]]]=]
  
local tableHeaderSimple = [=[{|class="wikitable" style="text-align:center"
+
local function render(args)
!Ship
+
     local success, data = U.requireModule('Data/FriendFleet/' .. args.friend)
!colspan="4"|Equipment
 
]=]
 
 
 
-- [[Category:Todo]]: need something better here
 
local friendTypes = require('Module:Fleets/Names')
 
 
 
local function getFleetType(fleet, event)
 
    local names = friendTypes[event] or {}
 
    if names[1] then
 
        for i, o in ipairs(names) do
 
            for i, ship in ipairs(o.ships) do
 
                if _.ifindBy(fleet, function (e) return _.startsWith(e.ship, ship) end) then
 
                    return o.name
 
                end
 
            end
 
        end
 
    else
 
        for name, ships in pairs(names) do
 
            for i, ship in ipairs(ships) do
 
                if _.ifindBy(fleet, function (e) return _.startsWith(e.ship, ship) end) then
 
                    return name
 
                end
 
            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 success, data = _.requireModule('Data/Fleet/Friend/' .. args.friend)
 
 
     if not success then
 
     if not success then
 
         return "''No data''"
 
         return "''No data''"
 
     end
 
     end
    local groups = {}
+
  local groups = {}
    for i, e in pairs(data) do
+
  for _, e in pairs(data) do
        if not args.map or _.ifind(e.maps, args.map) then
+
    if not args.map and not args.node or U.ifind(e.maps, args.map) or U.ifind(e.notes, args.node) then
            local fleetType = getFleetType(e.fleet, args.friend)
+
      local fleetType = getFleetName(e.fleet, args.friend)
            groups[fleetType] = groups[fleetType] or {}
+
      groups[fleetType] = groups[fleetType] or {}
            table.insert(groups[fleetType], e)
+
      table.insert(groups[fleetType], e)
        end
 
 
     end
 
     end
    local result = {}
+
  end
    table.insert(result, '{{#tag:tabber|')
+
  local result = {}
    for i, fleetType in ipairs(_.isort(_.keys(groups))) do
+
  local first = true
        local fleets = groups[fleetType]
+
  for _, fleetType in ipairs(U.isort(U.keys(groups))) do
        table.sort(fleets, function(a, b)
+
    local fleets = groups[fleetType]
            local l1 = table.getn(a.fleet)
+
    table.sort(
            local l2 = table.getn(b.fleet)
+
      fleets,
            local ships1 = _.ijoin(_.imap(a.fleet, function (e) return e.ship end), ', ')
+
      function(a, b)
             local ships2 = _.ijoin(_.imap(b.fleet, function (e) return e.ship end), ', ')
+
        local l1 = table.getn(a.fleet)
            if ships1 == ships2 then
+
        local l2 = table.getn(b.fleet)
                return l1 < l2
+
        local ships1 =
 +
          U.ijoin(
 +
          U.imap(
 +
            a.fleet,
 +
             function(e)
 +
              return e.ship
 
             end
 
             end
            return ships1 < ships2
+
          ),
        end)
+
          ', '
        table.insert(result, string.format('{{!}}-{{!}} %s =', fleetType))
+
         )
         table.insert(result, '<tabber>')
+
         local ships2 =
         for i, fleet in ipairs(fleets) do
+
          U.ijoin(
            table.insert(result, string.format('|-| %s =', 'Variant ' .. i))
+
          U.imap(
            if table.getn(fleet.notes) > 0 then
+
             b.fleet,
                table.insert(result, _.ijoin(fleet.notes, '<br>'))
+
             function(e)
            end               
+
              return e.ship
            table.insert(result, tableHeaderFriend)
 
             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
 +
          ),
 +
          ', '
 +
        )
 +
        if ships1 == ships2 then
 +
          return l1 < l2
 
         end
 
         end
 +
        return ships1 < ships2
 +
      end
 +
    )
 +
    for i, fleet in ipairs(fleets) do
 +
      if first then
 +
        table.insert(result, string.format(tableStart, U.ijoin(fleet.notes, ', ')))
 +
        first = false
 +
      end
 +
      local fleetNote = U.ijoin(fleet.notes, ', ') .. ' : ' ..fleetType .. ' (Variant ' .. i .. ', ' .. (fleet.date or '??') .. '~)'
 +
      if fleet.f2p and fleet.f2p > 0 then
 +
        fleetNote = fleetNote .. " '''{{color|green|{{tooltip|Regular|"  .. fleet.f2p ..  " samples}}}}'''"
 +
      end
 +
      if fleet.p2w and fleet.p2w > 0 then
 +
        fleetNote = fleetNote .. " '''{{color|red|{{tooltip|Powerful|"  .. fleet.p2w ..  " samples}}}}'''"
 +
      end
 +
      if table.getn(fleet.voices or {}) > 0 then
 +
        fleetNote = fleetNote .. ' ' .. U.ijoin(U.imap(fleet.voices, formatVoice), ' ')
 +
      end
 +
      table.insert(result, string.format('|-\n!colspan="11"|%s', fleetNote))
 +
      table.insert(result, tableHeader)
 +
      for _, ship in ipairs(fleet.fleet) do
 +
        table.insert(
 +
          result,
 +
          string.format(
 +
            '|-\n|%s||%s||style="text-align:left;white-space:nowrap"|%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 '',
 +
            ship.equipment[2] and getEquipmentCard(ship.equipment[2]) or '',
 +
            ship.equipment[3] and getEquipmentCard(ship.equipment[3]) or '',
 +
            ship.equipment[4] and getEquipmentCard(ship.equipment[4]) or '',
 +
            ship.equipment[5] and getEquipmentCard(ship.equipment[5]) or '',
 +
            ship.extraSlot and string.format('<span style="display:inline-block;border-radius:10px;border:5px solid steelblue">%s</span>', getEquipmentCard(ship.extraSlot)) or '',
 +
            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
 
     end
 
     end
    if openSubtab then
+
  end
        table.insert(result, '|}\n</tabber>')
+
  table.insert(result, '|}')
        openSubtab = false
+
  return table.concat(result, '\n')
    end
 
    if openTab then
 
        table.insert(result, '}}')
 
        openTab = false
 
    end
 
    return table.concat(result, '\n')
 
 
end
 
end
 
local getArgs = require('Module:GetArgs')
 
  
 
return {
 
return {
    render = function(frame)
+
  render = function(frame)
    return frame:preprocess(render(getArgs{ frame = frame:getParent() }))
+
    return frame:preprocess(render(getArgs {frame = frame:getParent()}))
    end,
+
  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 = 'Early Fall 2018 Event',
 
    }),
 
    test3 = render({
 
        friend = 'Early Fall 2018 Event',
 
        map = 'E-5',
 
    }),
 
 
}
 
}
 
-- print(p.test1)
 
-- print(p.test2)
 
-- print(p.test3)
 

Latest revision as of 06:35, 20 September 2021

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

local U = require('Module:Core')
local getArgs = require('Module:GetArgs')
local Ship = require('Module:Ship')
local Equipment = require('Module:Equipment')

local function getShipCard(ship)
  local s = Ship(ship)
  -- local i = s:id() or s:true_id()
  -- local is = i and (i < 10 and '00' .. i or i < 100 and '0' .. i or i) or '?'
  return '[[File:Ship Banner ' .. s:name() .. '.png|160px|link=' .. s:base_name() .. ']]<br>[[' .. s:base_name() .. '|' .. s:name() .. ']]'
end

local function getEquipmentCard(eq)
  local e = Equipment(eq)
  return '[[File:Equipment Card ' .. e:name():gsub("/", " ") .. '.png|100px|link=' .. e:name() .. ']]'
end

local function getFleetName(fleet, event)
  local names = require('Module:FleetNames')[event] or {}
  if names == 'flagship' then
    return Ship(fleet[1].ship):base_name() .. ' FS'
  elseif names[1] then
    for _, f in ipairs(names) do
      for i, ship in ipairs(f.ships) do
        for j, e in ipairs(fleet) do
          if f.flagship then
            if i == 1 and j == 1 and string.sub(e.ship, 1, string.len(ship)) == ship then
              return f.name
            end
          else
            if string.sub(e.ship, 1, string.len(ship)) == ship then
              return f.name
            end
          end
        end
      end
    end
  else
    for name, ships in pairs(names) do
      for _, ship in ipairs(ships) do
        for _, e in ipairs(fleet) do
          if string.sub(e.ship, 1, string.len(ship)) == ship then
            return name
          end
        end
      end
    end
  end
  return 'Unassorted'
end

local function formatHp(hps, maxHp)
  local minHp = U.imin(hps)
  if minHp / maxHp <= 0.5 then
    return string.format('{{color|red|{{tooltip|~/%s|%s}}}}', maxHp, U.ijoin(hps, ', '))
  elseif minHp / maxHp <= 0.75 then
    return string.format('{{color|coral|{{tooltip|~/%s|%s}}}}', maxHp, U.ijoin(hps, ', '))
  else
    return string.format('{{tooltip|~/%s|%s}}', maxHp, U.ijoin(hps, ', '))
  end
end

local function formatVoice(e)
  if type(e.voice) == 'number' and e.voice < 10 then
    return string.format('{{audio|file=Ship Voice %s Spring 2019 Event Friend Fleet %s.mp3}}', e.ship, e.voice)
  else
     return string.format('{{audio|file=Ship Voice %s %s.mp3}}', e.ship, e.voice)
  end
end

local tableStart =
  [=[{|class="wikitable mw-collapsible mw-collapsed" style="text-align:center;min-width:700px"
|-
!colspan="11"|%s]=]

local tableHeader = [=[|-
!Ship
!Lv.
!Equipment
![[File:Icon HP.png]]
![[File:Icon Gun.png]]
![[File:Icon Torpedo.png]]
![[File:Icon AA.png]]
![[File:Icon Armor.png]]]=]

local function render(args)
    local success, data = U.requireModule('Data/FriendFleet/' .. args.friend)
    if not success then
        return "''No data''"
    end
  local groups = {}
  for _, e in pairs(data) do
    if not args.map and not args.node or U.ifind(e.maps, args.map) or U.ifind(e.notes, args.node) then
      local fleetType = getFleetName(e.fleet, args.friend)
      groups[fleetType] = groups[fleetType] or {}
      table.insert(groups[fleetType], e)
    end
  end
  local result = {}
  local first = true
  for _, fleetType in ipairs(U.isort(U.keys(groups))) do
    local fleets = groups[fleetType]
    table.sort(
      fleets,
      function(a, b)
        local l1 = table.getn(a.fleet)
        local l2 = table.getn(b.fleet)
        local ships1 =
          U.ijoin(
          U.imap(
            a.fleet,
            function(e)
              return e.ship
            end
          ),
          ', '
        )
        local ships2 =
          U.ijoin(
          U.imap(
            b.fleet,
            function(e)
              return e.ship
            end
          ),
          ', '
        )
        if ships1 == ships2 then
          return l1 < l2
        end
        return ships1 < ships2
      end
    )
    for i, fleet in ipairs(fleets) do
      if first then
        table.insert(result, string.format(tableStart, U.ijoin(fleet.notes, ', ')))
        first = false
      end
      local fleetNote = U.ijoin(fleet.notes, ', ') .. ' : ' ..fleetType .. ' (Variant ' .. i .. ', ' .. (fleet.date or '??') .. '~)'
      if fleet.f2p and fleet.f2p > 0 then
        fleetNote = fleetNote .. " '''{{color|green|{{tooltip|Regular|"  .. fleet.f2p ..  " samples}}}}'''"
      end
      if fleet.p2w and fleet.p2w > 0 then
        fleetNote = fleetNote .. " '''{{color|red|{{tooltip|Powerful|"  .. fleet.p2w ..  " samples}}}}'''"
      end
      if table.getn(fleet.voices or {}) > 0 then
        fleetNote = fleetNote .. ' ' .. U.ijoin(U.imap(fleet.voices, formatVoice), ' ')
      end
      table.insert(result, string.format('|-\n!colspan="11"|%s', fleetNote))
      table.insert(result, tableHeader)
      for _, ship in ipairs(fleet.fleet) do
        table.insert(
          result,
          string.format(
            '|-\n|%s||%s||style="text-align:left;white-space:nowrap"|%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 '',
            ship.equipment[2] and getEquipmentCard(ship.equipment[2]) or '',
            ship.equipment[3] and getEquipmentCard(ship.equipment[3]) or '',
            ship.equipment[4] and getEquipmentCard(ship.equipment[4]) or '',
            ship.equipment[5] and getEquipmentCard(ship.equipment[5]) or '',
            ship.extraSlot and string.format('<span style="display:inline-block;border-radius:10px;border:5px solid steelblue">%s</span>', getEquipmentCard(ship.extraSlot)) or '',
            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
    end
  end
  table.insert(result, '|}')
  return table.concat(result, '\n')
end

return {
  render = function(frame)
    return frame:preprocess(render(getArgs {frame = frame:getParent()}))
  end
}