forked from RobinKuiper/Roll20APIScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WildMagic.js
94 lines (82 loc) · 3.99 KB
/
WildMagic.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
/*
* Version 0.1.0
* Made By Robin Kuiper
* Skype: RobinKuiper.eu
* Discord: Atheos#1095
* My Discord Server: https://discord.gg/AcC9VME
* Roll20: https://app.roll20.net/users/1226016/robin
* Github: https://github.com/RobinKuiper/Roll20APIScripts
* Reddit: https://www.reddit.com/user/robinkuiper/
* Patreon: https://patreon.com/robinkuiper
* Paypal.me: https://www.paypal.me/robinkuiper
*/
var WildMagic = WildMagic || (function() {
'use strict';
// Change the settings below.
const CHARACTERS = 'Xandir,Whisper'; // A comma seperated list of characters who use Wild Magic (Multiple characters: 'John,Jake,Aron').
const TIDES_MARKER = 'rolling-bomb'; // The status marker used while Tides of Chaos is active.
const TIDES_MACRO = ''; // The name of the Tides of Chaos macro (case sensitive).
const NORMAL_MACRO = ''; // The name of the normal macro (case sensitive).
on('chat:message', (msg) => {
if(msg && msg.rolltemplate && (msg.rolltemplate === 'spell' || msg.rolltemplate === 'atk' || msg.rolltemplate === 'dmg' || msg.rolltemplate === 'atkdmg')){
let tides = false;
let character_name = msg.content.match(/charname=([^\n{}]*[^"\n{}])/);
character_name = RegExp.$1;
let attack_name = msg.content.match(/rname=([^\n{}]*[^"\n{}])/);
attack_name = RegExp.$1;
let character = findObjs({ name: character_name, _type: 'character' }).shift();
let id = (character) ? character.get('id') : false;
if((msg.rolltemplate === 'atk' || msg.rolltemplate === 'dmg' || msg.rolltemplate === 'atkdmg') && (!id || !getObjects(getRepeatingSectionAttrs(id, 'spell-'), 'current', attack_name).length)) return;
let allowed_characters = CHARACTERS.split(',');
if(allowed_characters.includes(character_name)){
let tokens = findObjs({
_type: 'graphic',
name: character_name,
pageid: Campaign().get('playerpageid')
});
if(tokens){
tokens.forEach(token => {
if(tides) return;
if(token.get('status_'+TIDES_MARKER)){
tides = true;
}
})
}
let macros = findObjs({
_type: 'macro',
name: (tides) ? TIDES_MACRO : NORMAL_MACRO
});
if(!macros || !macros.length){
let text = (tides) ? 'Could not find the Tides of Chaos macro, please check it\'s name in the script.' : 'Could not find the normal macro, please check it\'s name in the script.';
sendChat('Wild Magic', text);
return
}
sendChat('Wild Magic', macros.shift().get('action'));
}
}
});
const getRepeatingSectionAttrs = (characterId, sectionName) => {
const prefix = `repeating_${sectionName}`;
return _.filter(findObjs({ type: 'attribute', characterid: characterId }),
attr => attr.get('name').indexOf(prefix) === 0);
}
const getObjects = (obj, key, val) => {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat(getObjects(obj[i], key, val));
} else
//if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
if (i == key && obj[i] == val || i == key && val == '') { //
objects.push(obj);
} else if (obj[i] == val && key == ''){
//only add if the object is not already in the array
if (objects.lastIndexOf(obj) == -1){
objects.push(obj);
}
}
}
return objects;
}
})();