Module:Item recipe: Difference between revisions

From Space Station 14 Wiki
(wip)
(wip)
Line 15: Line 15:
{ method = "protolathe", recipes = mw.loadJsonData("Module:Item recipe/recipes by lathe/protolathe.json") },
{ method = "protolathe", recipes = mw.loadJsonData("Module:Item recipe/recipes by lathe/protolathe.json") },
}
}
-- A table mapping overriding some recipes' products.
-- Used for recipes which use separate items for produced items.
-- This lets the system know proper items to display.
local recipes_products_overrides = mw.loadJsonData("Module:Item recipe/product overrides.json")


-- A table containing item recipes, identified by recipe IDs.
-- A table containing item recipes, identified by recipe IDs.
Line 68: Line 73:
end
end


local function first_item_matching_condition_numeric(table, condition)
local function find_first_numeric_table_item_matching_condition(table, condition)
for _, item in ipairs(table) do
for i, item in ipairs(table) do
if condition(item) then
if condition(item, i, table) then
return item
end
end
end
 
local function find_first_table_item_matching_condition(table, condition)
for key, item in pairs(table) do
if condition(item, key, table) then
return item
return item
end
end
Line 79: Line 92:


local function get_recipes_by_method(method)
local function get_recipes_by_method(method)
local recipe_group = first_item_matching_condition_numeric(
local recipe_group = find_first_numeric_table_item_matching_condition(
recipe_groups,  
recipe_groups,  
function (item)  
function (item)  
Line 91: Line 104:


return recipe_group.recipes
return recipe_group.recipes
end
-- Given an item ID as a some recipe product, lookups it in the overrides.
-- On match, returns a new item ID to use instead of given one (the "override" part).
local function resolve_recipe_product_override_if_needed(product_item_id)
local match = find_first_table_item_matching_condition(
recipes_products_overrides,
function (value, key) return key == product_item_id end
)
if match == nil then
return product_item_id
else
return match
end
end
end


Line 110: Line 138:
end
end


-- check for product overrides - replace the item ID if needed
item_id = resolve_recipe_product_override_if_needed(item_id)
-- do the search
for _, method in ipairs(methods_to_lookup) do
for _, method in ipairs(methods_to_lookup) do
local recipes_by_method = get_recipes_by_method(method)
for _, recipe in ipairs(get_recipes_by_method(method)) do
 
for _, recipe in ipairs(recipes_by_method) do
if recipe.result == item_id then
if recipe.result == item_id then
return {
return {
Line 125: Line 155:


-- ====================
-- ====================
-- Generates an HTML element, containing recipe components used to produce given item with a given method.
-- Returns a DIV containg {{item}}s.
function generate_recipe_materials(frame)
local args = getArgs(frame)
local method = args[1]
assert_value_not_nil(method, "failed to generate a recipe skeleton: method was not provided")
local product = args[2]
assert_value_not_nil(product, "failed to generate a recipe skeleton: product was not provided")
local matching_recipes = lookup_recipes_by_method_and_item_id(method, product)
local matching_recipes_len = numeric_table_length(matching_recipes)
end


function p.generate_item_recipe(frame)
function p.generate_item_recipe(frame)
Line 224: Line 238:


end
end
-- -- Generates a list of items needed for a recipe, along with exact amounts.
-- -- Needs a recipe ID passed as a single frame argument.
-- -- Uses {{Item}} to produce the items. Returns a div containing them.
-- function p.generate_recipe_items(frame)
-- local recipe_id = args[1]
-- assert_value_not_nil(recipe_id, "recipe ID was not provided")
-- end


return p
return p

Revision as of 09:41, 1 September 2024

Module documentation
View or edit this documentation (about module documentation)

Implements {{item recipe}}.

JSON files

JSON files that are updated automatically, syncing with the upstream:

Warning
Do not make changes to the above JSON files - any changes made will be erased on next update.

JSON files that are filled manually:

  • Module:Item recipe/order of materials.json - a 1 to 1 mapping of recipe materials to order at which they appear in recipes. Less number = higher order. Materials that do not have an order defined here, will appear after those that do.
  • Module:Item recipe/product overrides.json - a 1 to 1 mapping of recipe products to item IDs. Not all products are the same as item IDs they "represent", so sometimes a connection needs to be established explicitly.

-- Contains utilities for working with in-game item recipes.

-- todo: material sorting. based on alphabetical sorting? maybe at .json generation step, convert materials to an array?

local p = {} --p stands for package
local getArgs = require('Module:Arguments').getArgs
local itemModule = require('Module:Item')
local yesNo = require('Module:Yesno')

-- An array of recipe groups.
-- Recipes are grouped by `method`.
-- Note that the order matters for perfomance - the recipe lookup happens from top to bottom.
local recipe_groups = {
	{ method = "autolathe", 	recipes = mw.loadJsonData("Module:Item recipe/recipes by lathe/autolathe.json") },
	{ method = "protolathe", 	recipes = mw.loadJsonData("Module:Item recipe/recipes by lathe/protolathe.json") },
}

-- A table mapping overriding some recipes' products.
-- Used for recipes which use separate items for produced items.
-- This lets the system know proper items to display. 
local recipes_products_overrides = mw.loadJsonData("Module:Item recipe/product overrides.json")

-- A table containing item recipes, identified by recipe IDs.
-- local recipes_by_recipe_id =

-- A table containing item recipe categories, identified by recipe category IDs.
-- local recipy_categories_by_recipe_category_id = mw.loadJsonData("Module:Item recipe/recipy categories by recipe category id.json")

-- ====================


local function numeric_table_length(t)
	local count = 0
	for _ in ipairs(t) do count = count + 1 end
	return count
end

local function table_length(t)
	local count = 0
	for _ in pairs(t) do count = count + 1 end
	return count
end

local function table_has_value(tab, val)
	for _, value in ipairs(tab) do
		if value == val then
			return true
		end
	end

	return false
end

local function assert_value_not_nil(value, error_message)
	if value == nil then
		if error_message == nil then
			error("value is nil")
		else
			error(error_message)
		end
	end
end

-- Given a value, checks if it's "nil". 
-- * If it's not - returns the `value`.
-- * IF it is - returns the `value_if_nil`.
local function nil_or(value, value_if_nil)
	if value == nil then
		return value_if_nil
	else
		return value
	end
end

local function find_first_numeric_table_item_matching_condition(table, condition)
	for i, item in ipairs(table) do
		if condition(item, i, table) then
			return item
		end
	end
end

local function find_first_table_item_matching_condition(table, condition)
	for key, item in pairs(table) do
		if condition(item, key, table) then
			return item
		end
	end
end

-- ====================

local function get_recipes_by_method(method)
	local recipe_group = find_first_numeric_table_item_matching_condition(
		recipe_groups, 
		function (item) 
			return item.method == method 	
		end
	)

	if recipe_group == nil then
		error("failed to get recipes by method: no such recipe group with method " .. method)
	end

	return recipe_group.recipes
end

-- Given an item ID as a some recipe product, lookups it in the overrides.
-- On match, returns a new item ID to use instead of given one (the "override" part).
local function resolve_recipe_product_override_if_needed(product_item_id)
	local match = find_first_table_item_matching_condition(
		recipes_products_overrides, 
		function (value, key) return key == product_item_id end
	)

	if match == nil then
		return product_item_id
	else
		return match
	end
end

-- Searches a recipe for a given item, optionally with a specific production method.
-- @returns A table with `method` and `recipe`, or `nil` if no recipe was found.
local function lookup_recipe_by_item_id_and_method(item_id, method)
	assert_value_not_nil(item_id, "failed to lookup recipes by method and item ID: item ID was not provided")

	-- production methods to lookup through
	local methods_to_lookup = {}
	if method == nil then
		-- no method specified = look through all methods until the recipe is found
		for _, recipe_group in ipairs(recipe_groups) do
			table.insert(methods_to_lookup, recipe_group.method)
		end
	else
		-- method specified = only look through recipes with that production method
		table.insert(methods_to_lookup, method)
	end

	-- check for product overrides - replace the item ID if needed
	item_id = resolve_recipe_product_override_if_needed(item_id)

	-- do the search
	for _, method in ipairs(methods_to_lookup) do
		for _, recipe in ipairs(get_recipes_by_method(method)) do
			if recipe.result == item_id then
				return {
					method = method,
					recipe = recipe
				}
			end
		end
	end
end

-- ====================

function p.generate_item_recipe(frame)
	local args = getArgs(frame)

	-- [REQUIRED]
	
	-- Item name, alias or ID. Required.
	local item = args[1]
	assert_value_not_nil(item, "failed to generate a recipe for item: item was not provided")

	-- [OPTIONAL]

	-- Amount of item. Default is 1.
	-- Must be a string since Module:Item uses string amount. 
	-- All values from templates come as strings.
	local amount = nil_or(args[2], "1")
	
	-- Item production method. Can be "nil", in which case it's looked up.
	local method = nil

	-- Recipe layout.
	local layout = args["layout"] or args["lay"] or "horizontal"
	
	-- Whether to only generate a materials block.
	local materials_only = yesNo(args["materials only"] or args["mat only"] or false)

	-- ============

	local current_frame = mw:getCurrentFrame()

	local item_id = itemModule.lookup_item_id_by_name_and_amount{ [1] = item, [2] = amount }
	
	local recipe_lookup_result = lookup_recipe_by_item_id_and_method(item_id, method)
	assert_value_not_nil(recipe_lookup_result, "failed to generate a recipe for item: no recipe found for item " .. item_id .. " (method: " .. (method or "nil") ..")")

	local recipe = recipe_lookup_result.recipe
	local method = recipe_lookup_result.method

	
	local recipe_el = mw.html.create("div")
		:addClass("item-recipe")
	
	if layout == "vertical" or layout == "ver" then
		recipe_el:addClass("item-recipe-vertical")
	else
		recipe_el:addClass("item-recipe-horizontal")
	end
		
	if not materials_only then
		local product_el = mw.html.create("div")
			:addClass("item-recipe-product")
	
		recipe_el:node(current_frame:preprocess("{{item|" .. item_id .. "|" .. amount .. "}}"))
	
		recipe_el:node(product_el)
	
		local method_el = mw.html.create("div")
			:addClass("item-recipe-method")

		-- TODO: not all methods will be items, so this will eventually break.
		recipe_el:node(current_frame:preprocess("<span class='recipe-supplementary-text'>is made on</span> {{item|" .. method .. "}} <span class='recipe-supplementary-text'>with</span>"))

		recipe_el:node(method_el)

	end


	local materials_el = mw.html.create("div")
		:addClass("item-recipe-materials")

	assert_value_not_nil(recipe.materials, "failed to generate a recipe for item: no 'materials' are specified for item " .. item_id .. " recipe (method: " .. method ..")")

	for material, cost in pairs(recipe.materials) do
		materials_el:node(current_frame:preprocess("{{item|" .. material .. "|" .. (cost * amount) .. "}}"))
	end

	recipe_el:node(materials_el)


	return recipe_el
		:allDone()

end

return p