Module:Game Rating

local p = {} local h = {}

local cargo = require('Module:Cargo') local fp = require('Module:Std/functional') local utilsArg = require('Module:UtilsArg') local utilsMarkup = require('Module:UtilsMarkup') local utilsTable = require('Module:UtilsTable')

local data = require('Module:Game Rating/Data') local table_RatingOrganizations= 'RatingOrganizations' local table_Ratings= 'Ratings' local table_GameRatings= 'GameRatings'

--- -- Template:GameRating noinclude ---

-- As of 2017 it is not possible to declare multiple Cargo tables per page. -- Having multiple subpages call this function is a workaround to that. -- (The tables are effectively declared on the subpages, but the declaration code is all in one place.) function p.declareTable return fp.case(mw.title.getCurrentTitle.subpageText, {       [table_RatingOrganizations]= function            return cargo.declare(table_RatingOrganizations, { shortName= 'String', fullName= 'String', region= 'String', })       end,        [table_Ratings]= function(s)             return cargo.declare(table_Ratings, { organization= 'String', code= 'String', full= 'String', })       end,        [table_GameRatings]= function        return cargo.declare(table_GameRatings, { organization= 'String', rating= 'String', release= 'String', })       end,    }) end

function p.attachTables local attachMessages = fp.map(cargo.attach, {table_RatingOrganizations, table_Ratings, table_GameRatings}) return fp.reduce(function(acc, msg)       return acc .. '\n\n' .. msg    end, '', attachMessages) end

function p.storeOrganizationData for organizationName, organizationData in pairs(data.organizations) do       cargo.store(table_RatingOrganizations, {            shortName= organizationName,            fullName= organizationData.fullName,            region= organizationData.region,        }) for _,rating in pairs(organizationData.ratings) do           cargo.store(table_Ratings, {                organization= organizationName,                code= rating.code or rating,                full= rating.full or '',            }) end end end

--- -- Template:GameRating includeonly ---

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

function p._main(args) local organizationName, ratingCode, release = args.organizationName, args.ratingCode, args.release local organization = data.organizations[organizationName] local validate = utilsArg.validator({   	organizationName = {    		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 cargo.store(table_GameRatings, {       organization= organizationName,        rating= ratingCode,        release= release or '',    }) local organization = data.organizations[organizationName] local result = string.format("%s: %s",        mw.getCurrentFrame:expandTemplate{            title= 'Exp',            args= {                string.format('%s (%s)', organization.fullName, organization.region),                organizationName,            }        },        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

return p