Module:ListNPCs

local p = {}

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

local fields = '_pageName, name, image, type, locations, faction, teaches, quests'

local showQuests = false if args.showQuests ~= nil then showQuests = true end

local showTeaches = false if args.showTeaches ~= nil then showTeaches = true end local cargoArgs = {       orderBy = '_pageName', limit = 5000 }   if args.type ~= nil then cargoArgs.where = 'type LIKE \'%' .. args.type .. '%\''   end local result = mw.ext.cargo.query('NPCs', fields, cargoArgs)

if not next(result) then return 'No results found!' end if args.type ~= 'Merchant' then -- everything except merchants is just one list local html = mw.html.create buildoutput(nil, html, result, frame, showTeaches, showQuests) return html else -- merchants have a sub-category sort. -- same logic could be applied to other filters. local order = { 'General', 'Alchemy', 'Equipment', 'Food', 'Other' }       local html = mw.html.create buildwithfilter(order, html, result, frame, showTeaches, showQuests) return html end end

function buildwithfilter(order, html, data, frame, showTeaches, showQuests) -- prepare sorted list holders (create empty tables corresponding to the type) local sorted = {} for _, v in ipairs(order) do       sorted[v] = {} end

-- sort npcs into their holder for _, v in ipairs(data) do       local found = false for _, x in ipairs(order) do           if string.find(v.type, x) then table.insert(sorted[x], v)               found = true break end end if not found then table.insert(sorted['Other'], v)       end end

for _, v in ipairs(order) do       --buildoutput(v, html, sorted[v], frame) if #sorted[v] > 0 then buildoutput(v, html, sorted[v], frame, showTeaches, showQuests) end end end

function buildoutput(title, html, data, frame, showTeaches, showQuests) if title ~= nil then html:tag('h3'):wikitext(title) end

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

local trHeader = tbl:tag('tr') trHeader:tag('th'):wikitext('Image') trHeader:tag('th'):wikitext('Name') trHeader:tag('th'):wikitext('Type') trHeader:tag('th'):wikitext('Location(s)') trHeader:tag('th'):wikitext('Faction(s)') if showTeaches then trHeader:tag('th'):wikitext('Teaches') end if showQuests then trHeader:tag('th'):wikitext('Gives Quests') end

for _,row in ipairs(data) do       local tr = tbl:tag('tr') tr:tag('td'):cssText('text-align:center'):wikitext(frame:preprocess('')) tr:tag('td'):wikitext(frame:preprocess( .. ((row.name ~=  and row.name) or row._pageName) .. '')) tr:tag('td'):wikitext(frame:preprocess(row.type)) tr:tag('td'):wikitext(frame:preprocess(row.locations)) tr:tag('td'):wikitext(frame:preprocess((row.faction ~= '' and row.faction) or '-')) if showTeaches then tr:tag('td'):wikitext(frame:preprocess((row.teaches ~= '' and row.teaches) or '-')) end if showQuests then tr:tag('td'):wikitext(frame:preprocess((row.quests ~= '' and row.quests) or '-')) end end end

return p