Module:Recipe
Jump to navigation
Jump to search
local p = {}
local data_module_names = {
item = 'Module:Items/data',
recipe = 'Module:CraftingAugmenting/data'
}
local loaded_data_modules = {}
local function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
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 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 getItemRecipes(id)
return p.loadData("recipe")[tostring(id)]["crafting"]
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 getCraftingStats(item)
return item["craftingStats"]
end
local function getRecipes(item_crafting)
return item_crafting[1]
end
local function getDefaultMultiplier(item)
local defaultmultiplier = item["craftingStats"]["multiplier"]
if not defaultmultiplier then
defaultmultiplier = 1
end
return tostring(defaultmultiplier)
end
local function createTable(item, item_recipes)
local table = ""
local req_table = ""
local recipes_table = ""
local item_requirements = getCraftingStats(item)
local item_default_crafting_multiplier = getDefaultMultiplier(item)
req_table = req_table .. "{| class=\"wikitable\"\n"
req_table = req_table .. "|+Requirements\n"
req_table = req_table .. "|-\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 .. "|" .. tostring(item_requirements["level"]) .. "\n"
req_table = req_table .. "|" .. addSeparator(item_requirements["experience"]) .. "\n"
req_table = req_table .. "|-\n"
req_table = req_table .. "! scope=\"row\" | Category\n"
req_table = req_table .. "|" .. tostring(item_requirements["category"]) .. "\n"
req_table = req_table .. "|}\n"
recipes_table = recipes_table .. "{| class=\"wikitable\"\n"
recipes_table = recipes_table .. "|+Recipes\n"
recipes_table = recipes_table .. "|-\n"
recipes_table = recipes_table .. "!scope = \"col\" | Recipe\n"
recipes_table = recipes_table .. "!scope = \"col\" | Multiplier\n"
recipes_table = recipes_table .. "!scope = \"col\" | Item\n"
recipes_table = recipes_table .. "!scope = \"col\" | Quantity\n"
recipes_table = recipes_table .. "|-\n"
for index, wmrecipe in ipairs(item_recipes) do
local recipe_material_count = tostring(tablelength(item_recipes[index]["recipe"]))
local recipe_multiplier
if wmrecipe["multiplier"] then
recipe_multiplier = tostring(wmrecipe["multiplier"])
else
recipe_multiplier = item_default_crafting_multiplier
end
recipes_table = recipes_table .. "| rowspan=" .. recipe_material_count .. "| " .. tostring(index) .. "\n"
recipes_table = recipes_table .. "| rowspan=" .. recipe_material_count .. "| " .. recipe_multiplier .. "\n"
for material_id, quantity in pairs(wmrecipe["recipe"]) do
local material = getItem(material_id)
recipes_table = recipes_table .. "| " .. icon(material['name'], material['itemImage'], 1) .. "\n"
recipes_table = recipes_table .. "| " .. tostring(quantity) .. "\n"
recipes_table = recipes_table .. "|-\n"
end
end
recipes_table = recipes_table .. "|}\n"
table = req_table .. recipes_table
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[1]
item = findItem(name)
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