Module:Sandbox/Sound table

Jump to navigation Jump to search
Documentation[create] [refresh]
This module has no documentation. If you know how to use this module, please create it.
local p = {}

-- Raw variables
local emptyevent = [=[''None''&#x200B;<ref group="sound" name="emptyevent">Empty events do not have this parameter set.</ref>]=]
local nosubtitle = [=[''None''&#x200B;<ref group="sound" name="nosubtitle">[https://bugs.mojang.com/issues/?jql=project%20%3D%20MC%20AND%20resolution%20%3D%20Unresolved%20AND%20text%20~%20%22sound%20missing%22 Reports on Mojira].</ref>]=]

-- Variables
local typeHead = '' -- args.type

-- Rowspan autocalculation
local rowspanIndex
local rowspans

-- Current namespace for categories
local curNs = mw.title.getCurrentTitle().namespace
local nocat = false

-- Add category
local function addCategory( categoryName )
	if (curNs == 0) or (curNs == 10) or (curNs == 10000) or (curNs == 10002) or (curNs == 10004) and (not nocat) then
		return '[[Category:Pages missing ' .. categoryName .. ']]'
	end
	
	return ''
end

-- Takes all args and puts them into a json string
function p.printArgs(frame)
	local args = require( 'Module:ProcessArgs' ).norm( frame:getParent().args )
	args["errorCat"] = "[[Category:Pages with broken sound tables]]" -- Add error category when {{SoundLine}} isn't wrapped by {{SoundTable}}
	return mw.text.jsonEncode(args)
end

-- Deserialize the templates inside wrapper
function deserialize(args)
	local e
	local out = {}
	for k, v in pairs(args) do
		e, out[k] = pcall( mw.text.jsonDecode, v )
		if not e then
			local errorCat = ''
			local errorMsg = mw.message.new( 'scribunto-common-error-category' )
			if not errorMsg:isDisabled() then
				errorCat = '[[Category:'..errorMsg:plain()..']]'
			end
			out[k] = { description = v..' <strong class="error">Lua error: '..out[k]..errorCat..' Input:'.. v ..'</strong>' }
		end
	end
	return out
end

-- Processes lines
--[[
f - frame, access to parser for parsing stuff
args - arguments/parameters
prnt - the result string
]]
local function processLine(f, args, prnt)
	
	prnt('<tr>')
	
	if args.sound and headConfig[1] == true then
		local sound = ''
		
		if args.sound == '-' then
			sound = "''None''"
		elseif args.sound == nil then
			sound = '?' .. addCategory( 'sounds' )
		else
			local sounds = { args['sound'] }
			for count = 2, 20 do
				local index = 'sound' .. count
				if args[index] then
					table.insert( sounds, args[index] )
				end
			end
			
			for i, soundarg in ipairs( sounds ) do
				sound = sound .. "<span class=\"sound\"><span class=\"sound-title\"></span>[[File:".. soundarg .."|class=sound-audio]]</span>"
			end
			
		end
		
		prnt('<td rowspan="')
		--.. rowspan .. 

		rowspanIndex = rowspanIndex + 1
		if rowspans[rowspanIndex] == '?' then rowspans[rowspanIndex] = 0 end
		prnt((rowspans[rowspanIndex] or 0) + 1)

		prnt('">' .. sound .. '</td>')
	else
		rowspans[rowspanIndex] = rowspans[rowspanIndex] + 1
	end
	
	if headConfig[2] == true then
		local subtitle = args.subtitle
		if subtitle == '-' then
			subtitle = f:preprocess( nosubtitle )
		elseif args.sound == '-' and subtitle == nil then
			subtitle = f:preprocess( emptyevent )
		elseif args.translationkey == '-' then
			local content = f:expandTemplate{ title = 'bug', args = { args.subtitle } }
			subtitle = "''None''" .. '&#x200B;' .. f:extensionTag( 'ref', '&#x200B;' .. content .. '&#x200B;', { group = 'sound', name = 'bug' .. args.subtitle } ) .. '&#x200B;'
		elseif args.subtitle ~= nil then
			subtitle = '&#x200B;' .. args.subtitle
		else
			subtitle = '&#x200B; ?' .. addCategory( 'subtitles' )
		end

		prnt('<td>' .. tostring(subtitle) .. '</td>')
	end
	
	if headConfig[3] == true then
		local source = '?' .. addCategory( 'sound source' )
		if args.overridesource then
			source = args.source
		elseif args.source == 'master' then
			source = 'Master Volume'
		elseif args.source == 'music' then
			source = 'Music'
		elseif args.source == 'record' then
			source = 'Jukebox/Note Blocks'
		elseif args.source == 'weather' then
			source = 'Weather'
		elseif args.source == 'hostile' then
			source = 'Hostile Creatures'
		elseif args.source == 'neutral' then
			source = 'Friendly Creatures'
		elseif args.source == 'player' then
			source = 'Players'
		elseif args.source == 'block' then
			source = 'Blocks'
		elseif args.source == 'ambient' then
			source = 'Ambient/Environment'
		elseif args.source == 'voice' then
			source = 'Voice/Speech'
		elseif args.source == 'sound' then
			source = 'Sound'
		elseif args.source == 'dependent' then
			source = "''[[Sound#Entity-dependent categories|Entity-Dependent]]''"
		elseif args.source == 'none' or args.source == '-' then
			source = "''None''"
		end
		
		prnt('<td>' .. source .. '</td>')
	end
	
	if headConfig[4] == true then
		local desc = args.description or '?' .. addCategory( 'sound description' )
		prnt('<td>' .. desc .. '</td>')
	end
	
	if headConfig[5] == true then
		local id
	    if args.id then
	    	id = '<code>' .. args.id .. '</code>'
	    	id = id:gsub( '%.', '<wbr/>.')
	    else
	    	id = '?' .. addCategory( 'sound ID' )
	    end
	    if args.idnote then
	    	id = id .. '&#x200B;' .. args.idnote .. '&#x200B;'
	    end
	    
		prnt('<td>' .. id .. '</td>')
	end
	if headConfig[6] == true then
		local translationkey
		if args.sound == '-' and args.translationkey == nil then
			translationkey = f:preprocess( emptyevent )
		elseif args.subtitle == '-' then
			translationkey = f:preprocess( nosubtitle )
		elseif args.translationkey == '-' then
			local content = f:expandTemplate{ title = 'bug', args = { args.subtitle } }
			translationkey = "''None''" .. '&#x200B;' .. f:extensionTag( 'ref', '&#x200B;' .. content .. '&#x200B;', { group = 'sound', name = 'bug' .. args.subtitle } ) .. '&#x200B;'
		elseif string.lower(args.translationkey or '') == "none" then
			translationkey = "''None'' &#x200B;"
		elseif args.translationkey ~= nil then
			translationkey = '<code>' .. args.translationkey .. '</code>' .. '&#x200B;'
			translationkey = translationkey:gsub( '%.', '<wbr/>.')
		else
			translationkey = '&#x200B; ?' .. addCategory( 'subtitle key' )
		end
		if args.translationkeynote then
			translationkey = translationkey .. '&#x200B;' .. args.translationkeynote .. '&#x200B;'
		end
		prnt('<td>' .. translationkey .. '</td>')
	end
	if headConfig[7] == true then
		local volume = args.volume or '?' .. addCategory( 'sound volume' )
		prnt('<td>' .. volume .. '</td>') 
	end
	if headConfig[8] == true then
		local pitch = args.pitch or '?' .. addCategory( 'sound pitch' )
		prnt('<td>' .. pitch .. '</td>')
	end
	if headConfig[9] == true then
		local distance = args.distance or '?' .. addCategory( 'sound attenuation distance' )
		prnt('<td>' .. distance .. '</td>')
	end
	
	prnt('</tr>')
end

-- Count rowspan
-- If argument isn't nil, then it moves rowspanIndex one up. If it is, it adds one to the final rowspan of that rowspan index
function countRowspan(args)
	if args.sound ~= nil then
		rowspanIndex = rowspanIndex + 1
	end
	if args.sound == nil then
		rowspans[rowspanIndex] = (rowspans[rowspanIndex] or 0) + 1
	end
end

-- Main function
function p.main(frame)
	return p._main(frame, frame:getParent().args)
end

-- The dummy main function, do not call this one!
function p._main(f, args)
	local lines = deserialize(args)
	
	local result = {}
	
	if args.nocat and args.nocat ~= '' then nocat = true end
	
	rowspans = {}
    rowspanIndex = 0
    
	for _,v in ipairs(lines) do
		countRowspan(v)
	end

	rowspanIndex = 0

	typeHead = args['type'] or ''

	local _collapse = ''
	if (args['forcecollapsed'] and args['forcecollapsed'] ~= '') or typeHead == 'legends' then _collapse = 'collapsed' end
	table.insert(result, '<table class="wikitable collapsible '.. _collapse ..'" data-description="Sound">')

	headConfig = {} --[[
		[1]: Include Sound? (bool)
		[2]: Include Subtitles? (bool)
		[3]: Include Source? (bool)
		[4]: Include Description? (bool)
		[5]: Include Resource location? (bool)
		[6]: Include Translation key? (bool)
		[7]: Include Volume? (bool)
		[8]: Include Pitch? (bool)
		[9]: Include Attenuation distance? (bool)
	]]
    if typeHead == 'java' then
       headConfig = { true, true, true, true, true, true, true, true, true }
    elseif typeHead == 'bedrock' then
        headConfig = { true, false, true, true, true, false, true, true, false }
    elseif typeHead == 'dungeons' or typeHead == 'legends' or typeHead == 'earth' then
        headConfig = { true, false, false, true, true, false, false, false, false }
    elseif typeHead == 'historical' then
        headConfig = { true, false, true, true, true, false, true, true, true }
    else -- Simple
        table.insert(result, '')
        headConfig = { true, false, false, true, false, false, false, false, false }
	end

    local headerrow = mw.html.create( 'tr' )
	local colspan = 0
	for i, header in ipairs( headConfig ) do
		if header == true then				
			local h = headerrow:tag( 'th' )
			
			colspan = colspan + 1
			
			-- the "switch" statement in Lua
			if i == 1 then
				h:wikitext( 'Sound' )
			elseif i == 2 then
				h:wikitext( '[[Subtitles]]' )
			elseif i == 3 then
				h:wikitext( '<span class="explain" title="The category this sound falls into; what volume slider controls this sound in the Music and Sounds menu.">Source</span>' )
			elseif i == 4 then
				h:wikitext( '<span class="explain" title="When the game calls this sound event.">Description</span>' )
			elseif i == 5 then
				h:wikitext( '[[Resource location]]' )
			elseif i == 6 then
				h:wikitext( 'Translation key' )
			elseif i == 7 then
				h:wikitext( 'Volume' )
			elseif i == 8 then
				h:wikitext( 'Pitch' )
			elseif i == 9 then
				h:wikitext( 'Attenuation<br/>distance' )
			end
		end
	end

	local _title = args['title'] or 'Sounds'
	local _navbar = ''

	if args['templatepage'] then _navbar = '<div class="navbox-navbar">' .. f:expandTemplate{ title = 'Navbar', args = { args['templatepage'], mini="1" } } ..'</div>' end
	table.insert(result, '<tr><th colspan="'.. colspan ..'">'.. _navbar .. _title ..'</th></tr>' .. tostring(headerrow) .. '</tr>')

	for _,v in ipairs(lines) do
		processLine(f, v,function(a) table.insert(result, a) end)
	end
	table.insert(result, '</table>')

	local edition = ''
	
	if typeHead == 'historical' or typeHead == 'simple' then edition = typeHead else edition = '' .. string.upper(string.sub(typeHead, 1, 1 )) .. string.sub(typeHead, 2) end
	
	table.insert(result, '</table>[[Category:Pages with sound tables]][[Category:Pages with ' .. edition .. ' sound tables]]' .. f:expandTemplate{ title = 'notelist', args = { 'sound' } })
	return table.concat(result)
end
	
return p