Module:Infobox Scenery

From Roat Pkz
Jump to navigation Jump to search

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

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

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

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

	ret:defineParams{
		{ name = 'name', func = 'name' },
		{ name = 'image', func = 'image' },
		{ name = 'image_smw', func = { name = image_smw, params = { 'image' }, flag = 'p' } },
		{ name = 'release', func = 'release' },
		{ name = 'removal', func = 'removal' },
		{ name = 'aka', func = 'has_content' },
		{ name = 'members', func = 'has_content' },
		{ name = 'quest', func = 'has_content' },
		{ name = 'location', func = 'has_content' },
		{ name = 'options', func = 'has_content' },
		{ name = 'respawn', func = respawnarg },
		{ name = 'icon', func = 'has_content' },
		{ name = 'examine', func = 'has_content' },
		{ name = 'map', func = maparg },

        { name = 'usesinfobox', func = { name = tostring, params = { 'Scenery' }, flag = 'r' } },
		{ name = 'id', func = 'has_content' },
		{ name = 'id_smw', func = { name = idsmw, params = { 'id' }, flag = 'p' } },
		{ name = 'npcid', func = 'has_content' },
		{ name = 'npc_id_smw', func = { name = idsmw, params = { 'npcid' }, flag = 'p' } },
	}

	ret:defineLinks({ hide = true })

	local smw_mapping = {
		image_smw = 'Image',
		release = 'Release date',
		members = 'Is members only',
		id_smw = 'Object ID',
		npc_id_smw = 'NPC ID',
		usesinfobox = 'Uses infobox',
	}
	local smw_all_mapping = {}
	for param, property_name in pairs(smw_mapping) do
		smw_all_mapping[param] = 'All '..property_name
	end
	ret:useSMWSubobject(smw_mapping)
	ret:useSMWOne(smw_all_mapping)

	ret:setMaxButtons(8)
	ret:create()
	ret:cleanParams()

	ret:customButtonPlacement(true)
	ret:addButtonsCaption()

	ret:defineName('Infobox Scenery')
	ret:addClass('infobox-npc')

	ret:addRow{
		{ tag = 'argh', content = 'name', class='infobox-header', colspan = '20' }
	}
	:pad(20)

	ret:addRow{
		{ tag = 'argd', content = 'image', class='infobox-image infobox-full-width-content', colspan = '20' }
	}
	:pad(20)

	ret:addRow{
		{ tag = 'th', content = 'Released', colspan = '8' },
		{ tag = 'argd', content = 'release', colspan = '12' }
	}

	if ret:paramDefined('removal') then
		ret:addRow{
			{ tag = 'th', content = 'Removal', colspan = '8' },
			{ tag = 'argd', content = 'removal', colspan = '12' }
		}
	end

	if ret:paramDefined('aka') then
		ret:addRow{
			{ tag = 'th', content = 'AKA', colspan = '8' },
			{ tag = 'argd', content = 'aka', colspan = '12' }
		}
	end

	
	if ret:paramDefined('quest', 'all') then
		ret:addRow{
			{ tag = 'th', content = 'Quest', colspan = '8' },
			{ tag = 'argd', content = 'quest', colspan = '12' }
		}
	end
	
	ret:addRow{
		{ tag = 'th', content = 'Location', colspan = '8' },
		{ tag = 'argd', content = 'location', colspan = '12' }
	}
	
	if ret:paramDefined('respawn') then
		ret:addRow{
			{ tag = 'th', content = 'Respawn time', colspan = '8' },
			{ tag = 'argd', content = 'respawn', colspan = '12' }
		}
	end
	
	if ret:paramDefined('icon', 'all') then
		ret:addRow{
			{ tag = 'th', content = 'Map icon', colspan = '8' },
			{ tag = 'argd', content = 'icon', colspan = '12' }
		}
	end

	if ret:paramDefined('options') then
		ret:addRow{
			{ tag = 'th', content = 'Options', colspan = '8' },
			{ tag = 'argd', content = 'options', colspan = '12' }
		}
	end

	ret:addRow{
		{ tag = 'th', content = 'Examine', colspan = '8' },
		{ tag = 'argd', content = 'examine', colspan = '12' }
	}
	:pad(20)

	local map_defined = ret:paramGrep('map', function(x) return (x or 'N/A') ~= 'N/A' end)
	if map_defined then
		ret:addRow{
			{ tag = 'th', content = 'Map', class = 'infobox-subheader', colspan = '20' }
		}
		:addRow{
			{ tag = 'argd', content = 'map', colspan = '20', class = 'infobox-full-width-content infobox-image' }
		}
	end

	ret:addRow{
		{ tag = 'th', content = 'Advanced data', class = 'infobox-subheader', colspan = '20' },
		meta = {addClass = 'advanced-data'}
	}
	:pad(20, 'advanced-data')
	:addRow{
		{ tag = 'th', content = 'Object ID', colspan = '8' },
		{ tag = 'argd', content = 'id',  colspan = '12' },
		meta = {addClass = 'advanced-data'}
	}
	if ret:paramDefined('npcid') then
		ret:addRow{
			{ tag = 'th', content = 'NPC ID', colspan = '8' },
			{ tag = 'argd', content = 'npcid',  colspan = '12' },
			meta = {addClass = 'advanced-data'}
		}
	end
	ret: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

function respawnarg(arg)
	if not infobox.isDefined(arg) then
		return nil
	end

	-- if arg is a valid number, display ticks and seconds
	if tonumber(arg) then
		local plural = tonumber(arg) ~= 1 and 's' or ''
		return arg .. ' tick' .. plural .. ' (' .. arg * 0.6 .. ' seconds)'
	end

	-- if arg isn't a number, return it unmodified
	return arg
end

function maparg(arg)
	if not infobox.isDefined(arg) then
		return nil
	end

	if string.lower(arg) == 'no' then
		return 'N/A'
	end

	return arg
end

function image_smw(arg)
	local _img = {}
	for i in string.gmatch(arg, "[Ff]ile:.-%.png") do
		table.insert(_img, i)
	end
	for i in string.gmatch(arg, "[Ff]ile:.-%.gif") do
		table.insert(_img, i)
	end
	if #_img == 0 then
		return nil
	end
	return table.concat(_img, '&&SPLITPOINT&&')
end

-- Filter non-numerical IDs and separate with splitpoint for use in SMW
-- Example: '123 ,hist234, 456' => '123&&SPLITPOINT&&456'
function idsmw(id)
	if not infobox.isDefined(id) then
		return nil
	end
	local res = {}
	for id_i in string.gmatch(id, "[^,]+") do
		local trimmed = id_i:gsub("^%s*(.-)%s*$", "%1")
		if tonumber(trimmed) then
			table.insert(res, trimmed)
		end
	end
	return table.concat(res, '&&SPLITPOINT&&')
end

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

 	-- Add the associated category if the parameter has content
	local defined_args = {
		aka = 'Pages with AKA',
	}
	for n, v in pairs(defined_args) do
		if catargs[n] and catargs[n].one_defined then
			table.insert(ret, v)
		end
	end

 	-- Add the associated category if the parameter doesn't have content
 	local notdefined_args = {
 		image = '',
 		map = '',
 		members = '',
 		release = '',
 		examine = '',
 		options = '',
 		id = ''
 	}
	for n, v in pairs(notdefined_args) do
		if catargs[n] and catargs[n].all_defined == false then
			table.insert(ret, v)
		end
	end
	
	if args['options'] and catargs['options'].one_defined then
		if string.lower(args['options'].d or '') == 'none' then
			table.insert(ret, '')
		else
			table.insert(ret, '')
		end
	end
	
	local cat_map = {
		-- Parameters that have text
		-- map a category to a value
		matches = {
			members = { yes = '', no = '' },
		}
	}
	
	-- searches
	for n, v in pairs(cat_map.matches) do
		for m, w in pairs(v) do
			if args[n] then
				if 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.lower(tostring(x)) == m then
							table.insert(ret, w)
						end
					end
				end
			end
		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