Module:ResourceInfoboxGenerator: Difference between revisions

From Corsair Cove Official Wiki
Dorce (talk | contribs)
Created page with "local p = {} -- Load your data table once local rData = mw.loadData("Module:ResourceData") local bData = mw.loadData("Module:BuildingData") 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:expandTempl..."
 
Dorce (talk | contribs)
No edit summary
Line 4: Line 4:
local rData = mw.loadData("Module:ResourceData")
local rData = mw.loadData("Module:ResourceData")
local bData = mw.loadData("Module:BuildingData")
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
local function productionListToText(production)
    local parts = {}
    for i = 1, 3 do
    local building = production["source" .. i]
    local prod_output = production["quantity" .. i]
    local recipe = production["recipe" .. i]
    local building_link = "* [[File:" .. building.building_icon_filename .. "|16px|link=" .. building .. "]] [[" .. building .. "]]"
    local base_production = "* Base Production per minute" .. prod_output
    local max_production = "* Max Production per minute" .. prod_output
    local recipe = "Requires" .. recipeToText(recipe)
        table.insert(parts,
        building_link .. "\n" .. base_production .. "\n" .. max_production .. "\n* " .. recipe .. "\n"
        )
    end
    return table.concat(parts, "\n")
end


function p.makeInfobox(frame)
function p.makeInfobox(frame)
Line 20: Line 69:
         args = {
         args = {
             title = r.name,
             title = r.name,
             ricon_filename = r.iconFile,
             ricon_filename = r.icon_file,
             category = r.category,
             category = r.category,
             description = r.description,
             description = r.description,
             stats_list = "* [[Warehouse]] Stack Size: " .. r.stack_size .. "\n* [[Trade Post]] Stack Value: " .. r.stack_value
             stats_list = "* [[Warehouse]] Stack Size: " .. r.stack_size .. "\n* [[Trade Post]] Stack Value: " .. r.stack_value,
            produced_by_list = productionListToText(r.production)
         }
         }
     }
     }

Revision as of 18:28, 2 August 2026

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

local function productionListToText(production)
    local parts = {}

    for i = 1, 3 do
    	local building = production["source" .. i]
    	local prod_output = production["quantity" .. i]
    	local recipe = production["recipe" .. i]
    	local building_link = "* [[File:" .. building.building_icon_filename .. "|16px|link=" .. building .. "]] [[" .. building .. "]]"
    	local base_production = "* Base Production per minute" .. prod_output
    	local max_production = "* Max Production per minute" .. prod_output
    	local recipe = "Requires" .. recipeToText(recipe)
        table.insert(parts,
        	building_link .. "\n" .. base_production .. "\n" .. max_production .. "\n* " .. recipe .. "\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