-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
carry.js
367 lines (321 loc) · 9.23 KB
/
carry.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
var CarryTokens = (() => {
'use strict';
const CARRY_MENU_CMD = '!CARRY_TOKENS_MENU';
const CARRY_ABOVE_CMD = '!CARRY_TOKENS_CARRY_ABOVE';
const CARRY_BELOW_CMD = '!CARRY_TOKENS_CARRY_BELOW';
const DROP_ONE_CMD = '!CARRY_TOKENS_DROP_ONE';
const DROP_ALL_CMD = '!CARRY_TOKENS_DROP_ALL';
let MENU_CSS = {
'centeredBtn': {
'text-align': 'center'
},
'menu': {
'background': '#fff',
'border': 'solid 1px #000',
'border-radius': '5px',
'font-weight': 'bold',
'margin-bottom': '1em',
'overflow': 'hidden'
},
'menuBody': {
'padding': '5px',
'text-align': 'center'
},
'menuHeader': {
'background': '#000',
'color': '#fff',
'text-align': 'center'
}
};
/**
* Handlers for chat commands
*/
class Commands {
/**
* Command for carryAbove()
*/
static carryAbove(msg) {
Commands._enforcePermission(msg.playerid);
let argv = msg.content.split(' ');
let carrierId = argv[1];
let targetId = argv[2];
let carrier = getObj('graphic', carrierId);
let target = getObj('graphic', targetId);
carryAbove(carrier, target);
}
/**
* Command for carryBelow()
*/
static carryBelow(msg) {
Commands._enforcePermission(msg.playerid);
let argv = msg.content.split(' ');
let carrierId = argv[1];
let targetId = argv[2];
let carrier = getObj('graphic', carrierId);
let target = getObj('graphic', targetId);
carryBelow(carrier, target);
}
/**
* Command for drop()
*/
static dropOne(msg) {
Commands._enforcePermission(msg.playerid);
let argv = msg.content.split(' ');
let carrierId = argv[1];
let name = argv.slice(2).join(' ');
let carrier = getObj('graphic', carrierId);
let target = findObjs({
_type: 'graphic',
_pageid: carrier.get('_pageid'),
name
})[0];
if(!target)
throw new Error(`Token ${name} not found.`);
drop(carrier, target);
}
/**
* Command for dropAll()
*/
static dropAll(msg) {
Commands._enforcePermission(msg.playerid);
let argv = msg.content.split(' ');
let carrierId = argv[1];
let carrier = getObj('graphic', carrierId);
dropAll(carrier);
}
/**
* If a player does not have permission to use this script's functions,
* throw an Error for the incident.
* @param {string} playerId
*/
static _enforcePermission(playerId) {
let allowPlayerUse = getOption('allowPlayerUse');
let hasPermission = allowPlayerUse || _.isUndefined(allowPlayerUse) || playerIsGM(playerId);
if(!hasPermission)
throw new Error(`Player ${playerId} tried to use a restricted part of this script without permission.`);
}
/**
* Shows the menu.
*/
static showMenu(msg) {
Commands._enforcePermission(msg.playerid);
_showMenu(msg.who, msg.playerid);
}
}
/**
* Makes a token carry another token, without regard to their positions
* on the Z axis.
* @private
* @param {Graphic} carrier
* @param {Graphic} token
*/
function _carry(carrier, token) {
// A token can't carry itself.
if(carrier === token)
return;
// Initialize the carry list if it doesn't exist.
if(!carrier.carryList)
carrier.carryList = [];
// Add the carried token.
if(!carrier.carryList.includes(token)) {
carrier.carryList.push(token);
token.carriedBy = carrier;
}
}
/**
* Makes a token carry another token such that the carried token is
* above the carrier token on the Z axis.
* @param {Graphic} carrier
* @param {Graphic} token
*/
function carryAbove(carrier, token) {
_carry(carrier, token);
toFront(token);
}
/**
* Makes a token carry another token such that the carried token is
* below the carrier token on the Z axis.
* @param {Graphic} carrier
* @param {Graphic} token
*/
function carryBelow(carrier, token) {
_carry(carrier, token);
toBack(token);
}
/**
* Makes a token drop a token it's carrying.
* @param {Graphic} carrier
* @param {Graphic} token
*/
function drop(carrier, token) {
if(!carrier.carryList)
return;
let index = carrier.carryList.indexOf(token);
if(index !== -1) {
carrier.carryList.splice(index, 1);
delete token.carriedBy;
}
}
/**
* Makes a token drop all tokens it is carrying.
* @param {Graphic} carrier
*/
function dropAll(carrier) {
if(carrier.carryList)
delete carrier.carryList;
}
/**
* Fixes msg.who.
* @param {string} who
* @return {string}
*/
function _fixWho(who) {
return who.replace(/\(GM\)/, '').trim();
}
/**
* Returns a copy of a token's list of carried tokens.
* @param {Graphic} carrier
* @return {Graphic[]}
*/
function getCarriedTokens(carrier) {
return _.clone(carrier.carryList || []);
}
/**
* Gets the value of a One-Click user option for this script.
* @param {string} name
* @return {any}
*/
function getOption(name) {
let options = globalconfig && globalconfig.carrytokens;
if(!options)
options = (state.carrytokens && state.carrytokens.useroptions) || {};
return options[name];
}
/**
* If a token is carrying other tokens, move the carried tokens to the
* carrier token.
* @param {Graphic} carrier
*/
function moveCarriedTokens(carrier) {
// Move each of the tokens carried by the token.
if(carrier.carryList)
_.each(carrier.carryList, carried => {
carried.set('left', carrier.get('left'));
carried.set('top', carrier.get('top'));
carried.set('rotation', carrier.get('rotation'));
// If the carried token is carrying anything, move its carried
// tokens too.
moveCarriedTokens(carried);
});
}
/**
* Shows the list of effects which can be applied to a selected path.
* @param {string} who
* @param {string} playerid
*/
function _showMenu(who, playerid) {
let content = new HtmlBuilder('div');
content.append('.centeredBtn').append('a', 'Carry Above', {
href: `${CARRY_ABOVE_CMD} @{selected|token_id} @{target|token_id}`,
title: 'Make selected token carry target token on top.'
});
content.append('.centeredBtn').append('a', 'Carry Below', {
href: `${CARRY_BELOW_CMD} @{selected|token_id} @{target|token_id}`,
title: 'Make selected token carry target token underneath.'
});
content.append('.centeredBtn').append('a', 'Drop by Name', {
href: `${DROP_ONE_CMD} @{selected|token_id} ?{Drop token name:}`,
title: 'Make selected token drop a carried token.'
});
content.append('.centeredBtn').append('a', 'Drop All', {
href: `${DROP_ALL_CMD} @{selected|token_id}`,
title: 'Make selected token drop all carried tokens.'
});
let menu = _showMenuPanel('Carry Tokens', content);
_whisper(who, menu.toString(MENU_CSS));
}
/**
* Displays one of the script's menus.
* @param {string} header
* @param {(string|HtmlBuilder)} content
* @return {HtmlBuilder}
*/
function _showMenuPanel(header, content) {
let menu = new HtmlBuilder('.menu');
menu.append('.menuHeader', header);
menu.append('.menuBody', content)
return menu;
}
/**
* Whispers a Marching Order message to someone.
* @private
*/
function _whisper(who, msg) {
sendChat('Carry Tokens', '/w "' + _fixWho(who) + '" ' + msg);
}
on('chat:message', msg => {
try {
if(msg.content.startsWith(CARRY_MENU_CMD))
Commands.showMenu(msg);
if(msg.content.startsWith(CARRY_ABOVE_CMD))
Commands.carryAbove(msg);
if(msg.content.startsWith(CARRY_BELOW_CMD))
Commands.carryBelow(msg);
if(msg.content.startsWith(DROP_ONE_CMD))
Commands.dropOne(msg);
if(msg.content.startsWith(DROP_ALL_CMD))
Commands.dropAll(msg);
}
catch(err) {
log('Carry Tokens ERROR: ' + err.message);
sendChat('Carry Tokens ERROR:', '/w ' + _fixWho(msg.who) + ' ' + err.message);
log(err.stack);
}
});
// Do the carrying logic when the carrier tokens move.
on("change:graphic", obj => {
try {
// If the token was moved by a player while it was being carried,
// stop carrying it.
if(obj.carriedBy)
drop(obj.carriedBy, obj);
moveCarriedTokens(obj);
}
catch(err) {
log('Carry Tokens ERROR: ' + err.message);
log(err.stack);
}
});
// Create macros
on('ready', () => {
let players = findObjs({
_type: 'player'
});
// Create the macro, or update the players' old macro if they already have it.
_.each(players, player => {
let macro = findObjs({
_type: 'macro',
_playerid: player.get('_id'),
name: 'CarryTokensMenu'
})[0];
if(macro)
macro.set('action', CARRY_MENU_CMD);
else
createObj('macro', {
_playerid: player.get('_id'),
name: 'CarryTokensMenu',
action: CARRY_MENU_CMD
});
});
log('--- Initialized Carry Tokens ---');
});
// Exposed API
return {
carryAbove,
carryBelow,
drop,
dropAll,
getCarriedTokens,
getOption
};
})();