Module:Sandbox/BrandonXLF/1

This is an old revision of this page, as edited by BrandonXLF (talk | contribs) at 03:16, 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, floorNum, partNum)
	local out = '<div style="display: grid; grid-template-columns: max-content 1fr;">'
	
	if floorPart.name then
		out = out .. '<div>' .. floorPart.name .. '</div>'
		out = out .. '<div>' .. floorPart.desc .. '</div>'
	elseif floorPart.platform then
		out = out .. '<div style="grid-column: 1 / 3;">' .. floorPart.platform .. '</div>'
	else
		local prefix = floorNum .. '_' .. partNum .. '_'
		
		out = out .. 'Invalid floor part! Must have either <code>' .. prefix .. 'name</code> or <code>' .. prefix .. 'platform</code>.'
	end
	
	return out .. '</div>'
end

local function displayFloor(stationFloor, num)
	local out = '<div style="display: grid; grid-template-columns: max-content 1fr;">'
	
	out = out .. '<div>' .. stationFloor.letter .. '</div>'
	
	out = out .. '<div>'

	for i = 1, stationFloor.max do
		out = out .. displayFloorPart(stationFloor[i], num, i)
	end
	
	return out .. '</div></div>'
end

local function addArg(tbl, num)
	if num > tbl.max then
		tbl.max = num
	end
	
	if not tbl[num] then
		tbl[num] = { max = 0 }
	end
end
	
local function processArg(out, level, part, service, param, value)
	level = tonumber(level)
	part = tonumber(part)
	service = service and tonumber(service) or nil
	
	addArg(out, level)
	addArg(out[level], part)
	
	if service then
		addArg(out[level][part], service)
		out[level][part][service][param] = value
	else
		out[level][part][param] = value
	end
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, part, service, param = i:match('(%d+)_(%d+)_?(%d*)_(.+)')
			
			if level then
				processArg(out, level, part, service, 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