-
Notifications
You must be signed in to change notification settings - Fork 1
/
DnDBeyondSpellPointsV2.js
357 lines (356 loc) · 15.9 KB
/
DnDBeyondSpellPointsV2.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
// ==UserScript==
// @name DnDBeyond Spell Points (v2)
// @description Spell point tracker
// @version 2
// @author Mwr247
// @namespace Mwr247
// @homepageURL https://github.com/Mwr247/DnDBeyondSpellPointsV2
// @include https://www.dndbeyond.com/*characters/*
// @run-at document-idle
// @grant none
// ==/UserScript==
(() => {
'use strict';
const sp = [
// Class Level, Sorc Points, Spell Points, Max Slot Level
[1,0,4,1],
[2,2,6,1],
[3,3,14,2],
[4,4,17,2],
[5,5,27,3],
[6,6,32,3],
[7,7,38,4],
[8,8,44,4],
[9,9,57,5],
[10,10,64,5],
[11,11,73,6],
[12,12,73,6],
[13,13,83,7],
[14,14,83,7],
[15,15,94,8],
[16,16,94,8],
[17,17,107,9],
[18,18,114,9],
[19,19,123,9],
[20,20,133,9]
];
const sc = [
// Spell Level, Point Cost, Limit of 1
[1,2,false],
[2,3,false],
[3,5,false],
[4,6,false],
[5,7,false],
[6,8,true],
[7,10,true],
[8,11,true],
[9,13,true]
];
const player = {
id: location.pathname.split('/characters/')[1].split('/')[0],
level: null,
points: 0,
maxPoints: 0,
maxSpellLevel: 0,
data: null
};
let loaded = 10;
let spSystem = null;
let useSpellPoints = null;
let mergeSorcPoints = null;
let token = null;
let tokenExpires = 0;
const getToken = cb => {
console.log('refreshing token');
fetch('https://auth-service.dndbeyond.com/v1/cobalt-token', {
method: 'POST',
credentials: 'include'
}).then(resp => resp.json()).then(data => {
console.log('token updated');
token = data.token;
tokenExpires = Date.now() + data.ttl * 1000 - 10000;
cb();
}).catch(error => console.error(error));
};
const getData = (path='', obj={}, cb=()=>{}) => {
console.log('loading data');
const dataCall = (path, obj, cb) => () => {
console.log('data call to', path);
obj.headers = Object.assign(obj.headers || {}, {'Content-type': 'application/json;charset=utf-8', 'Authorization': 'Bearer ' + token});
if (obj.body) {obj.body = JSON.stringify(obj.body);}
fetch('https://character-service.dndbeyond.com/character/v4/' + path, obj).then(resp => resp.json()).then(data => {
cb(data.data);
}).catch(error => console.error(error));
};
if (token == null || tokenExpires <= Date.now()) {
getToken(dataCall(path, obj, cb));
} else {
dataCall(path, obj, cb)();
}
};
setTimeout(() => {
getData('character/' + player.id, {}, data => {
player.data = data;
spSystem = (player.data?.customActions || []).find(act => act.name === 'Spell Points');
useSpellPoints = spSystem?.isProficient === true;
mergeSorcPoints = spSystem?.isMartialArts === true;
const classes = data.classes.map(cl => {
const isCaster = (cl.definition.canCastSpells == true || cl.subclassDefinition?.canCastSpells == true) && cl.definition.id !== 7;
const level = cl.level || 1;
const divisor = cl.definition.spellRules?.multiClassSpellSlotDivisor || cl.subclassDefinition?.spellRules?.multiClassSpellSlotDivisor || 1;
const rounder = cl.definition.spellRules?.multiClassSpellSlotRounding || cl.subclassDefinition?.spellRules?.multiClassSpellSlotRounding || 1;
return isCaster * Math[rounder === 1 ? 'floor' : 'ceil'](level / divisor);
});
player.level = classes.reduce((a, b) => a + b, 0) || 1;
const sorcPoints = ((player.data?.actions?.class || []).find(act => act?.id === '1031') || {}).limitedUse?.maxUses || 0;
player.maxPoints = sorcPoints * mergeSorcPoints + sp[player.level - 1][2];
});
}, 100);
const init = () => {
const content = document.getElementById('character-tools-target');
if (!content) {return;}
const sheet = [...content.getElementsByClassName('ct-character-header-desktop')].length;
if (sheet) {
if (!useSpellPoints) {return;}
console.log('Spell point tracker active');
player.points = Math.max(player.maxPoints - (spSystem?.fixedValue || 0) * 1, 0);
player.maxSpellLevel = sp[player.level - 1][3];
const setPoints = val => {
val = Math.max(Math.min(val, player.maxPoints), 0);
player.points = val;
const tmp = Object.assign(spSystem, {characterId: +player.id, fixedValue: (player.maxPoints - val) || null});
getData('custom/action', {method: 'PUT', body: tmp}, data => {
console.log('updated spell point action');
spSystem.fixedValue = tmp.fixedValue;
});
(document.getElementById('pointsDisplay') || {}).innerText = player.points + ' / ' + player.maxPoints;
};
const cast = level => {
const cost = sc[level - 1][1];
return evt => {
if (player.points >= cost){
setPoints(player.points - cost);
console.log('cast level', level, 'spell with', cost, 'points');
} else {
alert('Insufficient spell points');
}
if (!sc[level - 1][2]) {evt.stopPropagation();}
};
};
const castClick = evt => {
console.log('checking levels');
setTimeout(() => {
[...content.getElementsByClassName('ct-content-group')].forEach(el => {
if (!/^CANTRIP/.test(el.innerText)) {
const level = +el.innerText[0];
console.log('level', level);
const lvl = el.querySelector('.ct-content-group__header-content');
if (!lvl.spFlag){
lvl.spFlag = true;
lvl.innerText += ' (Cost ' + sc[level - 1][1] + ')';
}
[...el.getElementsByClassName('ddbc-button')].filter(ele => /CAST$/.test(ele.innerText) && !ele.evtFlag).forEach(ele => {
ele.evtFlag = true;
ele.addEventListener('click', cast(level));
});
[...el.getElementsByClassName('ct-spells-spell')].filter(ele => !ele.evtFlag).forEach(ele => {
ele.evtFlag = true;
ele.addEventListener('click', panelOpenClick);
});
}
});
}, 10);
};
const actionCastClick = evt => {
console.log('checking actions');
setTimeout(() => {
[...content.getElementsByClassName('ddbc-combat-attack--spell')].filter(ele => !ele.evtFlag).forEach(ele => {
ele.evtFlag = true;
ele.addEventListener('click', panelOpenClick);
});
}, 10);
};
const panelOpenClick = evt => {
console.log('opened panel');
setTimeout(() => {
const spDetail = document.getElementsByClassName('ct-spell-detail')[0];
if (spDetail != null) {
const spCast = spDetail.querySelector('.ct-spell-caster__casting-action > button');
spCast.innerHTML = spCast.innerHTML.replace('Spell Slot', 'Spell Points');
const spLvl = spDetail.getElementsByClassName('ct-spell-caster__casting-level-current')[0];
const spCost = spDetail.getElementsByClassName('ct-spell-caster__casting-action-count--spellcasting')[0];
console.log('spell level:', spLvl.innerText[0]);
spCast.spLvl = spLvl.innerText[0];
spCost.innerText = sc[+spCast.spLvl - 1][1];
spCast.addEventListener('click', evt => cast(+spCast.spLvl)(evt));
[...spDetail.getElementsByClassName('ct-spell-caster__casting-level-action')].forEach(ele => {
ele.addEventListener('click', evt => {
setTimeout(() => {
console.log('spell level:', spLvl.innerText[0]);
spCast.spLvl = spLvl.innerText[0];
spCost.innerText = sc[+spCast.spLvl - 1][1];
}, 10);
});
});
}
}, 50);
};
const actionClick = evt => {
console.log('clicked actions');
setTimeout(() => {
[...content.querySelectorAll('.ct-actions__content .ddbc-tab-options__header')].forEach(ele => ele.addEventListener('click', actionCastClick));
actionCastClick(evt);
}, 50);
};
const spellClick = evt => {
console.log('clicked spells');
setTimeout(() => {
let tmp = content.getElementsByClassName('ct-spells-level-casting__info-group')[2];
let pdc = tmp.cloneNode(true);
pdc.childNodes[1].innerText = 'Spell Points';
pdc.childNodes[0].childNodes[0].innerText = '';
let pdSub = document.createElement('span');
pdSub.innerText = '–';
pdSub.style.color = '#BB0000';
pdSub.style.userSelect = 'none';
pdSub.style.cursor = 'pointer';
pdSub.addEventListener('click', evt => {
setPoints(player.points - 1);
});
pdc.childNodes[0].childNodes[0].appendChild(pdSub);
let pd = document.createElement('span');
pd.innerText = player.points + ' / ' + player.maxPoints;
pd.id = 'pointsDisplay';
pd.style.margin = '0 4px';
pd.style.cursor = 'pointer';
pd.addEventListener('click', evt => {
let val = prompt('Override Spell Points', player.points);
if (val == null) {return;}
else {val = +val;}
if (val >= 0) {setPoints(val);}
else {alert('Invalid point value');}
});
pdc.childNodes[0].childNodes[0].appendChild(pd);
let pdAdd = document.createElement('span');
pdAdd.innerText = '+';
pdAdd.style.color = '#00BB00';
pdAdd.style.userSelect = 'none';
pdAdd.style.cursor = 'pointer';
pdAdd.addEventListener('click', evt => {
setPoints(player.points + 1);
});
pdc.childNodes[0].childNodes[0].appendChild(pdAdd);
tmp.parentNode.appendChild(pdc);
[...content.querySelectorAll('.ct-spells__content .ddbc-tab-options__header')].forEach(ele => ele.addEventListener('click', castClick));
content.getElementsByClassName('ct-spells-filter__input')[0].addEventListener('input', castClick);
castClick(evt);
}, 50);
};
const rest = evt => {
setPoints(player.maxPoints);
};
const restClick = evt => {
setTimeout(() => {
document.querySelector('.ct-reset-pane__action .ct-button--confirm').addEventListener('click', rest);
}, 50);
};
content.getElementsByClassName('ct-primary-box__tab--spells')[0].addEventListener('click', spellClick);
content.getElementsByClassName('ct-primary-box__tab--actions')[0].addEventListener('click', actionClick);
actionClick();
content.querySelector('.ct-character-header-desktop__group--long-rest .ct-character-header-desktop__button').addEventListener('click', restClick);
loaded = 0;
} else if (/\/builder/.test(window.location.pathname)) {
const hashChange = evt => {
if (/\/home\/basic/.test(window.location.pathname)) {
setTimeout(() => {
useSpellPoints = spSystem?.isProficient === true;
mergeSorcPoints = spSystem?.isMartialArts === true;
const opt = [...content.getElementsByClassName('builder-field builder-field-toggles')].find(ele => /Optional Features/.test(ele.innerText));
const tmp = opt.getElementsByClassName('builder-field-toggles-field')[0];
const useSp = tmp.cloneNode(true);
useSp.childNodes[1].innerText = 'Use Spell Points (Variant Rule)';
useSp.childNodes[0].childNodes[0].classList.remove('ddbc-toggle-field--is-enabled', 'ddbc-toggle-field--is-disabled');
useSp.childNodes[0].childNodes[0].classList.add(useSpellPoints ? 'ddbc-toggle-field--is-enabled' : 'ddbc-toggle-field--is-disabled');
useSp.childNodes[0].addEventListener('click', evt => {
if (spSystem != null) {
const tmp = Object.assign(spSystem, {characterId: +player.id, isProficient: !spSystem.isProficient});
getData('custom/action', {method: 'PUT', body: tmp}, data => {
console.log('updated spell point action');
spSystem.isProficient = tmp.isProficient;
});
} else {
getData('custom/action', {method: 'POST', body: {'characterId': +player.id, 'name': 'Spell Points', 'actionType': '3'}}, data => {
console.log('created spell point action');
(player.data?.customActions || []).push(data);
spSystem = (player.data?.customActions || []).find(act => act?.name === 'Spell Points');
const tmp = Object.assign(spSystem, {characterId: +player.id, isProficient: !spSystem.isProficient});
getData('custom/action', {method: 'PUT', body: tmp}, data => {
console.log('updated spell point action');
spSystem.isProficient = tmp.isProficient;
});
});
}
useSpellPoints = !useSpellPoints;
useSp.childNodes[0].childNodes[0].classList.remove('ddbc-toggle-field--is-enabled', 'ddbc-toggle-field--is-disabled');
useSp.childNodes[0].childNodes[0].classList.add(useSpellPoints ? 'ddbc-toggle-field--is-enabled' : 'ddbc-toggle-field--is-disabled');
});
tmp.parentNode.appendChild(useSp);
const mergeSp = tmp.cloneNode(true);
mergeSp.childNodes[1].innerText = 'Combine Spell Points with Sorcery Points';
mergeSp.childNodes[0].childNodes[0].classList.remove('ddbc-toggle-field--is-enabled', 'ddbc-toggle-field--is-disabled');
mergeSp.childNodes[0].childNodes[0].classList.add(mergeSorcPoints ? 'ddbc-toggle-field--is-enabled' : 'ddbc-toggle-field--is-disabled');
mergeSp.childNodes[0].addEventListener('click', evt => {
if (spSystem != null) {
const tmp = Object.assign(spSystem, {characterId: +player.id, isMartialArts: !spSystem.isMartialArts});
getData('custom/action', {method: 'PUT', body: tmp}, data => {
console.log('updated spell point action');
spSystem.isMartialArts = tmp.isMartialArts;
});
} else {
getData('custom/action', {method: 'POST', body: {'characterId': +player.id, 'name': 'Spell Points', 'actionType': '3'}}, data => {
console.log('created spell point action');
(player.data?.customActions || []).push(data);
spSystem = (player.data?.customActions || []).find(act => act?.name === 'Spell Points');
const tmp = Object.assign(spSystem, {characterId: +player.id, isMartialArts: !spSystem.isMartialArts});
getData('custom/action', {method: 'PUT', body: tmp}, data => {
console.log('updated spell point action');
spSystem.isMartialArts = tmp.isMartialArts;
});
});
}
mergeSorcPoints = !mergeSorcPoints;
mergeSp.childNodes[0].childNodes[0].classList.remove('ddbc-toggle-field--is-enabled', 'ddbc-toggle-field--is-disabled');
mergeSp.childNodes[0].childNodes[0].classList.add(mergeSorcPoints ? 'ddbc-toggle-field--is-enabled' : 'ddbc-toggle-field--is-disabled');
});
tmp.parentNode.appendChild(mergeSp);
}, 50);
}
};
hashChange();
loaded = 0;
} else {
if (loaded-- > 0) {
console.log('attempting to load point tracker...');
setTimeout(init, 1000);
}else {
console.log('point tracker failed to load');
}
return;
}
// console.log('player', player);
};
let initializer = null;
let prevUrl = '';
const obs = new MutationObserver(mut => {
if (location.href !== prevUrl) {
prevUrl = location.href;
let delay = 1000;
if (/\/builder/.test(window.location.pathname) && loaded === 0) {
delay = 0;
}
clearTimeout(initializer);
initializer = setTimeout(init, delay);
}
});
obs.observe(document, {subtree: true, childList: true});
})();