-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuser.js
More file actions
91 lines (77 loc) · 2.35 KB
/
user.js
File metadata and controls
91 lines (77 loc) · 2.35 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
import axios from 'axios';
import { global } from './global.js';
import * as api from './4.api.js';
import eventManager from './eventManager.js';
import sleep from './sleep.js';
let selfData;
export function self() {
if (!selfData) {
selfData = Object.freeze({
id: global('selfId'),
username: global('selfUsername'),
mainGroup: global('selfMainGroup'),
});
}
return selfData;
}
export function name(user) {
return user.username;
}
export function isMod(user) {
return isPriority(user, 4);
}
export function isStaff(user) {
return isPriority(user, 6);
}
function isPriority(user, priority) {
if (typeof user?.mainGroup?.priority !== 'number') throw new Error('Invalid user');
return user.mainGroup.priority <= priority;
}
export function getCollection() {
return getDeckConfig().then(({ collection: data }) => parse(data));
}
export function getDecks() {
return getDeckConfig().then(({ decks: data }) => parse(data))
.then((decks) => decks.reduce((val, { soul: { name: soul }, cardsList: cards, artifacts }) => {
val[soul] = { artifacts, cards };
return val;
}, {}));
}
export function getArtifacts() {
return getDeckConfig().then(({ artifacts: data }) => parse(data));
}
export function getAllArtifacts() { // TODO: This isn't really a "user" function
return getDeckConfig().then(({ allArtifacts: data = [] }) => parse(data));
}
function getDeckConfig() {
return new Promise((res) => {
eventManager.on('Deck:Loaded', (data) => res(data));
sleep(500).then(() => res());
}).then((res) => {
if (res) return res;
return axios.get('DecksConfig').then(({ data }) => {
eventManager.singleton.emit('Deck:Loaded', data);
return data;
});
});
}
function parse(data) {
return typeof data === 'string' ? JSON.parse(data) : data;
}
export function getUCP() {
return axios.get(`CardSkinsConfig?action=profile&time=${Date.now()}`)
.then(({ data: { ucp = 0 } }) => ucp);
}
export function getCardSkins() {
return axios.get('CardSkinsConfig?action=profile')
.then(({ data: { cardSkins = '' } }) => JSON.parse(cardSkins));
}
const user = api.mod.user;
user.isMod = isMod;
user.isStaff = isStaff;
user.getCollection = getCollection;
user.getDecks = getDecks;
user.getArtifacts = getArtifacts;
user.getAllArtifacts = getAllArtifacts;
user.getUCP = getUCP;
user.getCardSkins = getCardSkins;