Module:Game Rating

local p = {} local h = {}

local utilsArg = require("Module:UtilsArg") local utilsCargo = require("Module:UtilsCargo") local utilsMarkup = require("Module:UtilsMarkup") local utilsTable = require("Module:UtilsTable")

local data = mw.loadData('Module:Game Rating/Data')

local cargoTable = "GameRatings"

function p.CargoDeclare(frame) return utilsCargo.declare(cargoTable, {		gameRelease = "String",		organizationId = "String",		organizationName = "String",		region = "String",		ratingCode = "String",       ratingName = "String",	}) end

function p.Main(frame) local args = frame:getParent.args return p.main({   	release = args.release,    	organizationAbb = args[1],    	ratingCode = args[2],    }) end

function p.main(args) local organizationAbb = args.organizationAbb local ratingCode = args.ratingCode local release = args.release local organization = data.organizations[organizationAbb] local validate = utilsArg.validator({   	organizationAbb = {    		nonEmpty = true,    		enum = utilsTable.keys(data.organizations)    	},    	ratingCode = {    		nonEmpty = true,    		enum = function    			return h.getRatingCodes(organization)			end    	}    }) local errorCategories = validate(args) if errorCategories then return utilsMarkup.categories(errorCategories) end if mw.title.getCurrentTitle.nsText == "" then utilsCargo.store(cargoTable, {	       organizationId= organizationAbb,	        organizationName = organization.fullName,	        region = organization.region,	        ratingCode = ratingCode,	        gameRelease = release or '',	        ratingName = h.getRatingName(organization, ratingCode)	    }) end local result = string.format("%s: %s",        mw.getCurrentFrame:expandTemplate{            title= 'Exp',            args= {                string.format('%s (%s)', organization.fullName, organization.region),                organizationAbb,            }        },        ratingCode    ) if release then local releaseText = tostring(   		mw.html.create('span')    			:css('font-size', 'smaller')    			:wikitext(string.format("(%s)", release))		) result = result .. ' ' .. releaseText end return result end

function h.getRatingCodes(organization) if not organization then return {} end local codes = {} for _, rating in ipairs(organization.ratings) do		table.insert(codes, rating.code or rating) end return codes end

function h.getRatingName(organization, ratingCode) local ratings = organization.ratings local ratingName = utilsTable.keyOf(ratings, ratingCode) or select(2, utilsTable.find(ratings, {code = ratingCode})).full return ratingName end

p.Schemas = { Data = { type = "record", required = true, properties = { {				name = "organizations", required = true, desc = "Information on video game rating organizations.", type = "map", keys = { type = "string" },				values = { type = "record", properties = { {							name = "fullName", required = true, type = "string", desc = "Full name of the rating organization.", },						{							name = "region", required = true, type = "string", desc = "The rating organization's jurisdiction.", },						{							name = "ratings", required = true, type = "array", items = { desc = "A collection of all the content ratings the organization has.", oneOf = { {										type = "record", properties = { {												name = "code", type = "string", required = true, desc = "The rating code.", },											{												name = "full", type = "string", required = true, desc = "What the the rating code stands for officially.", },										},									},									{										type = "string", desc = "The rating code just as a .", },								},							},						},					},				},			}		}	} }

return p