Module:AreaConnection: Difference between revisions
From IdleOn MMO Wiki
No edit summary |
No edit summary |
||
Line 48: | Line 48: | ||
local function create_elements(args) | local function create_elements(args) | ||
local name = Assert.existsMsg(args | local name = Assert.existsMsg(args.name, 'Area connection is missing name') | ||
local kind = args | local kind = args.kind | ||
local quantity = kind and Assert.existsMsg(tonumber(args | local quantity = kind and Assert.existsMsg(tonumber(args.quantity or 0), 'Connection has requirement but has invalid quantity') | ||
local players = kind and Assert.existsMsg(tonumber(args | local players = kind and Assert.existsMsg(tonumber(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. |
Revision as of 23:06, 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.name, 'Area connection is missing name')
local kind = args.kind
local quantity = kind and Assert.existsMsg(tonumber(args.quantity or 0), 'Connection has requirement but has invalid quantity')
local players = kind and Assert.existsMsg(tonumber(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, 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