Module:Resources

From Nova Roma Official Wiki
Revision as of 11:40, 18 March 2026 by DCello (talk | contribs)

Documentation for this module may be created at Module:Resources/doc

local p = {}

local icons = {
	apples = "[[File:icon_apples.png|30px|link=Apples]]",
	armaments = "[[File:icon_armament.png|30px|link=Armaments]]",
	bread = "[[File:resource bread.png|30px|link=Bread]]",
	charcoal = "[[File:resource charcoal.png|30px|link=Charcoal]]",
	clay = "[[File:icon_clay.png|30px|link=Clay]]",
	fish = "[[File:icon_fish.png|30px|link=Fish]]",
	gold = "[[File:icon_gold.png|30px|link=Gold]]",
	grapes = "[[File:icon_grapes.png|30px|link=Grapes]]",
	iron = "[[File:Resource_iron.png|30px|link=Iron]]",
	marble = "[[File:icon_marble.png|30px|link=Marble]]",
	meat = "[[File:icon_pork.png|30px|link=Meat]]",
	olives = "[[File:icon_olives.png|30px|link=Olives]]",
	oliveoil = "[[File:icon_oliveoil.png|30px|link=Olive Oil]]",
	pottery = "[[File:icon_pottery.png|30px|link=Pottery]]",
	salt = "[[File:icon_salt.png|30px|link=Salt]]",
	stone = "[[File:icon_stone.png|30px|link=Stone]]",
	tools = "[[File:icon_tools.png|30px|link=Tools]]",
	wheat = "[[File:icon_wheat.png|30px|link=Wheat]]",
	wine = "[[File:icon_wine.png|30px|link=Wine]]",
	wood = "[[File:icon_wood.png|30px|link=Wood]]",
	citizens = "[[File:Citizens.png|30px|link=Citizens]]",
}

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

-- outputs icon + linked name for a single resource
function p.label(frame)
    local a = frame.args[1] or frame:getParent().args[1]
    local b = frame.args[2] or frame:getParent().args[2]

    if not a then return "" end

    -- Case 1: {{32 Iron}} → a = "32", b = "Iron"
    if tonumber(a) and b then
        local amount = tonumber(a)
        local name = b:lower()
        local icon = icons[name]

        if not icon then
            return "''Unknown resource: " .. name .. "''"
        end

        return string.format("%s %d", icon, amount)
    end

    -- Case 2: {{Iron}} → a = "Iron"
    local name = a:lower()
    local icon = icons[name]

    if not icon then
        return "''Unknown resource: " .. name .. "''"
    end

    -- Extract link target
    local link = icon:match("link=([^%]]+)%]%]") or name

    return string.format("%s [[%s]]", icon, link)
end

return p