Module:Contract

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

require('strict')

local Contract = {}

--- @param categories table Plain text categories in array
local function convertCategories(categories)
    local mapped = {}
    for _, category in pairs(categories) do
        if category ~= nil then
            if string.sub(category, 1, 2) ~= '[[' then
                category = string.format('[[Category:%s]]', category)
            end

            table.insert(mapped, category)
        end
    end
    return table.concat(mapped)
end

-- @param frame table https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Frame_object
function Contract.main(frame)
    local args = require('Module:Arguments').getArgs(frame)
    local infobox = require('Module:InfoboxNew'):new()
    local categories = {}

    infobox:renderImage(args['image'])

    infobox:renderHeader({
        title = args['name'],
        subtitle = args['faction']
    })

    local zone_label = "Zone"
    if args['zone'] and ( string.lower(args['zone']) == "multizone" or string.find(args['zone'], ',' )) then
        zone_label = "Zones"
    end

    infobox:renderSection({
        content = {
            infobox:renderItem({
                label = "Type",
                data = args['type']
            }),
            infobox:renderItem({
                label = zone_label,
                data = args['zone']
            })
        },
        col = 2
    })

    infobox:renderSection({
        content = {
            infobox:renderItem({
                label = "Reputation level",
                data = args['rep lvl']
            })
        },
        col = 2
    })

    if args['prev'] then
        args['prev'] = '[[' .. args['prev'] .. ']]'
    end
    if args['next'] then
        args['next'] = '[[' .. args['next'] .. ']]'
    end

    infobox:renderSection({
        title = "Series",
        content = {
            infobox:renderItem({
                label = "Previous",
                data = args['prev']
            }),
            infobox:renderItem({
                label = "Next",
                data = args['next']
            })
        },
        col = 2
    })

    infobox:renderSection({
        title = "Story",
        content = {
            infobox:renderItem({
                data = args['description'],
            })
        }
    })

    local objectives = {}
    table.insert(objectives, infobox:renderItem({
        data = args['objective description'],
    }))

    -- intentionally limited to 5
    for i = 1, 5 do
        if args['objective' .. i] then
            table.insert(objectives, infobox:renderItem({
                label = 'Objective ' .. i,
                data = args['objective' .. i],
                desc = string.format("%s %s faction reputation", args['factionrep' .. i], args['faction'])
            }))
        else
            break
        end
    end

    infobox:renderSection({
        title = "Objectives",
        subtitle = args['restriction'],
        content = table.concat(objectives)
    })

    local style = ''

    local styles = {
        arachne = 'Module:Contract/arachne.css',
        cyac = 'Module:Contract/cyac.css',
        mida = 'Module:Contract/mida.css',
        nucal = 'Module:Contract/nucal.css',
        sekgen = 'Module:Contract/sekgen.css',
        traxus = 'Module:Contract/traxus.css'
    }

    if args['faction'] and styles[string.lower(args['faction'])] then
        style = frame:extensionTag {
            name = 'templatestyles', args = { src = styles[string.lower(args['faction'])] }
        }
    end

    return tostring(infobox:renderInfobox(nil, args['name'])) .. style .. convertCategories(categories)
end

return Contract