Module:CatsAdopted2

From Esports Wiki
Jump to: navigation, search

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

local util_args = require('Module:ArgsUtil')
local util_cargo = require('Module:CargoUtil')

local DAY_ORDER_BETTER = {
	Sunday = 1,
	Monday = 2,
	Tuesday = 3,
	Wednesday = 4,
	Thursday = 5,
	Friday = 6,
	Saturday = 7
}

--[[
	
	local queryResult = {
		[1] = { CatName = cat, Day = day },
		[2] = { CatName = cat, Day = day },
	}
	
	
]]

--[[
	{
		'Sunday', 'Monday', 'Tuesday',
		Sunday = { rowspan = 3, catNames = { 'Chloe', 'Max', 'Milo' } },
		Monday = { 'Chloe', 'Max', 'Milo' },
		Tuesday = {},
		etc.
	}
]]

local p = {}
local h = {}

function p.main(frame)
	local args = util_args.merge(true)
	local queryResult = h.makeAndRunQuery(args)
	local processedData = h.processDataForPrinting(queryResult)
	return h.makeOutput(processedData)
end

function h.makeAndRunQuery(args)
	local query = {
		tables = 'CatsAdopted',
		fields = {
			'Day',
			'CatName'
		},
		limit = 9999
	}
	return util_cargo.queryAndCast(query)
end

function h.processDataForPrinting(queryResult)
	local processedData = {}
	for _, row in ipairs(queryResult) do
		local day = row.Day
		local cat = row.CatName
		
		-- if we don't already have an entry for this day
		if not processedData[day] then
			processedData[#processedData+1] = day
			processedData[day] = {}
		end
		processedData[day][#processedData[day]+1] = cat
	end
	table.sort(processedData, h.compareDayOrder)
	return processedData
end

function h.compareDayOrder(day1, day2)
	return DAY_ORDER_BETTER[day1] < DAY_ORDER_BETTER[day2]
end

function h.makeOutput(processedData)
	local output = mw.html.create()
	local tbl = output:tag('table')
		:addClass('wikitable')
	for _, day in ipairs(processedData) do
		h.printDay(tbl, day, processedData[day])
	end	
	return output
end

function h.printDay(tbl, day, listOfCats)
	local tr = tbl:tag('tr')
	h.printDayName(tr, day)
	h.printListOfCats(tr, listOfCats)
end

function h.printDayName(tr, day)
	tr:tag('td')
		:wikitext(day)
end

function h.printListOfCats(tr, listOfCats)
	tr:tag('td')
		:wikitext(table.concat(listOfCats, ', '))
end

return p