Module:Recipes

From Guinea Isles Wiki
Revision as of 16:34, 20 September 2025 by Squeaky (talk | contribs)

Module:Recipes is a module designed around managing the data of in-game recipes of all kinds. It allows users to query Module:Recipes/data.json, which contains this information in JSON format.

Functions


--------------------------
-- Module for crafting recipe data
------------------------
local data = mw.loadJsonData('Module:Recipes/data.json')

local p = {}

-- Returns the entry in data.json with the corresponding id as a key. Args:
-- id is they key in data.json to lookup. This is generally an item name as a page title
-- eg. "Plainwood Log" will return all recipes involving a plainwood log
function p.get_entry(id)
	if data[id] ~= nil then
		return data[id]
	end
	
	return nil
end

-- Similar to get_entry, but only returns a list of recipes where id is 
-- in the input/materials, not the output. i.e. returns a table of every
-- "product" this item makes.
function p.get_item_products(id)
	local entry_data = p.get_entry(id)
	if entry_data ~= nil then
		local ret = {}
		
		-- Iterate over each recipe and then each output
		for recipe_id, recipe in pairs(entry_data) do
			if recipe["mats"] ~= nil then
				for _, mat in pairs(recipe["mats"]) do
					mw.log("Testing " .. mat["name"])
					if mat["name"] == id then
						mw.log("Is in mats!")
						mw.log("id " .. recipe_id)
						ret[recipe_id] = entry_data[recipe_id]
						break
					end
				end
			end
		end
		
		mw.log(ret)
		for key,val in pairs(ret) do
			mw.log(key)
			mw.log(val)
		end
		
		return ret
	end
	
	return {}
end

-- Similar to get_entry, but only returns a list of recipes where id is 
-- in the output, not the input/mateirals. i.e. returns a table of every
-- way to make this item.
function p.get_item_creation(id)
	local entry_data = p.get_entry(id)
	if entry_data ~= nil then
		local ret = {}
		
		-- Iterate over each recipe and then each output
		for recipe_id, recipe in pairs(entry_data) do
			if recipe["output"] ~= nil then
				for _, output in pairs(recipe["output"]) do
					if output["name"] == id then
						ret[recipe_id] = entry_data[recipe_id]
						break
					end
				end
			end
		end
		
		for key, _ in pairs(ret) do
			mw.log(key)
		end
		
		return ret
	end
	
	return {}
end

return p