Module:Sandbox/User:Riblet15/Infobox Scenery

From Old School RuneScape Wiki
Jump to navigation Jump to search
Module documentation
This documentation is transcluded from Template:Module sandbox/doc. [edit] [history] [purge]
Module:Sandbox/User:Riblet15/Infobox Scenery requires Module:Infobox.
Module:Sandbox/User:Riblet15/Infobox Scenery requires Module:Mainonly.
Module:Sandbox/User:Riblet15/Infobox Scenery requires Module:Trailblazer Region.

This module is a sandbox for Riblet15. It can be used to test changes to existing modules, prototype new modules, or just experimenting with lua features.

Invocations of this sandbox should be kept in userspace; if the module is intended for use in other namespaces, it should be moved out of the sandbox into a normal module and template.

This default documentation can be overridden by creating the /doc subpage of this module, as normal.

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

local onmain = require('Module:Mainonly').on_main
local infobox = require('Module:Infobox')
local tb = require('Module:Trailblazer Region')._main
VariablesLua = mw.ext.VariablesLua

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 = 'trailblazerRegion', func = trailblazerarg },
		{ name = 'trailblazerRegion_smw', func = { name = 'has_content', params = { 'trailblazerRegion' }, flag = 'p' } },
		{ name = 'options', func = 'has_content' },
		{ name = 'respawn', func = respawnarg },
		{ 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' } },
		{ name = 'twisted', func = 'has_content' },
		{ name = 'twisteddisp', func = { name = twistedarg, params = { 'twisted' }, flag = 'p' } },
	}

	ret:defineLinks({ hide = true })

	local smw_mapping = {
		image_smw = 'Image',
		members = 'Is members only',
		id_smw = 'Object ID',
		npc_id_smw = 'NPC ID',
		usesinfobox = 'Uses infobox',
		trailblazerRegion_smw = 'Trailblazer Region',
	}
	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: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

	ret:addRow{
		{ tag = 'th', content = '[[Members]]', colspan = '8' },
		{ tag = 'argd', content = 'members', colspan = '12' }
	}
	:addRow{
		{ tag = 'th', content = '[[Quest]]', colspan = '8' },
		{ tag = 'argd', content = 'quest', colspan = '12' }
	}
	:addRow{
		{ tag = 'th', content = '[[Locations|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('trailblazerRegion') then
	--	ret:addRow{
	--		{ tag = 'th', content = '[[Trailblazer]] region', colspan = '8' },
	--		{ tag = 'argd', content = 'trailblazerRegion', 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
	if ret:paramDefined('twisteddisp') then
		ret:addRow{
			{ tag = 'th', content = '[[Twisted League]]', colspan = '8' },
			{ tag = 'argd', content = 'twisteddisp', 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 twistedarg(arg)
	if not infobox.isDefined(arg) then
		return nil
	end

	if string.lower(arg or '') == 'yes' then
		return "[[File:Twisted League icon.png|20x20px|This content is present in Twisted League.|link=Twisted League]] Yes"
	else
		return 'No'
	end
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 trailblazerarg(arg)
	if not infobox.isDefined(arg) then
		return nil
	end
	
	if string.lower(arg) == 'no' then
		return 'N/A'
	end
	
	ret = {}
	for region in string.gmatch(arg, "[^,]+") do
		local trimmed = region:gsub("^%s*(.-)%s*$", "%1")
		table.insert(ret, tb(trimmed))
	end
	return table.concat(ret, '')
end

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

 	-- 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 = 'Needs image',
 		map = 'Needs map',
 		members = 'Needs members status',
 		release = 'Needs release date',
 		examine = 'Needs examine added',
 		options = 'Needs options',
 		id = 'Needs 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, 'Non-interactive scenery')
		else
			table.insert(ret, 'Interactive scenery')
		end
	end
	
	if args['twisted'] then
		if string.lower(args['twisted'].d or '') == 'yes' then
			table.insert(ret, 'Items accessible in Twisted League')
			VariablesLua.vardefine('is_twisted', 'Yes')
		end
		if args['twisted'].switches then
			for i, twisted_i in ipairs(args['twisted'].switches) do
				if string.lower(twisted_i or '') == 'yes' then
					table.insert(ret, 'Items accessible in Twisted League')
					if args['version'] then
						if args['version'].switches then
							local suffix = args['version'].switches[i] or i
							VariablesLua.vardefine('is_twisted_'..suffix, 'Yes')
							VariablesLua.vardefine('is_twisted', 'Yes')
						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