-
Notifications
You must be signed in to change notification settings - Fork 1
/
flight.js
101 lines (86 loc) · 3.58 KB
/
flight.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var bshields = bshields || {};
bshields.flight = (function() {
'use strict';
var version = 3.4,
commands = {
fly: function(args, msg) {
var height = parseInt(args[0]) || 0;
markStatus('fluffy-wing', height, msg.selected);
},
/**
* To add new command, use this template:
commandname: function(args, msg) {
var num = parseInt(args[0]) || 0;
markStatus('statusmarker-name', num, msg.selected);
},
* Statusmarker names are listed at https://wiki.roll20.net/API:Objects#Graphic_.28Token.2FMap.2FCard.2FEtc..29
* commandname should be ALL LOWER-CASE and CANNOT contain spaces. If commandname includes anything other than a-z0-9_
* or if it begins with a number, it must be enclosed in quotes, eg:
'command-name': function...
*/
help: function(command, args, msg) {
if (_.isFunction(commands[`help_${command}`])) {
commands[`help_${command}`](args, msg);
}
},
help_fly: function(args, msg) {
sendChat(`Flight v${version}`, 'Specify !fly &'+'lt;number&'+'gt; to add that number as wings on the selected token.');
}
};
function markStatus(marker, num, selected) {
var markerStr = '',
token, markers;
if (!selected) return;
selected = _.reject(selected, (o) => o._type !== 'graphic');
if (!selected.length) return;
if(num) {
markerStr = _.chain(num.toString().split(''))
.map((d) => `${marker}@${d}`)
.value()
.reverse()
.join(',');
}
_.each(selected, (obj) => {
token = getObj('graphic', obj._id);
if (token && token.get('subtype') === 'token') {
token.set(`status_${marker}`, false);
markers = token.get('statusmarkers');
markers = markers ? markers.trim() : '';
markers += (markers.length ? ',' : '') + markerStr;
token.set('statusmarkers', markers);
}
});
}
function handleInput(msg) {
var isApi = msg.type === 'api',
args = msg.content.trim().splitArgs(),
command, arg0, isHelp;
if (isApi) {
command = args.shift().substring(1).toLowerCase();
arg0 = args.shift() || '';
isHelp = arg0.toLowerCase() === 'help' || arg0.toLowerCase() === 'h' || command === 'help';
if (!isHelp) {
if (arg0 && arg0.length > 0) {
args.unshift(arg0);
}
if (_.isFunction(commands[command])) {
commands[command](args, msg);
}
} else if (_.isFunction(commands.help)) {
commands.help(command === 'help' ? arg0 : command, args, msg);
}
} else if (_.isFunction(commands['msg_' + msg.type])) {
commands['msg_' + msg.type](args, msg);
}
}
function registerEventHandlers() {
on('chat:message', handleInput);
}
return {
registerEventHandlers: registerEventHandlers
};
}());
on('ready', function() {
'use strict';
bshields.flight.registerEventHandlers();
});