Module:Infobox Construction

From RuneRealm 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 Construction/doc. [edit]
Module:Infobox Construction's function main is invoked by Template:Infobox Construction.
Module:Infobox Construction requires Module:Infobox.
Module:Infobox Construction requires Module:Mainonly.

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

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

-- Main function called with invokes
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 = 'icon', func = 'has_content' },
		{ name = 'icon_smw', func = { name = image_smw, params = { 'icon' }, flag = 'p' } },
		{ name = 'furniturename', func = 'has_content' },
		{ name = 'release', func = 'release' },
		{ name = 'level', func = 'has_content' },
		{ name = 'level_smw', func = { name = numericarg, params = { 'level' }, flag = 'p' } },
		{ name = 'experience', func = { name = experiencearg, params = { 'experience' } } },
		{ name = 'experience_smw', func = { name = numericarg, params = { 'experience' } } },
		{ name = 'room', func = 'has_content' },
		{ name = 'hotspot', func = 'has_content' },
		{ name = 'flatpack', func = 'has_content' },
		{ name = 'actions', func = 'has_content' },
		{ name = 'examine', func = 'has_content' },

        { name = 'usesinfobox', func = { name = tostring, params = { 'Construction' }, flag = 'r' } },
		{ name = 'id', func = 'has_content' },
		{ name = 'id_smw', func = { name = idsmw, params = { 'id' }, flag = 'p' } },
		{ name = 'itemid', func = 'has_content' },
		{ name = 'item_id_smw', func = { name = idsmw, params = { 'itemid' }, flag = 'p' } },
		{ name = 'uses_skill', func = { name = tostring, params = { 'Construction' }, flag = 'r' } },
	}

	ret:defineLinks({ hide = true })
	
	ret:useSMWSubobject({
		image_smw = 'Image',
		icon_smw = 'Icon',
		level_smw = 'Construction level',
		experience_smw = 'Construction experience',
		uses_skill = 'Uses skill',
		usesinfobox = 'Uses infobox',
		id_smw = 'Object ID',
		item_id_smw = 'Item ID',
	})

	ret:create()
	ret:cleanParams()

	ret:customButtonPlacement(true)
	ret:addButtonsCaption()

	ret:defineName('Infobox Construction')

	ret:addRow{
		{ tag = 'argh', content = 'name', class='infobox-header', colspan = '20' }
	}
	:pad(20)
	:addRow{
		{ tag = 'argd', content = 'image', class='infobox-image infobox-full-width-content', colspan = '20' }
	}
	:pad(20)
	:addRow{
		{ tag = 'th', content = 'Icon', colspan = '7' },
		{ tag = 'argd', content = 'icon', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = 'Furniture name', colspan = '7' },
		{ tag = 'argd', content = 'furniturename', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = 'Released', colspan = '7' },
		{ tag = 'argd', content = 'release', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = '[[Construction|Level]]', colspan = '7' },
		{ tag = 'argd', content = 'level', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = '[[Experience]]', colspan = '7' },
		{ tag = 'argd', content = 'experience', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = 'Room', colspan = '7' },
		{ tag = 'argd', content = 'room', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = '[[Hotspot]]', colspan = '7' },
		{ tag = 'argd', content = 'hotspot', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = '[[Flatpack]]able', colspan = '7' },
		{ tag = 'argd', content = 'flatpack', colspan = '13' }
	}
	if ret:paramDefined('actions') then
		ret:addRow{
			{ tag = 'th', content = 'Actions', colspan = '7' },
			{ tag = 'argd', content = 'actions', colspan = '13' }
		}
	end
	ret:addRow{
		{ tag = 'th', content = 'Examine', colspan = '7' },
		{ tag = 'argd', content = 'examine', colspan = '13' }
	}
	:pad(20)
	: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 = '7' },
		{ tag = 'argd', content = 'id',  colspan = '13' },
		meta = {addClass = 'advanced-data'}
	}
	:addRow{
		{ tag = 'th', content = 'Icon Item ID', colspan = '7' },
		{ tag = 'argd', content = 'itemid',  colspan = '13' },
		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 experiencearg(arg)
	if not infobox.isDefined(arg) then
		return nil
	end

	local argNoCommas = string.gsub(arg, ',', '')
	if tonumber(argNoCommas) == nil then
		return arg
	else
		return string.format('<span class="infobox-quantity" data-val-each="%s"><span class="infobox-quantity-replace">%s</span> xp</span>', argNoCommas, arg)
	end
end

function numericarg(level)
	if infobox.isDefined(level) then
		local r = tonumber(level)
		return r
	end
	return nil
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

-- split items with multiple images for smw (e.g. [[File:Arrow 1.png]] [[File:Arrow 2.png]])
function image_smw(arg)
	local _img = {}
	for i in string.gmatch(arg, "[Ff]ile:.-%.png") do
		table.insert(_img, i)
	end
	if #_img == 0 then
		return nil
	end
	return table.concat(_img, '&&SPLITPOINT&&')
end

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

	-- 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',
		experience = 'Needs experience info',
		examine = 'Needs examine added',
		id = 'Needs ID',
		itemid = '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