Module:EnchantableItems

local p = {}

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' local cargoArgs = { orderBy = '_pageName', groupBy = 'name', limit = 5000 } local data = mw.ext.cargo.query('EnchantmentTags', fields, cargoArgs)

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

if #results < 1 then return 'No results!' elseif #results > 50 then return 'Lots! See "Equipment" above.' end

return buildhtml(results, frame, args) end

-- build list of compatible enchants (too complex for cargo query) function search(data, args) local results = {}

for _, item in ipairs(data) do       local isCompatible = false

if notempty(args.slots) and notempty(item.tags) then local slots = split(args.slots, ',') local itemTags = split(item.tags, ',') if hasmatch(slots, itemTags) then if notempty(args.tags) then local enchTags = split(args.tags, ',') isCompatible = hasmatch(enchTags, itemTags) else isCompatible = true end end end

if not isCompatible and notempty(args.items) then local items = split(args.items, ',') isCompatible = contains(items, item._pageName) end

if isCompatible then table.insert(results, item) end end

return results end

function buildhtml(results, frame, args) local itemList = '' for _, v in ipairs(results) do       if itemList ~= '' then itemList = itemList .. ',' end itemList = itemList .. v._pageName end

local output = '' output = output .. frame:preprocess('') output = output .. frame:preprocess('') output = output .. frame:preprocess('') return output 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