Module:Game Rating

local p = {} local h = {}

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

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

function p.StoreOrganizationName(frame) return h.getOrg(frame, "fullName") end function p.StoreRegion(frame) return h.getOrg(frame, "region") end function p.StoreRatingName(frame) local args = frame:getParent.args local org = data.organizations[args[1] or ""] local code = args[2] return org and code and h.getRatingName(org, code) end function h.getOrg(frame, key) local args = frame:getParent.args local org = data.organizations[args[1] or ""] return org and org[key] end

function p.Main(frame) local args, err = utilsArg.parse(frame:getParent.args, p.Templates["Game Rating"]) if err then return utilsMarkup.categories(err.categories) end return p.printRating(args.organization, args.code, args.release) end

function p.printRating(organizationAbb, ratingCode, release) local organization = data.organizations[organizationAbb] 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.organizationsEnum local organizations = utilsTable.keys(data.organizations) organizations.reference = "Module:Game Rating/Data" return organizations end

function h.ratingCodesEnum(organizationId) local organization = data.organizations[organizationId] if not organization then return {} end local codes = {} for _, rating in ipairs(organization.ratings) do		table.insert(codes, rating.code or rating) end codes.reference = "Module:Game Rating/Data" return codes end

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

p.Templates = { ["Game Rating"] = { purpose = "This template is used inside Template:Infobox Game to record a game's content ratings.", params = { [1] = {				name = "organization", type = "string", required = true, enum = h.organizationsEnum, desc = "Abbreviatied name of a game rating body.", },			[2] = {				name = "code", type = "string", required = true, enum = h.ratingCodesEnum, enumDependsOn = "organization", desc = "Rating code that is valid for the given organization." },			release = { type = "string", canOmit = true, desc = "Used to specify ratings that apply to a specific version of a game.", },		},		paramOrder = {1, 2, "release"}, format = "inline", boilerplate = { list = true, },		examples = { {"ESRB", "E"}, {"PEGI", "7", release = "Nintendo 3DS Virtual Console"}, {"ESRB", "💩"}, {"POOP"}, }	} }

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