-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathformat.js
65 lines (60 loc) · 2.5 KB
/
format.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var _ = require('underscore'),
Types = require("./gametypes");
(function () {
FormatChecker = Class.extend({
init: function () {
this.formats = [];
/*
s = string
n = number
t = s & n
*/
/* ==== PLAYER ==== */
this.formats[Types.Messages.CLIENT.login] = ['n', 'n', 's', 'n', 'n'];
this.formats[Types.Messages.CLIENT.change_info] = ['n'];
this.formats[Types.Messages.CLIENT.change_name] = ['t'];
this.formats[Types.Messages.CLIENT.get_my_avatars] = [];
this.formats[Types.Messages.CLIENT.equip] = [];
/* ==== CHANEL ==== */
this.formats[Types.Messages.CLIENT.chat] = ['t', 'n'];
this.formats[Types.Messages.CLIENT.tab] = ['n'];
this.formats[Types.Messages.CLIENT.refresh_friends] = [];
this.formats[Types.Messages.CLIENT.refresh_guildies] = [];
this.formats[Types.Messages.CLIENT.channel_rooms] = ['s'];
this.formats[Types.Messages.CLIENT.quick_join] = [];
/* ==== ROOM ==== */
this.formats[Types.Messages.CLIENT.room_create] = ['t', 't', 'n', 'n'];
this.formats[Types.Messages.CLIENT.room_join] = ['t', 't'];
},
check: function (msg) {
var message = msg.slice(0),
type = message[0],
format = this.formats[type];
message.shift();
if (format) {
if (message.length !== format.length) {
return false;
}
for (var i = 0, n = message.length; i < n; i += 1) {
if (format[i] === 'n' && message[i] !== null && !_.isNumber(message[i])) {
return false;
}
if (format[i] === 's' && message[i] !== null && !_.isString(message[i])) {
return false;
}
}
return true;
}
/*else if(type === Types.Messages.WHO) {
// WHO messages have a variable amount of params, all of which must be numbers.
return message.length > 0 && _.all(message, function(param) { return _.isNumber(param) });
}*/
else {
log.error("Unknown message type: " + type);
return false;
}
}
});
var checker = new FormatChecker;
exports.check = checker.check.bind(checker);
})();