Module:Sandbox/Fleet

Revision as of 14:39, 31 May 2019 by がか (talk | contribs) (Created page with "-- Example how to not write modules. local _ = require('Module:Core') local JSON = require('Module:Core/JSON') local loadData = require('Module:Data').load local ShipBattleC...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Sandbox/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)
    return "[[" .. ship .. "]]"
end

local function getEquipmentCard(eq)
    return "[[" .. eq .. "]]"
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
]=]

-- [[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
        return "''No data''"
    end
    local groups = {}
    for i, e in pairs(data) do
        if not args.map or _.ifind(e.maps, args.map) then
            local fleetType = getFleetType(e.fleet, args.friend)
            groups[fleetType] = groups[fleetType] or {}
            table.insert(groups[fleetType], e)
        end
    end
    local result = {}
    table.insert(result, '{{#tag:tabber|')
    for i, fleetType in ipairs(_.isort(_.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 = _.ijoin(_.imap(a.fleet, function (e) return e.ship end), ', ')
            local ships2 = _.ijoin(_.imap(b.fleet, function (e) return e.ship end), ', ')
            if ships1 == ships2 then
                return l1 < l2
            end
            return ships1 < ships2
        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 =', 'Variant ' .. i))
            if table.getn(fleet.notes) > 0 then
                table.insert(result, _.ijoin(fleet.notes, '<br>'))
            end                
            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
    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 = 'Early Fall 2018 Event',
    }),
    test3 = render({
        friend = 'Early Fall 2018 Event',
        map = 'E-5',
    }),
}

-- print(p.test1)
-- print(p.test2)
-- print(p.test3)