Module:SortieList

Revision as of 10:05, 19 October 2016 by がか (talk | contribs)

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

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

local templates = {

    header = [[{| class="article-table" style="width:100%;"]],

    footer = "|}",

    map_rows = [=[|-
! colspan="2" style="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}]=],

    eo_map_rows = [=[|-
| colspan="4" style="text-align:center;" | ${banner}
|-
! colspan="4" | [[${link}|World ${id}: ${name}]]<br />${name_en}
|-
! style="width:10%;" | Difficulty
| style="width:25%;" | ${difficulty}
! rowspan="3" style="width:11%;" | Fluff text
| style="width:54%;" | ${strategy_name}
|-
! Exp/node
| ${ship_exp}
| rowspan="2" | ${strategy_info}
|-
! Items
| ${items_string}]=],

}

local item_names = {
    "fuel", "ammo", "steel", "bauxite",
    "bucket", "devmat", "conmat",
    "furniture_box_small", "furniture_box_medium", "furniture_box_large",
}

function formatItems(items)
    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|18px]]", ResourceIcons[item_name]) or item_name
            table.insert(images, image)
        end
    end
    return table.concat(images, " ")
end

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

function formatShipExp(exp)
    return exp == false and "Variable" or exp 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{
            map_data.eo and templates.eo_map_rows or templates.map_rows,
            id = id,
            link = string.format("World %d#%s", world_id, id),
            banner = string.format("[[File:%s Banner.png]]", id),
            name = map_data.name or "?",
            name_en = map_data.name_en or "?",
            difficulty = formatDifficulty(map_data.stars),
            ship_exp = formatShipExp(map_data.ship_exp),
            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

return SortieList