Jump to content

Module:Video game series reviews/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ferret (talk | contribs) at 23:14, 25 April 2016. 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/sandbox')

local p = {}

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

local function sortByNumber(a,b) 
	return a['num'] < b['num']
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;

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['years'] = yesno(args.years);
	end			
	
	Vgwd.setDateFormat("mdy");
	Vgwd.setSystem(nil);
	Vgwd.setGenerateReferences(true);
	Vgwd.setShowSystem(true);
	Vgwd.setShowUpdateLink(false);
	
	local games = {};
	local gameRead = {};
	
	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
					local game = {}
					game['num'] = num;
					game['name'] = args["game"..num];
					game['qid'] = args["qid"..num];
					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];
					
					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
								-- Try to get label and enwiki link from Wikidata for this entity.
							end;
						else
							-- Entity wasn't found... How to represent? TODO.
						end;
					end;
				
					checkColumn(game, 'mc');
					checkColumn(game, 'gr');
					checkColumn(game, 'fam');
					checkColumn(game, 'years');
					checkColumn(game, 'sales');

					gameRead[num] = true;
					table.insert(games,game);
				end;
			end;
		end;		
	end;

	table.sort(games, sortByNumber)

	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['years'] 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['years'] 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