Module:Video game reviews/sandbox

This is an old revision of this page, as edited by Jackmcbarn (talk | contribs) at 19:44, 17 July 2014 (combine the reviewer and aggregator loops). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
--
-- Module layout for reviewer section in the work
--
-- 1 get args
-- 2 extract all reviewer args
-- 3 sort args for reviewer then system
-- 4 output args value where reviewer + system correlate
--

require('Module:No globals')

local p = {}

local reviewer = {{'[[1UP.com]]', '1UP'}, {'[[ActionTrip]]', 'Atrip'}, {'[[Adventure Gamers]]', 'AdvGamers'}, {'[[AllGame]]', 'Allgame'}, {'[[Amstrad Action]]', 'AAction'}, {'[[Amtix!]]', 'Amtix'}, {'[[Computer and Video Games]]', 'CVG'}, {'[[Crash (magazine)|Crash]]', 'CRASH'}, {'[[Destructoid]]', 'Destruct'}, {'[[Dragon (magazine)]]', 'Dragon'}, {'[[Edge (magazine)|Edge]]', 'Edge'}, {'[[Electronic Gaming Monthly]]', 'EGM'}, {'[[Eurogamer]]', 'EuroG'}, {'[[Famitsu]]', 'Fam'}, {'[[G4 (TV channel)|G4]]', 'G4'}, {'GameCritics', 'GCritics'}, {'[[GameFan]]', 'GameFan'}, {'[[Game Informer]]', 'GI'}, {'[[GamePro]]', 'GamePro'}, {'[[Game Revolution]]', 'GameRev'}, {'[[GamesMaster]]', 'GMaster'}, {'[[GamesRadar]]', 'GRadar'},  {'[[GameSpot]]', 'GSpot'}, {'[[GameSpy]]', 'GSpy'}, {'[[GamesTM]]', 'GTM'}, {'[[GameTrailers]]', 'GT'}, {'GameWire', 'GW'}, {'[[GameZone]]', 'GameZone'}, {'[[Giant Bomb]]', 'GB'}, {'[[Hyper (magazine)|Hyper Magazine]]', 'Hyper'}, {'[[IGN]]', 'IGN'}, {'[[Joystiq]]', 'Joystiq'}, {'[[Maximum PC]]', 'MaxPC'}, {'[[NGC Magazine]]', 'N64'}, {'[[NGC Magazine]]', 'NGC'}, {'[[NGamer]]', 'NG'}, {'NintendoLife', 'NLife'}, {'[[Nintendo Power]]', 'NP'}, {'Nintendo World Report', 'NWR'}, {'[[Official Nintendo Magazine]]', 'ONM'}, {'[[Official U.S. PlayStation Magazine|Official PlayStation Magazine (US)]]', 'OPM'}, {'[[PlayStation Official Magazine (UK)|Official PlayStation Magazine (UK)]]', 'OPMUK'}, {'[[Official PlayStation Magazine (Australia)]]', 'OPMAU'}, {'[[Official Xbox Magazine]]', 'OXM'}, {'[[Official Xbox Magazine|Official Xbox Magazine UK]]', 'OXMUK'}, {'[[PALGN]]', 'PALGN'}, {'[[PC Format]]', 'PCF'}, {'[[PC Gamer|PC Gamer UK]]', 'PCGUK'}, {'[[PC Gamer|PC Gamer US]]', 'PCGUS'}, {'[[PC PowerPlay]]', 'PCPP'}, {'[[PC Zone]]', 'PCZone'}, {'[[Play (UK magazine)|Play Magazine]]', 'Play'}, {'[[Polygon (website)|Polygon]]', 'Poly'}, {'[[PlayStation: The Official Magazine|PlayStation Magazine]]', 'PSM'}, {'[[PSM3|PlayStation 3 Magazine]]', 'PSM3'}, {'[[RPGamer]]', 'RPG'}, {'[[Sinclair User]]', 'SUser'}, {'[[TeamXbox]]', 'TX'}, {'[[VideoGamer.com]]', 'VG'}, {'[[X-Play]]', 'XPlay'}, {'[[Your Sinclair]]', 'YSinclair'}}

local aggregator = {{'[[GameRankings]]','GR'}, {'[[Metacritic]]','MC'}}

local system = {{'[[Atari 2600]]','A2600'}, {'[[ColecoVision]]','CV'}, {'[[Nintendo DS|DS]]','DS'}, {'[[Game Boy]]','GB'}, {'[[Game Boy Advance|GBA]]','GBA'}, {'[[Game Boy Color|GBC]]','GBC'}, {'[[List of video game consoles|General]]', 'GEN'}, {'[[iOS]]','iOS'}, {'[[N-Gage (device)|N-Gage]]','N-G'}, {'[[Nintendo 64|N64]]','N64'}, {'[[Nintendo Entertainment System|NES]]','NES'}, {'[[GameCube|GC]]','NGC'}, {'[[Nvidia Shield|Shield]]','NSHI'}, {'[[Personal Computer|PC]]','PC'}, {'[[PlayStation|PS]]','PS'}, {'[[PlayStation 2|PS2]]','PS2'}, {'[[PlayStation 3|PS3]]','PS3'}, {'[[PlayStation 4|PS4]]','PS4'}, {'[[PlayStation Portable|PSP]]','PSP'}, {'[[Dreamcast]]','SDC'}, {'[[Sega Game Gear|SGG]]','SGG'}, {'[[Super Nintendo Entertainment System|SNES]]','SNES'}, {'[[Sega Genesis|Mega Drive]]','SMD'}, {'[[Master System]]','SMS'}, {'[[Sega Saturn|Saturn]]','SSAT'}, {'[[TurboGrafx-16]]','TG16'}, {'[[PlayStation Vita]]','VITA'}, {'[[Xbox]]','XBOX'}, {'[[Xbox 360]]','X360'}, {'[[Xbox One]]','XONE'}, {'[[Wii]]','WII'}}

local data = mw.loadData('Module:Video game reviews/data')

local HtmlBuilder = require('Module:HtmlBuilder')
local categoryHandler = require('Module:Category handler').main

local function getActiveSystems(args)
	local activeSystems = {}
	for k in pairs(data.systems) do
		local argValue = args[k]
		if argValue and argValue:lower() == 'true' then
			table.insert(activeSystems, k)
		end
	end
	return activeSystems
end

local function getArgKeyTables(orderedKeys)
	local reviewers, aggregators, awards = {}, {}, {}
	for _,v in ipairs(orderedKeys) do
		if v:match('^rev(%d+)$') then
			table.insert(reviewers, v)
		elseif v:match('^agg(%d+)$') then
			table.insert(aggregators, v)
		elseif v:match('^award(%d+)$') then
			table.insert(awards, v)
		end
	end
	return reviewers, aggregators, awards
end

local function renderTitleRow(tbl, plain, title)
	local titleCell = tbl.tag('tr').tag('th').css('font-size', '120%')

	if plain then
		titleCell
			.tag('span')
			.css('padding-left', '5.7em')
			.wikitext(' ')
	end

	if title then
		titleCell
			.wikitext(title)
	else
		titleCell
			.addClass('Reception')
			.wikitext('Reception')
	end
end

local function reviews(halfkeysrev, halfkeysagg, platforms, custome_agg, custome_rev, args)
	local arg_system
	local tbl2
	if (#halfkeysrev > 0 or #halfkeysagg > 0) and platforms >= 1 then
		 tbl2 = HtmlBuilder.create('table')
			.addClass('infobox wikitable')
			.attr('cellpadding', 0)
			.attr('cellspacing', 0)
			.css('width', '100%')
			.css('border-bottom', 'none')
			.css('margin', '0em')
			.done()
	elseif (#halfkeysrev > 0 or #halfkeysagg > 0 or custome_agg > 0 or custome_rev > 0) and  platforms == 0 then
		tbl2 = HtmlBuilder.create('table')
			.addClass('infobox wikitable')
			.attr('cellpadding', 0)
			.attr('cellspacing', 0)
			.css('width', '100%')
			.css('font-size', '100%')
			.css('border-bottom', 'none')
			.css('margin', '0em')
			.done()
	end
	if #halfkeysrev > 0 and (platforms > 1 or string.lower(tostring(args['showplatforms'])) == 'true') then
		tbl2.tag('tr').tag('th')
			.attr('colspan', #system + 1)
			.css('background', '#d1dbdf')
			.css('font-size', '120%')
			.wikitext('Review scores')

		tbl2
			.tag('tr')
				.tag('th')
					.attr('rowspan', '2')
					.css('background', '#e8f4f8')
					.css('text-align', 'center')
					.css('vertical-align', 'middle')
					.wikitext('Publication')
					.done()
				.tag('th')
					.attr('colspan', #system)
					.css('background', '#e8f4f8')
					.css('vertical-align', 'middle')
					.wikitext('Score')
		local forloop = tbl2.tag('tr')
		for j = 1, #system do
			if string.lower(tostring(args[tostring(system[j][2])])) == 'true' then
				forloop.tag('th').wikitext(tostring(system[j][1]))
			end
		end
	elseif #halfkeysrev > 0 and platforms == 1 and string.lower(tostring(args['showplatforms'])) ~= 'true' then
		tbl2.tag('tr').tag('th')
			.attr('colspan', '2')
			.css('background', '#d1dbdf')
			.css('font-size', '120%')
			.wikitext('Review scores')

		tbl2
			.tag('tr')
				.tag('th')
					.css('background', '#e8f4f8')
					.css('text-align', 'center')
					.css('vertical-align', 'middle')
					.wikitext('Publication')
					.done()
				.tag('th')
					.css('background', '#e8f4f8')
					.css('vertical-align', 'middle')
					.wikitext('Score')
	end
	if platforms >= 1 then
		for i = 1, #reviewer do
			for k = 1, #halfkeysrev do
				if reviewer[i][2] == halfkeysrev[k] then
					local forloop = tbl2.tag('tr')
					forloop.tag('td')
						.css('vertical-align','middle')
						.wikitext(reviewer[i][1])

					for j = 1, #system do
						local temp = tostring(reviewer[i][2]) .. '_' .. tostring(system[j][2])
						if string.lower(tostring(args[tostring(system[j][2])])) == 'true' and args[temp] ~= nil then
							forloop.tag('td')
								.css('vertical-align', 'middle')
								.wikitext(tostring(args[temp]))
						elseif string.lower(tostring(args[tostring(system[j][2])])) == 'true' and string.lower(tostring(args['na'])) == 'true' then
							forloop.tag('td')
								.css('color', 'lightgray')
								.css('vertical-align','middle')
								.css('text-align', 'center')
								.addClass('table-na')
								.wikitext('N/A')
						elseif string.lower(tostring(args[tostring(system[j][2])])) == 'true' then
							forloop.tag('td')
						end
					end
				end
			end
		end
		for i = 1, 10 do
			if args['rev' .. tostring(i)] ~= nil then
				local forloop = tbl2.tag('tr')
				forloop.tag('td').wikitext(tostring(args['rev' .. tostring(i)]))
				for j = 1, #system do
					local temp = 'rev'.. tostring(i) .. '_' .. tostring(system[j][2])
					if string.lower(tostring(args[tostring(system[j][2])])) == 'true' and args[temp] ~= nil then
						forloop.tag('td')
							.css('vertical-align','middle')
							.wikitext(tostring(args[temp]))
					elseif string.lower(tostring(args[tostring(system[j][2])])) == 'true' and string.lower(tostring(args['na'])) == 'true' then
						forloop.tag('td')
							.css('color', 'lightgray')
							.css('vertical-align','middle')
							.css('text-align', 'center')
							.addClass('table-na')
							.wikitext('N/A')
					elseif string.lower(tostring(args[tostring(system[j][2])])) == 'true' then
						forloop.tag('td')
					end
				end
			end
		end
		if (#halfkeysagg > 0 and platforms > 1) and #halfkeysrev == 0 then
			tbl2
				.tag('tr')
					.tag('th')
						.attr('colspan', #system+1)
						.css('background', '#d1dbdf')
						.css('font-size', '120%')
						.wikitext('Aggregate scores')
			tbl2
				.tag('tr')
					.tag('th')
						.attr('rowspan', '2')
						.css('background', '#e8f4f8')
						.css('text-align', 'center')
						.css('vertical-align', 'middle')
						.wikitext('Publication')
						.done()
					.tag('th')
						.attr('colspan', #system+1)
						.css('background', '#e8f4f8')
						.css('vertical-align', 'middle')
						.wikitext('Score')
			local forloop = tbl2.tag('tr')
			for j = 1, #system do
				if string.lower(tostring(args[tostring(system[j][2])])) == 'true' then
					forloop.tag('th').wikitext(tostring(system[j][1]))
				end
			end
		elseif (#halfkeysagg > 0 and #halfkeysrev > 0) or (#halfkeysagg > 0 and platforms == 1)then
			tbl2
				.tag('tr')
					.tag('th')
						.attr('colspan', #system+1)
						.css('background', '#d1dbdf')
						.css('font-size', '120%')
						.wikitext('Aggregate scores')
		end
		for i = 1, #aggregator do
			for k = 1, #halfkeysagg do
				if aggregator[i][2] == halfkeysagg[k] then
					local forloop = tbl2.tag('tr')
					forloop.tag('td')
						.css('vertical-align','middle')
						.wikitext(aggregator[i][1])

					for j = 1, #system do
						local temp = tostring(aggregator[i][2]) .. '_' .. tostring(system[j][2])
						if string.lower(tostring(args[tostring(system[j][2])])) == 'true' and args[temp] ~= nil then
							forloop.tag('td').wikitext(tostring(args[temp]))
						elseif string.lower(tostring(args[tostring(system[j][2])])) == 'true' and string.lower(tostring(args['na'])) == 'true' then
							forloop.tag('td')
								.css('color', 'lightgray')
								.css('vertical-align','middle')
								.css('text-align', 'center')
								.addClass('table-na')
								.wikitext('N/A')
						elseif string.lower(tostring(args[tostring(system[j][2])])) == 'true' then
							forloop.tag('td')
						end
					end
				end
			end
		end
	elseif platforms == 0 then
		if #halfkeysagg > 0 or custome_agg > 0 then
			tbl2
				.tag('tr')
					.tag('th')
						.attr('colspan', 2)
						.css('background', '#d1dbdf')
						.css('font-size', '120%')
						.wikitext('Aggregate scores')
						.done()
				.tag('tr')
					.tag('th')
						.css('background', '#e8f4f8')
						.css('text-align', 'center')
						.wikitext('Aggregator')
						.done()
					.tag('th')
						.css('background', '#e8f4f8')
						.wikitext('Score')
		end
		for i = 1, #aggregator do
			for k = 1, #halfkeysagg do
				if aggregator[i][2] == halfkeysagg[k] then
					tbl2
						.tag('tr')
							.tag('td')
								.css('text-align', 'center')
								.css('vertical-align', 'middle')
								.wikitext(aggregator[i][1])
								.done()
							.tag('td')
								.css('text-align', 'center')
								.css('font-size', '110%')
								.wikitext(tostring(args[tostring(aggregator[i][2])]))
				end
			end
		end
		for i = 1, 10 do
			if args['agg' .. tostring(i)] ~= nil then
				tbl2
					.tag('tr')
						.tag('td')
							.css('text-align', 'center')
							.css('vertical-align', 'middle')
							.wikitext(tostring(args['agg' .. tostring(i)]))
							.done()
						.tag('td')
							.css('text-align', 'center')
							.css('font-size', '110%')
							.wikitext(tostring(args['agg'.. tostring(i) .. 'Score']))
			end
		end
		if #halfkeysrev > 0 or custome_rev > 0 then
			tbl2.tag('tr').tag('th')
				.attr('colspan', #system + 1)
				.css('background', '#d1dbdf')
				.css('font-size', '120%')
				.wikitext('Review scores')
			tbl2
				.tag('tr')
					.tag('th')
						.css('background', '#e8f4f8')
						.css('text-align', 'center')
						.wikitext('Publication')
						.done()
					.tag('th')
						.attr('colspan', '25')
						.css('background', '#e8f4f8')
						.wikitext('Score')

			for i = 1, #reviewer do
				for k = 1, #halfkeysrev do
					if reviewer[i][2] == halfkeysrev[k] then
						tbl2
							.tag('tr')
								.tag('td')
									.css('text-align', 'center')
									.css('vertical-align', 'middle')
									.wikitext(reviewer[i][1])
									.done()
								.tag('td')
									.css('text-align', 'center')
									.css('font-size', '110%')
									.wikitext(tostring(args[tostring(reviewer[i][2])]))
					end
				end
			end
			for i = 1, 10 do
				if args['rev' .. tostring(i)] ~= nil then
					tbl2
						.tag('tr')
							.tag('td')
								.css('text-align', 'center')
								.css('vertical-align', 'middle')
								.wikitext(tostring(args['rev' .. tostring(i)]))
								.done()
							.tag('td')
								.css('text-align', 'center')
								.css('font-size', '110%')
								.wikitext(tostring(args['rev'.. tostring(i) .. 'Score']))
				end
			end
		end
	end
	return tbl2
end

local function awards(args, awardKeys)
	if #awardKeys ~= 0 then
		local Cell = HtmlBuilder.create('table')
			.addClass('infobox wikitable')
			.css('width', '100%')
			.css('margin', '0em')
			.css('border-top', 'none')
			.attr('cellpadding', 3)
			.attr('cellspacing', 0)
			.tag('tr')
				.tag('th')
					.attr('colspan', 2)
					.css('background', '#d1dbdf')
					.css('font-size', '120%')
					.css('border-top', 'none')
					.wikitext('Awards')
				.done()
			.done()
			.tag('tr')
				.tag('th')
					.wikitext('Publication')
				.done()
				.tag('th')
					.wikitext('Award')
				.done()
			.done()

		for _,v in ipairs(awardKeys) do
			 Cell.tag('tr')
				.tag('td')
					.css('font-weight','bold')
					.css('background-color','#f2f2f2')
					.wikitext(args[v .. 'Pub'])
					.done()
				.tag('td')
					.css('background-color','#f2f2f2')
					.wikitext(args[v])
		end
		return Cell
	end
end

local function renderMainTable(halfkeysrev, halfkeysagg, awardKeys, platforms, custome_agg, custome_rev, args)
	local tbl

	if args['award1'] or ((#halfkeysagg > 0 or #halfkeysrev > 0 or custome_agg > 0 or custome_rev > 0) and platforms >= 0) then
		if platforms == 0 then
			-- Width: 20% Seems better since it scales with the article size.
			tbl = HtmlBuilder.create('table')
				.attr('cellpadding', 0)
				.attr('cellspacing', 0)
				.css('background', 'transparent')
				.css('padding', '0em')
				.css('margin', '0em 1em 1em 1em')
				.css('text-align', 'center')
				.css('font-size', '80%')
			if args.width then
				tbl
					.css('width',args.width)
			else
				tbl
					.css('width', '23em')
			end
		else
			tbl = HtmlBuilder.create('table')
				.attr('cellpadding', 0)
				.attr('cellspacing', 0)
				.css('background', 'transparent')
				.css('padding', '0em')
				.css('margin', '0em 1em 1em 1em')
				.css('text-align', 'center')
				.css('font-size', '80%')
		end
		if args.state then
			tbl
				.addClass(args.state)
		else
			tbl
				.addClass('collapsible')
		end

		if args.title and (args.state ~= 'plain' and args.state ~= 'off') then
			tbl
				.addClass('collapsible')
				.addClass(args.state)
		end

		tbl.css('float', args.align or 'right').css('clear', args.align or 'right')

		renderTitleRow(tbl, args.state == 'plain', args.title)

		if args.subtitle then
			tbl
				.tag('tr')
					.tag('th')
						.css('font-size', '120%')
						.wikitext(args.subtitle)
		end

		tbl
			.tag('tr')
				.tag('td')
					.node(reviews(halfkeysrev, halfkeysagg, platforms, custome_agg, custome_rev, args))

		tbl
			.tag('tr')
				.tag('td')
					.node(awards(args, awardKeys))
	else
		local category = categoryHandler{
			main = '[[Category:Empty templates on articles]]'
		}
		category = category or ''
		tbl = HtmlBuilder.create()
		tbl.wikitext(category)
	end
	return tbl
end

function p._reviewbox(args)
	local halfkeysrev, halfkeysagg = {}, {}
	
	local sortedArgKeys = {}
	for k in pairs(args) do
		if type(k) == 'string' then
			table.insert(sortedArgKeys, k)
		end
	end
	table.sort(sortedArgKeys)
	
	local activeSystems = getActiveSystems(args)
	local platforms = #activeSystems
	local reviewerKeys, aggregatorKeys, awardKeys = getArgKeyTables(sortedArgKeys)
	local custome_rev, custome_agg = #reviewerKeys, #aggregatorKeys

	if platforms ~= 0 then
		local seen = {}
		for _,argKey in ipairs(sortedArgKeys) do
			local splitPos = argKey:find('_')
			if splitPos then
				local halfarg = argKey:sub(1, splitPos - 1)
				if not seen[halfarg] then
					seen[halfarg] = true
					if data.reviewers[halfarg] then
						table.insert(halfkeysrev, halfarg)
					elseif data.aggregators[halfarg] then
						table.insert(halfkeysagg, halfarg)
					end
				end
			end
		end
	else
		for _,argKey in ipairs(sortedArgKeys) do
			if not argKey:find('_') then
				if data.reviewers[argKey] then
					table.insert(halfkeysrev, argKey)
				elseif data.aggregators[argKey] then
					table.insert(halfkeysagg, argKey)
				end
			end
		end
	end
	return renderMainTable(halfkeysrev, halfkeysagg, awardKeys, platforms, custome_agg, custome_rev, args)
end

function p.reviewbox(frame)
	-- ParserFunctions considers the empty string to be false, so to preserve the previous
	-- behavior of {{Video game multiple console reviews}}, change any empty arguments to nil, so Lua will consider
	-- them false too.
	local args = {}
	local parent_args = frame:getParent().args;
	for k, v in pairs(parent_args) do
		if v ~= '' then
			args[k] = v
		end
	end

	return p._reviewbox(args)
end

return p