Module:ResourceInfoboxGenerator

From Corsair Cove Official Wiki
Revision as of 18:48, 2 August 2026 by Dorce (talk | contribs)

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

local p = {}

-- Load your data table once
local rData = mw.loadData("Module:ResourceData")
local bData = mw.loadData("Module:BuildingData")

function p.debugRecipe(frame)
    local recipe = rData["Stone Blocks"].production.recipe1
    mw.logObject(recipe, "recipe")
    return recipeToText(recipe)
end

local function recipeToText(recipe)
    if not recipe or next(recipe) == nil then
        return "''None''"
    end

    local parts = {}

    local i = 1
    while recipe["ingredient"..i] do
		local ingredient = recipe["ingredient"..i]
		local icon = resourceData[ingredient] and resourceData[ingredient].icon_file or ""
		
		table.insert(parts,
		    string.format(
		        "%d [[File:%s|16px]] [[%s]]",
		        recipe["ingredient"..i.."_qty"],
		        icon,
		        ingredient
		    )
		)
        i = i + 1
    end

    if #parts == 0 then
        return "''None''"
    end

    return table.concat(parts, ", ")
end

local function productionListToText(production)
    local parts = {}

    for i = 1, 3 do
    	local building_name = production["source" .. i]
    	if building_name == "" then
    		return table.concat(parts, "\n")
    	end
    	
    	local building = bData[building_name]
    	local prod_output = production["quantity" .. i]
    	local recipe = production["recipe" .. i]
    	local building_link = "* [[File:" .. building.building_icon_filename .. "|16px]] [[" .. building_name .. "]]"
    	local base_production = "* Base Production: " .. prod_output
    	local max_production = "* Max Production: " .. prod_output
    	local recipe_text = "Requires: " .. recipeToText(recipe)
        table.insert(parts,
        	building_link .. "\n" .. base_production .. "\n" .. max_production .. "\n* " .. recipe_text .. "\n"
        )
    end

    return table.concat(parts, "\n")
end

function p.makeInfobox(frame)
    local name = frame.args.name or mw.title.getCurrentTitle().text

    -- Find the ruilding in the data table
    local r = rData[name]

    if not r then
        return "No data found for '" .. tostring(name) .. "'"
    end

    -- Pass the data to your existing template
    return frame:expandTemplate{
        title = "Resource infobox",
        args = {
            title = r.name,
            ricon_filename = r.icon_file,
            category = r.category,
            description = r.description,
            stats_list = "* [[Warehouse]] Stack Size: " .. r.stack_size .. "\n* [[Trade Post]] Stack Value: " .. r.stack_value,
            produced_by_list = productionListToText(r.production)
        }
    }
end

return p