|
| 1 | +import axios from 'axios'; |
1 | 2 | import { global } from './global'; |
2 | 3 | import * as api from './4.api'; |
| 4 | +import eventManager from './eventManager'; |
| 5 | +import sleep from './sleep'; |
3 | 6 |
|
4 | 7 | let selfData; |
5 | 8 | export function self() { |
@@ -30,6 +33,43 @@ function isPriority(user, priority) { |
30 | 33 | return user.mainGroup.priority <= priority; |
31 | 34 | } |
32 | 35 |
|
| 36 | +export function getCollection() { |
| 37 | + return getDeckConfig().then(({ collection: data }) => parse(data)); |
| 38 | +} |
| 39 | + |
| 40 | +export function getDecks() { |
| 41 | + return getDeckConfig().then(({ decks: data }) => parse(data)); |
| 42 | +} |
| 43 | + |
| 44 | +export function getArtifacts() { |
| 45 | + return getDeckConfig().then(({ artifacts: data }) => parse(data)); |
| 46 | +} |
| 47 | + |
| 48 | +export function getAllArtifacts() { |
| 49 | + return getDeckConfig().then(({ allArtifacts: data }) => parse(data)); |
| 50 | +} |
| 51 | + |
| 52 | +function getDeckConfig() { |
| 53 | + return new Promise((res) => { |
| 54 | + eventManager.on('Deck:Loaded', (data) => res(data)); |
| 55 | + sleep(500).then(() => res()); |
| 56 | + }).then((res) => { |
| 57 | + if (res) return res; |
| 58 | + return axios.get('DecksConfig').then(({ data }) => { |
| 59 | + eventManager.singleton.emit('Deck:Loaded', data); |
| 60 | + return data; |
| 61 | + }); |
| 62 | + }); |
| 63 | +} |
| 64 | + |
| 65 | +function parse(data) { |
| 66 | + return typeof data === 'string' ? JSON.parse(data) : data; |
| 67 | +} |
| 68 | + |
33 | 69 | const user = api.mod.user; |
34 | 70 | user.isMod = isMod; |
35 | 71 | user.isStaff = isStaff; |
| 72 | +user.getCollection = getCollection; |
| 73 | +user.getDecks = getDecks; |
| 74 | +user.getArtifacts = getArtifacts; |
| 75 | +user.getAllArtifacts = getAllArtifacts; |
0 commit comments