Module:Infobox NPC

From Old School Near-Reality 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 for [[Template:Infobox NPC]]
------------------------
local p = {}

local onmain = require('Module:Mainonly').on_main
local paramtest = require('Module:Paramtest')
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 = 'level', func = 'has_content' },
		{ name = 'race', func = 'has_content' },
		{ name = 'location', func = 'has_content' },
		{ 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' } },
	}

	ret:defineLinks({ hide = true })

	local smw_mapping = {
		release = 'Release date',
		image_smw = 'Image',
		id_smw = 'NPC ID',
		examine = 'Examine',
		location = 'NPC location',
		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: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
	
	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('shop') then
		ret:addRow{
			{ tag = 'th', content = '[[Shop]]', colspan = '8' },
			{ tag = 'argd', content = 'shop', 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'}
	}
	: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 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',
	}
	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',
		release = 'Needs release date',
		examine = 'Needs examine added',
		id = 'Needs ID',
		map = 'Needs map',
		options = 'Needs options',
	}
	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