Module:ListBuilds

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, DLC, author, type, name, tags, description, breakthrough1, breakthrough2, breakthrough3, faction, weapon, offhand, helmet, boots, armor, backpack, quickslot1, quickslot2, quickslot3, quickslot4, quickslot5, quickslot6, quickslot7, quickslot8' local cargoArgs = { orderBy = 'type, _pageName', groupBy = 'name', limit = 5000 } if args.category ~= nil then cargoArgs['where'] = "type='" .. args.category .. "'"		end local data = mw.ext.cargo.query('CharacterBuilds', fields, cargoArgs)

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

-- put builds into tables based on the 'type' of the build local categories = {}

for _, v in ipairs(data) do       local tbl = find(categories, v.type) if tbl == nil then categories[v.type] = {} tbl = categories[v.type] end table.insert(tbl, v)   end

-- sort the categories alphabetically ("for v in pairs" will iterate the keys) local sorted = {} for v in pairs(categories) do table.insert(sorted, v) end table.sort(sorted) -- lua's default sort

-- build table output local html = mw.html.create -- iterate alphabetical key list for _, type in ipairs(sorted) do       if type ~= 'Other' then if #categories[type] > 0 then buildoutput(type, categories[type], html, frame) end end end

if categories['Other'] ~= nil and #categories['Other'] > 0 then buildoutput('Other', categories['Other'], html, frame) end -- ''The sources above were generated dynamically. Click [ here] to force an update of the data.'' html:tag('i') :wikitext(frame:preprocess('The builds above were listed dynamically. Click [ here] to force an update of the data.'))

return html end

function buildoutput(title, data, html, frame) html:wikitext(frame:preprocess(' ' .. title .. ' '))

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

local header = table:tag('tr') header:tag('th'):cssText('min-width:120px'):wikitext('Build Name') header:tag('th'):wikitext('Tags') --header:tag('th'):wikitext('Description') header:tag('th'):wikitext('Faction') header:tag('th'):wikitext('Breakthroughs') header:tag('th'):wikitext('Equipment') header:tag('th'):wikitext('Quickslots') header:tag('th'):wikitext('Author') header:tag('th'):wikitext('DLC Required')

for _,v in ipairs(data) do       local tr = table:tag('tr')

tr:tag('td'):cssText('min-width: 120px !important; max-width:120px !important; text-align:center'):wikitext(frame:preprocess( .. v.name .. )) tr:tag('td'):cssText('min-width:120px; max-width: 135px'):wikitext(frame:preprocess(v.tags)) --tr:tag('td'):cssText('min-width: 250px'):wikitext(frame:preprocess(v.description)) local faction = tr:tag('td'):cssText('text-align: center;') if notempty(v.faction) then faction:wikitext(frame:preprocess('')) else faction:wikitext('Any') end

local breakthroughs = { v.breakthrough1, v.breakthrough2, v.breakthrough3 } local breakthroughText = AppendImages(breakthroughs, ' • ') tr:tag('td'):cssText('text-align:center; min-width: 100px'):wikitext(frame:preprocess(breakthroughText))

local equips = { v.weapon, v.offhand, v.helmet, v.armor, v.boots, v.backpack } local equipText = AppendImages(equips, '•') tr:tag('td'):cssText('text-align:center; min-width: 190px'):wikitext(frame:preprocess(equipText))

local slots = { v.quickslot1, v.quickslot2, v.quickslot3, v.quickslot4, v.quickslot5, v.quickslot6, v.quickslot7, v.quickslot8 } local qsText = AppendImages(slots, ' ') if not notempty(qsText) then qsText = 'Not listed' end tr:tag('td'):cssText('text-align:center; min-width: 250px'):wikitext(frame:preprocess(qsText)) local author = 'Anonymous' if notempty(v.author) then author =  .. v.author ..  end tr:tag('td'):cssText('min-width:140px; max-width: 125px'):wikitext(frame:preprocess(author))

local dlcs = 'None' if notempty(v.DLC) then dlcs = '' local list = split(v.DLC, ',') for _,dlc in ipairs(list) do               if dlcs ~= '' then dlcs = dlcs .. ', ' end dlcs = dlcs .. ''           end end tr:tag('td'):wikitext(frame:preprocess(dlcs)) end

return table end

function AppendImages(list, delimiter) local string = '' for _, v in ipairs(list) do       string = AppendImageIfNotEmpty(v, string, delimiter) end return string end

function AppendImageIfNotEmpty(name, appendto, delimiter) if notempty(name) then if notempty(appendto) and notempty(delimiter) then appendto = appendto .. delimiter end appendto = appendto .. ''   end return appendto 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