-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathquests.js
More file actions
54 lines (47 loc) · 1.33 KB
/
quests.js
File metadata and controls
54 lines (47 loc) · 1.33 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
import axios from 'axios';
import Quest, { getId } from 'src/structures/quests/Quest.js';
import eventManager from './eventManager.js';
/**
* @type {Map<number, Quest>}
*/
const quests = new Map();
let fetching = false;
export function getQuests() {
return [...quests.values()];
}
function update(data, { callback, event = true }) {
const previous = getQuests().map((q) => q.clone());
$(data).find('.questTable progress').parentsUntil('tbody', 'tr').each((_, el) => {
const id = getId(el);
if (!id) return; // Pass quest or malformed data
if (quests.has(id)) {
quests.get(id).update(el);
} else {
quests.set(id, new Quest(el));
}
});
const updated = getQuests();
if (event) eventManager.emit('questProgress', updated, previous);
if (typeof callback === 'function') {
callback({
quests: updated,
previous,
});
}
}
export function fetch(callback, event = true) {
if (fetching) return;
fetching = true;
axios.get('/Quests').then((response) => {
fetching = false;
update(response.data, { callback, event });
}).catch((error) => {
fetching = false;
console.error(error);
if (typeof callback === 'function') callback({ error });
});
}
eventManager.on(':load:Quests', () => {
update(document, { event: false });
});
eventManager.on('getVictory getDefeat', fetch);