Module:Recipe: Difference between revisions
SlyAceZeta (talk | contribs) m (slightly customizing the output) |
SlyAceZeta (talk | contribs) (thanks to BryghtShadow, adding search function for finding by name; adding some more documentation; removing disambiguations from pagenames) |
||
Line 1: | Line 1: | ||
--[[ |
|||
This module standardizes the display of crafting recipes |
|||
and makes it easy to display a specified item's recipe |
|||
or all recipes that use a specified ingredient. |
|||
Thanks to User:BryghtShadow for helping out with the loops! |
|||
--]] |
|||
-- <nowiki> |
|||
local p = {} |
local p = {} |
||
local data = mw.loadData('Module:Recipe/data') |
local data = mw.loadData('Module:Recipe/data') |
||
local getArgs = require('Dev:Arguments').getArgs |
local getArgs = require('Dev:Arguments').getArgs |
||
function p.getItemID(id) |
|||
-- removing disambiguations from pagenames |
|||
id = string.gsub(id, " %(Subnautica%)", "") |
|||
id = string.gsub(id, " %(Below Zero%)", "") |
|||
-- if item ID is provided directly... |
|||
⚫ | |||
⚫ | |||
⚫ | |||
-- ...otherwise try to find by item name |
|||
for k, v in pairs(data) do |
|||
if v.name == id then -- item found |
|||
return k |
|||
⚫ | |||
end |
|||
return nil -- Could not find item |
|||
end |
|||
function p.main(frame) |
function p.main(frame) |
||
local args = getArgs(frame) |
local args = getArgs(frame) |
||
local prod |
|||
local output = "" |
local output = "" |
||
-- First, get the ID of the product item provided by template; pagename used by default |
|||
if args[1] then |
|||
⚫ | |||
prod = args[1] |
|||
local itemID = getItemID(itemID) |
|||
⚫ | |||
if not itemID then error("Could not find recipe for item: " .. id) end |
|||
⚫ | |||
⚫ | |||
-- Item is found, start processing it |
|||
⚫ | |||
if data[itemID].original then |
|||
-- If the item is made as part of a different item's recipe |
|||
error ("No standalone recipe for item: " .. itemID .. " (use " .. data[itemID].original .. "' instead)") |
|||
⚫ | |||
⚫ | |||
if data[prod].recipe then |
|||
if data[itemID].recipe then |
|||
-- Recipe is found, start creating the output |
|||
⚫ | |||
for i,v in ipairs(data[itemID].recipe) do |
|||
⚫ | |||
⚫ | |||
⚫ | |||
else |
|||
⚫ | |||
end |
end |
||
return output |
|||
⚫ | |||
-- If there is no recipe for the item |
|||
⚫ | |||
end |
end |
||
else |
|||
error ("'" .. prod .. "' recipe does not exist") |
|||
end |
end |
||
end |
end |
||
return p |
return p |
||
-- </nowiki> |
Revision as of 01:16, 9 June 2022
This module standardizes the display of crafting recipes and makes it easy to display a specified item's recipe or all recipes that use a specified ingredient. See Template:RecipeNew for more information. Thanks to BryghtShadow for helping out with the loops!
--[[
This module standardizes the display of crafting recipes
and makes it easy to display a specified item's recipe
or all recipes that use a specified ingredient.
Thanks to User:BryghtShadow for helping out with the loops!
--]]
-- <nowiki>
local p = {}
local data = mw.loadData('Module:Recipe/data')
local getArgs = require('Dev:Arguments').getArgs
function p.getItemID(id)
-- removing disambiguations from pagenames
id = string.gsub(id, " %(Subnautica%)", "")
id = string.gsub(id, " %(Below Zero%)", "")
-- if item ID is provided directly...
if data[id] then
return id
end
-- ...otherwise try to find by item name
for k, v in pairs(data) do
if v.name == id then -- item found
return k
end
end
return nil -- Could not find item
end
function p.main(frame)
local args = getArgs(frame)
local output = ""
-- First, get the ID of the product item provided by template; pagename used by default
local id = args[1] or mw.title.getCurrentTitle().text
local itemID = getItemID(itemID)
if not itemID then error("Could not find recipe for item: " .. id) end
-- Item is found, start processing it
if data[itemID].original then
-- If the item is made as part of a different item's recipe
error ("No standalone recipe for item: " .. itemID .. " (use " .. data[itemID].original .. "' instead)")
else
if data[itemID].recipe then
-- Recipe is found, start creating the output
for i,v in ipairs(data[itemID].recipe) do
output = output .. "[[File:" .. data[v[1]].image .. "|50px]] " .. data[v[1]].name .. " -- x" .. v[2] .. "<br>"
end
return output
else
-- If there is no recipe for the item
error ("Cannot craft item: '" .. itemID .. "' (use Template:Uses instead)")
end
end
end
return p
-- </nowiki>