Difference between revisions of "Module:Recipe"

From Idlescape Wiki
Jump to navigation Jump to search
m
Line 8: Line 8:
 
local loaded_data_modules = {}
 
local loaded_data_modules = {}
  
function p.loadData(data_type)  
+
function p.loadData(data_type)
 
local module_name = data_module_names[data_type]
 
local module_name = data_module_names[data_type]
 
if loaded_data_modules[module_name] == nil then
 
if loaded_data_modules[module_name] == nil then
 
loaded_data_modules[module_name] = mw.loadData(module_name)
 
loaded_data_modules[module_name] = mw.loadData(module_name)
 
end
 
end
+
 
 
return loaded_data_modules[module_name]
 
return loaded_data_modules[module_name]
 
end
 
end
Line 19: Line 19:
 
local function findItem(name)
 
local function findItem(name)
 
local lname = name:lower()
 
local lname = name:lower()
+
 
 
--Remove leading and trailing spaces.
 
--Remove leading and trailing spaces.
 
lname = lname:gsub('^%s*(.-)%s*$', '%1')
 
lname = lname:gsub('^%s*(.-)%s*$', '%1')
Line 43: Line 43:
 
return newUrl
 
return newUrl
 
end
 
end
+
 
 
if url:sub(1,1) ~= "/" then
 
if url:sub(1,1) ~= "/" then
 
newUrl = "/" .. newUrl
 
newUrl = "/" .. newUrl
 
end
 
end
+
 
 
newUrl = "https://www.play.idlescape.com" .. newUrl
 
newUrl = "https://www.play.idlescape.com" .. newUrl
 
return newUrl
 
return newUrl
Line 90: Line 90:
 
local function createTable(item, item_recipe)
 
local function createTable(item, item_recipe)
 
local s = ""
 
local s = ""
 +
local s3 = ""
 +
local material = ""
 
s = s .. "{| class=\"wikitable\"\n"
 
s = s .. "{| class=\"wikitable\"\n"
 
s = s .. "|+Recipe\n"
 
s = s .. "|+Recipe\n"
Line 97: Line 99:
 
s = s .. "!Multiplier\n"
 
s = s .. "!Multiplier\n"
 
s = s .. "|-\n"
 
s = s .. "|-\n"
+
 
if item ~= 0 then
+
if item == 0 or item_recipe then
+
return s
 
else
 
else
        local s3 = ""
 
 
for recipe in item_recipe do
 
for recipe in item_recipe do
 
             for materialId, quantity in pairs(recipe["recipe"]) do
 
             for materialId, quantity in pairs(recipe["recipe"]) do
                 local material = getItem(materialId)
+
                 material = getItem(materialId)
 
                 s3 = s3 .. "|" .. icon(material['name'], material['itemImage'], 1) .. "\n"
 
                 s3 = s3 .. "|" .. icon(material['name'], material['itemImage'], 1) .. "\n"
 
                 s3 = s3 .. "|" .. addSeparator(quantity) .. "\n"
 
                 s3 = s3 .. "|" .. addSeparator(quantity) .. "\n"
Line 114: Line 115:
 
             end
 
             end
 
         end
 
         end
         s = s .. s3
+
         s = s .. s3
 
end
 
end
 
s = s .. "|}"
 
s = s .. "|}"
Line 129: Line 130:
 
local item = 0
 
local item = 0
 
local wikitable = ""
 
local wikitable = ""
+
local item_recipe = 0
if args[1] then
+
name = args[1]
name = args[1]
+
item = findItem(name)
item = findItem(name)
+
item_recipe = p.loadData("recipe")[tostring(item["id"])]['crafting']
local item_recipe = p.loadData("recipe")[tostring(item["id"])]['crafting']
 
if not item_recipe[1] then
 
return 'item isn\'t craftable'
 
        end
 
end
 
 
 
wikitable = createTable(item, item_recipe)
 
wikitable = createTable(item, item_recipe)
 
return wikitable
 
return wikitable

Revision as of 16:11, 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 = ""
	local s3 = ""
	local material = ""
	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 or item_recipe then
		return s
	else
		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 = ""
	local item_recipe = 0
	name = args[1]
	item = findItem(name)
	item_recipe = p.loadData("recipe")[tostring(item["id"])]['crafting']
	wikitable = createTable(item, item_recipe)
	return wikitable
end

return p