Module:Recipe

From Idlescape Wiki
Revision as of 16:39, 23 May 2024 by HASH (talk | contribs)
Jump to navigation Jump to search

local p = {}

local data_module_names = {
	item = 'Module:Items/data',
	recipe = 'Module:CraftingAugmenting/data'
}

local loaded_data_modules = {}

function p.loadData(data_type)
	local module_name = data_module_names[data_type]
	if loaded_data_modules[module_name] == nil then
		loaded_data_modules[module_name] = mw.loadData(module_name)
	end

	return loaded_data_modules[module_name]
end

local function findItem(name)
	local lname = name:lower()

	 --Remove leading and trailing spaces.
	lname = lname:gsub('^%s*(.-)%s*$', '%1')
	for key, item in pairs(p.loadData("item")) do
		if lname == item['name']:lower() then
			return item
		end
	end
	return 0
end

local function getItem(id)
	return p.loadData("item")[tostring(id)]
end

local function getItemName(id)
	return p.loadData("item")[tostring(id)]['name']
end

local function fullUrl(url)
	local newUrl = url
	if url:sub(1,5) == "https" then
		return newUrl
	end

	if url:sub(1,1) ~= "/" then
		newUrl = "/" .. newUrl
	end

	newUrl = "https://www.play.idlescape.com" .. newUrl
	return newUrl
end

local function icon(name, url, word)
	local s = fullUrl(url)
	s = "[[" .. name .. "|<img src=\"" .. s
	s = s .. "\" alt=\""  .. name .. "\" width=\"20\">"
	if word then
		s = s .. name
	end
	s = s .. "]]"
	return s
end

local function itemImage(id, word)
	local item = p.loadData("item")[tostring(id)]
	local url = ""
	if item['itemIcon'] then
		url = item['itemIcon']
	else
		url = item['itemImage']
	end
	return icon(item['name'], url, word)
end

local function img(id)
	local url = ""
	if item['itemIcon'] then
		url = item['itemIcon']
	else
		url = item['itemImage']
	end
	return fullUrl(url)
end

local function addSeparator(num)
	return tostring(tonumber(num)):reverse():gsub("(%d%d%d)","%1,"):gsub(",(%-?)$","%1"):reverse()
end

local function createTable(item, item_recipe)
	local s = ""
	local s3 = ""
	local material = ""
	local num = 0
	s = s .. "{| class=\"wikitable\"\n"
	s = s .. "|+Recipe\n"
	s = s .. "|-\n"
	s = s .. "!Material\n"
	s = s .. "!Quantity\n"
	s = s .. "!Multiplier\n"
	s = s .. "|-\n"

		for recipe = 1, #item_recipe do
			num = num + 1
--            for materialId, quantity in pairs(recipe["recipe"]) do
--                material = getItem(materialId)
--                s3 = s3 .. "|" .. icon(material['name'], material['itemImage'], 1) .. "\n"
--                s3 = s3 .. "|" .. addSeparator(quantity) .. "\n"
--                if recipe["multiplier"] then
--                    s3 = s3 .. "|" .. addSeparator(recipe["multipier"]) .. "\n"
--                else
--                    s3 = s3 .. "|" .. addSeparator(1) .. "\n"
--                end
--            end
        end
        s = s .. s3
	s = s .. "|}"
	return num
end

function p.item(frame)
	local args = frame:getParent().args
	return p._item(args)
end

function p._item(args)
	local name = ""
	local item = 0
	local wikitable = ""
	local item_recipe = 0
	name = args[1]
	item = findItem(name)
	if item == 0 then
		return "item not found"
	end
	item_recipe = p.loadData("recipe")["150"]['crafting']
	if item_recipe == 0 then
		return "item uncraftable"
	end
	wikitable = createTable(item, item_recipe)
	return wikitable
end

return p