-
Notifications
You must be signed in to change notification settings - Fork 62
/
StatHandler.js
148 lines (136 loc) · 5.77 KB
/
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
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
class StatHandler {
constructor() {
this.cache = {};
//this.token=token;
}
getStat(monsterid, callback, open5eSlug = undefined) {
let self = this;
if (monsterid in this.cache) {
callback(self.cache[monsterid]);
}
else if(monsterid == 'open5e'){
if(open5eSlug in cached_open5e_items){
callback(cached_open5e_items[open5eSlug].monsterData)
}
else{
fetch_monsters([open5eSlug], function (response) {
if (response !== false) {
update_open5e_item_cache(response.map(m => SidebarListItem.open5eMonster(m)));
}
callback(cached_open5e_items[open5eSlug].monsterData)
}, true);
}
}
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, open5eSlug = undefined, tokenId = undefined, adv=false, dis=false) {
let dice = adv ? '2d20kh1+' : dis ? '2d20kl1+' : '1d20+'
if(window.TOKEN_OBJECTS[tokenId].options.combatGroupToken){
let modArray = [];
let statArray = [];
let promises = [];
for(let i in window.TOKEN_OBJECTS){
if(i == tokenId)
continue;
if(window.TOKEN_OBJECTS[i].options.combatGroup == tokenId){
if(window.TOKEN_OBJECTS[i].options.monster =='open5e'){
promises.push(new Promise((resolve, reject) => {
this.getStat(window.TOKEN_OBJECTS[i].options.monster, function(data) {
modArray.push(Math.floor((data.stats[1].value - 10) / 2.0));
statArray.push(data.stats[1].value);
resolve();
}, window.TOKEN_OBJECTS[i].options.itemId);
}));
}
else if(window.TOKEN_OBJECTS[i].options.monster =='customStat'){
let modifier = (window.TOKEN_OBJECTS[i]?.options?.customInit != undefined) ? parseInt(window.TOKEN_OBJECTS[i].options.customInit) : (window.TOKEN_OBJECTS[i]?.options?.customStat != undefined && window.TOKEN_OBJECTS[i]?.options?.customStat[1]?.mod != undefined) ? parseInt(window.TOKEN_OBJECTS[i].options.customStat[1].mod) : 0;
modArray.push(modifier);
statArray.push((window.TOKEN_OBJECTS[i]?.options?.customInit != undefined || (window.TOKEN_OBJECTS[i]?.options?.customStat != undefined && window.TOKEN_OBJECTS[i]?.options?.customStat[1]?.mod != undefined)) ? ((modifier*2)+10) : 0);
}
else {
promises.push(new Promise((resolve, reject) => {
this.getStat(window.TOKEN_OBJECTS[i].options.monster, function(stat) {
modArray.push(Math.floor((stat.data.stats[1].value - 10) / 2.0));
statArray.push(stat.data.stats[1].value);
resolve();
}, window.TOKEN_OBJECTS[i].options.itemId);
}));
}
}
}
Promise.all(promises).then(() =>{
const sumMod = modArray.reduce((a, b) => a + b, 0);
const modifier = (sumMod / modArray.length) || 0;
const sumStat = statArray.reduce((a, b) => a + b, 0);
const stat = (sumStat / statArray.length) || 0;
let expression = dice + modifier;
let roll = new rpgDiceRoller.DiceRoll(expression);
console.log(expression + "->" + roll.total);
let total = parseFloat(Math.floor(roll.total) + stat/100).toFixed(2);
let combatSettingData = getCombatTrackersettings();
if(combatSettingData['tie_breaker'] !='1'){
total = parseInt(total);
}
callback(total);
});
}
else if(monsterid =='open5e')
{
this.getStat(monsterid, function(data) {
let modifier = Math.floor((data.stats[1].value - 10) / 2.0);
let expression = dice + modifier;
let roll = new rpgDiceRoller.DiceRoll(expression);
console.log(expression + "->" + roll.total);
let total = parseFloat(roll.total + data.stats[1].value/100).toFixed(2);
let combatSettingData = getCombatTrackersettings();
if(combatSettingData['tie_breaker'] !='1'){
total = parseInt(total);
}
callback(total);
}, open5eSlug);
}
else if(monsterid =='customStat'){
let modifier = (window.TOKEN_OBJECTS[tokenId]?.options?.customInit != undefined) ? parseInt(window.TOKEN_OBJECTS[tokenId].options.customInit) : (window.TOKEN_OBJECTS[tokenId]?.options?.customStat != undefined && window.TOKEN_OBJECTS[tokenId]?.options?.customStat[1]?.mod != undefined) ? parseInt(window.TOKEN_OBJECTS[tokenId].options.customStat[1].mod) : 0;
let expression = (!isNaN(modifier)) ? dice + modifier : '0';
let roll = new rpgDiceRoller.DiceRoll(expression);
let decimalAdd = (window.TOKEN_OBJECTS[tokenId]?.options?.customInit != undefined || (window.TOKEN_OBJECTS[tokenId]?.options?.customStat != undefined && window.TOKEN_OBJECTS[tokenId]?.options?.customStat[1]?.mod != undefined)) ? ((modifier*2)+10)/100 : 0
console.log(expression + "->" + roll.total);
let total = parseFloat(roll.total + decimalAdd).toFixed(2);
let combatSettingData = getCombatTrackersettings();
if(combatSettingData['tie_breaker'] !='1'){
total = parseInt(total);
}
callback(total);
}
else{
this.getStat(monsterid, function(stat) {
let modifier = Math.floor((stat.data.stats[1].value - 10) / 2.0);
let expression = dice + modifier;
let roll = new rpgDiceRoller.DiceRoll(expression);
console.log(expression + "->" + roll.total);
let total = parseFloat(roll.total + stat.data.stats[1].value/100).toFixed(2);
let combatSettingData = getCombatTrackersettings();
if(combatSettingData['tie_breaker'] !='1'){
total = parseInt(total);
}
callback(total);
}, open5eSlug);
}
}
}