-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvalidator.js
46 lines (39 loc) · 1.54 KB
/
validator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const fs = require('fs');
const basicValidation = require("./validators/generic_hash.js");
var validators = {};
// Load up all the validators!
const validatorFiles = fs.readdirSync('./validators').filter(file => file.endsWith('.js'));
for (const file of validatorFiles) {
const validator = require(`./validators/${file}`);
validator.games.forEach(game => {
if (validators[game]) {
console.error(`Validator for game ${game} is defined again in /validators/${file}. Aggregating validation checks.`);
validators[game].push(validator);
} else {
validators[game] = [validator];
}
});
}
function validateScore(game, name, score, metaData = "", validation = "") {
if (validators[game]) {
for (let i = 0; i < validators[game].length; i++) {
if (!validators[game][i].validateScore(game, name, score, metaData, validation)) {
return false; // if any validators fail, return false
}
}
}
// This check allows any game to use the basic hash validation
// by ending their game key name with '-basic-validation'
// this allows for backwards compatibility with old games
// but lets new games opt-into the new basic validation :)
if (game.endsWith("-basic-validation")) {
if (!basicValidation.validateScore(game, name, score, metaData, validation)){
return false;
}
}
// If we get here all validation passed, return true!
return true;
}
module.exports = {
validateScore
}