Module:Sandbox/Broono/Item Recipes
Jump to navigation
Jump to search
Documentation for this module may be created at Module:Sandbox/Broono/Item Recipes/doc
local p = {}
local addSeparator = require('Module:AddSeparator')
local findId = require('Module:FindId')
local data = {}
data.craftingAugmenting = mw.loadData('Module:CraftingAugmenting/data')
data.items = mw.loadData('Module:Items/data')
local COLLAPSIBLE = 7
local COLLAPSED = 15
function table.length(t)
local count = 0
for _ in pairs(t) do count = count + 1 end
return count
end
local function split(s, sep)
local t = {}
if sep == nil or sep == '' then
sep = '%s'
end
for m in s:gmatch('([^' .. sep .. ']+)') do
table.insert(t, m)
end
return t
end
local function strip(s)
return s:match('^%s*(.-)%s*$')
end
local function addItemIds(t, names)
local _names = split(names, ';')
for _, name in pairs(_names) do
name = strip(name)
if name ~= '' then
local id = findId._findId({name, 'item'})
if type(id) == 'number' then
table.insert(t, {['id'] = id})
else
table.insert(t, {['id'] = id, ['name'] = name})
end
end
end
end
local function addOtherItemFields(t)
for i in pairs(t) do
if type(t[i].id) == 'number' then
local id = tostring(t[i].id)
local craftingStats
if data.items[id].craftingStats then
craftingStats = data.items[id].craftingStats
else
craftingStats = data.items[id]
end
t[i].name = data.items[id].name
t[i].level = craftingStats.level
t[i].experience = craftingStats.experience
t[i].category = craftingStats.category
end
end
end
local function addRecipes(t)
for i in pairs(t) do
if type(t[i].id) == 'number' then
local id = tostring(t[i].id)
t[i].recipes = {}
if data.craftingAugmenting[id].crafting then
for _, recipe in pairs(data.craftingAugmenting[id].crafting) do
table.insert(t[i].recipes, recipe.recipe)
t[i].skill = 'Crafting'
end
elseif data.craftingAugmenting[id].scrollcrafting then
table.insert(t[i].recipes, data.craftingAugmenting[id].scrollcrafting)
t[i].skill = 'Scrollcrafting'
elseif data.craftingAugmenting[id].runecrafting then
table.insert(t[i].recipes, data.craftingAugmenting[id].runecrafting)
t[i].skill = 'Runecrafting'
elseif data.craftingAugmenting[id].smithing then
table.insert(t[i].recipes, data.craftingAugmenting[id].smithing)
t[i].skill = 'Smithing'
end
end
end
end
local function renderRecipes(t)
local recipes = {}
for _, r in pairs(t) do
local ingredients = {}
for id, amt in pairs(r) do
local ingredient = addSeparator._addSeparator({amt}) .. 'x'
ingredient = ingredient .. ' {{Img|' .. data.items[id].name .. '|30}}'
table.insert(ingredients, ingredient)
end
table.insert(recipes, table.concat(ingredients, ', '))
end
return '\n| ' .. table.concat(recipes, '\n|-\n| ')
end
local function generateTable(t)
local tbl = ''
tbl = tbl .. '{| class=\'wikitable sortable'
if table.length(t) > COLLAPSIBLE then tbl = tbl .. ' mw-collapsible' end
if table.length(t) > COLLAPSED then tbl = tbl .. ' mw-collapsed' end
tbl = tbl .. '\''
tbl = tbl .. '\n! scope=\'col\' | Icon'
tbl = tbl .. '\n! scope=\'col\' | Name'
tbl = tbl .. '\n! scope=\'col\' | Skill (Level)'
tbl = tbl .. '\n! scope=\'col\' | Experience'
tbl = tbl .. '\n! scope=\'col\' | Recipes'
for i in pairs(t) do
tbl = tbl .. '\n|-'
if type(t[i].id) ~= 'number' then
tbl = tbl .. '\n| colspan=\'5\' | '
tbl = tbl .. '\'\'Could not find item with name: \'\'\''
tbl = tbl .. t[i].name .. '\'\'\'. Please check spelling or update'
tbl = tbl .. ' data modules.\'\''
else
local rowspan
if table.length(t[i].recipes) > 1 then
rowspan = 'rowspan=\'' .. table.length(t[i].recipes) .. '\' | '
else
rowspan = ''
end
tbl = tbl .. '\n| ' .. rowspan
tbl = tbl .. '{{Img|' .. t[i].name .. '|30|alt=}}'
local itemName
if t[i].name == mw.title.getCurrentTitle().fullText then
itemName = '\'\'\'' .. t[i].name .. '\'\'\''
else
itemName = '[[' .. t[i].name .. ']]'
end
if rowspan ~= '' then
tbl = tbl .. '\n| scope=\'row\' ' .. rowspan .. itemName
else
tbl = tbl .. '\n| scope=\'row\' | ' .. itemName
end
tbl = tbl .. '\n| ' .. rowspan .. t[i].skill
tbl = tbl .. ' (' .. t[i].level .. ')'
tbl = tbl .. '\n| ' .. rowspan .. t[i].experience
tbl = tbl .. renderRecipes(t[i].recipes)
end
end
tbl = tbl .. '\n|}'
return tbl
end
function p.itemRecipes(frame)
return p._craftingRecipe(frame:getParent().args)
end
function p._itemRecipes(args)
local names = args[1]
if not names or names == '' then
names = mw.title.getCurrentTitle().fullText
end
local items = {}
addItemIds(items, names)
addOtherItemFields(items)
addRecipes(items)
return generateTable(items)
end
return p