- 欢迎光临Satisfactory国区BWIKI。请登录您的B站账号,方可使用页面编辑与评论功能。

全站通知:

模块:CreatedUsing

来自幸福工厂WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

此模块的文档可以在模块:CreatedUsing/doc创建

-- 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',
        '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',
    }
	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
    
    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(round(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].."x''' "..productItem)
	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 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