Module:Sandbox/BrandonXLF/1

This is an old revision of this page, as edited by BrandonXLF (talk | contribs) at 03:01, 18 April 2023. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- Sandbox, do not delete

require('strict')

local p = {}

local function displayFloorPart(floorPart)
	if floorPart.name then
		return floorPart.name .. ' ' .. floorPart.desc
	elseif floorPart.dir then
		return floorPart.dir .. ' ' .. floorPart.desc
	else
		return 'Invalid floor part! Must have either <code>x_y_name</code> or <code>x_y_dir</code>.'
	end
end

local function displayFloor(stationFloor, number)
	local out = ''
	
	out = out .. stationFloor.letter

	for i = 1, stationFloor.max do
		out = out .. displayFloorPart(stationFloor[i])
	end
	
	return out
end
	
local function processArg(out, level, levelPart, param, value)
	local level = tonumber(level)
	local levelPart = tonumber(levelPart)
	
	if level > out.max then
		out.max = level
	end
	
	if not out[level] then
		out[level] = { max = 0 }
	end
	
	if levelPart > out[level].max then
		out[level].max = levelPart
	end
	
	if not out[level][levelPart] then
		out[level][levelPart] = {}
	end
	
	out[level][levelPart][param] = value
end
	
local function processArgs(args)
	local out = { max = 0 }
	
	for i, v in pairs(args) do
		if type(i) == 'number' then
			local level = tonumber(i)
			
			if not out[level] then
				out[level] = { max = 0 }
			end
			
			out[level].letter = v
		else
			local level, levelPart, param = i:match('(%d+)_(%d+)_(.+)')
			
			if level then
				processArg(out, level, levelPart, param, v)
			end
		end
	end
	
	return out
end


function p.main(frame)
	local sortedArgs = processArgs(frame.args)
	local out = ''
	
	for i = 1, sortedArgs.max do
		out = out .. displayFloor(sortedArgs[i], i)
	end
	
	return out
end

return p