Module:Recipes: Difference between revisions

From Guinea Isles Wiki
No edit summary
No edit summary
Line 6: Line 6:
local p = {}
local p = {}


-- Returns the entry in data.json with the corresponding id as a key.
-- Returns the entry in data.json with the corresponding id as a key. Args:
-- id is the name of a Page. If no id is specified, uses current page's name as id.  
-- id is they key in data.json to lookup. This is generally an item name as a page title
function p._get_item_recipes(args)
-- eg. "Plainwood Log" will return all recipes involving a plainwood log
local output_id = nil
function p.get_entry(id)
if args["id"] ~= nil then
if data[id] ~= nil then
output_id = args["id"]
return data[id]
else
output_id = mw.title.getCurrentTitle().text
end
end
if data[output_id] ~= nil then
return nil
return tostring(data[output_id])
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
end
return "No recipe found with id " .. tostring(output_id) .. "!"
return {}
end
end


function p.get_item_recipes(frame)
-- Similar to get_entry, but only returns a list of recipes where id is
local args = frame:getParent().args
-- in the output, not the input/mateirals. i.e. returns a table of every
return p._get_item_recipes(args)
-- 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
end


return p
return p

Revision as of 16:34, 20 September 2025

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