Module:Infobox Item

From RuneScape Classic Wiki
Jump to: navigation, search

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

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

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

function p.main(frame)
	local args = frame:getParent().args
	local ret = infobox.new(args)

	ret:defineParams{
		{ name = 'version', func = 'has_content' },
		{ name = 'name', func = 'name' },
		{ name = 'image', func = 'image' },

		{ name = 'release', func = 'release' },
		{ name = 'members', func = 'has_content' },
		{ name = 'quest', func = 'has_content' },

		{ name = 'tradeable', func = 'has_content' },
		{ name = 'equipable', func = 'has_content' },
		{ name = 'stackable', func = 'has_content' },
		{ name = 'options', func = 'has_content' },
		{ name = 'cert', func = 'has_content' },
		{ name = 'examine', func = 'has_content' },
		
		{ name = 'raw_value', func = { name = valraw, params = { 'value' }, flag = 'p' } },
		{ name = 'value', func = { name = valuearg, params = { 'raw_value' } } },
		{ name = 'high', func = { name = alchvalues, params = { 'raw_value', 0.6}, flag = { 'd', 'r' } } },
		{ name = 'low', func = { name = alchvalues, params = { 'raw_value', 0.4}, flag = { 'd', 'r' } } },
		{ name = 'high_smw', func = { name = alchvalues_smw, params = { 'raw_value', 0.6}, flag = { 'd', 'r' } } },
		
		{ name = 'id', func = 'has_content' },
		{ name = 'id_smw', func = { name = idsmw, params = { 'id' }, flag = 'p' } },
	}

	ret:useSMWSubobject({
		version = 'Version anchor',
		raw_value = 'Value',
		high_smw = 'High Alchemy value',
		members = 'Is members only',
		id_smw = 'Item ID',
	})

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

	ret:defineLinks({ hide = true })


	ret:addButtonsCaption()

	ret:defineName('Infobox Item')
	ret:addClass('infobox-item')

	ret:addRow{
		{ tag = 'argh', content = 'name', class='infobox-header', colspan = '20' }
	}
	:pad(20)
	:addRow{
		{ tag = 'argd', content = 'image', class = 'infobox-image inventory-image infobox-full-width-content', colspan = '20' }
	}
	:pad(20)
	:addRow{
		{ tag = 'th', content = 'Released', colspan = '7' },
		{ tag = 'argd', content = 'release', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = '[[Members]]', colspan = '7' },
		{ tag = 'argd', content = 'members', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = '[[Quest items|Quest item]]', colspan = '7' },
		{ tag = 'argd', content = 'quest', colspan = '13' }
	}
	:pad(20)
	:addRow{
		{ tag = 'th', content = 'Properties', class = 'infobox-subheader', colspan = '20' }
	}
	:pad(20)
	:addRow{
		{ tag = 'th', content = '[[Trade|Tradeable]]', colspan = '7' },
		{ tag = 'argd', content = 'tradeable', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = '[[Equipment|Equipable]]', colspan = '7' },
		{ tag = 'argd', content = 'equipable', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = '[[Item|Stackable]]', colspan = '7' },
		{ tag = 'argd', content = 'stackable', colspan = '13' }
	}
	if ret:paramDefined('options') then
		ret:addRow{
			{ tag = 'th', content = 'Options', colspan = '7' },
			{ tag = 'argd', content = 'options', colspan = '13' }
		}
	end
	if ret:paramDefined('cert') then
		ret:addRow{
			{ tag = 'th', content = '[[Certificate]]', colspan = '7' },
			{ tag = 'argd', content = 'cert', colspan = '13' }
		}
	end
	ret:addRow{
		{ tag = 'th', content = '[[Examine]]', colspan = '7' },
		{ tag = 'argd', content = 'examine', colspan = '13' }
	}
	:pad(20)
	:addRow{
		{ tag = 'th', content = 'Values', class = 'infobox-subheader', colspan = '20' }
	}
	:pad(20)
	:addRow{
		{ tag = 'th', content = 'Value', colspan = '7' },
		{ tag = 'argd', content = 'value', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = '[[High level alchemy|High alch]]', colspan = '7' },
		{ tag = 'argd', content = 'high', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = '[[Low level alchemy|Low alch]]', colspan = '7' },
		{ tag = 'argd', content = 'low', colspan = '13' }
	}
	:pad(20)
	:addRow{
		{ tag = 'th', content = 'Advanced data', class = 'infobox-subheader', colspan = '20' },
		meta = {addClass = 'advanced-data'}
	}
	:pad(20, 'advanced-data')
	:addRow{
		{ tag = 'th', content = 'Item ID', colspan = '7' },
		{ tag = 'argd', content = 'id',  colspan = '13' },
		meta = {addClass = 'advanced-data'}
	}
	:pad(20, 'advanced-data')

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

	return ret:tostring()
end

-- Return raw value as a number, or nil if not defined
function valraw(arg)
	if not infobox.isDefined(arg) then
		return nil
	end

	return tonumber(arg)
end

function valuearg(value)
	if not infobox.isDefined(value) then
		return nil
	end

	return plural('coin', value)
end

function alchvalues(value, multiplier)
	if not infobox.isDefined(value) then
		return nil
	end

	local alch_value = math.floor(value * multiplier)
	return plural('coin', alch_value)
end

function alchvalues_smw(value, multiplier)
	if not infobox.isDefined(value) then
		return nil
	end

	return math.floor(value * multiplier)
end

function idsmw(id)
	if not infobox.isDefined(id) then
		return nil
	end
	return string.gsub(id, ',', '&&SPLITPOINT&&')
end

function plural(word, amount, alt_plural_word)
	local output_amount = commas(tonumber(amount) or 1)
	if tonumber(amount) == 1 then
		return string.format('%s %s', output_amount, word)
	elseif alt_plural_word then
		return string.format('%s %s', output_amount, alt_plural_word)
	else
		return string.format('%s %ss', output_amount, word)
	end
end

function addcategories(args, catargs)
	local ret = { 'Items' }
	local cat_map = {
		-- Added if the parameter has no content
		notdefined = {
			image = 'Needs image',
			members = 'Needs members status',
			release = 'Needs release date',
			examine = 'Needs examine added',
			value = 'Items missing value',
			quest = 'Items missing quest',
			id = 'Needs ID',
			},
		-- Parameters that have text
		-- map a category to a value
		grep = {
			members = { yes = 'Members\' items', no = 'Free-to-play items' },
			stackable = { yes = 'Stackable items' },
			equipable = { yes = 'Equipable items' },
			tradeable = { yes = 'Tradeable items', no = 'Untradeable items' },
		}
	}

	-- undefined categories
	for n, v in pairs(cat_map.notdefined) do
		if catargs[n] and catargs[n].all_defined == false then
			table.insert(ret, v)
		end
	end

	-- searches
	for n, v in pairs(cat_map.grep) do
		for m, w in pairs(v) do
			if args[n] then
				if string.find(string.lower(tostring(args[n].d) or ''), m) then
					table.insert(ret, w)
				end
				if args[n].switches then
					for _, x in ipairs(args[n].switches) do
						if string.find(string.lower(tostring(x)), m) then
							table.insert(ret, w)
						end
					end
				end
			end
		end
	end

	-- quest items
	-- just look for a link
	if args.quest.d:find('%[%[') then
		table.insert(ret, 'Quest items')
	elseif args.quest.switches then
		for _, v in ipairs(args.quest.switches) do
			if v:find('%[%[') then
				table.insert(ret, 'Quest items')
				break
			end
		end
	end

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

	return table.concat(ret, '')
end

return p