Module:UsedInEnchantments

local p = {}

function p.incense(frame) if frame == mw.getCurrentFrame then args = require('Module:ProcessArgs').merge(true) else frame = mw.getCurrentFrame end

local name = mw.title.getCurrentTitle.text

local fields = '_pageName, name, incense, effects, DLC' local cargoArgs = { orderBy = '_pageName', groupBy = 'name', where = 'incense="' .. name .. '"', limit = 5000 } local data = mw.ext.cargo.query('EnchantRecipes', fields, cargoArgs)

return buildhtml(data, frame) end

function p.main(frame) if frame == mw.getCurrentFrame then args = require('Module:ProcessArgs').merge(true) else frame = mw.getCurrentFrame end

-- basic cargo query

local fields = '_pageName, name, tags, slots, items, effects, DLC' local cargoArgs = { orderBy = '_pageName', groupBy = 'name', limit = 5000 } local data = mw.ext.cargo.query('EnchantRecipes', fields, cargoArgs)

-- actual search local results = search(data, args)

if #results < 1 then return 'No results found!' end

return buildhtml(results, frame) end

-- build list of compatible enchants (too complex for cargo query) function search(data, args) local itemTags = split(args.tags, ',') local results = {} for _,enchant in ipairs(data) do               local isCompatible = false -- check for explicit item requirements if notempty(enchant.items) then local name = args.name or mw.title.getCurrentTitle.text local items = split(enchant.items, ',') if contains(items, name) then isCompatible = true end end -- check slot and tags if not isCompatible and notempty(enchant.slots) then local slots = split(enchant.slots, ',') if hasmatch(slots, itemTags) then if notempty(enchant.tags) then local enchTags = split(enchant.tags, ',') isCompatible = hasmatch(enchTags, itemTags) else isCompatible = true end end end -- actually add it to the matches list if isCompatible then table.insert(results, enchant) end end return results end

function buildhtml(results, frame) -- build table output local html = mw.html.create -- removing hatnote for now: html:wikitext(frame:preprocess(' is compatible with the following Enchantments:'))

local table = html:tag('table'):addClass('wikitable sortable')

local header = table:tag('tr') header:tag('th'):wikitext('Enchantment') header:tag('th'):wikitext('Effects')

for _,v in ipairs(results) do       local tr = table:tag('tr') tr:tag('td'):cssText('text-align:center'):wikitext(frame:preprocess( .. v.name ..  .. " "))       tr:tag('td'):wikitext(frame:preprocess(v.effects)) end

return html end

--- HELPERS

function find(tbl, val) for k, v in pairs(tbl) do       if k == val then return v end end return nil end

function notempty(string) return string ~= nil and string ~= '' end

function hasmatch(table1, table2) for _,v in ipairs(table1) do       if contains(table2, v) then return true end end return false end

function contains(table, value) for _,v in ipairs(table) do       if v == value then return true end end return false end

function split(inputstr, sep) if sep == nil then sep = "%s" end local t={} if notempty(inputstr) then for str in string.gmatch(inputstr .. ',', "([^"..sep.."]+)") do           table.insert(t, str) end end return t end

return p