Jump to content

Module:Video game series reviews

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ferret (talk | contribs) at 20:01, 10 May 2016 (Rolling in Wikidata implementation from sandbox.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local Vgwd = require('Module:Video game wikidata')

local p = {}

local columns = {
	['mc'] = nil,
	['gr'] = nil,
	['fam'] = nil,
	['sales'] = nil,
	['year'] = nil,
}

local function sortByNumber(a,b) 
	return a['num'] < b['num']
end;

local function sortByPublicationDate(a,b)
	local aP = a['pubDate'];
	local bP = b['pubDate'];
	if(aP ~= nil and bP ~= nil) then
		if(aP.year == bP.year) then
			if(aP.month == bP.month) then
				return aP.day < bP.day;
			else
				return aP.month < bP.month;
			end;
		else
			return aP.year < bP.year;
		end;
	elseif(aP ~= nil) then
		return false;
	end;
	
	return true;
end;

local function getVGWD(frame, reviewer)
	-- Obey local override on displaying this reviewer, don't bother trying to get Wikidata.
	if(columns[reviewer] == false) then
		return nil;
	end;

	local vgwdScore = Vgwd.setReviewer(reviewer);
	if(vgwdScore ~= nil) then
		return vgwdScore;
	end;

	return Vgwd.printReviewScores(frame);
end;

local function checkColumn(game, column)
	if(game[column] ~= nil and columns[column] ~= false) then
		columns[column] = true;
	end;
end;

local function buildGame(frame, args, num, qid)
	local game = {}
	game['num'] = num;
	game['name'] = args["game"..num];
	if(qid == nil) then
		game['qid'] = args["qid"..num];
	else
		game['qid'] = qid;
	end;

	game['mc'] = args["mc"..num];
	game['gr'] = args["gr"..num];
	game['fam'] = args["fam"..num];
	game['sales'] = args["sales"..num];
	game['year'] = args["year"..num];
	
	-- Check GR and Famitsu before Wikidata. We only show GR and Famitsu if locally populated or specificly set, per template guidelines (GR only if adds value over MC, Famitsu only if Japanese series)
	checkColumn(game, 'gr');
	checkColumn(game, 'fam');
					
	if(game['qid'] ~= nil) then
		local vgwdScore = Vgwd.setGame(game["qid"]);
		if(vgwdScore == nil) then
			game['updateLink'] = Vgwd.getUpdateLink();
			if(game['mc'] == nil) then
				game['mc'] = getVGWD(frame, 'mc')
			end;
			if(game['gr'] == nil) then
				game['gr'] = getVGWD(frame, 'gr')
			end;
			if(game['fam'] == nil) then
				game['fam'] = getVGWD(frame, 'fam')
			end;
			if(game['name'] == nil) then
				local sitelink = Vgwd.getSitelink();
				local label = Vgwd.getLabel();
				if(sitelink ~= label) then
					game['name'] = "[[" .. sitelink .. "|" .. label "]]";
				else
					game['name'] = "[[" .. sitelink .. "]]";									
				end;
			end;
			if(game['year'] == nil) then
				local pubDate = Vgwd.getEarliestPublicationDate();
				game['pubDate'] = pubDate;
				if(pubDate ~= nil) then
					game['year'] = pubDate.year;
				end;
			end;
		else
			-- Entity wasn't found... How to represent? TODO.
		end;
	end;
				
	checkColumn(game, 'mc');
	checkColumn(game, 'year');
	checkColumn(game, 'sales');
	
	return game;
end;

function p.main(frame)
	local args = getArgs(frame)
	return p._main(frame, args)
end

function p._main(frame, args)
	-- Get specified values of column display parameters. Nil = Unspecified.
	if(args["mc"]) then
		columns['mc'] = yesno(args.mc);
	end
	if(args["gr"]) then
		columns['gr'] = yesno(args.gr);
	end	
	if(args["fam"]) then
		columns['fam'] = yesno(args.fam);
	end		
	if(args["sales"]) then
		columns['sales'] = yesno(args.sales);
	end			
	if(args["years"]) then
		columns['year'] = yesno(args.years);
	end			
	
	local seriesQid = args["seriesQid"]; -- Should be nil, but for testing we have to be able to supply one.
	
	Vgwd.setDateFormat(args["df"]);
	Vgwd.setSystem(nil);
	Vgwd.setGenerateReferences(true);
	Vgwd.setShowSystem(true);
	Vgwd.setShowUpdateLink(false);
	
	local games = {};
	local gameRead = {};
	
	-- Look for locally provided gameN and qidN parameters first.
	for k, v in pairs(args) do
		if(string.find(k, "game%d+") or string.find(k, "qid%d+")) then
			local num = tonumber(string.match(k, "%d+"))
			if(num ~= nil) then
				if(gameRead[num] == nil) then
					gameRead[num] = true;
					table.insert(games, buildGame(frame,args,num));
				end;
			end;
		end;		
	end;

	table.sort(games, sortByNumber);

	-- So after all that, we didn't find any games to output? Let's try another route, pull the list of games from the series itself.
	if(#games < 1) then
		-- Reset vgwd to current page, presumably a series.
		local vgwdScore = Vgwd.setGame(seriesQid);
		if(vgwdScore == nil) then
			local parts = Vgwd.getParts();
			for i, qid in ipairs(parts) do
				table.insert(games, buildGame(frame,{},i,qid));
			end;
			
			table.sort(games, sortByPublicationDate);
		else
			-- Entity wasn't found... How to represent? TODO.
		end;
	end;

	local ret = "{| class=\"wikitable plainrowheaders\" style=\"font-size: 90%; float: right; clear: right; margin:0.5em 0 0.5em 1em;\"\n"
	
	ret = ret .."|+ style=\"font-size: 111.11%;\" | "
	if args.title then
		ret = ret .. args.title
	elseif columns['sales'] then
		if columns['fam'] then
			ret = ret .. "Sales and review scores"
		elseif columns['gr'] or columns['mc'] then
			ret = ret .. "Sales and aggregate review scores"
		else
			ret = ret .. "Sales"
		end
	elseif columns['fam'] then
		if columns['gr'] or columns['mc'] then
			ret = ret .. "Japan and Western review scores"
		else
			ret = ret .. "Famitsu review scores"
		end
	else
		ret = ret .. "Aggregate review scores"
	end

	if args.updated then
		ret = ret .. "<br /><small>''As of " .. args.updated ..".''</small>"
	end
	ret = ret .. " \n"
	
	ret = ret .. "! scope=\"col\" | Game \n"
	if columns['year'] then
		ret = ret .. "! scope=\"col\" | Year\n"
	end	
	if columns['sales'] then
		ret = ret .. "! scope=\"col\" | " .. (args.sales_title or "Units sold") .. " \n"
	end
	if columns['fam'] then
		ret = ret .. "! scope=\"col\" | " .. (args.fam_title or  "''[[Famitsu scores|Famitsu]]''") .. " \n"
	end
	if columns['gr'] then
		ret = ret .. "! scope=\"col\" | " .. (args.gr_title or  "[[GameRankings]]") .. " \n"
	end
	if columns['mc'] then
		ret = ret .. "! scope=\"col\" | " .. (args.mc_title or  "[[Metacritic]]") .. " \n"
	end
	
	-- Print the reviews
    for i,game in ipairs(games) do
		ret = ret .. "|-\n"
		ret = ret .. "! scope=\"row\" | ''" .. game['name'] .. "''"
		if(game['updateLink']) then
			ret = ret .. " " .. game['updateLink'];
		end;
		ret = ret .. "\n"
	
		if columns['year'] then
			ret = ret .. "| style=\"text-align: center;\" | " .. (game['year'] or '') .. " \n"
		end	
		if columns['sales'] then
			ret = ret .. "| style=\"text-align: center;\" | " .. (game['sales'] or '') .. " \n"
		end
		if columns['fam'] then
			ret = ret .. "| style=\"text-align: center;\" | " .. (game['fam'] or '') .. " \n"
		end
		if columns['gr'] then
			ret = ret .. "| style=\"text-align: center;\" | " .. (game['gr'] or '') .. " \n"
		end
		if columns['mc'] then
			ret = ret .. "| style=\"text-align: center;\" | " .. (game['mc'] or '') .. " \n"
		end	
	end;

	ret = ret .. "|}"

	return ret
end

return p