Module:AreaConnection: Difference between revisions

From IdleOn MMO Wiki
No edit summary
No edit summary
Line 79: Line 79:
function p._main(args)
function p._main(args)
     local root = mw.html.create('span')
     local root = mw.html.create('span')
     for value in ipairs(create_elements(args)) do
     for _, value in ipairs(create_elements(args)) do
         root:tag('span')
         root:tag('span')
             :css('display', 'block')
             :css('display', 'block')

Revision as of 22:38, 23 July 2024

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]]', name)
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

local function create_players_element(players)
    return string.format('(%s %s)', players, "Players")
end

local function create_elements(args)
	local name = Assert.existsMsg(args[1], 'Area connection is missing name')
    local kind = args[2]
    local quantity = kind and Assert.existsMsg(tonumber(args[3] or 0), 'Connection has requirement but has invalid quantity')
	local players = kind and Assert.existsMsg(tonumber(args[4] 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, quantity)
    }

    -- 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