forked from cyruzzo/AboveVTT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncounterHandler.js
More file actions
131 lines (111 loc) · 3.93 KB
/
EncounterHandler.js
File metadata and controls
131 lines (111 loc) · 3.93 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
class EncounterHandler {
/** @return {string} the id of the ABoveVTT encounter that we created for this session */
avttId;
/** @return {object} a map of DDB encounter objects where the key is the encounter id and the value is the encounter object */
encounters = {};
/** @return {object} the AboveVTT encounter object that we created for this session */
get avttEncounter() {
return this.encounters[this.avttId];
}
constructor(avttId) {
if (typeof avttId === "string" && avttId.length > 0) {
this.avttId = avttId;
} else if (is_encounters_page()) {
let urlId = window.location.pathname.split("/").pop();
this.avttId = urlId;
} else {
throw new Error(`Failed to create EncounterHandler with avttId: ${avttId} on page ${window.location.href}`);
}
this.encounters = {};
}
async fetchAllEncounters() {
const encounterList = await DDBApi.fetchAllEncounters();
encounterList.forEach(encounter => this.encounters[encounter.id] = encounter);
return this.encounters;
}
/// We build an encounter named `AboveVTT` this will fetch it if it exists, and create it if it doesn't
fetch_encounter(encounterId, callback) {
if (typeof callback !== 'function') {
callback = function(){};
}
if (this.encounters[encounterId] !== undefined) {
callback(this.encounters[encounterId]);
return;
}
DDBApi.fetchEncounter(encounterId)
.then(encounter => {
window.EncounterHandler.encounters[encounter.id] = encounter;
callback(encounter);
})
.catch(error => {
console.warn(`fetch_encounter failed`, error);
callback(false, error);
});
}
fetch_encounter_monsters(encounterId, callback) {
if (typeof callback !== 'function') {
console.warn("fetch_encounter_monsters was called without a callback");
return;
}
let encounter = this.encounters[encounterId];
if (encounter?.monsters === undefined || encounter.monsters === null || encounter.monsters.length === 0) {
// nothing to fetch
callback([]);
return;
}
let monsterIds = encounter.monsters.map(m => m.id);
if (monsterIds.length > 0) {
console.log("fetch_encounter_monsters starting");
fetch_monsters(monsterIds, callback);
}
}
}
async function fetch_monsters(monsterIds, callback, open5e=false) {
if (typeof callback !== 'function') {
console.warn("fetch_monsters was called without a callback.");
return;
}
if( !Array.isArray(monsterIds) ){
console.warn("fetch_monsters was called without a valid array of monster IDs.");
callback([]);
return;
}
monsterIds = monsterIds.filter(id => id !== null);
if (typeof monsterIds === undefined || monsterIds.length === 0) {
callback([]);
return;
}
if(!open5e){
let uniqueMonsterIds = [...new Set(monsterIds)];
const monsterData = await DDBApi.fetchMonsters(uniqueMonsterIds);
let promises = [];
for(let i=0; i<monsterData.length; i++){
if(monsterData[i].isReleased || monsterData[i].isHomebrew){
promises.push(new Promise(async (resolve, reject) => {
let moreInfo = await DDBApi.fetchMoreInfo(`${monsterData[i].url}`);
let treasure = $(moreInfo)?.find('.treasure-link').closest('.tags');
let treasureLinks = treasure.find('a');
treasureLinks.addClass('tooltip-hover');
treasureLinks.attr('data-moreinfo', function(){
return this.href;
})
treasure = treasure.html();
let gear = $(moreInfo)?.find('.mon-stat-block-2024__tidbit-label:contains("Gear")').siblings('.mon-stat-block-2024__tidbit-data').html();
let initMod, initScore;
monsterData[i].treasure = treasure;
monsterData[i].gear = gear;
resolve();
}))
}
}
Promise.all(promises).then(() => {
callback(monsterData);
});
}
else{
let uniqueMonsterIds = [...new Set(monsterIds)];
let queryParam = uniqueMonsterIds.map(id => `${id}`).join("%2C");
let groupOpen5e = await getGroupOpen5e(queryParam);
callback(groupOpen5e);
}
}