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 some reason the above does not work for some, so we have to check index 1 and up until there are no more
	local index = 0
	while true do
		local ingredient = ingredients[index]
		if ingredient then
			table.insert(out, getItemDisplayName(ingredient.stringID) .. ',' .. ingredient.amount)
		elseif index > 1 then
			break
		end
		index = index + 1
	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 some reason the above does not work for some, so we have to check index 1 and up until there are no more
			local index = 0
			while true do
				local ingredient = recipe.ingredients[index]
				if ingredient then
					if ingredient.stringID == getItemStringID(itemStringID) then
						found = true
						break
					end
				elseif index > 1 then
					break
				end
				index = index + 1
			end
			if found then
				table.insert(out, getItemDisplayName(recipe.stringID) .. ';' .. formatRecipeOld(recipe, ';'))
				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