Module:Recommended equipment

From Roat Pkz
Jump to navigation Jump to search

Documentation for this module may be created at Module:Recommended equipment/doc

local p = {}

local paramTest = require('Module:Paramtest')
local onMain = require('Module:Mainonly').on_main
local yesNo = require('Module:Yesno')

local SlotInfo = {
	{ name = 'head', icon = 'Head slot', txt = 'Head', link = '' },
	{ name = 'neck', icon = 'Neck slot', txt = 'Neck', link = ' ' },
	{ name = 'cape', icon = 'Cape slot', txt = 'Back', link = '' },
	{ name = 'body', icon = 'Body slot', txt = 'Body', link = '' },
	{ name = 'legs', icon = 'Legs slot', txt = 'Legs', link = '' },
	{ name = 'weapon', icon = 'Weapon slot', txt = 'Weapon', link = '' },
	{ name = 'shield', icon = 'Shield slot', txt = 'Shield', link = '' },
	{ name = '2h', icon = '2h slot', txt = 'Two-handed weapon', link = '' },
	{ name = 'ammo', icon = 'Ammo slot', txt = 'Ammo/Spell', link = '' },
	{ name = 'hands', icon = 'Hands slot', txt = 'hands', link = '' },
	{ name = 'feet', icon = 'Feet slot', txt = 'Boots', link = '' },
	{ name = 'ring', icon = 'Ring slot', txt = 'Ring', link = '' },
	{ name = 'special', icon = 'Special attack orb', txt = 'Special attack', link = '' },
}

function buildRow(slot, rowData, largestRowSize)
	local row = mw.html.create('tr')
	row:tag('td'):wikitext('[[File:' .. slot.icon .. '.png|' .. slot.txt .. '|link=' .. slot.link .. ']]')
	for _, cellData in ipairs(rowData) do
		row:tag('td'):wikitext(cellData)
	end
	
	-- If there are any rows that empty compared to other rows fill up the remaining td's with N/As
	for i = 1, largestRowSize - #rowData, 1 do
		row:tag('td'):addClass('table-na'):wikitext('N/A')	
	end
	return row
end

function p._main(args)
	
	-- Tracking for colspan and N/A cells
	local largestRowSize = 0
	-- Intended as a hidden setting, use sparingly
	local maxRowsPerSlot = paramTest.default_to(args.rows, 5)
	
	-- Find the greatest row count
	for i, slot in ipairs(SlotInfo) do
		local slotRowSize = 0
		
		for i = 1, maxRowsPerSlot, 1 do
			if(paramTest.has_content(args[slot.name .. i])) then
				slotRowSize = i
			end
		end
		
		if(largestRowSize < slotRowSize) then
			largestRowSize = slotRowSize	
		end
		if(largestRowSize == maxRowsPerSlot) then
			break
		end
	end
	
	local ret = mw.html.create('table'):addClass('wikitable sticky-header')
	
	if(paramTest.has_content(args.style)) then
		ret:tag('caption'):wikitext('Recommended equipment for ' .. args.style)
	end
	
	ret:tag('tr')
		:tag('th'):wikitext('Slot'):done()
		:tag('th'):attr('colspan', largestRowSize):wikitext('Item (most effective → least effective)'):done()
	
	
	local slotInputs = {}
	for _, slot in ipairs(SlotInfo) do
		local slotRow = {}
		for i = 1, maxRowsPerSlot, 1 do
			local gear = args[slot.name .. i]
			if(paramTest.has_content(gear)) then
				table.insert(slotRow, gear)
			end
		end
		
		if(#slotRow > 0) then
			ret:node(buildRow(slot, slotRow, largestRowSize))
			slotInputs[slot.name] = slotRow
		end
	end
	
	local useSmw = yesNo(args.smw or '', true)
	if(useSmw and onMain()) then
		local jsonObject = { ['Recommended Equipment'] = slotInputs, style = args.style}
		local smwmap = {
			['Recommended Equipment JSON'] = mw.text.jsonEncode(jsonObject),
		}
	end
	
	return ret
end

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

return p