Module:CombatStylesDisplay

Module documentation
This documentation is transcluded from Template:No documentation/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:CombatStylesDisplay/doc. [edit]
Module:CombatStylesDisplay's function main is invoked by Template:CombatStylesDisplay.
Module:CombatStylesDisplay requires Module:Array.
Module:CombatStylesDisplay requires Module:Paramtest.

local p = {}

local contains = require('Module:Array').contains
local paramTest = require('Module:Paramtest')

local poisonableWeaponTypes = { 'Spear', 'Stab Sword', 'Thrown' }

-- Remove any weapons where args.range isn't equal to smw range
function filterByRange(data, range)
	local filteredData = {}
	for _, weapon in ipairs(data) do
		if(range == weapon.range) then
			table.insert(filteredData, weapon)
		end
	end
	return filteredData
end

function loadData(weaponType, range, limit)
	local query = {
		'[[All Combat style::' .. weaponType .. ']]',
		'?=#-'
	}
	query.limit = limit or 500
	
	if(contains(poisonableWeaponTypes, weaponType)) then
		table.insert(query, '?Has subobject#-= ')
	end
	
	if(range > 0) then
		table.insert(query, '?All Weapon attack range#-=range')
	end
	
	local t1 = os.clock()
	local smwdata = mw.smw.ask(query)
	local t2 = os.clock()
	mw.log(string.format('SMW: entries %d, time elapsed: %.3f ms.', #smwdata, (t2 - t1) * 1000))
	
	if(range > 0) then
		smwdata = filterByRange(smwdata, range)
	end
	
	return smwdata
end

function buildPoisonSuperscript(weaponData)
	local p, pp, ppp, kp = '', '', '', ''
	for _, subObject in ipairs(weaponData[2]) do
		if(subObject == weaponData[1] .. '#(p)') then
			p = '[[' .. weaponData[1] .. '(p)|(p)]]'
		elseif(subObject == weaponData[1] .. '#(p+)') then
			pp = '[[' .. weaponData[1] .. '(p+)|(p+)]]'
		elseif(subObject == weaponData[1] .. '#(p++)') then
			ppp = '[[' .. weaponData[1] .. '(p++)|(p++)]]'
		elseif(subObject == weaponData[1] .. '#(kp)') then
			kp = '[[' .. weaponData[1] .. '(kp)|(kp)]]'
		end
	end
	local ret = p .. pp .. ppp .. kp
	return ret ~= nil and '<sup>' .. ret .. '</sup> ' or ''
end

function p._main(args)
	local weaponType = string.gsub(" " .. mw.language.getContentLanguage():lc(args[1]), "%W%l", string.upper):sub(2)
	local range = paramTest.defaults{ {tonumber(args.range), 0} }
	
	local data = loadData(weaponType, range, args.limit)
	
	-- ## Modify column-width by +1 if the columns are too small
	local ret = mw.html.create('div'):css( {['column-fill'] = 'balance', ['column-width'] = '17em'})
	for _, weaponData in ipairs(data) do
		local poisonSup = ''
		if(weaponData[2] ~= nil and contains(poisonableWeaponTypes, weaponType)) then
			poisonSup = buildPoisonSuperscript(weaponData)
		end

		ret:tag('div'):wikitext('[[' .. weaponData[1] .. ']]' .. tostring(poisonSup)):css( {['white-space'] = 'nowrap'}):done()
	end
	
	return tostring(ret)
end

--mw.log( p._main( {'Spear'} ) )

function p.main(frame)
	local args = frame:getParent().args
	--mw.logObject(args)
	return p._main(args)
end

return p