Module:AreaConnection: Difference between revisions

From IdleOn MMO Wiki
No edit summary
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 6: Line 6:


--- Gets the icon for the specified name.
--- Gets the icon for the specified name.
--- @param name string The name of the icon
--- @param name string The name of the icon.
--- @return string
--- @return string
local function get_icon(name)
local function get_icon(name)
return string.format('[[File:%s.png|16px]]', 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
end


Line 21: Line 28:
--- Creates the "requirements" element.
--- Creates the "requirements" element.
--- @param kind string? The requirement type.
--- @param kind string? The requirement type.
--- @param quantity number The requirement quantity.
--- @param quantity number? The requirement quantity.
--- @return string
--- @return string
local function create_requirement_element(kind, quantity)
local function create_requirement_element(kind, quantity)
Line 43: Line 50:
end
end


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


--- Creates the components of the areas.
--- @param args table The frame arguments.
--- @return string[]
local function create_elements(args)
local function create_elements(args)
local name = Assert.existsMsg(args[1], 'Area connection is missing name')
local name = Assert.existsMsg(args.name, 'Area connection is missing name')
     local kind = args[2]
     local kind = check_empty(args.kind)
     local quantity = kind and Assert.existsMsg(tonumber(args[3] or 0), 'Connection has requirement but has invalid quantity')
     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(args[4] or 1), 'Connection has requirement but has invalid player count')
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.
     -- Name and requirement elements are always printed.
     local ret = {
     local ret = {
         create_name_element(name),
         create_name_element(name),
         create_requirement_element(kind, quantity)
         create_requirement_element(kind, amount)
     }
     }


Line 79: Line 92:
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')

Latest revision as of 23:34, 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|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