Module:ResourceInfoboxGenerator

From Corsair Cove Official Wiki
Revision as of 18:49, 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")

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

function Export.debugRecipe(frame)
    local out = {}

    local resource = resourceData["Stone Blocks"]
    table.insert(out, "Resource exists: " .. tostring(resource ~= nil))

    if resource then
        table.insert(out, "<pre>" .. mw.dumpObject(resource.production) .. "</pre>")

        local recipe = resource.production.recipe1

        table.insert(out, "Recipe exists: " .. tostring(recipe ~= nil))
        table.insert(out, "next(recipe): " .. tostring(next(recipe)))
        table.insert(out, "<pre>" .. mw.dumpObject(recipe) .. "</pre>")

        table.insert(out, "ingredient1: " .. tostring(recipe.ingredient1))
        table.insert(out, "ingredient1_qty: " .. tostring(recipe.ingredient1_qty))

        if recipe.ingredient1 then
            table.insert(out, "resourceData lookup: " .. tostring(resourceData[recipe.ingredient1] ~= nil))

            if resourceData[recipe.ingredient1] then
                table.insert(out,
                    "<pre>" ..
                    mw.dumpObject(resourceData[recipe.ingredient1]) ..
                    "</pre>")
            end
        end

        table.insert(out, "recipeToText() returns:")
        table.insert(out, "<pre>" .. recipeToText(recipe) .. "</pre>")
    end

    return table.concat(out, "\n\n")
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