Module:SortieList

Revision as of 09:16, 19 October 2016 by がか (talk | contribs) (Created page with "local getArgs = require("Module:GetArgs") local find = require("Module:Utils").find local format = require("Module:StringInterpolation").format local ResourceIcons = require("...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local getArgs = require("Module:GetArgs")
local find = require("Module:Utils").find
local format = require("Module:StringInterpolation").format
local ResourceIcons = require("Module:ResourceIcons")

local templates = {

    header = [[{| class="article-table" border="0" cellpadding="1" cellspacing="1" style="width:100%;"]],

    footer = "|}",

    map_rows = [=[|-
! colspan="2" style="text-align:center;width:40%;" | [[${link}|World ${id}: ${name}]]<br />${name_en}
| rowspan="4" style="text-align:center;width:60%;" | ${banner}
|-
! Difficulty
| ${difficulty}
|-
! EXP/node
| ${ship_exp}
|-
! Items
| ${items_string}
|-
! Fluff text
| ${strategy_name}
| ${strategy_info}]=],

}

function formatItems(items)
    local item_names = {
        "fuel", "ammo", "steel", "bauxite",
        "bucket", "devmat", "conmat",
        "furniture_box_small", "furniture_box_medium", "furniture_box_large",
    }
    if items == nil then
        return "?"
    end
    if items == false then
        return "None"
    end
    local images = {}
    for _, item_name in ipairs(item_names) do
        if items[item_name] then
            local image = ResourceIcons[item_name] and string.format("[[File:%s]]", ResourceIcons[item_name]) or "?"
            table.insert(images, image)
        end
    end
    return table.concat(images, " ")
end

function formatDifficulty(stars)
    return stars and string.rep("☆", stars) or "?"
end

function loadMapData(world_id, id)
    local success, table = pcall(function() return require(string.format("Module:World %d/%s", world_id, id)) end)
    return success and table or {}
end

function formatTable(maps)
    local rows = {}
    table.insert(rows, templates.header)
    for _, id in ipairs(maps) do
        local world_id = string.match(id, "%d") or "?"
        local map_data = loadMapData(world_id, id)
        table.insert(rows, format{
            templates.map_rows,
            id = id,
            link = string.format("World %d#%s", world_id, id),
            banner = string.format("[[File:%s.png]]", id),
            name = map_data.name or "?",
            name_en = map_data.name_en or "?",
            difficulty = formatDifficulty(map_data.stars),
            ship_exp = map_data.ship_exp or "?",
            items_string = formatItems(map_data.items),
            strategy_name = map_data.strategy_name or "?",
            strategy_info = map_data.strategy_info or "?",
            -- strategy_name_en
            -- strategy_info_en
            -- hp
            -- kills_required
            -- hq_non_boss_exp
            -- hq_boss_exp
            -- eo
        })
    end
    table.insert(rows, templates.footer)
    return table.concat(rows, "\n")
end

local SortieList = {}

function SortieList.format(frame, args)
    args = args or getArgs{frame = frame:getParent()}
    return formatTable(args)
end

-- SortieList.t = SortieList.format(nil, { "1-1", "1-2", "1-3", "1-4" })
-- print(p.t)

return SortieList