Module:NecesseRecipes

From Necesse Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:NecesseRecipes/doc

local p = {}

local itemsData = require("Module:NecesseItems")
local recipesData = require("Module:NecesseCache").loadRecipes()

function getItemData(itemStringID, value, defaultValue)
	return itemsData.getItemData({
		['args'] = {
			['stringID'] = itemStringID,
			['value'] = value,
			['default'] = defaultValue	
		}
	});
end

function getItemStringID(itemNameOrStringID)
	return itemsData.getItemStringID({
		['args'] = {
			['name'] = itemNameOrStringID,
			['default'] = itemNameOrStringID	
		}
	});
end

function getItemDisplayName(itemStringID)
	if itemStringID == 'inventory' then return 'Inventory' end
	return getItemData(itemStringID, 'displayName', itemStringID)
end

function formatIngredientsOld(ingredients)
	local out = {}
	for i, ingredient in ipairs(ingredients) do
		table.insert(out, getItemDisplayName(ingredient.stringID) .. ',' .. ingredient.amount)
	end
	return table.concat(out, '/')
end

function formatRecipeOld(recipe, delimiter)
	return recipe.amount .. delimiter .. formatIngredientsOld(recipe.ingredients) .. delimiter .. getItemDisplayName(recipe.station)
end

function getRecipesTableOld(itemStringIDs)
	if type(itemStringIDs) ~= 'table' then
		itemStringIDs = { itemStringIDs }
	end
	local recipesArray = recipesData['recipes']
	local out = {}
	for i, recipe in ipairs(recipesArray) do
		for j, itemStringID in ipairs(itemStringIDs) do
			if recipe.stringID == getItemStringID(itemStringID) then
				table.insert(out, formatRecipeOld(recipe, '<RCRD>'))
				break
			end
		end
	end
	return out
end

function getRecipesUsagesTableOld(itemStringIDs)
	if type(itemStringIDs) ~= 'table' then
		itemStringIDs = { itemStringIDs }
	end
	local recipesArray = recipesData['recipes']
	local out = {}
	for i, recipe in ipairs(recipesArray) do
		for j, itemStringID in ipairs(itemStringIDs) do
			local found = false
			for k, ingredient in ipairs(recipe.ingredients) do
				if ingredient.stringID == getItemStringID(itemStringID) then
					table.insert(out, getItemDisplayName(recipe.stringID) .. ';' .. formatRecipeOld(recipe, ';'))
					found = true
					break
				end
			end
			if found then break end
		end
	end
	return out
end

function p.getRecipesOld(frame)
	local itemStringID = frame.args['stringID'] or frame.args[1]
	if itemStringID then
		return table.concat(getRecipesTableOld(itemStringID), '<MANY>')
	end
	return 'MISSING ITEM "stringID" PARAMETER'

end

function p.getRecipeUsagesOld(frame)
	local itemStringID = frame.args['stringID'] or frame.args[1]
	if itemStringID then
		return table.concat(getRecipesUsagesTableOld(itemStringID), '<MANY>')
	end
	return 'MISSING ITEM "stringID" PARAMETER'

end

return p