Module:Demographics/PopQuery

From Ways of Darkness
Jump to navigation Jump to search

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

local p = { }

function p.filter(value,fltable)
	if fltable == nil then
		return true
	elseif type(fltable) == "table" then
	local match = false
		for _,v in pairs(fltable) do
			if value == v then
				match = true
			end
		end
		return match
	elseif type(fltable) == type(value) then
		if value == fltable then
			return true
			else
			return false
		end
	else
	return true
	end
end

function p.filterpops(filterRace, filterReligion, filterOccupation, filterAgegroup, filterGender, filterCountry, filterRegion, filterArea)
	local z = { }
	local pops = mw.loadData( 'Module:Demographics/Pops' )
	for popId,popTable in pairs(pops) do
		if p.filter(popTable['race']['raceName'],filterRace) and p.filter(popTable['religion']['religionName'],filterReligion) and p.filter(popTable['occupation']['occupationName'],filterOccupation) and p.filter(popTable['agegroup']['agegrpName'],filterAgegroup) and p.filter(popTable['female'],filterGender) and p.filter(popTable['area']['region']['country']['countryName'],filterCountry) and p.filter(popTable['area']['region']['regionName'],filterRegion) and p.filter(popTable['area']['areaName'],filterArea) then
		table.insert(z, popTable)
		end
	end
	return z
end

function p.query(groupBy, filterRace, filterReligion, filterOccupation, filterAgegroup, filterGender, filterCountry, filterRegion, filterArea)
	local pops = p.filterpops(filterRace, filterReligion, filterOccupation, filterAgegroup, filterGender, filterCountry, filterRegion, filterArea)
	local queried = { }
	for popId,popTable in pairs(pops) do
	local groupId = groupBy(popTable)
		if queried[groupId] == nil then
			queried[groupId] = popTable['quantity']
		else
			queried[groupId] = queried[groupId] + popTable['quantity']
		end
	end
	return queried
end

return p