Module:Infobox Random Event

From the RuneScape Wiki, the wiki for all things RuneScape
Jump to navigation Jump to search
Module documentation
This documentation is transcluded from Module:Infobox Random Event/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:Infobox Random Event/doc. [edit]
Module:Infobox Random Event's function main is invoked by Template:Infobox Random Event.
Module:Infobox Random Event requires Module:Clean image.
Module:Infobox Random Event requires Module:Infobox.
Module:Infobox Random Event requires Module:Mainonly.
Module:Infobox Random Event requires Module:Paramtest.
Module:Infobox Random Event requires Module:Yesno.
Function list
L 13 — p.main
L 107 — imgarg
L 114 — maparg
L 131 — mapdisp
L 144 — addcategories

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

local onmain = require('Module:Mainonly').on_main
local yesno = require('Module:Yesno')
local paramtest = require('Module:Paramtest')
local infobox = require('Module:Infobox')
local cleanimg = require('Module:Clean image').clean

-- 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 = { name = imgarg, params = { 'image' }, flag = { 'd' } } },
		{ name = 'release', func = 'release' },
		{ name = 'removal', func = 'removal' },
		{ name = 'members', func = 'has_content' },
		{ name = 'location', func = 'has_content' },
		{ name = 'music', func = 'has_content' },
		{ name = 'map', func = { name = maparg, params = { 'map' }, flag = { 'd' } } },
		{ name = 'mapdisp', func = { name = mapdisp, params = { 'map' }, flag = { 'd' } }, dupes = true },
	}

	ret:defineLinks()

	ret:create()
	ret:cleanParams()
    
	ret:customButtonPlacement(true)							
	ret:addButtonsCaption()

	ret:defineName('Infobox Random Event')
	
	-- PARAMETER: name
	ret:addRow{
		{ tag = 'argh', content = 'name', class='infobox-header',  colspan = '4' }
	}

	-- PARAMETER: image
	ret:addRow{
		{ tag = 'argd', content = 'image', class='infobox-image infobox-full-width-content', colspan = '4' }
	}
	:pad(4)
	
	-- PARAMETER: release
	ret:addRow{
		{ tag = 'th', content = 'Released', colspan = '1' },
		{ tag = 'argd', content = 'release', colspan = '3' }
	}
	
	-- PARAMETER: removal
	if ret:paramDefined('removal') then
		ret:addRow{ 
			{ tag = 'th', content = 'Removal', colspan = '1' },
			{ tag = 'argd', content = 'removal', colspan = '3' } 
		}
	end
	
	-- PARAMETER: members
	ret:addRow{
		{ tag = 'th', content = '[[Members]]', colspan = '1' },
		{ tag = 'argd', content = 'members', colspan = '3' }
	}
	
	-- PARAMETER: location
	if ret:paramDefined('location') then
		ret:addRow{
			{ tag = 'th', content = 'Location', colspan = '1' },
			{ tag = 'argd', content = 'location', colspan = '3' }
		}
	end
	
	-- PARAMETER: music
	if ret:paramDefined('music') then
		ret:addRow{
			{ tag = 'th', content = 'Music', colspan = '1' },
			{ tag = 'argd', content = 'music', colspan = '3' }
		}
	end
	ret:pad(4)

	-- PARAMETER: map
	local map_defined  = ret:paramGrep('mapdisp', function(x) return (x or 'false') ~= 'false' end)
	if map_defined == true then
		ret:addRow{
			{ tag = 'th', content = 'Map', class = 'infobox-subheader', colspan = '4' }
		}
		ret:addRow{
			{ tag = 'argd', content = 'map', class = 'infobox-image infobox-full-width-content', colspan = '4' } }
	end
	
	ret:finish()
	
	if onmain() then
		local a1 = ret:param('all')
		local a2 = ret:categoryData()
		ret:wikitext(addcategories(a1, a2))
	end
	return ret:tostring()
end

function imgarg(arg)
	if infobox.isDefined(arg) then
		return cleanimg{ file = arg, width = 300, height = 300 }
	end
	return nil
end

function maparg(arg)
	if infobox.isDefined(arg) then
		local low = string.lower(arg)
		if yesno(low) == false or low == 'none' then
			return "''No map to display.''"
		elseif low == 'name' then
			return string.format('[[File:%s location.png]]', mw.title.getCurrentTitle().text)
		elseif string.find(low, '.png') then
			return cleanimg{ file = arg, width = 250 }
		else
			return arg
		end
	else
		return nil
	end
end

function mapdisp(map)
	if infobox.isDefined(map) then
		local yn_map = map == "''No map to display.''"
		if yn_map then
			return 'false'
		else
			return 'true'
		end
	else
		return 'false'
	end
end

function addcategories(args, catargs)
	local ret = { 'Random events' }
	
	local cat_map = {
		-- Added if the parameter has content
		defined = {
			
		},
		-- Add the associated category if the parameter doesn't have content
		notdefined = {
			image = 'Needs image',
			members = 'Needs members status',
			release = 'Needs release date',
			map = 'Needs map',
		},
	}
	
	-- Run and add mapped categories
	for n, v in pairs(cat_map.defined) do
		if catargs[n] and catargs[n].one_defined then
			table.insert(ret,v)
		end
	end
	for n, v in pairs(cat_map.notdefined) 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