Module:StrangeCubeTable

From A KoL Wiki

Documentation for this module may be created at Module:StrangeCubeTable/doc

local p = {}

-- Fisher–Yates shuffle
local function shuffle(t)
	for i = #t, 2, -1 do
		local j = math.random(i)
		t[i], t[j] = t[j], t[i]
	end
end

function p.main(frame)
	math.randomseed(os.time())

	-- Fill cells with digits 3 to 14
	local cells = {}
	for n = 3, 14 do
		table.insert(cells, n)
	end

	-- Suffixes
	local suffix = { 'bl','br','bt','lb','lr','lt','rb','rl','rt','tb','tl','tr' }

	-- Replace first 12 with random-suffixed files
	for i = 1, 12 do
		local s = suffix[math.random(#suffix)]
		local num = tostring(cells[i])
		cells[i] = string.format('[[File:%s%s.gif|Piece %s%s]]', num, s, num, s)
	end

	-- Push the three fixed pieces
	table.insert(cells, '[[File:1hr.gif|Piece 1hr]]')
	table.insert(cells, '[[File:2lr.gif|Piece 2lr]]')
	table.insert(cells, '[[File:15te.gif|Piece 15te]]')

	-- Shuffle cells
	shuffle(cells)

	-- Unshift empty at the front
	table.insert(cells, 1, '')

	-- Build HTML with mw.html
	local html = mw.html.create('table')
		:css('margin-left', 'auto')
		:css('margin-right', 'auto')
		:tag('tr')
			:tag('td')
				:tag('div') -- container
					:done() -- we'll keep a handle to the container next
	local container = html

	for i = 0, 3 do
		local row = container:tag('div'):css('clear', 'both')
		for j = 0, 3 do
			local c = i * 4 + j + 1 -- +1 for Lua indexing and leading ''
			row:tag('div')
				:css('text-align', 'center')
				:css('width', '50px')
				:css('height', '50px')
				:css('border', '1px solid black')
				:css('float', 'left')
				:css('margin', '2px 0 0 2px')
				:wikitext(cells[c] or '')
				:done()
		end
	end

	return tostring(container:allDone())
end

return p