Module:Infobox NPC

From Discord Dungeons Wiki
Jump to navigation Jump to search

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

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

local onmain = require('Module:Mainonly').on_main
local paramtest = require('Module:Paramtest')
local infobox = require('Module:Experimental/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 = 'members', func = 'has_content' },
		{ name = 'quest', func = 'has_content' },
		{ name = 'location', func = 'has_content' },
		{ name = 'shop', func = 'has_content' },
		{ name = 'gender', func = 'has_content' },
        { 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',
		members = 'Is members only',
		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

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

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

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

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