Module:Infobox Bonuses

From RuneScape Classic Wiki
Jump to: navigation, search

Documentation for this module may be created at Module:Infobox Bonuses/doc

--------------------------
-- Module for [[Template:Infobox Bonuses]]
------------------------
local p = {}

local onmain = require('Module:Mainonly').on_main
local infobox = require('Module:Infobox')

local slots = {
	weapon = '[[Weapon slot|Weapon]]',
	neck = '[[Neck slot|Neck]]',
	head = '[[Head slot|Head]]',
	body = '[[Body slot|Body]]',
	shield = '[[Shield slot|Shield]]',
	legs = '[[Legs slot|Legs]]',
	hands = '[[Hands slot|Hands]]',
	cape = '[[Cape slot|Cape]]',
	feet = '[[Feet slot|Feet]]',
	['replace-body'] = '[[Replace-body slot|Replace-body]]',
	['replace-legs'] = '[[Replace-legs slot|Replace-legs]]',
	['replace-head'] = '[[Replace-head slot|Replace-head]]',
}
function p.main(frame)
	local args = frame:getParent().args
	local ret = infobox.new(args)

	ret:defineParams{
		{ name = 'aim', func = numeric_arg },
		{ name = 'power', func = numeric_arg },
		{ name = 'armour', func = numeric_arg },
		{ name = 'magic', func = numeric_arg },
		{ name = 'prayer', func = numeric_arg },
		{ name = 'slot', func = slot_arg },
		{ name = 'image', func = { name = image_arg, params = { 'image', 'caption' } } },
	}

	ret:defineLinks({ hide = true })

	ret:customButtonPlacement(true)
	ret:create()
	ret:cleanParams()

	ret:addButtonsCaption()

	ret:defineName('Infobox Bonuses')
	ret:addClass('infobox-bonuses')

	local image_defined = ret:paramGrep('image', function(x) return (x or 'N/A') ~= 'N/A' end)
	local image_td = nil
	if image_defined then
		image_td = { tag = 'argd', content = 'image', class='infobox-bonuses-image', rowspan = '9' }
	end

	ret:addRow{
		{ tag = 'th', content = 'Bonuses', class='infobox-subheader', colspan = '5' },
		image_td
	}

	:pad(5)
	:addRow{
		{ tag = 'th', content = '[[WeaponAim|Aim]]', class='infobox-nested' },
		{ tag = 'th', content = '[[WeaponPower|Power]]', class='infobox-nested' },
		{ tag = 'th', content = '[[Armour (equipment type)|Armour]]', class='infobox-nested' },
		{ tag = 'th', content = '[[Magic]]', class='infobox-nested' },
		{ tag = 'th', content = '[[Prayer]]', class='infobox-nested' },
	}
	:addRow{
		{ tag = 'argd', content = 'aim', class='infobox-nested' },
		{ tag = 'argd', content = 'power', class='infobox-nested' },
		{ tag = 'argd', content = 'armour', class='infobox-nested' },
		{ tag = 'argd', content = 'magic', class='infobox-nested' },
		{ tag = 'argd', content = 'prayer', class='infobox-nested' },
	}
	:pad(5)
	:addRow{
		{ tag = 'th', content = 'Slot', class = 'infobox-subheader', colspan = '5' }
	}
	:pad(5)
	:addRow{
		{ tag = 'argd', content = 'slot', class = 'infobox-full-width-content', colspan = '5' },
	}
	:pad(5)

	if onmain() then
		local a1 = ret:param('all')
		local a2 = ret:categoryData()
		ret:wikitext(addcategories(a1, a2))
	end
	return ret:tostring()
end

function numeric_arg(arg)
	if not infobox.isDefined(arg) or not tonumber(arg) then
		return nil
	end
	return arg
end

function image_arg(image, caption)
	if not infobox.isDefined(image) then
		return nil
	end

	if string.lower(image) == 'no' then
		return 'N/A'
	end
	local caption_result = ''
	if infobox.isDefined(caption) and string.lower(caption) ~= 'no' then
		caption_result = '<br /><small>'..caption..'</small>'
	end
	return image..caption_result
end

function slot_arg(arg)
	if not infobox.isDefined(arg) then
		return nil
	end
	ret = {}
	for slot in string.gmatch(arg, "[^,]+") do
		local trimmed = slot:gsub("^%s*(.-)%s*$", "%1")
		if slots[trimmed] then
			table.insert(ret, slots[trimmed])
		end
	end
	return table.concat(ret, ', ')
end

function addcategories(args, catargs)
	local ret = { 'All equipable items' }

 	-- Add the associated category if the parameter doesn't have content
 	local notdefined_args = {
 		image = 'Needs image',
 	}
	for n, v in pairs(notdefined_args) do
		if catargs[n] and catargs[n].all_defined == false then
			table.insert(ret, v)
		end
	end

	-- combine table and format category wikicode
	for i, v in ipairs(ret) do
		if (v ~= '') then
			ret[i] = string.format('[[Category:%s]]', v)
		end
	end

	return table.concat(ret, '')
end

return p