Skip to content

Commit 39b0635

Browse files
committed
Efficiency improvement
Two function calls were being called on every chat entry when they didn't need to be.
1 parent ea04e4e commit 39b0635

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

Triggered Cleanup/script.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "Triggered Cleanup",
33
"script": "Triggered Cleanup.js",
4-
"version": "1.0",
5-
"previousversions": [],
6-
"description": "Originally designed for a specific table, this script was made with one-offs and LC/Westmarches in mind. Many APIs find difficulties in running scripts when numerous tokens are left on a table. This script removes all tokens linked to a character sheet when using the command [!cleanup] in the chat. It also deletes tokens when the linked character sheet is deleted. It will ignore any tokens on a map that has the word [bullpen] in the name (not case-sensitive)",
4+
"version": "1.1",
5+
"previousversions": [1.0],
6+
"description": "Originally designed for a specific table, this script was made with one-offs and LCs/Westmarches in mind. Many APIs find difficulties in running scripts when numerous tokens are left on a table. This script removes all tokens linked to a character sheet when using the command [!cleanup] in the chat. It also deletes tokens when the linked character sheet is deleted. It will ignore any tokens on a map that has the word [bullpen] in the name (not case-sensitive)",
77
"authors": "Jeffrey Simons",
88
"roll20userid": "4069317",
99
"useroptions": [],
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Improved efficiency by reorganizing when certain calls were made.
2+
3+
Found that two findObjs calls were performed with every chat entry. This should now only trigger when the API command is used.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* This script wildelete all tokens that are linked to a character when entering [!cleanup] in the chat (not case-sensitive).
2+
The script will also delete tokens for character sheets that are deleted.
3+
The script will not delete any tokens on a map with the phrase "Bullpen" (not case sensitive) in the name.
4+
*/
5+
on('destroy:character',function(obj){
6+
log(obj.get('_id'))
7+
let tokens = findObjs({ type: 'graphic', represents: obj.get('_id') });
8+
const inMap = new Array()
9+
let maps = findObjs({type: 'page'})
10+
for (let i = 0; i < maps.length; i++) {
11+
if (maps[i].get('name').toLowerCase().includes('bullpen')){
12+
inMap.push(maps[i])
13+
}
14+
}
15+
for(let i = 0; i < tokens.length; i++){
16+
let flag = 1
17+
for(let j = 0; j < inMap.length; j++){
18+
if(tokens[i].get('_pageid')===inMap[j].get('_id')){
19+
log("inMap Name: " + inMap[j].get('name'))
20+
flag = 0
21+
}
22+
}
23+
if (flag){
24+
tokens[i].remove();
25+
}
26+
}
27+
})
28+
29+
on("chat:message", function(msg) {
30+
if(msg.content.toLowerCase() === "!cleanup"){
31+
let tokens = findObjs({ type: 'graphic' });
32+
const inMap = new Array()
33+
let maps = findObjs({type: 'page'})
34+
for (let i = 0; i < maps.length; i++) {
35+
if (maps[i].get('name').toLowerCase().includes('bullpen')){
36+
inMap.push(maps[i])
37+
}
38+
}
39+
for(let i = 0; i < tokens.length; i++){
40+
log("Token: " + tokens[i].get('_id') + " & " + tokens[i].get('represents'))
41+
let flag = 1
42+
for(let j = 0; j < inMap.length; j++){
43+
if(tokens[i].get('represents') === ''){
44+
flag = 0
45+
}
46+
if(tokens[i].get('_pageid')===inMap[j].get('_id')){
47+
flag = 0
48+
}
49+
}
50+
if (flag){
51+
tokens[i].remove();
52+
}
53+
}
54+
}
55+
})

0 commit comments

Comments
 (0)