Module:ResourceInfoboxGenerator: Difference between revisions

From Corsair Cove Official Wiki
Dorce (talk | contribs)
No edit summary
Dorce (talk | contribs)
No edit summary
 
(19 intermediate revisions by the same user not shown)
Line 6: Line 6:


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


     local i = 1
     local i = 1
     while recipe["ingredient"..i] do
     while recipe["ingredient"..i] do
local ingredient = recipe["ingredient"..i]
local ingredient_name = recipe["ingredient"..i]
local icon = resourceData[ingredient] and resourceData[ingredient].icon_file or ""
local ingredient = rData[ingredient_name]
local icon = ingredient and ingredient.icon_file or ""
table.insert(parts,
table.insert(parts,
Line 22: Line 19:
        recipe["ingredient"..i.."_qty"],
        recipe["ingredient"..i.."_qty"],
        icon,
        icon,
        ingredient
        ingredient_name
    )
    )
)
)
Line 47: Line 44:
     local prod_output = production["quantity" .. i]
     local prod_output = production["quantity" .. i]
     local recipe = production["recipe" .. i]
     local recipe = production["recipe" .. i]
     local building_link = "* [[File:" .. building.building_icon_filename .. "|16px|link=" .. building_name .. "]] [[" .. building_name .. "]]"
     local building_link = "* [[File:" .. building.building_icon_filename .. "|16px]] [[" .. building_name .. "]]"
     local base_production = "* Base Production per minute" .. prod_output
     local base_production = "* Base Production: '''" .. prod_output .. "'''"
     local max_production = "* Max Production per minute" .. prod_output
     local max_production = "* Max Production: '''" .. prod_output .. "'''"
     local recipe = "Requires" .. recipeToText(recipe)
     local recipe_text = "Requires: " .. recipeToText(recipe)
         table.insert(parts,
         table.insert(parts,
         building_link .. "\n" .. base_production .. "\n" .. max_production .. "\n* " .. recipe .. "\n"
         building_link .. "\n" .. base_production .. "\n" .. max_production .. "\n* " .. recipe_text .. "\n"
         )
         )
     end
     end
Line 70: Line 67:


     -- Pass the data to your existing template
     -- Pass the data to your existing template
    local stack_size_string = "* [[Warehouse]] Stack Size: " .. r.stack_size
    local sell_value_string = "\n* [[Trade Post]] Stack Value: " .. r.stack_value .. " [[File:T ICO resource coin.png|16px]]"
   
     return frame:expandTemplate{
     return frame:expandTemplate{
         title = "Resource infobox",
         title = "Resource infobox",
         args = {
         args = {
             title = r.name,
             title = r.name,
             ricon_filename = r.icon_file,
             icon_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 = tostring(stack_size_string) .. tostring(sell_value_string),
             produced_by_list = productionListToText(r.production)
             produced_by_list = productionListToText(r.production)
         }
         }

Latest revision as of 19:22, 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)
    local parts = {}

    local i = 1
    while recipe["ingredient"..i] do
		local ingredient_name = recipe["ingredient"..i]
		local ingredient = rData[ingredient_name]
		local icon = ingredient and ingredient.icon_file or ""
		
		table.insert(parts,
		    string.format(
		        "%d [[File:%s|16px]] [[%s]]",
		        recipe["ingredient"..i.."_qty"],
		        icon,
		        ingredient_name
		    )
		)
        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
    local stack_size_string = "* [[Warehouse]] Stack Size: " .. r.stack_size
    local sell_value_string = "\n* [[Trade Post]] Stack Value: " .. r.stack_value .. " [[File:T ICO resource coin.png|16px]]"
    
    return frame:expandTemplate{
        title = "Resource infobox",
        args = {
            title = r.name,
            icon_filename = r.icon_file,
            category = r.category,
            description = r.description,
            stats_list = tostring(stack_size_string) .. tostring(sell_value_string),
            produced_by_list = productionListToText(r.production)
        }
    }
end

return p