Difference between revisions of "Module:Recipe"

From Idlescape Wiki
Jump to navigation Jump to search
m
m
 
(15 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
local p = {}
 
local p = {}
 +
 +
local findId = require("Module:FindId")
  
 
local data_module_names = {
 
local data_module_names = {
 
item = 'Module:Items/data',
 
item = 'Module:Items/data',
recipe = 'Module:CraftingAugmenting/data'
+
crafting = 'Module:Crafting/data'
 
}
 
}
  
 
local loaded_data_modules = {}
 
local loaded_data_modules = {}
 +
 +
local function tablelength(T)
 +
local count = 0
 +
for _ in pairs(T) do count = count + 1 end
 +
return count
 +
end
 +
 +
local function pairsByKeys (t, f)
 +
local a = {}
 +
local orgi_key_type
 +
local orgi_key_numbered
 +
for n in pairs(t) do
 +
    if tonumber(n) == nil then
 +
        table.insert(a, n)
 +
        orgi_key_type = "word"
 +
    elseif type(n) == "number" then
 +
        table.insert(a, n)
 +
        orgi_key_type = "int"
 +
    elseif type(n) == "string" and type(tonumber(n) == "number") then
 +
            orgi_key_type = "number"
 +
            table.insert(a, tonumber(n))
 +
end
 +
end
 +
table.sort(a, f)
 +
local key
 +
local value
 +
local i = 0      -- iterator variable
 +
local iter = function ()  -- iterator function
 +
i = i + 1
 +
if a[i] == nil then return nil
 +
    elseif orgi_key_type == "word" or orgi_key_type == "int" then
 +
        key = a[i]
 +
        value = t[a[i]]
 +
    elseif orgi_key_type == "number" then
 +
        key = tostring(a[i])
 +
        value = t[tostring(a[i])]
 +
end
 +
    return key, value
 +
end
 +
return iter
 +
end
  
 
function p.loadData(data_type)
 
function p.loadData(data_type)
Line 15: Line 58:
  
 
return loaded_data_modules[module_name]
 
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
 
end
  
Line 34: Line 64:
 
end
 
end
  
local function getItemName(id)
+
local function getItemRecipes(id)
return p.loadData("item")[tostring(id)]['name']
+
local a = p.loadData("crafting")[tostring(id)]
 +
if a == nil then
 +
return 0
 +
else
 +
return a
 +
end
 
end
 
end
  
Line 52: Line 87:
 
end
 
end
  
local function icon(name, url, word)
+
local function itemImageLink(item)
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 = ""
 
local url = ""
 
if item['itemIcon'] then
 
if item['itemIcon'] then
Line 71: Line 94:
 
url = item['itemImage']
 
url = item['itemImage']
 
end
 
end
return icon(item['name'], url, word)
+
return fullUrl(url)
 
end
 
end
  
local function img(id)
+
local function itemImage(item, width, height, word)
local url = ""
+
local s = itemImageLink(item)
if item['itemIcon'] then
+
s = "[[" .. item['name'] .. "|<img src=\"" .. s
url = item['itemIcon']
+
s = s .. "\" alt=\""  .. item['name'] .. "\"style=\"width:"
 +
if width == nil then
 +
s = s .. "auto; height: "
 +
else
 +
s = s ..  tostring(width) .. "; height: "
 +
end
 +
if height == nil then
 +
s = s .. "auto;\">"
 
else
 
else
url = item['itemImage']
+
s = s .. height .. ";\">"
 +
end
 +
if word == 1 then
 +
s = s .. item['name']
 
end
 
end
return fullUrl(url)
+
s = s .. "]]"
 +
return s
 +
end
 +
 
 +
local function itemLink(name)
 +
local s= "[[" .. name .. "|" .. name .. "]]"
 +
return s
 
end
 
end
  
Line 88: Line 127:
 
end
 
end
  
local function createTable(item, item_recipe)
+
local function addSeparatorRound(num)
local s = ""
+
return string.format("%.2f",tostring(tonumber(num))):reverse():gsub("(%d%d%d)","%1,"):gsub(",(%-?)$","%1"):reverse()
local s3 = ""
+
end
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
+
local function getDefaultMultiplier(item)
num = num + 1
+
local defaultmultiplier = item["craftingStats"]["multiplier"]
--           for materialId, quantity in pairs(recipe["recipe"]) do
+
if not defaultmultiplier then
--                material = getItem(materialId)
+
defaultmultiplier = 1
--                s3 = s3 .. "|" .. icon(material['name'], material['itemImage'], 1) .. "\n"
+
end
--               s3 = s3 .. "|" .. addSeparator(quantity) .. "\n"
+
return tostring(defaultmultiplier)
--               if recipe["multiplier"] then
+
end
--                   s3 = s3 .. "|" .. addSeparator(recipe["multipier"]) .. "\n"
+
 
--               else
+
local function getPrice(item)
--                    s3 = s3 .. "|" .. addSeparator(1) .. "\n"
+
if item['tradeable'] == false then
--               end
+
return "Untradeable"
--           end
+
else
        end
+
local price = require("Module:Market")["_price"]({item['name'], 1, 1})
        s = s .. s3
+
if price then
s = s .. "|}"
+
return math.floor(price)
return num
+
else
 +
return "No Entry"
 +
end
 +
end
 +
end
 +
 
 +
local function createTable(item, item_recipes)
 +
local table = ""
 +
local req_table = ""
 +
local recipes_table = ""
 +
local item_requirements = item["craftingStats"]
 +
local item_default_crafting_multiplier = getDefaultMultiplier(item)
 +
 
 +
req_table = req_table .. "{| class=\"wikitable\"style=\"width:100%;\"\n"
 +
req_table = req_table .. "|+Crafting Requirements\n"
 +
req_table = req_table .. "|-\n"
 +
req_table = req_table .. "!scope=\"col\" | Category\n"
 +
req_table = req_table .. "!scope = \"col\" | Level\n"
 +
req_table = req_table .. "!scope = \"col\" | XP\n"
 +
req_table = req_table .. "|-\n"
 +
req_table = req_table .. "|style=\"text-align:center;\"|" .. tostring(item_requirements["category"]) .. "\n"
 +
req_table = req_table .. "|style=\"text-align:center;\"|" .. tostring(item_requirements["level"]) .. "\n"
 +
req_table = req_table .. "|style=\"text-align:right;\"|" .. addSeparator(item_requirements["experience"]) .. "\n"
 +
req_table = req_table .. "|}\n"
 +
 
 +
recipes_table = recipes_table .. "{| class=\"wikitable\"style=\"width:100%;\"\n"
 +
recipes_table = recipes_table .. "|+Recipes\n"
 +
recipes_table = recipes_table .. "|-\n"
 +
recipes_table = recipes_table .. "!scope = \"col\" colspan=\"2\"| Item\n"
 +
recipes_table = recipes_table .. "!scope = \"col\" | Quantity\n"
 +
recipes_table = recipes_table .. "!scope = \"col\" | Cost\n"
 +
recipes_table = recipes_table .. "|-\n"
 +
for index, wmrecipe in pairsByKeys(item_recipes) do
 +
local recipe_material_amount = tostring(tablelength(item_recipes[index]["recipe"]))
 +
local recipe_material_count = 0
 +
local recipe_multiplier
 +
local recipeTotalCost = 0
 +
local itemPrice = getPrice(item)
 +
if wmrecipe["multiplier"] then
 +
recipe_multiplier = tostring(wmrecipe["multiplier"])
 +
else
 +
recipe_multiplier = item_default_crafting_multiplier
 +
end
 +
for material_id, quantity in pairsByKeys(wmrecipe["recipe"]) do
 +
local material = getItem(material_id)
 +
local materialPrice = getPrice(material)
 +
recipes_table = recipes_table .. "| " .. itemImage(material, "42px", nil) .. "\n"
 +
recipes_table = recipes_table .. "| " .. itemLink(material["name"]) .. "\n"
 +
recipes_table = recipes_table .. "|style=\"text-align:right;\"|" .. tostring(quantity) .. "\n"
 +
if type(materialPrice) =="string" then
 +
recipes_table = recipes_table .. "|style=\"text-align:right;\"|".. materialPrice .."\n"
 +
recipes_table = recipes_table .. "|-\n"
 +
else
 +
recipeTotalCost = recipeTotalCost + materialPrice*quantity
 +
recipes_table = recipes_table .. "|style=\"text-align:right;\"|" .. addSeparator(math.floor(materialPrice*quantity)) .. "\n"
 +
recipes_table = recipes_table .. "|-\n"
 +
end
 +
end
 +
recipes_table = recipes_table .. "!scope=\"row\"colspan=\"3\" style=\"text-align:right;\"|Total cost\n"
 +
recipes_table = recipes_table .. "|style=\"text-align:right;\"|"..addSeparator(recipeTotalCost).."\n"
 +
recipes_table = recipes_table .. "|-\n"
 +
recipes_table = recipes_table .. "| " .. itemImage(item, "42px", nil) .. "\n"
 +
recipes_table = recipes_table .. "| " .. itemLink(item["name"]) .. "\n"
 +
recipes_table = recipes_table .. "|style=\"text-align:right;\"| " .. recipe_multiplier .. " \n"
 +
if type(itemPrice) =="string" then
 +
recipes_table = recipes_table .. "|style=\"text-align:right;\"|" .. itemPrice .. "\n"
 +
else
 +
recipes_table = recipes_table .. "|style=\"text-align:right;\"|" .. addSeparator(math.floor(itemPrice)) .. " \n"
 +
end
 +
recipes_table = recipes_table .. "|-\n"
 +
recipes_table = recipes_table .. "!scope=\"row\"colspan=\"3\" style=\"text-align:right;\"|Profit\n"
 +
if type(itemPrice) =="string" then
 +
recipes_table = recipes_table .. "|style=\"text-align:right;\"|" .. itemPrice .."\n"
 +
recipes_table = recipes_table .. "|-\n"
 +
else
 +
recipes_table = recipes_table .. "|style=\"text-align:right;\"| " .. addSeparatorRound(recipeTotalCost-itemPrice*0.95) .. " \n"
 +
recipes_table = recipes_table .. "|-\n"
 +
end
 +
end
 +
recipes_table = recipes_table .. "|}\n"
 +
 
 +
table = "<div style=\"width:-moz-fit-content;width:fit-content;\">\n" .. req_table .. recipes_table .."</div>"
 +
return table
 
end
 
end
  
Line 128: Line 240:
 
local item = 0
 
local item = 0
 
local wikitable = ""
 
local wikitable = ""
local item_recipe = 0
+
local item_recipes = 0
name = args[1]
+
name = args["item"]
item = findItem(name)
+
item = getItem(findId._findId({name, "item"}))
 
if item == 0 then
 
if item == 0 then
 
return "item not found"
 
return "item not found"
 
end
 
end
item_recipe = p.loadData("recipe")[tostring(item["id"])]['crafting']['recipe']['96']
+
item_recipes = getItemRecipes(item["id"])
if item_recipe == 0 then
+
if item_recipes == 0 then
 
return "item uncraftable"
 
return "item uncraftable"
 
end
 
end
wikitable = createTable(item, item_recipe)
+
wikitable = createTable(item, item_recipes)
return item_recipe
+
 
 +
return wikitable
 +
 
 
end
 
end
  
 
return p
 
return p

Latest revision as of 07:26, 3 June 2024


local p = {}

local findId = require("Module:FindId")

local data_module_names = {
	item = 'Module:Items/data',
	crafting = 'Module:Crafting/data'
}

local loaded_data_modules = {}

local function tablelength(T)
	local count = 0
	for _ in pairs(T) do count = count + 1 end
	return count
end

local function pairsByKeys (t, f)
	local a = {}
	local orgi_key_type
	local orgi_key_numbered
	for n in pairs(t) do
    	if tonumber(n) == nil then
        	table.insert(a, n)
        	orgi_key_type = "word"
    	elseif type(n) == "number" then
        	table.insert(a, n)
        	orgi_key_type = "int"
    	elseif type(n) == "string" and type(tonumber(n) == "number") then
            orgi_key_type = "number"
            table.insert(a, tonumber(n))
		end
	end
	table.sort(a, f)
	local key
	local value
	local i = 0      -- iterator variable
	local iter = function ()   -- iterator function
		i = i + 1
		if a[i] == nil then return nil
    	elseif orgi_key_type == "word" or orgi_key_type == "int" then
        	key = a[i]
        	value = t[a[i]]
    	elseif orgi_key_type == "number" then
        	key = tostring(a[i])
        	value = t[tostring(a[i])]
		end
    	return key, value
	end
	return iter
end

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 getItem(id)
	return p.loadData("item")[tostring(id)]
end

local function getItemRecipes(id)
	local a = p.loadData("crafting")[tostring(id)]
	if a == nil then
		return 0
	else
		return a
	end
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 itemImageLink(item)
	local url = ""
	if item['itemIcon'] then
		url = item['itemIcon']
	else
		url = item['itemImage']
	end
	return fullUrl(url)
end

local function itemImage(item, width, height, word)
	local s = itemImageLink(item)
	s = "[[" .. item['name'] .. "|<img src=\"" .. s
	s = s .. "\" alt=\""  .. item['name'] .. "\"style=\"width:"
	if width == nil then
		s = s .. "auto; height: "
	else
		s = s ..  tostring(width) .. "; height: "
	end
	if height == nil then
		s = s .. "auto;\">"
	else
		s = s .. height .. ";\">"
	end
	if word == 1 then
		s = s .. item['name']
	end
	s = s .. "]]"
	return s
end

local function itemLink(name)
	local s= "[[" .. name .. "|" .. name .. "]]"
	return s
end

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

local function addSeparatorRound(num)
	return string.format("%.2f",tostring(tonumber(num))):reverse():gsub("(%d%d%d)","%1,"):gsub(",(%-?)$","%1"):reverse()
end

local function getDefaultMultiplier(item)
	local defaultmultiplier = item["craftingStats"]["multiplier"]
	if not defaultmultiplier then
		defaultmultiplier = 1
	end
	return tostring(defaultmultiplier)
end

local function getPrice(item)
	if item['tradeable'] == false then
		return "Untradeable"
	else
		local price = require("Module:Market")["_price"]({item['name'], 1, 1})
		if price then
			return math.floor(price)
		else
			return "No Entry"
		end
	end
end

local function createTable(item, item_recipes)
	local table = ""
	local req_table = ""
	local recipes_table = ""
	local item_requirements = item["craftingStats"]
	local item_default_crafting_multiplier = getDefaultMultiplier(item)

	req_table = req_table .. "{| class=\"wikitable\"style=\"width:100%;\"\n"
	req_table = req_table .. "|+Crafting Requirements\n"
	req_table = req_table .. "|-\n"
	req_table = req_table .. "!scope=\"col\" | Category\n"
	req_table = req_table .. "!scope = \"col\" | Level\n"
	req_table = req_table .. "!scope = \"col\" | XP\n"
	req_table = req_table .. "|-\n"
	req_table = req_table .. "|style=\"text-align:center;\"|" .. tostring(item_requirements["category"]) .. "\n"
	req_table = req_table .. "|style=\"text-align:center;\"|" .. tostring(item_requirements["level"]) .. "\n"
	req_table = req_table .. "|style=\"text-align:right;\"|" .. addSeparator(item_requirements["experience"]) .. "\n"
	req_table = req_table .. "|}\n"

	recipes_table = recipes_table .. "{| class=\"wikitable\"style=\"width:100%;\"\n"
	recipes_table = recipes_table .. "|+Recipes\n"
	recipes_table = recipes_table .. "|-\n"
	recipes_table = recipes_table .. "!scope = \"col\" colspan=\"2\"| Item\n"
	recipes_table = recipes_table .. "!scope = \"col\" | Quantity\n"
	recipes_table = recipes_table .. "!scope = \"col\" | Cost\n"
	recipes_table = recipes_table .. "|-\n"
	for index, wmrecipe in pairsByKeys(item_recipes) do
		local recipe_material_amount = tostring(tablelength(item_recipes[index]["recipe"]))
		local recipe_material_count = 0
		local recipe_multiplier
		local recipeTotalCost = 0
		local itemPrice = getPrice(item)
		if wmrecipe["multiplier"] then
			recipe_multiplier = tostring(wmrecipe["multiplier"])
		else
			recipe_multiplier = item_default_crafting_multiplier
		end
		for material_id, quantity in pairsByKeys(wmrecipe["recipe"]) do
			local material = getItem(material_id)
			local materialPrice = getPrice(material)
			recipes_table = recipes_table .. "| " .. itemImage(material, "42px", nil) .. "\n"
			recipes_table = recipes_table .. "| " .. itemLink(material["name"]) .. "\n"
			recipes_table = recipes_table .. "|style=\"text-align:right;\"|" .. tostring(quantity) .. "\n"
			if type(materialPrice) =="string" then
				recipes_table = recipes_table .. "|style=\"text-align:right;\"|".. materialPrice .."\n"
				recipes_table = recipes_table .. "|-\n"
			else
				recipeTotalCost = recipeTotalCost + materialPrice*quantity
				recipes_table = recipes_table .. "|style=\"text-align:right;\"|" .. addSeparator(math.floor(materialPrice*quantity)) .. "\n"
				recipes_table = recipes_table .. "|-\n"
			end
		end
		recipes_table = recipes_table .. "!scope=\"row\"colspan=\"3\" style=\"text-align:right;\"|Total cost\n"
		recipes_table = recipes_table .. "|style=\"text-align:right;\"|"..addSeparator(recipeTotalCost).."\n"
		recipes_table = recipes_table .. "|-\n"
		recipes_table = recipes_table .. "| " .. itemImage(item, "42px", nil) .. "\n"
		recipes_table = recipes_table .. "| " .. itemLink(item["name"]) .. "\n"
		recipes_table = recipes_table .. "|style=\"text-align:right;\"| " .. recipe_multiplier .. " \n"
		if type(itemPrice) =="string" then
			recipes_table = recipes_table .. "|style=\"text-align:right;\"|" .. itemPrice .. "\n"
		else
			recipes_table = recipes_table .. "|style=\"text-align:right;\"|" .. addSeparator(math.floor(itemPrice)) .. " \n"
		end
		recipes_table = recipes_table .. "|-\n"
		recipes_table = recipes_table .. "!scope=\"row\"colspan=\"3\" style=\"text-align:right;\"|Profit\n"
		if type(itemPrice) =="string" then
			recipes_table = recipes_table .. "|style=\"text-align:right;\"|" .. itemPrice .."\n"
			recipes_table = recipes_table .. "|-\n"
		else
			recipes_table = recipes_table .. "|style=\"text-align:right;\"| " .. addSeparatorRound(recipeTotalCost-itemPrice*0.95) .. " \n"
			recipes_table = recipes_table .. "|-\n"
		end
	end
	recipes_table = recipes_table .. "|}\n"

	table = "<div style=\"width:-moz-fit-content;width:fit-content;\">\n" .. req_table .. recipes_table .."</div>"
	return table
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_recipes = 0
	name = args["item"]
	item = getItem(findId._findId({name, "item"}))
	if item == 0 then
		return "item not found"
	end
	item_recipes = getItemRecipes(item["id"])
	if item_recipes == 0 then
		return "item uncraftable"
	end
	wikitable = createTable(item, item_recipes)

	return wikitable

end

return p