clashofclans.js 3.0.0-dev.392ca4c → 3.0.0-dev.577f128
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +66 -47
- package/README.md +12 -8
- package/dist/client/Client.d.ts +38 -3
- package/dist/client/Client.js +32 -19
- package/dist/client/PollingClient.d.ts +107 -34
- package/dist/client/PollingClient.js +269 -8
- package/dist/index.d.ts +0 -1
- package/dist/index.js +5 -2
- package/dist/index.mjs +5 -2
- package/dist/rest/HTTPError.d.ts +0 -4
- package/dist/rest/HTTPError.js +1 -5
- package/dist/rest/RESTManager.d.ts +46 -5
- package/dist/rest/RESTManager.js +55 -30
- package/dist/rest/RequestHandler.d.ts +30 -2
- package/dist/rest/RequestHandler.js +69 -33
- package/dist/struct/CapitalRaidSeason.d.ts +49 -0
- package/dist/struct/CapitalRaidSeason.js +40 -0
- package/dist/struct/Clan.d.ts +8 -2
- package/dist/struct/Clan.js +3 -0
- package/dist/struct/ClanCapital.js +1 -1
- package/dist/struct/ClanMember.d.ts +3 -1
- package/dist/struct/ClanMember.js +1 -0
- package/dist/struct/ClanWar.d.ts +15 -3
- package/dist/struct/ClanWar.js +26 -15
- package/dist/struct/Player.d.ts +4 -2
- package/dist/struct/Player.js +1 -0
- package/dist/struct/Ranking.d.ts +1 -1
- package/dist/struct/Unit.d.ts +0 -2
- package/dist/struct/Unit.js +0 -2
- package/dist/struct/index.d.ts +2 -0
- package/dist/struct/index.js +7 -1
- package/dist/types/api.d.ts +93 -0
- package/dist/types/index.js +5 -1
- package/dist/types/lib.d.ts +4 -4
- package/dist/util/Constants.d.ts +38 -3
- package/dist/util/Constants.js +25 -6
- package/dist/util/Util.js +2 -2
- package/dist/util/raw.json +1 -1
- package/package.json +6 -59
- package/dist/client/EventManager.d.ts +0 -86
- package/dist/client/EventManager.js +0 -278
|
@@ -1,25 +1,286 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PollingClient = void 0;
|
|
4
|
+
const HTTPError_1 = require("../rest/HTTPError");
|
|
4
5
|
const Constants_1 = require("../util/Constants");
|
|
5
6
|
const Client_1 = require("./Client");
|
|
6
|
-
const EventManager_1 = require("./EventManager");
|
|
7
7
|
/**
|
|
8
|
-
* Represents
|
|
8
|
+
* Represents a Polling Event Client.
|
|
9
9
|
* ```js
|
|
10
10
|
* const { PollingClient } = require('clashofclans.js');
|
|
11
|
-
* const
|
|
11
|
+
* const pollingClient = new PollingClient({ keys: ['***'] });
|
|
12
12
|
* ```
|
|
13
13
|
*/
|
|
14
14
|
class PollingClient extends Client_1.Client {
|
|
15
15
|
constructor(options) {
|
|
16
16
|
super(options);
|
|
17
|
-
this.
|
|
17
|
+
this._clanTags = new Set();
|
|
18
|
+
this._playerTags = new Set();
|
|
19
|
+
this._warTags = new Set();
|
|
20
|
+
this._clans = new Map();
|
|
21
|
+
this._players = new Map();
|
|
22
|
+
this._wars = new Map();
|
|
23
|
+
this._pollingEvents = {
|
|
24
|
+
clans: [],
|
|
25
|
+
wars: [],
|
|
26
|
+
players: []
|
|
27
|
+
};
|
|
28
|
+
this.inMaintenance = Boolean(false);
|
|
29
|
+
this._maintenanceStartTime = null;
|
|
18
30
|
}
|
|
19
|
-
/**
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
31
|
+
/** Initialize the PollingEvent Manager to start pulling the data by polling api. */
|
|
32
|
+
async init() {
|
|
33
|
+
this.seasonEndHandler();
|
|
34
|
+
this.maintenanceHandler();
|
|
35
|
+
this.clanUpdateHandler();
|
|
36
|
+
this.playerUpdateHandler();
|
|
37
|
+
this.warUpdateHandler();
|
|
38
|
+
return Promise.resolve(this.eventNames());
|
|
39
|
+
}
|
|
40
|
+
/** Add clan tags to clan polling events. */
|
|
41
|
+
addClans(tags) {
|
|
42
|
+
for (const tag of Array.isArray(tags) ? tags : [tags]) {
|
|
43
|
+
this._clanTags.add(this.util.formatTag(tag));
|
|
44
|
+
}
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
/** Delete clan tags from clan polling events. */
|
|
48
|
+
deleteClans(tags) {
|
|
49
|
+
for (const tag of Array.isArray(tags) ? tags : [tags]) {
|
|
50
|
+
const key = this.util.formatTag(tag);
|
|
51
|
+
this._clans.delete(key);
|
|
52
|
+
this._clanTags.delete(key);
|
|
53
|
+
}
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
/** Add player tags for player polling events. */
|
|
57
|
+
addPlayers(tags) {
|
|
58
|
+
for (const tag of Array.isArray(tags) ? tags : [tags]) {
|
|
59
|
+
this._playerTags.add(this.util.formatTag(tag));
|
|
60
|
+
}
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
/** Delete player tags from player polling events. */
|
|
64
|
+
deletePlayers(tags) {
|
|
65
|
+
for (const tag of Array.isArray(tags) ? tags : [tags]) {
|
|
66
|
+
const key = this.util.formatTag(tag);
|
|
67
|
+
this._players.delete(key);
|
|
68
|
+
this._playerTags.delete(key);
|
|
69
|
+
}
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
/** Add clan tags for war polling events. */
|
|
73
|
+
addWars(tags) {
|
|
74
|
+
for (const tag of Array.isArray(tags) ? tags : [tags]) {
|
|
75
|
+
this._warTags.add(this.util.formatTag(tag));
|
|
76
|
+
}
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
/** Delete clan tags from war polling events. */
|
|
80
|
+
deleteWars(tags) {
|
|
81
|
+
for (const tag of Array.isArray(tags) ? tags : [tags]) {
|
|
82
|
+
const key = this.util.formatTag(tag);
|
|
83
|
+
this._wars.delete(`${key}:${1}`);
|
|
84
|
+
this._wars.delete(`${key}:${2}`);
|
|
85
|
+
this._warTags.delete(key);
|
|
86
|
+
}
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Set your own custom clan polling event.
|
|
91
|
+
*
|
|
92
|
+
* In order to emit the custom polling event, you must have this filter function that returns a boolean.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```js
|
|
96
|
+
* client.addClans(['#2PP', '#8QU8J9LP']);
|
|
97
|
+
*
|
|
98
|
+
* client.setClanEvent({
|
|
99
|
+
* name: 'clanMemberUpdate',
|
|
100
|
+
* filter: (oldClan, newClan) => {
|
|
101
|
+
* return oldClan.memberCount !== newClan.memberCount;
|
|
102
|
+
* }
|
|
103
|
+
* });
|
|
104
|
+
*
|
|
105
|
+
* client.on('clanMemberUpdate', (oldClan, newClan) => {
|
|
106
|
+
* console.log(oldClan.memberCount, newClan.memberCount);
|
|
107
|
+
* });
|
|
108
|
+
*
|
|
109
|
+
* (async function () {
|
|
110
|
+
* await client.init();
|
|
111
|
+
* })();
|
|
112
|
+
* ```
|
|
113
|
+
* @returns
|
|
114
|
+
*/
|
|
115
|
+
setClanEvent(event) {
|
|
116
|
+
if (!event.name)
|
|
117
|
+
throw new Error('Event name is required.');
|
|
118
|
+
if (typeof event.filter !== 'function')
|
|
119
|
+
throw new Error('Filter function is required.');
|
|
120
|
+
this._pollingEvents.clans.push(event);
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Set your own custom war event.
|
|
125
|
+
*
|
|
126
|
+
* In order to emit the custom event, you must have this filter function that returns a boolean.
|
|
127
|
+
*/
|
|
128
|
+
setWarEvent(event) {
|
|
129
|
+
if (!event.name)
|
|
130
|
+
throw new Error('Event name is required.');
|
|
131
|
+
if (typeof event.filter !== 'function')
|
|
132
|
+
throw new Error('Filter function is required.');
|
|
133
|
+
this._pollingEvents.wars.push(event);
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Set your own custom player event.
|
|
138
|
+
*
|
|
139
|
+
* In order to emit the custom event, you must have this filter function that returns a boolean.
|
|
140
|
+
*/
|
|
141
|
+
setPlayerEvent(event) {
|
|
142
|
+
if (!event.name)
|
|
143
|
+
throw new Error('Event name is required.');
|
|
144
|
+
if (typeof event.filter !== 'function')
|
|
145
|
+
throw new Error('Filter function is required.');
|
|
146
|
+
this._pollingEvents.players.push(event);
|
|
147
|
+
return this;
|
|
148
|
+
}
|
|
149
|
+
async maintenanceHandler() {
|
|
150
|
+
setTimeout(this.maintenanceHandler.bind(this), 10000).unref();
|
|
151
|
+
if (!(this.listenerCount(Constants_1.PollingEvents.MaintenanceStart) && this.listenerCount(Constants_1.PollingEvents.MaintenanceEnd)))
|
|
152
|
+
return;
|
|
153
|
+
try {
|
|
154
|
+
const res = await this.rest.getClans({ maxMembers: Math.floor(Math.random() * 40) + 10, limit: 1 });
|
|
155
|
+
if (res.status === 200 && this.inMaintenance) {
|
|
156
|
+
this.inMaintenance = Boolean(false);
|
|
157
|
+
const duration = Date.now() - this._maintenanceStartTime.getTime();
|
|
158
|
+
this._maintenanceStartTime = null;
|
|
159
|
+
this.emit(Constants_1.PollingEvents.MaintenanceEnd, duration);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
catch (error) {
|
|
163
|
+
if (error instanceof HTTPError_1.HTTPError && error.status === 503 && !this.inMaintenance) {
|
|
164
|
+
this.inMaintenance = Boolean(true);
|
|
165
|
+
this._maintenanceStartTime = new Date();
|
|
166
|
+
this.emit(Constants_1.PollingEvents.MaintenanceStart);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
seasonEndHandler() {
|
|
171
|
+
const end = this.util.getSeasonEndTime().getTime() - Date.now();
|
|
172
|
+
// Why this? setTimeout can be up to 24.8 days or 2147483647ms [(2^31 - 1) Max 32bit Integer]
|
|
173
|
+
if (end > 24 * 60 * 60 * 1000) {
|
|
174
|
+
setTimeout(this.seasonEndHandler.bind(this), 60 * 60 * 1000);
|
|
175
|
+
}
|
|
176
|
+
else if (end > 0) {
|
|
177
|
+
setTimeout(() => {
|
|
178
|
+
this.emit(Constants_1.PollingEvents.NewSeasonStart, this.util.getSeasonId());
|
|
179
|
+
}, end + 100).unref();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
async clanUpdateHandler() {
|
|
183
|
+
this.emit(Constants_1.PollingEvents.ClanLoopStart);
|
|
184
|
+
for (const tag of this._clanTags)
|
|
185
|
+
await this.runClanUpdate(tag);
|
|
186
|
+
this.emit(Constants_1.PollingEvents.ClanLoopEnd);
|
|
187
|
+
setTimeout(this.clanUpdateHandler.bind(this), 10000);
|
|
188
|
+
}
|
|
189
|
+
async playerUpdateHandler() {
|
|
190
|
+
this.emit(Constants_1.PollingEvents.PlayerLoopStart);
|
|
191
|
+
for (const tag of this._playerTags)
|
|
192
|
+
await this.runPlayerUpdate(tag);
|
|
193
|
+
this.emit(Constants_1.PollingEvents.PlayerLoopEnd);
|
|
194
|
+
setTimeout(this.playerUpdateHandler.bind(this), 10000);
|
|
195
|
+
}
|
|
196
|
+
async warUpdateHandler() {
|
|
197
|
+
this.emit(Constants_1.PollingEvents.WarLoopStart);
|
|
198
|
+
for (const tag of this._warTags)
|
|
199
|
+
await this.runWarUpdate(tag);
|
|
200
|
+
this.emit(Constants_1.PollingEvents.WarLoopEnd);
|
|
201
|
+
setTimeout(this.warUpdateHandler.bind(this), 10000);
|
|
202
|
+
}
|
|
203
|
+
async runClanUpdate(tag) {
|
|
204
|
+
if (this.inMaintenance)
|
|
205
|
+
return null;
|
|
206
|
+
const clan = await this.getClan(tag).catch(() => null);
|
|
207
|
+
if (!clan)
|
|
208
|
+
return null;
|
|
209
|
+
const cached = this._clans.get(clan.tag);
|
|
210
|
+
if (!cached)
|
|
211
|
+
return this._clans.set(clan.tag, clan);
|
|
212
|
+
for (const { name, filter } of this._pollingEvents.clans) {
|
|
213
|
+
try {
|
|
214
|
+
if (!(await filter(cached, clan)))
|
|
215
|
+
continue;
|
|
216
|
+
this.emit(name, cached, clan);
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
this.emit(Constants_1.PollingEvents.Error, error);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return this._clans.set(clan.tag, clan);
|
|
223
|
+
}
|
|
224
|
+
async runPlayerUpdate(tag) {
|
|
225
|
+
if (this.inMaintenance)
|
|
226
|
+
return null;
|
|
227
|
+
const player = await this.getPlayer(tag).catch(() => null);
|
|
228
|
+
if (!player)
|
|
229
|
+
return null;
|
|
230
|
+
const cached = this._players.get(player.tag);
|
|
231
|
+
if (!cached)
|
|
232
|
+
return this._players.set(player.tag, player);
|
|
233
|
+
for (const { name, filter } of this._pollingEvents.players) {
|
|
234
|
+
try {
|
|
235
|
+
if (!(await filter(cached, player)))
|
|
236
|
+
continue;
|
|
237
|
+
this.emit(name, cached, player);
|
|
238
|
+
}
|
|
239
|
+
catch (error) {
|
|
240
|
+
this.emit(Constants_1.PollingEvents.Error, error);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return this._players.set(player.tag, player);
|
|
244
|
+
}
|
|
245
|
+
async runWarUpdate(tag) {
|
|
246
|
+
if (this.inMaintenance)
|
|
247
|
+
return null;
|
|
248
|
+
const clanWars = await this.getWars(tag).catch(() => null);
|
|
249
|
+
if (!clanWars?.length)
|
|
250
|
+
return null;
|
|
251
|
+
clanWars.forEach(async (war, index) => {
|
|
252
|
+
const key = `${tag}:${index}`;
|
|
253
|
+
const cached = this._wars.get(key);
|
|
254
|
+
if (!cached)
|
|
255
|
+
return this._wars.set(key, war);
|
|
256
|
+
for (const { name, filter } of this._pollingEvents.wars) {
|
|
257
|
+
try {
|
|
258
|
+
if (!(await filter(cached, war)))
|
|
259
|
+
continue;
|
|
260
|
+
this.emit(name, cached, war);
|
|
261
|
+
}
|
|
262
|
+
catch (error) {
|
|
263
|
+
this.emit(Constants_1.PollingEvents.Error, error);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
// check for war end
|
|
267
|
+
if (index === 1 && cached.warTag !== war.warTag) {
|
|
268
|
+
const data = await this.getLeagueWar({ clanTag: tag, round: 'PreviousRound' }).catch(() => null);
|
|
269
|
+
if (data && data.warTag === cached.warTag) {
|
|
270
|
+
for (const { name, filter } of this._pollingEvents.wars) {
|
|
271
|
+
try {
|
|
272
|
+
if (!(await filter(cached, data)))
|
|
273
|
+
continue;
|
|
274
|
+
this.emit(name, cached, data);
|
|
275
|
+
}
|
|
276
|
+
catch (error) {
|
|
277
|
+
this.emit(Constants_1.PollingEvents.Error, error);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return this._wars.set(key, war);
|
|
283
|
+
});
|
|
23
284
|
}
|
|
24
285
|
}
|
|
25
286
|
exports.PollingClient = PollingClient;
|
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,6 @@ export * from './client/PollingClient';
|
|
|
3
3
|
export * from './rest/RESTManager';
|
|
4
4
|
export * from './rest/RequestHandler';
|
|
5
5
|
export * from './rest/HTTPError';
|
|
6
|
-
export * from './client/EventManager';
|
|
7
6
|
export * from './rest/Throttler';
|
|
8
7
|
export * from './util/Util';
|
|
9
8
|
export * from './struct';
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
@@ -15,7 +19,6 @@ __exportStar(require("./client/PollingClient"), exports);
|
|
|
15
19
|
__exportStar(require("./rest/RESTManager"), exports);
|
|
16
20
|
__exportStar(require("./rest/RequestHandler"), exports);
|
|
17
21
|
__exportStar(require("./rest/HTTPError"), exports);
|
|
18
|
-
__exportStar(require("./client/EventManager"), exports);
|
|
19
22
|
__exportStar(require("./rest/Throttler"), exports);
|
|
20
23
|
__exportStar(require("./util/Util"), exports);
|
|
21
24
|
__exportStar(require("./struct"), exports);
|
package/dist/index.mjs
CHANGED
|
@@ -8,8 +8,11 @@ export const BatchThrottler = mod.BatchThrottler;
|
|
|
8
8
|
export const BuilderTroops = mod.BuilderTroops;
|
|
9
9
|
export const CWLRounds = mod.CWLRounds;
|
|
10
10
|
export const CacheStore = mod.CacheStore;
|
|
11
|
+
export const CapitalRaidSeason = mod.CapitalRaidSeason;
|
|
12
|
+
export const CapitalRaidSeasonMember = mod.CapitalRaidSeasonMember;
|
|
11
13
|
export const ChatLanguage = mod.ChatLanguage;
|
|
12
14
|
export const Clan = mod.Clan;
|
|
15
|
+
export const ClanCapital = mod.ClanCapital;
|
|
13
16
|
export const ClanMember = mod.ClanMember;
|
|
14
17
|
export const ClanWar = mod.ClanWar;
|
|
15
18
|
export const ClanWarAttack = mod.ClanWarAttack;
|
|
@@ -20,6 +23,7 @@ export const ClanWarLeagueRound = mod.ClanWarLeagueRound;
|
|
|
20
23
|
export const ClanWarLog = mod.ClanWarLog;
|
|
21
24
|
export const ClanWarMember = mod.ClanWarMember;
|
|
22
25
|
export const Client = mod.Client;
|
|
26
|
+
export const ClientEvents = mod.ClientEvents;
|
|
23
27
|
export const DarkElixirSpells = mod.DarkElixirSpells;
|
|
24
28
|
export const DarkElixirTroops = mod.DarkElixirTroops;
|
|
25
29
|
export const DevSiteAPIBaseURL = mod.DevSiteAPIBaseURL;
|
|
@@ -39,11 +43,9 @@ export const Leagues = mod.Leagues;
|
|
|
39
43
|
export const LegendLeagueId = mod.LegendLeagueId;
|
|
40
44
|
export const LegendStatistics = mod.LegendStatistics;
|
|
41
45
|
export const Location = mod.Location;
|
|
42
|
-
export const NotInWarError = mod.NotInWarError;
|
|
43
46
|
export const Player = mod.Player;
|
|
44
47
|
export const PlayerClan = mod.PlayerClan;
|
|
45
48
|
export const PollingClient = mod.PollingClient;
|
|
46
|
-
export const PollingEventManager = mod.PollingEventManager;
|
|
47
49
|
export const PollingEvents = mod.PollingEvents;
|
|
48
50
|
export const PrivateWarLogError = mod.PrivateWarLogError;
|
|
49
51
|
export const QueueThrottler = mod.QueueThrottler;
|
|
@@ -52,6 +54,7 @@ export const RankedClan = mod.RankedClan;
|
|
|
52
54
|
export const RankedPlayer = mod.RankedPlayer;
|
|
53
55
|
export const RawData = mod.RawData;
|
|
54
56
|
export const RequestHandler = mod.RequestHandler;
|
|
57
|
+
export const RestEvents = mod.RestEvents;
|
|
55
58
|
export const Season = mod.Season;
|
|
56
59
|
export const SeasonRankedPlayer = mod.SeasonRankedPlayer;
|
|
57
60
|
export const SiegeMachines = mod.SiegeMachines;
|
package/dist/rest/HTTPError.d.ts
CHANGED
|
@@ -19,10 +19,6 @@ export declare class HTTPError extends Error {
|
|
|
19
19
|
maxAge: number;
|
|
20
20
|
constructor(error: any, status: number, path: string, maxAge: number, method?: string);
|
|
21
21
|
}
|
|
22
|
-
export declare const NotInWarError: {
|
|
23
|
-
message: string;
|
|
24
|
-
reason: string;
|
|
25
|
-
};
|
|
26
22
|
export declare const PrivateWarLogError: {
|
|
27
23
|
message: string;
|
|
28
24
|
reason: string;
|
package/dist/rest/HTTPError.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PrivateWarLogError = exports.
|
|
3
|
+
exports.PrivateWarLogError = exports.HTTPError = void 0;
|
|
4
4
|
const messages = {
|
|
5
5
|
500: 'Unknown error happened when handling the request.',
|
|
6
6
|
504: 'The user aborted this request.',
|
|
@@ -32,10 +32,6 @@ class HTTPError extends Error {
|
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
exports.HTTPError = HTTPError;
|
|
35
|
-
exports.NotInWarError = {
|
|
36
|
-
message: 'Clan is not in war at this moment.',
|
|
37
|
-
reason: 'notInWar'
|
|
38
|
-
};
|
|
39
35
|
exports.PrivateWarLogError = {
|
|
40
36
|
message: 'Access denied, clan war log is private.',
|
|
41
37
|
reason: 'privateWarLog'
|
|
@@ -1,10 +1,43 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { EventEmitter } from 'node:events';
|
|
2
3
|
import { Util } from '../util/Util';
|
|
3
|
-
import { APIClan, APIClanList, APIClanMemberList, APIClanRankingList, APIClanVersusRankingList, APIClanWar, APIClanWarLeagueGroup, APIClanWarLog, APIGoldPassSeason, APILabelList, APILeague, APILeagueList, APILeagueSeasonList, APILocation, APILocationList, APIPlayer, APIPlayerRankingList, APIPlayerSeasonRankingList, APIPlayerVersusRankingList, APIVerifyToken, APIWarLeague, APIWarLeagueList, SearchOptions, ClanSearchOptions, RESTOptions, OverrideOptions, LoginOptions } from '../types';
|
|
4
|
+
import { APIClan, APIClanList, APIClanMemberList, APICapitalRaidSeasons, APIClanRankingList, APIClanVersusRankingList, APIClanWar, APIClanWarLeagueGroup, APIClanWarLog, APIGoldPassSeason, APILabelList, APILeague, APILeagueList, APILeagueSeasonList, APILocation, APILocationList, APIPlayer, APIPlayerRankingList, APIPlayerSeasonRankingList, APIPlayerVersusRankingList, APIVerifyToken, APIWarLeague, APIWarLeagueList, SearchOptions, ClanSearchOptions, RESTOptions, OverrideOptions, LoginOptions, APICapitalLeagueList, APICapitalLeague, APIClanCapitalRankingList } from '../types';
|
|
5
|
+
import { RestEvents } from '../util/Constants';
|
|
6
|
+
import { RequestHandler } from './RequestHandler';
|
|
7
|
+
export interface IRestEvents {
|
|
8
|
+
[RestEvents.Error]: [error: unknown];
|
|
9
|
+
[RestEvents.Debug]: [path: string, status: number, message: string];
|
|
10
|
+
[RestEvents.RateLimited]: [path: string, status: number, message: string];
|
|
11
|
+
}
|
|
12
|
+
export interface RESTManager {
|
|
13
|
+
emit: (<K extends keyof IRestEvents>(event: K, ...args: IRestEvents[K]) => boolean) & (<S extends string | symbol>(event: Exclude<S, keyof IRestEvents>, ...args: any[]) => boolean);
|
|
14
|
+
off: (<K extends keyof IRestEvents>(event: K, listener: (...args: IRestEvents[K]) => void) => this) & (<S extends string | symbol>(event: Exclude<S, keyof IRestEvents>, listener: (...args: any[]) => void) => this);
|
|
15
|
+
on: (<K extends keyof IRestEvents>(event: K, listener: (...args: IRestEvents[K]) => void) => this) & (<S extends string | symbol>(event: Exclude<S, keyof IRestEvents>, listener: (...args: any[]) => void) => this);
|
|
16
|
+
once: (<K extends keyof IRestEvents>(event: K, listener: (...args: IRestEvents[K]) => void) => this) & (<S extends string | symbol>(event: Exclude<S, keyof IRestEvents>, listener: (...args: any[]) => void) => this);
|
|
17
|
+
removeAllListeners: (<K extends keyof IRestEvents>(event?: K) => this) & (<S extends string | symbol>(event?: Exclude<S, keyof IRestEvents>) => this);
|
|
18
|
+
/**
|
|
19
|
+
* Emitted for general debugging information.
|
|
20
|
+
* @public
|
|
21
|
+
* @event
|
|
22
|
+
*/
|
|
23
|
+
debug: string;
|
|
24
|
+
/**
|
|
25
|
+
* Emitted when the client encounters an error.
|
|
26
|
+
* @public
|
|
27
|
+
* @event
|
|
28
|
+
*/
|
|
29
|
+
error: string;
|
|
30
|
+
/**
|
|
31
|
+
* Emitted when the client is rate limited.
|
|
32
|
+
* @public
|
|
33
|
+
* @event
|
|
34
|
+
*/
|
|
35
|
+
rateLimited: string;
|
|
36
|
+
}
|
|
4
37
|
/** Represents a REST Manager of the client. */
|
|
5
|
-
export declare class RESTManager {
|
|
38
|
+
export declare class RESTManager extends EventEmitter {
|
|
6
39
|
/** Request Handler for the RESTManager. */
|
|
7
|
-
|
|
40
|
+
requestHandler: RequestHandler;
|
|
8
41
|
constructor(options?: RESTOptions);
|
|
9
42
|
/** Contains various general-purpose utility methods. */
|
|
10
43
|
get util(): typeof Util;
|
|
@@ -33,14 +66,20 @@ export declare class RESTManager {
|
|
|
33
66
|
getClanWarLeagueGroup(clanTag: string, options?: OverrideOptions): Promise<import("../types").Response<APIClanWarLeagueGroup>>;
|
|
34
67
|
/** Get info about a CWL round by WarTag. */
|
|
35
68
|
getClanWarLeagueRound(warTag: string, options?: OverrideOptions): Promise<import("../types").Response<APIClanWar>>;
|
|
69
|
+
/** Retrieve clan's capital raid seasons. */
|
|
70
|
+
getCapitalRaidSeasons(tag: string, options?: OverrideOptions): Promise<import("../types").Response<APICapitalRaidSeasons>>;
|
|
36
71
|
/** Get info about a player by tag. */
|
|
37
72
|
getPlayer(playerTag: string, options?: OverrideOptions): Promise<import("../types").Response<APIPlayer>>;
|
|
38
73
|
/** Verify Player API token that can be found from the Game settings. */
|
|
39
74
|
verifyPlayerToken(playerTag: string, token: string, options?: OverrideOptions): Promise<import("../types").Response<APIVerifyToken>>;
|
|
40
|
-
/** Get list of Leagues. */
|
|
75
|
+
/** Get a list of Leagues. */
|
|
41
76
|
getLeagues(options?: SearchOptions): Promise<import("../types").Response<APILeagueList>>;
|
|
42
77
|
/** Get a League info. */
|
|
43
78
|
getLeague(leagueId: string | number, options?: OverrideOptions): Promise<import("../types").Response<APILeague>>;
|
|
79
|
+
/** Get a list of Capital leagues. */
|
|
80
|
+
getCapitalLeagues(options?: SearchOptions): Promise<import("../types").Response<APICapitalLeagueList>>;
|
|
81
|
+
/** Get a Capital League info. */
|
|
82
|
+
getCapitalLeague(leagueId: string | number, options?: OverrideOptions): Promise<import("../types").Response<APICapitalLeague>>;
|
|
44
83
|
/** Get Legend League season Ids. */
|
|
45
84
|
getLeagueSeasons(leagueId: number, options?: SearchOptions): Promise<import("../types").Response<APILeagueSeasonList>>;
|
|
46
85
|
/** Get Legend League season rankings by season Id. */
|
|
@@ -61,6 +100,8 @@ export declare class RESTManager {
|
|
|
61
100
|
getVersusClanRanks(locationId: number | string, options?: SearchOptions): Promise<import("../types").Response<APIClanVersusRankingList>>;
|
|
62
101
|
/** Get player versus rankings for a specific location. */
|
|
63
102
|
getVersusPlayerRanks(locationId: number | string, options?: SearchOptions): Promise<import("../types").Response<APIPlayerVersusRankingList>>;
|
|
103
|
+
/** Get clan capital rankings for a specific location. */
|
|
104
|
+
getClanCapitalRanks(locationId: number | string, options?: SearchOptions): Promise<import("../types").Response<APIClanCapitalRankingList>>;
|
|
64
105
|
/** Get list of clan labels. */
|
|
65
106
|
getClanLabels(options?: SearchOptions): Promise<import("../types").Response<APILabelList>>;
|
|
66
107
|
/** Get list of player labels. */
|
package/dist/rest/RESTManager.js
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RESTManager = void 0;
|
|
4
|
-
const
|
|
4
|
+
const node_events_1 = require("node:events");
|
|
5
5
|
const Util_1 = require("../util/Util");
|
|
6
|
+
const Constants_1 = require("../util/Constants");
|
|
7
|
+
const RequestHandler_1 = require("./RequestHandler");
|
|
6
8
|
/** Represents a REST Manager of the client. */
|
|
7
|
-
class RESTManager {
|
|
9
|
+
class RESTManager extends node_events_1.EventEmitter {
|
|
8
10
|
constructor(options) {
|
|
9
|
-
|
|
11
|
+
super();
|
|
12
|
+
this.requestHandler = new RequestHandler_1.RequestHandler(options)
|
|
13
|
+
.on(Constants_1.RestEvents.Debug, this.emit.bind(this, Constants_1.RestEvents.Debug))
|
|
14
|
+
.on(Constants_1.RestEvents.Error, this.emit.bind(this, Constants_1.RestEvents.Error))
|
|
15
|
+
.on(Constants_1.RestEvents.RateLimited, this.emit.bind(this, Constants_1.RestEvents.RateLimited));
|
|
10
16
|
}
|
|
11
17
|
/** Contains various general-purpose utility methods. */
|
|
12
18
|
get util() {
|
|
@@ -21,122 +27,141 @@ class RESTManager {
|
|
|
21
27
|
* ```
|
|
22
28
|
*/
|
|
23
29
|
login(options) {
|
|
24
|
-
return this.
|
|
30
|
+
return this.requestHandler.init(options);
|
|
25
31
|
}
|
|
26
32
|
/** Set Clash of Clans API keys. */
|
|
27
33
|
setKeys(keys) {
|
|
28
|
-
this.
|
|
34
|
+
this.requestHandler.setKeys(keys);
|
|
29
35
|
return this;
|
|
30
36
|
}
|
|
31
37
|
/** Search all clans by name and/or filtering the results using various criteria. */
|
|
32
38
|
getClans(query, options) {
|
|
33
|
-
return this.
|
|
39
|
+
return this.requestHandler.request(`/clans${Util_1.Util.queryString(query)}`, options);
|
|
34
40
|
}
|
|
35
41
|
/** Get info about a clan. */
|
|
36
42
|
getClan(clanTag, options) {
|
|
37
|
-
return this.
|
|
43
|
+
return this.requestHandler.request(`/clans/${Util_1.Util.encodeURI(clanTag)}`, options);
|
|
38
44
|
}
|
|
39
45
|
/** Get list of clan members. */
|
|
40
46
|
getClanMembers(clanTag, options) {
|
|
41
47
|
const query = Util_1.Util.queryString(options);
|
|
42
|
-
return this.
|
|
48
|
+
return this.requestHandler.request(`/clans/${Util_1.Util.encodeURI(clanTag)}/members${query}`, options);
|
|
43
49
|
}
|
|
44
50
|
/** Get clan war log. */
|
|
45
51
|
getClanWarLog(clanTag, options) {
|
|
46
52
|
const query = Util_1.Util.queryString(options);
|
|
47
|
-
return this.
|
|
53
|
+
return this.requestHandler.request(`/clans/${Util_1.Util.encodeURI(clanTag)}/warlog${query}`, options);
|
|
48
54
|
}
|
|
49
55
|
/** Get info about currently running war in the clan. */
|
|
50
56
|
getCurrentWar(clanTag, options) {
|
|
51
|
-
return this.
|
|
57
|
+
return this.requestHandler.request(`/clans/${Util_1.Util.encodeURI(clanTag)}/currentwar`, options);
|
|
52
58
|
}
|
|
53
59
|
/** Get info about clan war league. */
|
|
54
60
|
getClanWarLeagueGroup(clanTag, options) {
|
|
55
|
-
return this.
|
|
61
|
+
return this.requestHandler.request(`/clans/${Util_1.Util.encodeURI(clanTag)}/currentwar/leaguegroup`, options);
|
|
56
62
|
}
|
|
57
63
|
/** Get info about a CWL round by WarTag. */
|
|
58
64
|
getClanWarLeagueRound(warTag, options) {
|
|
59
|
-
return this.
|
|
65
|
+
return this.requestHandler.request(`/clanwarleagues/wars/${Util_1.Util.encodeURI(warTag)}`, options);
|
|
66
|
+
}
|
|
67
|
+
/** Retrieve clan's capital raid seasons. */
|
|
68
|
+
getCapitalRaidSeasons(tag, options) {
|
|
69
|
+
const query = Util_1.Util.queryString(options);
|
|
70
|
+
return this.requestHandler.request(`/clans/${Util_1.Util.encodeURI(tag)}/capitalraidseasons${query}`, options);
|
|
60
71
|
}
|
|
61
72
|
/** Get info about a player by tag. */
|
|
62
73
|
getPlayer(playerTag, options) {
|
|
63
|
-
return this.
|
|
74
|
+
return this.requestHandler.request(`/players/${Util_1.Util.encodeURI(playerTag)}`, options);
|
|
64
75
|
}
|
|
65
76
|
/** Verify Player API token that can be found from the Game settings. */
|
|
66
77
|
verifyPlayerToken(playerTag, token, options) {
|
|
67
78
|
const opts = { method: 'POST', body: JSON.stringify({ token }), ...options };
|
|
68
|
-
return this.
|
|
79
|
+
return this.requestHandler.request(`/players/${Util_1.Util.encodeURI(playerTag)}/verifytoken`, opts);
|
|
69
80
|
}
|
|
70
|
-
/** Get list of Leagues. */
|
|
81
|
+
/** Get a list of Leagues. */
|
|
71
82
|
getLeagues(options) {
|
|
72
83
|
const query = Util_1.Util.queryString(options);
|
|
73
|
-
return this.
|
|
84
|
+
return this.requestHandler.request(`/leagues${query}`, options);
|
|
74
85
|
}
|
|
75
86
|
/** Get a League info. */
|
|
76
87
|
getLeague(leagueId, options) {
|
|
77
|
-
return this.
|
|
88
|
+
return this.requestHandler.request(`/leagues/${leagueId}`, options);
|
|
89
|
+
}
|
|
90
|
+
/** Get a list of Capital leagues. */
|
|
91
|
+
getCapitalLeagues(options) {
|
|
92
|
+
const query = Util_1.Util.queryString(options);
|
|
93
|
+
return this.requestHandler.request(`/capitalleagues${query}`, options);
|
|
94
|
+
}
|
|
95
|
+
/** Get a Capital League info. */
|
|
96
|
+
getCapitalLeague(leagueId, options) {
|
|
97
|
+
return this.requestHandler.request(`/capitalleagues/${leagueId}`, options);
|
|
78
98
|
}
|
|
79
99
|
/** Get Legend League season Ids. */
|
|
80
100
|
getLeagueSeasons(leagueId, options) {
|
|
81
101
|
const query = Util_1.Util.queryString(options);
|
|
82
|
-
return this.
|
|
102
|
+
return this.requestHandler.request(`/leagues/${leagueId}/seasons${query}`, options);
|
|
83
103
|
}
|
|
84
104
|
/** Get Legend League season rankings by season Id. */
|
|
85
105
|
getSeasonRankings(leagueId, seasonId, options) {
|
|
86
106
|
const query = Util_1.Util.queryString(options);
|
|
87
|
-
return this.
|
|
107
|
+
return this.requestHandler.request(`/leagues/${leagueId}/seasons/${seasonId}${query}`, options);
|
|
88
108
|
}
|
|
89
109
|
/** Get list of Clan War Leagues. */
|
|
90
110
|
getWarLeagues(options) {
|
|
91
111
|
const query = Util_1.Util.queryString(options);
|
|
92
|
-
return this.
|
|
112
|
+
return this.requestHandler.request(`/warleagues${query}`, options);
|
|
93
113
|
}
|
|
94
114
|
/** Get info about a Clan War League. */
|
|
95
115
|
getWarLeague(leagueId, options) {
|
|
96
|
-
return this.
|
|
116
|
+
return this.requestHandler.request(`/warleagues/${leagueId}`, options);
|
|
97
117
|
}
|
|
98
118
|
/** Get list of Locations. */
|
|
99
119
|
getLocations(options) {
|
|
100
120
|
const query = Util_1.Util.queryString(options);
|
|
101
|
-
return this.
|
|
121
|
+
return this.requestHandler.request(`/locations${query}`, options);
|
|
102
122
|
}
|
|
103
123
|
/** Get info about a Location. */
|
|
104
124
|
getLocation(locationId, options) {
|
|
105
|
-
return this.
|
|
125
|
+
return this.requestHandler.request(`/locations/${locationId}`, options);
|
|
106
126
|
}
|
|
107
127
|
/** Get clan rankings for a specific location. */
|
|
108
128
|
getClanRanks(locationId, options) {
|
|
109
129
|
const query = Util_1.Util.queryString(options);
|
|
110
|
-
return this.
|
|
130
|
+
return this.requestHandler.request(`/locations/${locationId}/rankings/clans${query}`, options);
|
|
111
131
|
}
|
|
112
132
|
/** Get player rankings for a specific location. */
|
|
113
133
|
getPlayerRanks(locationId, options) {
|
|
114
134
|
const query = Util_1.Util.queryString(options);
|
|
115
|
-
return this.
|
|
135
|
+
return this.requestHandler.request(`/locations/${locationId}/rankings/players${query}`, options);
|
|
116
136
|
}
|
|
117
137
|
/** Get clan versus rankings for a specific location. */
|
|
118
138
|
getVersusClanRanks(locationId, options) {
|
|
119
139
|
const query = Util_1.Util.queryString(options);
|
|
120
|
-
return this.
|
|
140
|
+
return this.requestHandler.request(`/locations/${locationId}/rankings/clans-versus${query}`, options);
|
|
121
141
|
}
|
|
122
142
|
/** Get player versus rankings for a specific location. */
|
|
123
143
|
getVersusPlayerRanks(locationId, options) {
|
|
124
144
|
const query = Util_1.Util.queryString(options);
|
|
125
|
-
return this.
|
|
145
|
+
return this.requestHandler.request(`/locations/${locationId}/rankings/players-versus${query}`, options);
|
|
146
|
+
}
|
|
147
|
+
/** Get clan capital rankings for a specific location. */
|
|
148
|
+
getClanCapitalRanks(locationId, options) {
|
|
149
|
+
const query = Util_1.Util.queryString(options);
|
|
150
|
+
return this.requestHandler.request(`/locations/${locationId}/rankings/capitals${query}`, options);
|
|
126
151
|
}
|
|
127
152
|
/** Get list of clan labels. */
|
|
128
153
|
getClanLabels(options) {
|
|
129
154
|
const query = Util_1.Util.queryString(options);
|
|
130
|
-
return this.
|
|
155
|
+
return this.requestHandler.request(`/labels/clans${query}`, options);
|
|
131
156
|
}
|
|
132
157
|
/** Get list of player labels. */
|
|
133
158
|
getPlayerLabels(options) {
|
|
134
159
|
const query = Util_1.Util.queryString(options);
|
|
135
|
-
return this.
|
|
160
|
+
return this.requestHandler.request(`/labels/players${query}`, options);
|
|
136
161
|
}
|
|
137
162
|
/** Get info about gold pass season. */
|
|
138
163
|
getGoldPassSeason(options) {
|
|
139
|
-
return this.
|
|
164
|
+
return this.requestHandler.request('/goldpass/seasons/current', options);
|
|
140
165
|
}
|
|
141
166
|
}
|
|
142
167
|
exports.RESTManager = RESTManager;
|