Module:UserBuild

local util_game = require('Module:GameUtil') local util_cargo = require('Module:CargoUtil') local HEADERS = { 'Character', 'Stats', 'Armor', 'Weapons', 'Notes' } local SPECIAL = { 'Strength', 'Perception', 'Endurance', 'Charisma', 'Intelligence', 'Agility', 'Luck' }

function formatArgs(args) return { args.name, util_game.special(args), string.format('%s', args.armor), string.format('%s', args.weapons), args.notes, perks = perks(args), } end

function perks(args) local perkData = getPerkData(args) return processPerkData(perkData) end

function getPerkData(args) local tbl = { Strength = {}, Perception = {}, Endurance = {}, Charisma = {}, Intelligence = {}, Agility = {}, Luck = {} }	for k, v in pairs(args) do		if k:sub(1,4) == 'perk' then tbl[k:sub(5,-2)][tonumber(k:sub(-1))] = v		end end return tbl end function processPerkData(data) local formatted = {} local cargoData = getPerkCargo for i, row in pairs(data) do		row_formatted = mw.html.create('dl') for k, v in ipairs(row) do			formatPerk(row_formatted, v, cargoData[v]) end formatted[i] = row_formatted end return formatted end

function getPerkCargo local query = { tables = { 'Perks_F76' }, fields = { 'Name', 'Value', 'Description' }, groupBy = Name, limit = 999 }	local result = util_cargo.queryAndCast(query) return util_cargo.makeOrderedDict(result, 'Name') -- don't bother ordering end

function formatPerk(tbl, v, lookup) tbl:tag('dt'):wikitext(string.format('%s (%s)', v, lookup.Value)):done :tag('dd'):wikitext(lookup.Description) return end

function addHeader(tbl) local tr = tbl:tag('tr') for _, v in ipairs(HEADERS) do		tbl:tag('th'):wikitext(v) end return end

function addArgs(tbl, formatted) local tr = tbl:tag('tr') for _, v in ipairs(formatted) do		tbl:tag('td'):wikitext(v) end return end

function addPerkHeader(tbl) tbl:tag('tr') :tag('th') :attr('colspan','10') :wikitext('Perks') return end

function addPerks(tbl, perks) local tbl2 = tbl:tag('tr'):tag('td'):attr('colspan','10'):tag('table') local tr = tbl2:tag('tr') for _, v in ipairs(SPECIAL) do		tr:tag('th'):css('width','14.29%'):wikitext(v) end tr = tbl2:tag('tr') for _, v in ipairs(SPECIAL) do		tr:tag('td'):css('vertical-align','top'):node(perks[v]) end return end

local p = {}

function p.main(frame) if frame == mw.getCurrentFrame then args = require('Module:ProcessArgs').merge(true) else frame = mw.getCurrentFrame end local formatted = formatArgs(args) local tbl = mw.html.create('table'):addClass('va-table') addHeader(tbl) addArgs(tbl, formatted) addPerkHeader(tbl) addPerks(tbl, formatted.perks) return tbl end return p