-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathStatHandler.js
More file actions
175 lines (160 loc) · 6.72 KB
/
StatHandler.js
File metadata and controls
175 lines (160 loc) · 6.72 KB
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
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(window.cached_monster_items[monsterid]){
callback({data: window.cached_monster_items[monsterid].monsterData})
}
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)), function(){callback(cached_open5e_items[open5eSlug].monsterData)});
}
}, true);
}
}
else {
fetch_monsters([monsterid], async function (response) {
if (response !== false) {
update_monster_item_cache(response.map(m => SidebarListItem.Monster(m)), function(){callback({data: window.cached_monster_items[monsterid].monsterData})});
}
});
}
}
rollInit(monsterid, callback, open5eSlug = undefined, tokenId = undefined, adv=false, dis=false) {
let dice = adv ? '2d20kh1+' : dis ? '2d20kl1+' : '1d20+'
if(window.all_token_objects[tokenId]?.options?.combatGroupToken){
let modArray = [];
let statArray = [];
let promises = [];
for(let i in window.all_token_objects){
if(i == tokenId )
continue;
if(window.all_token_objects[i].options.combatGroup == tokenId){
if(window.all_token_objects[i].isPlayer()){
const pc = find_pc_by_player_id(i);
modArray.push(pc.initiativeBonus);
statArray.push(pc.abilities[1].score);
}
else if(window.all_token_objects[i].options.monster =='open5e'){
promises.push(new Promise((resolve, reject) => {
this.getStat(window.all_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.all_token_objects[i].options.itemId);
}));
}
else if(window.all_token_objects[i].options.monster =='customStat'){
let modifier = (window.all_token_objects[i]?.options?.customInit != undefined) ? parseInt(window.all_token_objects[i].options.customInit) : (window.all_token_objects[i]?.options?.customStat != undefined && window.all_token_objects[i]?.options?.customStat[1]?.mod != undefined) ? parseInt(window.all_token_objects[i].options.customStat[1].mod) : 0;
modArray.push(modifier);
statArray.push((window.all_token_objects[i]?.options?.customInit != undefined || (window.all_token_objects[i]?.options?.customStat != undefined && window.all_token_objects[i]?.options?.customStat[1]?.mod != undefined)) ? ((modifier*2)+10) : 0);
}
else if(window.all_token_objects[i].options.monster != undefined){
promises.push(new Promise((resolve, reject) => {
this.getStat(window.all_token_objects[i].options.monster, function(stat) {
if(stat.data.initiativeBonus != null){
modArray.push(stat.data.initiativeBonus);
}
else{
modArray.push(Math.floor((stat.data.stats[1].value - 10) / 2.0));
}
statArray.push(stat.data.stats[1].value);
resolve();
}, window.all_token_objects[i].options.itemId);
}));
}
else{
modArray.push(0);
statArray.push(10);
}
}
}
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'){
const currentToken = window.TOKEN_OBJECTS[tokenId];
const options = currentToken?.options;
const customInitStatic = options?.customInitStatic;
const customInitMod = options?.customInit;
const statMod = options?.customStat[1]?.mod || 0;
let total;
if(!customInitMod && customInitStatic != undefined){
let decimalAdd = statMod ? ((parseInt(statMod) * 2) + 10) / 100 : 0
total = parseFloat(parseInt(customInitStatic) + decimalAdd).toFixed(2);
}
else {
let modifier = (customInitMod != undefined) ? parseInt(customInitMod) : (statMod != undefined) ? parseInt(statMod) : 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);
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;
if(stat.data.initiativeBonus != null){
modifier = stat.data.initiativeBonus;
}
else{
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);
}
}
}