Module:CreatedUsing

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

local p = {}

-- round to 1 decimal for display (string) -- just straight up copied this from AlternateRecipesTable function round(n) local tentimes = math.floor(n*10+0.5) local suffix = '' if tentimes % 10 == 0 then suffix = '.0' end return (tentimes/10)..suffix end

-- query is a function that gets all recipes for the building function getRecipes(building) local cargoTable = 'crafting_recipes' local fields = { 'recipeName', 'alternateRecipe', 'product', 'craftingTime', 'productCount', 'productsPerMinute', 'product2', 'productCount2', 'productsPerMinute2', 'product3', 'productCount3', 'productsPerMinute3', 'product4', 'productCount4', 'productsPerMinute4', -- 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', }

local queryArgs = { where = 'craftedIn= "' .. building .. '"' }   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)

local craftingTime = tonumber(recipe['craftingTime']) local ingredientsPerMinute = (60 / craftingTime) * tonumber(recipe['quantity'..index]) parentHtml:tag('span') :css('float', 'right') :wikitext(round(ingredientsPerMinute)..'/min') 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]}}

parentHtml:wikitext(""..recipe['productCount'..index].."x "..productItem)

parentHtml:tag('span') :css('float', 'right') :wikitext(recipe['productsPerMinute'..index]..'/min') parentHtml:tag('br'):css('clear', 'both') end

function p.makeTable(frame) local recipes = getRecipes(frame.args['building']) local recipeTable = mw.html.create('table') recipeTable:addClass('wikitable')

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

for i, recipe in ipairs(recipes) do       local recipeRow = recipeTable:tag('tr')

-- if the recipe is an alternate, we should add the alternate icon. if recipe.alternateRecipe == '1' then local t = frame:expandTemplate{title = 'AlternateIcon'} recipeRow:tag('td'):wikitext(t..' '..recipe['recipeName']) else recipeRow:tag('td'):wikitext(recipe['recipeName']) end recipeRow:tag('td'):wikitext(recipe['craftingTime'])

local allIngredients = recipeRow:tag('td')

local ingredientIndex = 1 while recipe['ingredient'..ingredientIndex] and recipe['ingredient'..ingredientIndex] ~= '' 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] ~=  then makeProduct(frame, recipe, index, allProducts) end end end return tostring(recipeTable) end

return p