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,    		oneOf = utilsTable.keys(data.organizations)    	},    	ratingCode = {    		nonEmpty = true,    		oneOf = 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({code = ratingCode})(ratings)).full return ratingName end

p.Schemas = { data = { type = "record", required = true, properties = { {				name = "organizations", required = true, desc = "Information on the video game rating organizations", type = "string", }		}	} }

return p