Module:AreaConnection: Difference between revisions
From IdleOn MMO Wiki
No edit summary |
No edit summary |
||
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]]', 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 43: | Line 50: | ||
end | end | ||
--- Creates the "player requirement" element. | |||
--- @param players The number of players. | |||
--- @return string | |||
local function create_players_element(players) | local function create_players_element(players) | ||
return string.format('(%s %s)', players, " | return string.format('(%s %s)', players, "players") | ||
end | end | ||
--- Creates the components of the areas. | |||
--- @param args The frame arguments. | |||
--- @return string[] | |||
local function create_elements(args) | local function create_elements(args) | ||
local name = Assert.existsMsg(args.name, 'Area connection is missing name') | local name = Assert.existsMsg(args.name, 'Area connection is missing name') | ||
local kind = args.kind | local kind = check_empty(args.kind) | ||
local quantity = kind and Assert.existsMsg(tonumber(args. | local quantity = 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.players 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') | ||
-- Quantity | |||
local quantity = kind and Assert.existsMsg(tonumber(args.quantity or 0), | |||
'Connection has requirement but has invalid quantity') | |||
-- Name and requirement elements are always printed. | -- Name and requirement elements are always printed. |
Revision as of 23:20, 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
--- 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 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 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 quantity = 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')
-- Quantity
local quantity = kind and Assert.existsMsg(tonumber(args.quantity or 0),
'Connection has requirement but has invalid quantity')
-- 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