-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.js
131 lines (116 loc) · 5.27 KB
/
client.js
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
const { Connector} = require('./connector');
const { Card } = require('./card');
const { ChallengeView } = require('./challenge');
const { Player } = require('./player');
const { BattleLog } = require('./battle');
const { Location, ClanRanking } = require('./location');
const { PlayerRanking } = require('./season');
const { Tournament, GlobalTournament } = require('./tournament');
const { Rarity } = require('./utils');
class Client {
constructor(apiToken) {
this.connector = new Connector(apiToken);
}
async getCards(rarity = [Rarity.COMMON, Rarity.RARE, Rarity.EPIC, Rarity.LEGENDARY, Rarity.CHAMPION],
name = []) {
rarity = Connector.pack(rarity);
name = Connector.pack(name);
const cards = (await this.connector.request('cards')).items;
return cards
.map(Card.fromJSON)
.filter(card => rarity.includes(card.rarity))
.filter(card => name.length === 0 ? true : name.includes(card.name));
}
async getChallenges() {
const challenges = (await this.connector.request('challenges'));
return challenges
.map(ChallengeView.fromJSON);
}
async getPlayer(tag) {
// Allow getPlayer to be called on a tagged object
tag = this.argumentToString(`getPlayer(${tag})`, tag, 'tag');
const player = await this.connector.request(['players', tag]);
return Player.fromJSON(player);
}
async getUpcomingChests(tag, index = undefined) {
// Allow getUpcomingChests to be called on a tagged object
tag = this.argumentToString(`getUpcomingChests(${tag}, ${index})`, tag, 'tag');
const chests = (await this.connector.request(['players', tag, 'upcomingchests'])).items;
if (index) {
return chests
.filter(chest => chest.index === index)
.map(chest => chest.name)[0];
} else {
return chests;
}
}
async getBattleLog(tag) {
// Allow getBattleLog to be called on a tagged object
tag = this.argumentToString(`getBattleLog(${tag})`, tag, 'tag');
const battleLog = await this.connector.request(['players', tag, 'battlelog']);
return BattleLog.fromJSON(battleLog);
}
async getLocations(limit = undefined) {
const limitQuery = limit === undefined ? {} : {limit: limit};
const locations = (await this.connector.request('locations', limitQuery)).items;
return locations
.map(Location.fromJSON);
}
async getClanRanks(locationId, clanWars = false, limit = undefined) {
// Allow getClanRanks to be called on an identified object
locationId = this.argumentToString(`getClanRanks(${location}, ${clanWars}, ${limit})`, locationId, 'id');
const limitQuery = limit === undefined ? {} : {limit: limit};
const clanRanks = (await this.connector.request(
['locations', locationId, 'rankings', clanWars ? 'clanwars' : 'clans'], limitQuery)).items;
return clanRanks
.map(ClanRanking.fromJSON);
}
async getPlayerRanks(seasonId, pathOfLegends = false, limit = undefined) {
if (pathOfLegends) {
throw new Error('[!] PathOfLegends option is currently not being supported by the API.');
}
// Allow getPlayerRanks to be called on an identified object
seasonId = this.argumentToString(`getClanRanks(${seasonId}, ${pathOfLegends}, ${limit})`, seasonId, 'id');
const limitQuery = limit === undefined ? {} : {limit: limit};
const playerRanks = (await this.connector.request(
['locations', 'global', pathOfLegends ? 'pathoflegend' : 'seasons', seasonId, 'rankings', 'players'],
limitQuery)).items;
return playerRanks
.map(PlayerRanking.fromJSON);
}
async getTournaments(name, limit = undefined) {
// Allow getTournaments to be called on a named object
name = this.argumentToString(`getTournaments(${name}, ${limit})`, name, 'name');
const query = limit === undefined ? {name: name} : {name: name, limit: limit};
const tournaments = (await this.connector.request('tournaments', query)).items;
return tournaments
.map(Tournament.fromJSON);
}
async getTournament(tag) {
// Allow getTournaments to be called on a tagged object
tag = this.argumentToString(`getTournament(${tag})`, tag, 'tag');
const tournament = await this.connector.request(['tournaments', tag]);
return Tournament.fromJSON(tournament);
}
async getGlobalTournament() {
const tournaments = (await this.connector.request('globaltournaments')).items;
return tournaments
.map(GlobalTournament.fromJSON);
}
argumentToString(caller, object, key) {
if (object === null || object === undefined) {
throw new Error(`[!] ${caller}: Empty argument provided`);
}
switch (object.constructor.name) {
case 'String': return object;
case 'Object': {
if (object[key]) {
return object[key];
} else {
throw new Error(`[!] ${caller}: API failure, maybe you provided an invalid argument.`);
}
}
}
}
}
module.exports = {Client}