Module:AreaConnection

From IdleOn MMO Wiki

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

require('strict')

local Assert = require('Module:Assert')

local p = {}

--- Gets the icon for the specified name.
--- @param name string The name of the icon.
--- @return string
local function get_icon(name)
	return string.format('[[File:%s.png|16px|baseline|link=]]', name)
end

--- Checks whether a string is empty, and returns nil if it is.
--- @param str string? The string to check.
--- @return string? The string, or nil if it is empty.
local function check_empty(str)
	return str and str:len() > 0 and str or nil
end

--- Creates and returns the "name" element.
--- @param name string The name.
--- @return string
local function create_name_element(name)
    return name
end

--- Creates the "requirements" element.
--- @param kind string? The requirement type.
--- @param quantity number? The requirement quantity.
--- @return string
local function create_requirement_element(kind, quantity)
    -- No kind specified, so there is no portal requirement.
    if kind == nil then
        return '(No requirements)'
    end

    -- Icon to display next to the portal requirement.
    local icon = kind == 'kills'
        and get_icon('Status Skull0')
        or get_icon(kind .. ' Skill Icon')

    -- The label to prepend to the quantity.
    local label = kind == 'kills'
        and ''
        or 'LV.'

    -- Format the element.
    return string.format('(%s%s %s)', label, quantity, icon)
end

--- Creates the "player requirement" element.
--- @param players number The number of players.
--- @return string
local function create_players_element(players)
    return string.format('(%s %s)', players, "players")
end

--- Creates the components of the areas.
--- @param args table The frame arguments.
--- @return string[]
local function create_elements(args)
	local name = Assert.existsMsg(args.name, 'Area connection is missing name')
    local kind = check_empty(args.kind)
    local amount = kind and Assert.existsMsg(tonumber(check_empty(args.amount) or 0), 'Connection has requirement but has invalid amount')
	local players = kind and Assert.existsMsg(tonumber(check_empty(args.players) or 1), 'Connection has requirement but has invalid player count')

    -- Name and requirement elements are always printed.
    local ret = {
        create_name_element(name),
        create_requirement_element(kind, amount)
    }

    -- Only display the "players" element if more than 1 player is required to unlock the portal.
    if kind and players > 1 then
        table.insert(ret, create_players_element(players))
    end

    return ret
end

--- Template entry point
--- @param frame table The template frame
--- @return string
function p.main(frame)
    return p._main(frame:getParent().args)
end

--- Module entry point
--- @param args string[]
--- @return string
function p._main(args)
    local root = mw.html.create('span')
    for _, value in ipairs(create_elements(args)) do
        root:tag('span')
            :css('display', 'block')
            :css('text-align', 'center')
            :wikitext(value)
    end
    return tostring(root)
end

return p