Module:Resources: Difference between revisions
From Nova Roma Official Wiki
No edit summary |
No edit summary |
||
| Line 21: | Line 21: | ||
if amount and name and icons[name] then | if amount and name and icons[name] then | ||
-- If amount exists: "icon amount" | -- If amount exists: "icon amount" with <br> | ||
table.insert(result, icons[name] .. " " .. amount) | table.insert(result, {icons[name] .. " " .. amount, true}) | ||
else | else | ||
-- If only name: "icon" | -- If only name: "icon" with space | ||
name = resource:match("(%w+)") | name = resource:match("(%w+)") | ||
if name and icons[name] then | if name and icons[name] then | ||
table.insert(result, icons[name]) | table.insert(result, {icons[name], false}) | ||
end | end | ||
end | end | ||
end | end | ||
-- Join with <br> for amounts, space for names only | |||
local output = {} | |||
for i, item in ipairs(result) do | |||
table.insert(output, item[1]) | |||
end | |||
local final = "" | |||
for i, item in ipairs(result) do | |||
final = final .. item[1] | |||
if item[2] and i < #result then | |||
final = final .. "<br>" | |||
elseif not item[2] and i < #result then | |||
final = final .. " " | |||
end | |||
end | |||
return final | |||
end | end | ||
return p | return p | ||
Revision as of 22:13, 24 February 2026
Documentation for this module may be created at Module:Resources/doc
local p = {}
local icons = {
wood = "[[File:icon_wood.png|30px|link=Wood]]",
tools = "[[File:icon_tools.png|30px|link=Tools]]",
stone = "[[File:icon_stone.png|30px|link=Stone]]",
gold = "[[File:icon_gold.png|30px|link=Gold]]",
food = "[[File:icon_wheat.png|30px|link=Food]]",
weapons = "[[File:icon_armament.png|30px|link=Armament]]",
charcoal = "Charcoal",
}
function p.format(frame)
local input = frame.args[1] or ""
local result = {}
-- Split by comma and process each resource
for resource in input:gmatch("[^,]+") do
resource = resource:match("^%s*(.-)%s*$") -- trim whitespace
local amount, name = resource:match("(%d+)%s+(%w+)")
if amount and name and icons[name] then
-- If amount exists: "icon amount" with <br>
table.insert(result, {icons[name] .. " " .. amount, true})
else
-- If only name: "icon" with space
name = resource:match("(%w+)")
if name and icons[name] then
table.insert(result, {icons[name], false})
end
end
end
-- Join with <br> for amounts, space for names only
local output = {}
for i, item in ipairs(result) do
table.insert(output, item[1])
end
local final = ""
for i, item in ipairs(result) do
final = final .. item[1]
if item[2] and i < #result then
final = final .. "<br>"
elseif not item[2] and i < #result then
final = final .. " "
end
end
return final
end
return p


