Module:Infobox NPC

From Old School RuneScape Wiki
Jump to navigation Jump to search
Module documentation
This documentation is transcluded from Template:No documentation/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:Infobox NPC/doc. [edit]
Module:Infobox NPC's function main is invoked by Template:Infobox NPC.
Module:Infobox NPC requires Module:Infobox.
Module:Infobox NPC requires Module:Mainonly.
Module:Infobox NPC requires Module:Paramtest.
Module:Infobox NPC requires Module:Trailblazer Region.

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

local onmain = require('Module:Mainonly').on_main
local paramtest = require('Module:Paramtest')
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 = 'level', func = 'has_content' },
		{ name = 'members', func = 'has_content' },
		{ name = 'race', 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 = 'shop', func = 'has_content' },
		{ name = 'gender', func = 'has_content' },
		{ name = 'options', func = 'has_content' },
		{ name = 'examine', func = 'has_content' },
		{ name = 'map', func = maparg },

        { name = 'usesinfobox', func = { name = tostring, params = { 'NPC' }, flag = 'r' } },
		{ name = 'id', func = 'has_content' },
		{ name = 'id_smw', func = { name = idsmw, params = { 'id' }, flag = 'p' } },
		{ name = 'twisted', func = 'has_content' },
		{ name = 'twisteddisp', func = { name = twistedarg, params = { 'twisted' }, flag = 'p' } },
	}

	ret:defineLinks({ hide = true })

	local smw_mapping = {
		release = 'Release date',
		image_smw = 'Image',
		id_smw = 'NPC ID',
		members = 'Is members only',
		location = 'NPC location',
		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 NPC')
	ret:addClass('infobox-npc')

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

	:addRow{
		{ tag = 'argd', content = 'image', colspan = '20', class = 'infobox-full-width-content' }
	}
	: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 = 'Also called', colspan = '8' },
			{ tag = 'argd', content = 'aka', colspan = '12' }
		}
	end

	if ret:paramDefined('level') then
		ret:addRow{
			{ tag = 'th', content = '[[Combat level]]', colspan = '8' },
			{ tag = 'argd', content = 'level', colspan = '12' }
		}
	end

	ret:addRow{
		{ tag = 'th', content = '[[Members]]', colspan = '8' },
		{ tag = 'argd', content = 'members', colspan = '12' }
	}
	
	if ret:paramDefined('race') then
		ret:addRow{
			{ tag = 'th', content = '[[Races|Race]]', colspan = '8' },
			{ tag = 'argd', content = 'race', colspan = '12' }
		}
	end

	ret:addRow{
		{ tag = 'th', content = '[[Locations|Location]]', colspan = '8' },
		{ tag = 'argd', content = 'location', colspan = '12' }
	}

	--if ret:paramDefined('trailblazerRegion') then
	--	ret:addRow{
	--		{ tag = 'th', content = '[[Trailblazer]] region', colspan = '8' },
	--		{ tag = 'argd', content = 'trailblazerRegion', colspan = '12' }
	--	}
	--end

	if ret:paramDefined('shop') then
		ret:addRow{
			{ tag = 'th', content = '[[Shop|Sells items]]', colspan = '8' },
			{ tag = 'argd', content = 'shop', colspan = '12' }
		}
	end

	if ret:paramDefined('quest') then
		ret:addRow{
			{ tag = 'th', content = '[[Quest]]', colspan = '8' },
			{ tag = 'argd', content = 'quest', colspan = '12' }
		}
	end

	if ret:paramDefined('gender') then
		ret:addRow{
			{ tag = 'th', content = '[[Gender]]', colspan = '8' },
			{ tag = 'argd', content = 'gender', colspan = '12' }
		}
	end

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

	ret:addRow{
		{ tag = 'th', content = '[[Examine]]', colspan = '8' },
		{ tag = 'argd', content = 'examine', colspan = '12' }
	}

	ret: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 = 'NPC ID', colspan = '8' },
		{ tag = 'argd', content = 'id',  colspan = '12' },
		meta = {addClass = 'advanced-data'}
	}
	if ret:paramDefined('twisteddisp', 'all') 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 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 = string.match(arg, "File:.-%.png")
	return _img
end

local idsmw_nil_ids = {
	removed = true,
	no = true
}

function idsmw(id)
	if infobox.isDefined(id) then
		if idsmw_nil_ids[string.lower(tostring(id))] then
			return nil
		end
		local r = string.gsub(id, ',', '&&SPLITPOINT&&')
		return r
	end
	return nil
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 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 addcategories(args, catargs)
	local ret = { 'Non-player characters' }

	-- Add the associated category if the parameter has content
	local defined_args = {
		aka = 'Pages with AKA',
		shop = 'Merchants',
		quest = 'Quest NPCs'
	}
	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',
		members = 'Needs members status',
		release = 'Needs release date',
		examine = 'Needs examine added',
		id = 'Needs ID',
		map = 'Needs map',
		options = 'Needs options',
	--	trailblazerRegion = 'Needs Trailblazer region',
	}
	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['twisted'] then
		if string.lower(args['twisted'].d or '') == 'yes' then
			table.insert(ret, 'NPCs 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, 'NPCs accessible in Twisted League')
					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

	-- 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