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 | -- id is they key in data.json to lookup. This is generally an item name as a page title | ||
function p. | -- eg. "Plainwood Log" will return all recipes involving a plainwood log | ||
function p.get_entry(id) | |||
if | if data[id] ~= nil then | ||
return data[id] | |||
end | end | ||
if | 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 | end | ||
return | return {} | ||
end | end | ||
function p. | -- Similar to get_entry, but only returns a list of recipes where id is | ||
local | -- 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 | 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