Module:Sandbox/Fleet

Revision as of 10:58, 6 June 2019 by がか (talk | contribs)

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:KanMusu' .. is .. 'Banner.png|160px|link=' .. s:base_name() .. ']]<br>[[' .. s:base_name() .. '|' .. s:name() .. ']]'
end

local function getEquipmentCard(eq)
  return '[[File:Equipment' .. (Equipment(eq):id() or '?') .. '-1.png|100px|link=' .. eq .. ']]'
end

local function getFleetName(fleet, event)
  local names = require('Module:Fleet/Names')[event] or {}
  if 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)
  return string.format('<span class="explain" title="%s">~/%s</span>', U.ijoin(hps, ', '), maxHp)
end

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

local tableHeader = [=[|-
!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 function render(args)
    local success, data = U.requireModule('Data/Fleet/Friend/' .. args.friend)
    if not success then
        return "''No data''"
    end
  local groups = {}
  for _, e in pairs(data) do
    if not args.map or U.ifind(e.maps, args.map) 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 = fleetType .. ' (Variant ' .. i .. ')'
      if fleet.p2w and fleet.p2w > 0 then
        fleetNote = fleetNote .. " '''{{color|red|P2W}}'''"
      end
      if fleet.f2p and fleet.f2p > 0 then
        fleetNote = fleetNote .. " '''{{color|green|F2P}}'''"
      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||%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
    end
  end
  table.insert(result, '|}')
  return table.concat(result, '\n')
end

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