forked from cyruzzo/AboveVTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatHandler.js
43 lines (38 loc) · 986 Bytes
/
StatHandler.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
class StatHandler {
constructor() {
this.cache = {};
//this.token=token;
}
getStat(monsterid, callback) {
let self = this;
if (monsterid in this.cache) {
callback(self.cache[monsterid]);
}
else {
get_cobalt_token(function(token) {
$.ajax({
url: 'https://monster-service.dndbeyond.com/v1/Monster/' + monsterid,
beforeSend: function(xhr) {
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
},
xhrFields: {
withCredentials: true
},
success: function(data) {
self.cache[monsterid] = data;
callback(data);
}
});
});
}
}
rollInit(monsterid, callback) {
this.getStat(monsterid, function(stat) {
var modifier = Math.round((stat.data.stats[1].value - 10) / 2.0);
var expression = "1d20+" + modifier;
var roll = new rpgDiceRoller.DiceRoll(expression);
console.log(expression + "->" + roll.total);
callback(roll.total);
});
}
}