Difference between revisions of "Module:Recipe"

From Idlescape Wiki
Jump to navigation Jump to search
Line 3: Line 3:
 
local data_module_names = {
 
local data_module_names = {
 
item = 'Module:Items/data',
 
item = 'Module:Items/data',
recipe = 'module:craftingAugmenting/data'
+
recipe = 'module:CraftingAugmenting/data'
 
}
 
}
  

Revision as of 11:17, 23 May 2024


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 = ""
	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"
	
	if item ~= 0 then
	
	else
        local s3 = ""
		for recipe in item_recipe do
            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	
	end
	s = s .. "|}"
	return s
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 = ""
	
	if args[1] then
		name = args[1]
		item = findItem(name)
		item_recipe = p.loadData('recipe')[tonumber(item[id])]['crafting']
		if not item_recipe[1] then
			return 'item isn\'t craftable'
        end
	end
	
	wikitable = createTable(item, item_recipe)
	return wikitable
end

return p