Module:CreatedUsing

From Satisfactory Wiki
Jump to navigation Jump to search
Template-info.svg Documentation

Displays a table with all recipes craftable using a particular building.

 {{#invoke: createdUsing | makeTable | building=Refinery}}

NOTE: You must use named args here. For some reason, positional/anonymous/numbered args are not working correctly and I'm too tired to keep debugging it. Lua error: Error: Table crafting_recipes not found..


-- this module is used to construct a table of all recipes available on a particular production building
local cargo = mw.ext.cargo
local p = {}

-- query is a function that gets all recipes for the building
function getRecipes(building, unreleased, limit)
    local cargoTable = 'crafting_recipes'
    local fields = {
        'recipeName', 'alternateRecipe',
        'experimental', 'unreleased', 'season',
        'product', 'craftingTime', 'productCount', 'productsPerMinute', 
        'product2', 'productCount2', 'productsPerMinute2',
        'product3', 'productCount3', 'productsPerMinute3',
        'product4', 'productCount4', 'productsPerMinute4',
        'craftedIn',
        -- skip the rest of the product fields for now. when this breaks later because there's some monstercrafter that makes 5 different outputs, then we can bother with it
        'ingredient1', 'quantity1', 'ingredient2', 'quantity2',
        'ingredient3', 'quantity3', 'ingredient4', 'quantity4',
        'ingredient5', 'quantity5', 'ingredient6', 'quantity6',
    }
	local queryArgs = {}
	if building == 'Craft Bench' then
	    queryArgs = {
	        where = 'inCraftBench = 1'
	    }
	elseif building == 'Equipment Workshop' then
	    queryArgs = {
	        where = 'inWorkshop = 1'
	    }
	else
		queryArgs = {
	        where = 'craftedIn= "' .. building .. '"'
	    }
    end
    
    if (not unreleased) then
    	queryArgs.where = queryArgs.where .. ' and (unreleased is null or unreleased=False)'
    end
    
    queryArgs.limit = limit or 200
    
    return cargo.query(cargoTable, table.concat(fields, ','), queryArgs)
end

function makeIngredient(frame, recipe, index, parentHtml)
    -- pulled a lot of this formatting from the AlternateRecipeTable module. I'm not 100% satisfied with the HTML that
    -- it outputs, but I can live with it.
    local ingredientItem = frame:expandTemplate{ title = 'ItemLink', args = {recipe['ingredient'..index]}}

    parentHtml:wikitext("'''"..recipe['quantity'..index].."x''' "..ingredientItem)
    
	if (recipe['craftingTime'] ~= nil and recipe['craftingTime'] ~= '') then
		local craftingTime = tonumber(recipe['craftingTime'])
	    local ingredientsPerMinute = (60 / craftingTime) * tonumber(recipe['quantity'..index])
	    parentHtml:tag('span')
	        :css('float', 'right')
	        :wikitext(ingredientsPerMinute..'/min')
    end
    parentHtml:tag('br'):css('clear', 'both')
end

-- makeProduct is just makeIngredient copied and with the table keys changed
function makeProduct(frame, recipe, index, parentHtml)
    -- pulled a lot of this formatting from the AlternateRecipeTable module. I'm not 100% satisfied with the HTML that
    -- it outputs, but I can live with it.
    local productItem = frame:expandTemplate{ title = 'ItemLink', args = {recipe['product'..index]}}
	
	if recipe.craftedIn == 'Build Gun' then
		parentHtml:wikitext(productItem)	
	else
    	parentHtml:wikitext("'''"..(recipe['productCount'..index] or '').."x''' "..(productItem or ''))
	end
	
	if recipe.craftedIn == 'Build Gun' then
	-- do nothing
	elseif tonumber(recipe['productsPerMinute'..index]) == 0 then     
		parentHtml:tag('span')
        	:css('float', 'right')
        	:wikitext('Must be crafted Manually')
    else
	    parentHtml:tag('span')
	        :css('float', 'right')
	        :wikitext(recipe['productsPerMinute'..index]..'/min')
    end
    parentHtml:tag('br'):css('clear', 'both')
end

function p.makeTable(frame)
	local limit = tonumber(frame.args.limit)
	if (limit ~= limit) then
		limit = nil
	end

    local unreleased = frame.args.unreleased and (#frame.args.unreleased > 0) or false
	local isBuildGun = frame.args.building == 'Build Gun'
    local recipes = getRecipes(frame.args['building'], unreleased, limit)
    local recipeTable = mw.html.create('table')
    recipeTable:addClass('wikitable')

    local headerRow = recipeTable:tag('tr')
    headerRow:tag('th'):wikitext("Recipe Name")
	if (not isBuildGun) then
		headerRow:tag('th'):wikitext("Crafting Time (sec)")
	end
    headerRow:tag('th'):wikitext("Ingredients")
    headerRow:tag('th'):wikitext("Products")

    for i, recipe in ipairs(recipes) do
        local recipeRow = recipeTable:tag('tr')
		local recipeName = recipeRow:tag("td")
			:cssText("position:relative;overflow:hidden;overflow:clip")

        -- if the recipe is an alternate, we should add the alternate icon.
		if recipe.alternateRecipe == '1' then
			recipeName:wikitext(frame:expandTemplate{title = 'AlternateIcon'}, ' ')
		end

		recipeName:wikitext(recipe['recipeName'])
		if (recipe.experimental == "1") then
			recipeName:wikitext(frame:expandTemplate{title = "CraftingTable/experimental"})
		elseif (recipe.unreleased == "1") then
			recipeName:wikitext(frame:expandTemplate{title = "CraftingTable/unreleased"})
		end

		local season = recipe.season
		if (season ~= nil and season ~= "") then
			recipeName:wikitext(frame:expandTemplate{
				title = "CraftingTable/season",
				args  = { season = season },
			})
		end

		if (not isBuildGun) then 	
        recipeRow:tag('td'):wikitext(recipe['craftingTime'])
		end

        local allIngredients = recipeRow:tag('td')

        local ingredientIndex = 1
        while recipe['ingredient'..ingredientIndex] and (recipe['ingredient'..ingredientIndex] or '') ~= '' do
            makeIngredient(frame, recipe, ingredientIndex, allIngredients)
            ingredientIndex = ingredientIndex + 1
        end

        -- we have to do the first product outside of the loop, because only subsequent products are numbered
        local allProducts = recipeRow:tag('td')
        -- since we're only going up to 4 products, we'll just hard-code the iterator here
        for i, index in ipairs{'', '2','3','4'} do
            if (recipe['product'..index] or '') ~= '' then
                makeProduct(frame, recipe, index, allProducts)
            end
        end
    end
    return tostring(recipeTable)
end

return p