Skip to content

Commit 72dcd80

Browse files
committed
Metascripts fix
1 parent 3424b52 commit 72dcd80

3 files changed

Lines changed: 97 additions & 89 deletions

File tree

TokenController/4.0.0/TokenController.js

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ API_Meta.TokenController = { offset: Number.MAX_SAFE_INTEGER, lineCount: -1 };
6969

7070
const TokenController = (() => {
7171
const NAME = 'TokenController';
72-
const VERSION = '4.2.0';
73-
const AUTHOR = 'Scott E. Schwarz';
72+
const VERSION = '4.0.1';
73+
const AUTHOR = 'Developesque';
7474
let errorMessage = "";
7575
let listingVars = false;
7676
let combatStarted = false;
@@ -229,10 +229,14 @@ const TokenController = (() => {
229229

230230
log(`${NAME} ${VERSION} by ${AUTHOR} Ready Meta Offset : ${API_Meta.TokenController.offset}`);
231231
createMenu();
232+
233+
on('chat:message', chatHandler);
234+
on('change:graphic', graphicHandler);
235+
on('change:campaign:turnorder', turnorderHandler);
232236
});
233237

234238
// Token Destruction
235-
on("change:graphic", function (obj, prev) {
239+
const graphicHandler = function (obj, prev) {
236240
if (obj.get("_subtype") !== "token") {
237241
return;
238242
}
@@ -265,10 +269,47 @@ const TokenController = (() => {
265269
rotation: obj.get('rotation')
266270
});
267271
});
268-
});
272+
};
269273

274+
// When Roll20 combat starts
275+
const turnorderHandler = function (campaign, prev) {
276+
const rawCurrent = campaign.get('turnorder');
277+
const rawPrev = (prev && typeof prev.get === 'function') ? prev.get('turnorder') : prev;
278+
let current = [];
279+
let prevOrder = [];
280+
try {
281+
current = typeof rawCurrent === 'string' ? (JSON.parse(rawCurrent || '[]') || []) : (Array.isArray(rawCurrent) ? rawCurrent : []);
282+
} catch (e) { current = []; }
283+
try {
284+
prevOrder = typeof rawPrev === 'string' ? (JSON.parse(rawPrev || '[]') || []) : (Array.isArray(rawPrev) ? rawPrev : []);
285+
} catch (e) { prevOrder = []; }
286+
const wasEmpty = !prevOrder || prevOrder.length === 0;
287+
const isNowActive = current && current.length > 0;
288+
289+
if (isNowActive && current[0] && typeof current[0] === 'object' && current[0].id) {
290+
currentCombatant = current[0].id;
291+
}
292+
293+
if (combatStarted && !isNowActive) {
294+
combatStarted = false;
295+
//sendChat('System', '🏆 Combat has ended!');
296+
return;
297+
}
298+
299+
if (combatStarted && state[NAME].storedVariables.combatPatrol) {
300+
pathTokens(true);
301+
}
302+
303+
// When Roll20 combat starts
304+
if (!combatStarted && !wasEmpty && isNowActive) {
305+
combatStarted = true;
306+
//sendChat('System', '⚔️ Combat has started!');
307+
sendChat(`${NAME}`, `/w GM ⚔️ Patrols have been stopped! Combat Patrolling is ${state[NAME].storedVariables.combatPatrol ? "enabled" : "disabled"}`);
308+
}
309+
};
310+
270311
// Commands
271-
on("chat:message", function (msg) {
312+
const chatHandler = function (msg) {
272313
try {
273314
if (msg.type === "api" && (msg.content.toLowerCase().startsWith("!token-control") || msg.content.toLowerCase().startsWith("!tc"))) {
274315

@@ -497,44 +538,7 @@ const TokenController = (() => {
497538
log(` ${NAME} Error: ${err}<br/>${errorMessage}`);
498539
errorMessage = "";
499540
}
500-
});
501-
502-
// When Roll20 combat starts
503-
on('change:campaign:turnorder', function (campaign, prev) {
504-
const rawCurrent = campaign.get('turnorder');
505-
const rawPrev = (prev && typeof prev.get === 'function') ? prev.get('turnorder') : prev;
506-
let current = [];
507-
let prevOrder = [];
508-
try {
509-
current = typeof rawCurrent === 'string' ? (JSON.parse(rawCurrent || '[]') || []) : (Array.isArray(rawCurrent) ? rawCurrent : []);
510-
} catch (e) { current = []; }
511-
try {
512-
prevOrder = typeof rawPrev === 'string' ? (JSON.parse(rawPrev || '[]') || []) : (Array.isArray(rawPrev) ? rawPrev : []);
513-
} catch (e) { prevOrder = []; }
514-
const wasEmpty = !prevOrder || prevOrder.length === 0;
515-
const isNowActive = current && current.length > 0;
516-
517-
if (isNowActive && current[0] && typeof current[0] === 'object' && current[0].id) {
518-
currentCombatant = current[0].id;
519-
}
520-
521-
if (combatStarted && !isNowActive) {
522-
combatStarted = false;
523-
//sendChat('System', '🏆 Combat has ended!');
524-
return;
525-
}
526-
527-
if (combatStarted && state[NAME].storedVariables.combatPatrol) {
528-
pathTokens(true);
529-
}
530-
531-
// When Roll20 combat starts
532-
if (!combatStarted && !wasEmpty && isNowActive) {
533-
combatStarted = true;
534-
//sendChat('System', '⚔️ Combat has started!');
535-
sendChat(`${NAME}`, `/w GM ⚔️ Patrols have been stopped! Combat Patrolling is ${state[NAME].storedVariables.combatPatrol ? "enabled" : "disabled"}`);
536-
}
537-
});
541+
};
538542

539543
function cleanupStaleTokens() {
540544
const beforePaths = state[NAME].storedVariables.activeTokenPaths.length;

TokenController/TokenController.js

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ API_Meta.TokenController = { offset: Number.MAX_SAFE_INTEGER, lineCount: -1 };
6969

7070
const TokenController = (() => {
7171
const NAME = 'TokenController';
72-
const VERSION = '4.2.0';
73-
const AUTHOR = 'Scott E. Schwarz';
72+
const VERSION = '4.0.1';
73+
const AUTHOR = 'Developesque';
7474
let errorMessage = "";
7575
let listingVars = false;
7676
let combatStarted = false;
@@ -229,10 +229,14 @@ const TokenController = (() => {
229229

230230
log(`${NAME} ${VERSION} by ${AUTHOR} Ready Meta Offset : ${API_Meta.TokenController.offset}`);
231231
createMenu();
232+
233+
on('chat:message', chatHandler);
234+
on('change:graphic', graphicHandler);
235+
on('change:campaign:turnorder', turnorderHandler);
232236
});
233237

234238
// Token Destruction
235-
on("change:graphic", function (obj, prev) {
239+
const graphicHandler = function (obj, prev) {
236240
if (obj.get("_subtype") !== "token") {
237241
return;
238242
}
@@ -265,10 +269,47 @@ const TokenController = (() => {
265269
rotation: obj.get('rotation')
266270
});
267271
});
268-
});
272+
};
269273

274+
// When Roll20 combat starts
275+
const turnorderHandler = function (campaign, prev) {
276+
const rawCurrent = campaign.get('turnorder');
277+
const rawPrev = (prev && typeof prev.get === 'function') ? prev.get('turnorder') : prev;
278+
let current = [];
279+
let prevOrder = [];
280+
try {
281+
current = typeof rawCurrent === 'string' ? (JSON.parse(rawCurrent || '[]') || []) : (Array.isArray(rawCurrent) ? rawCurrent : []);
282+
} catch (e) { current = []; }
283+
try {
284+
prevOrder = typeof rawPrev === 'string' ? (JSON.parse(rawPrev || '[]') || []) : (Array.isArray(rawPrev) ? rawPrev : []);
285+
} catch (e) { prevOrder = []; }
286+
const wasEmpty = !prevOrder || prevOrder.length === 0;
287+
const isNowActive = current && current.length > 0;
288+
289+
if (isNowActive && current[0] && typeof current[0] === 'object' && current[0].id) {
290+
currentCombatant = current[0].id;
291+
}
292+
293+
if (combatStarted && !isNowActive) {
294+
combatStarted = false;
295+
//sendChat('System', '🏆 Combat has ended!');
296+
return;
297+
}
298+
299+
if (combatStarted && state[NAME].storedVariables.combatPatrol) {
300+
pathTokens(true);
301+
}
302+
303+
// When Roll20 combat starts
304+
if (!combatStarted && !wasEmpty && isNowActive) {
305+
combatStarted = true;
306+
//sendChat('System', '⚔️ Combat has started!');
307+
sendChat(`${NAME}`, `/w GM ⚔️ Patrols have been stopped! Combat Patrolling is ${state[NAME].storedVariables.combatPatrol ? "enabled" : "disabled"}`);
308+
}
309+
};
310+
270311
// Commands
271-
on("chat:message", function (msg) {
312+
const chatHandler = function (msg) {
272313
try {
273314
if (msg.type === "api" && (msg.content.toLowerCase().startsWith("!token-control") || msg.content.toLowerCase().startsWith("!tc"))) {
274315

@@ -497,44 +538,7 @@ const TokenController = (() => {
497538
log(` ${NAME} Error: ${err}<br/>${errorMessage}`);
498539
errorMessage = "";
499540
}
500-
});
501-
502-
// When Roll20 combat starts
503-
on('change:campaign:turnorder', function (campaign, prev) {
504-
const rawCurrent = campaign.get('turnorder');
505-
const rawPrev = (prev && typeof prev.get === 'function') ? prev.get('turnorder') : prev;
506-
let current = [];
507-
let prevOrder = [];
508-
try {
509-
current = typeof rawCurrent === 'string' ? (JSON.parse(rawCurrent || '[]') || []) : (Array.isArray(rawCurrent) ? rawCurrent : []);
510-
} catch (e) { current = []; }
511-
try {
512-
prevOrder = typeof rawPrev === 'string' ? (JSON.parse(rawPrev || '[]') || []) : (Array.isArray(rawPrev) ? rawPrev : []);
513-
} catch (e) { prevOrder = []; }
514-
const wasEmpty = !prevOrder || prevOrder.length === 0;
515-
const isNowActive = current && current.length > 0;
516-
517-
if (isNowActive && current[0] && typeof current[0] === 'object' && current[0].id) {
518-
currentCombatant = current[0].id;
519-
}
520-
521-
if (combatStarted && !isNowActive) {
522-
combatStarted = false;
523-
//sendChat('System', '🏆 Combat has ended!');
524-
return;
525-
}
526-
527-
if (combatStarted && state[NAME].storedVariables.combatPatrol) {
528-
pathTokens(true);
529-
}
530-
531-
// When Roll20 combat starts
532-
if (!combatStarted && !wasEmpty && isNowActive) {
533-
combatStarted = true;
534-
//sendChat('System', '⚔️ Combat has started!');
535-
sendChat(`${NAME}`, `/w GM ⚔️ Patrols have been stopped! Combat Patrolling is ${state[NAME].storedVariables.combatPatrol ? "enabled" : "disabled"}`);
536-
}
537-
});
541+
};
538542

539543
function cleanupStaleTokens() {
540544
const beforePaths = state[NAME].storedVariables.activeTokenPaths.length;

TokenController/script.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "TokenController",
33
"script": "TokenController.js",
4-
"version": "4.0.0",
5-
"previousversions": ["3.0.0"],
4+
"version": "4.0.1",
5+
"previousversions": ["4.0.0"],
66
"description": "# Token Controller\n\rBring your scene to life with simple but dynamic patrol pathing. Select a pre-defined path or quickly create your own (not map or position dependent, just works with grids), select a token, and click start. Tokens may have multiple paths assigned to them at a time, allowing for intricate path layering. Version 2 will have a Path Building tool!",
7-
"authors": "Scott E. Schwarz",
7+
"authors": "Developesque",
88
"roll20userid": "6975764",
99
"useroptions": [],
1010
"dependencies": [

0 commit comments

Comments
 (0)