clashofclans.js 3.0.0-dev.392ca4c → 3.0.0-dev.5d2e14a
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 +64 -47
- package/README.md +12 -8
- package/dist/client/Client.d.ts +29 -2
- package/dist/client/Client.js +14 -6
- package/dist/client/PollingClient.d.ts +106 -33
- package/dist/client/PollingClient.js +266 -7
- package/dist/index.d.ts +0 -1
- package/dist/index.js +5 -2
- package/dist/index.mjs +2 -1
- package/dist/rest/RESTManager.d.ts +39 -4
- package/dist/rest/RESTManager.js +40 -29
- package/dist/rest/RequestHandler.d.ts +30 -2
- package/dist/rest/RequestHandler.js +12 -6
- package/dist/struct/CapitalRaidSeason.d.ts +29 -0
- package/dist/struct/CapitalRaidSeason.js +22 -0
- package/dist/struct/Clan.d.ts +1 -1
- package/dist/struct/ClanCapital.js +1 -1
- package/dist/struct/Player.d.ts +1 -1
- 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.js +5 -1
- package/dist/types/api.d.ts +58 -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,278 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PollingEventManager = void 0;
|
|
4
|
-
const HTTPError_1 = require("../rest/HTTPError");
|
|
5
|
-
const Constants_1 = require("../util/Constants");
|
|
6
|
-
const Util_1 = require("../util/Util");
|
|
7
|
-
/** Represents PollingEvent Manager of the {@link PollingClient}. */
|
|
8
|
-
class PollingEventManager {
|
|
9
|
-
constructor(pollingClient) {
|
|
10
|
-
this.pollingClient = pollingClient;
|
|
11
|
-
this._clanTags = new Set();
|
|
12
|
-
this._playerTags = new Set();
|
|
13
|
-
this._warTags = new Set();
|
|
14
|
-
this._clans = new Map();
|
|
15
|
-
this._players = new Map();
|
|
16
|
-
this._wars = new Map();
|
|
17
|
-
this._pollingEvents = {
|
|
18
|
-
clans: [],
|
|
19
|
-
wars: [],
|
|
20
|
-
players: []
|
|
21
|
-
};
|
|
22
|
-
this._inMaintenance = Boolean(false);
|
|
23
|
-
this._maintenanceStartTime = null;
|
|
24
|
-
}
|
|
25
|
-
/** Initialize the PollingEvent Manager to start pulling the data by polling api. */
|
|
26
|
-
async init() {
|
|
27
|
-
this.seasonEndHandler();
|
|
28
|
-
this.maintenanceHandler();
|
|
29
|
-
this.clanUpdateHandler();
|
|
30
|
-
this.playerUpdateHandler();
|
|
31
|
-
this.warUpdateHandler();
|
|
32
|
-
return Promise.resolve(this.pollingClient.eventNames());
|
|
33
|
-
}
|
|
34
|
-
/** Add clan tags to clan polling events. */
|
|
35
|
-
addClans(tags) {
|
|
36
|
-
for (const tag of Array.isArray(tags) ? tags : [tags]) {
|
|
37
|
-
this._clanTags.add(this.pollingClient.util.formatTag(tag));
|
|
38
|
-
}
|
|
39
|
-
return this;
|
|
40
|
-
}
|
|
41
|
-
/** Delete clan tags from clan polling events. */
|
|
42
|
-
deleteClans(tags) {
|
|
43
|
-
for (const tag of Array.isArray(tags) ? tags : [tags]) {
|
|
44
|
-
const key = this.pollingClient.util.formatTag(tag);
|
|
45
|
-
this._clans.delete(key);
|
|
46
|
-
this._clanTags.delete(key);
|
|
47
|
-
}
|
|
48
|
-
return this;
|
|
49
|
-
}
|
|
50
|
-
/** Add player tags for player polling events. */
|
|
51
|
-
addPlayers(tags) {
|
|
52
|
-
for (const tag of Array.isArray(tags) ? tags : [tags]) {
|
|
53
|
-
this._playerTags.add(this.pollingClient.util.formatTag(tag));
|
|
54
|
-
}
|
|
55
|
-
return this;
|
|
56
|
-
}
|
|
57
|
-
/** Delete player tags from player polling events. */
|
|
58
|
-
deletePlayers(tags) {
|
|
59
|
-
for (const tag of Array.isArray(tags) ? tags : [tags]) {
|
|
60
|
-
const key = this.pollingClient.util.formatTag(tag);
|
|
61
|
-
this._players.delete(key);
|
|
62
|
-
this._playerTags.delete(key);
|
|
63
|
-
}
|
|
64
|
-
return this;
|
|
65
|
-
}
|
|
66
|
-
/** Add clan tags for war polling events. */
|
|
67
|
-
addWars(tags) {
|
|
68
|
-
for (const tag of Array.isArray(tags) ? tags : [tags]) {
|
|
69
|
-
this._warTags.add(this.pollingClient.util.formatTag(tag));
|
|
70
|
-
}
|
|
71
|
-
return this;
|
|
72
|
-
}
|
|
73
|
-
/** Delete clan tags from war polling events. */
|
|
74
|
-
deleteWars(tags) {
|
|
75
|
-
for (const tag of Array.isArray(tags) ? tags : [tags]) {
|
|
76
|
-
const key = this.pollingClient.util.formatTag(tag);
|
|
77
|
-
this._wars.delete(`${key}:${1}`);
|
|
78
|
-
this._wars.delete(`${key}:${2}`);
|
|
79
|
-
this._warTags.delete(key);
|
|
80
|
-
}
|
|
81
|
-
return this;
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Set your own custom clan polling event.
|
|
85
|
-
*
|
|
86
|
-
* In order to emit the custom polling event, you must have this filter function that returns a boolean.
|
|
87
|
-
*
|
|
88
|
-
* @example
|
|
89
|
-
* ```js
|
|
90
|
-
* client.pollingEvents.addClans(['#2PP', '#8QU8J9LP']);
|
|
91
|
-
*
|
|
92
|
-
* client.pollingEvents.setClanEvent({
|
|
93
|
-
* name: 'clanMemberUpdate',
|
|
94
|
-
* filter: (oldClan, newClan) => {
|
|
95
|
-
* return oldClan.memberCount !== newClan.memberCount;
|
|
96
|
-
* }
|
|
97
|
-
* });
|
|
98
|
-
*
|
|
99
|
-
* client.on('clanMemberUpdate', (oldClan, newClan) => {
|
|
100
|
-
* console.log(oldClan.memberCount, newClan.memberCount);
|
|
101
|
-
* });
|
|
102
|
-
*
|
|
103
|
-
* (async function () {
|
|
104
|
-
* await client.pollingEvents.init();
|
|
105
|
-
* })();
|
|
106
|
-
* ```
|
|
107
|
-
* @returns
|
|
108
|
-
*/
|
|
109
|
-
setClanEvent(event) {
|
|
110
|
-
if (!event.name)
|
|
111
|
-
throw new Error('Event name is required.');
|
|
112
|
-
if (typeof event.filter !== 'function')
|
|
113
|
-
throw new Error('Filter function is required.');
|
|
114
|
-
this._pollingEvents.clans.push(event);
|
|
115
|
-
return this;
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Set your own custom war event.
|
|
119
|
-
*
|
|
120
|
-
* In order to emit the custom event, you must have this filter function that returns a boolean.
|
|
121
|
-
*/
|
|
122
|
-
setWarEvent(event) {
|
|
123
|
-
if (!event.name)
|
|
124
|
-
throw new Error('Event name is required.');
|
|
125
|
-
if (typeof event.filter !== 'function')
|
|
126
|
-
throw new Error('Filter function is required.');
|
|
127
|
-
this._pollingEvents.wars.push(event);
|
|
128
|
-
return this;
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* Set your own custom player event.
|
|
132
|
-
*
|
|
133
|
-
* In order to emit the custom event, you must have this filter function that returns a boolean.
|
|
134
|
-
*/
|
|
135
|
-
setPlayerEvent(event) {
|
|
136
|
-
if (!event.name)
|
|
137
|
-
throw new Error('Event name is required.');
|
|
138
|
-
if (typeof event.filter !== 'function')
|
|
139
|
-
throw new Error('Filter function is required.');
|
|
140
|
-
this._pollingEvents.players.push(event);
|
|
141
|
-
return this;
|
|
142
|
-
}
|
|
143
|
-
async maintenanceHandler() {
|
|
144
|
-
setTimeout(this.maintenanceHandler.bind(this), 10000).unref();
|
|
145
|
-
try {
|
|
146
|
-
const res = await this.pollingClient.rest.getClans({ maxMembers: Math.floor(Math.random() * 40) + 10, limit: 1 });
|
|
147
|
-
if (res.status === 200 && this._inMaintenance) {
|
|
148
|
-
this._inMaintenance = Boolean(false);
|
|
149
|
-
const duration = Date.now() - this._maintenanceStartTime.getTime();
|
|
150
|
-
this._maintenanceStartTime = null;
|
|
151
|
-
this.pollingClient.emit(Constants_1.PollingEvents.MaintenanceEnd, duration);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
catch (error) {
|
|
155
|
-
if (error instanceof HTTPError_1.HTTPError && error.status === 503 && !this._inMaintenance) {
|
|
156
|
-
this._inMaintenance = Boolean(true);
|
|
157
|
-
this._maintenanceStartTime = new Date();
|
|
158
|
-
this.pollingClient.emit(Constants_1.PollingEvents.MaintenanceStart);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
seasonEndHandler() {
|
|
163
|
-
const end = Util_1.Util.getSeasonEndTime().getTime() - Date.now();
|
|
164
|
-
// Why this? setTimeout can be up to 24.8 days or 2147483647ms [(2^31 - 1) Max 32bit Integer]
|
|
165
|
-
if (end > 24 * 60 * 60 * 1000) {
|
|
166
|
-
setTimeout(this.seasonEndHandler.bind(this), 60 * 60 * 1000);
|
|
167
|
-
}
|
|
168
|
-
else if (end > 0) {
|
|
169
|
-
setTimeout(() => {
|
|
170
|
-
this.pollingClient.emit(Constants_1.PollingEvents.NewSeasonStart, Util_1.Util.getSeasonId());
|
|
171
|
-
}, end + 100).unref();
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
async clanUpdateHandler() {
|
|
175
|
-
this.pollingClient.emit(Constants_1.PollingEvents.ClanLoopStart);
|
|
176
|
-
for (const tag of this._clanTags)
|
|
177
|
-
await this.runClanUpdate(tag);
|
|
178
|
-
this.pollingClient.emit(Constants_1.PollingEvents.ClanLoopEnd);
|
|
179
|
-
setTimeout(this.clanUpdateHandler.bind(this), 10000);
|
|
180
|
-
}
|
|
181
|
-
async playerUpdateHandler() {
|
|
182
|
-
this.pollingClient.emit(Constants_1.PollingEvents.PlayerLoopStart);
|
|
183
|
-
for (const tag of this._playerTags)
|
|
184
|
-
await this.runPlayerUpdate(tag);
|
|
185
|
-
this.pollingClient.emit(Constants_1.PollingEvents.PlayerLoopEnd);
|
|
186
|
-
setTimeout(this.playerUpdateHandler.bind(this), 10000);
|
|
187
|
-
}
|
|
188
|
-
async warUpdateHandler() {
|
|
189
|
-
this.pollingClient.emit(Constants_1.PollingEvents.WarLoopStart);
|
|
190
|
-
for (const tag of this._warTags)
|
|
191
|
-
await this.runWarUpdate(tag);
|
|
192
|
-
this.pollingClient.emit(Constants_1.PollingEvents.WarLoopEnd);
|
|
193
|
-
setTimeout(this.warUpdateHandler.bind(this), 10000);
|
|
194
|
-
}
|
|
195
|
-
async runClanUpdate(tag) {
|
|
196
|
-
if (this._inMaintenance)
|
|
197
|
-
return null;
|
|
198
|
-
const clan = await this.pollingClient.getClan(tag).catch(() => null);
|
|
199
|
-
if (!clan)
|
|
200
|
-
return null;
|
|
201
|
-
const cached = this._clans.get(clan.tag);
|
|
202
|
-
if (!cached)
|
|
203
|
-
return this._clans.set(clan.tag, clan);
|
|
204
|
-
for (const { name, filter } of this._pollingEvents.clans) {
|
|
205
|
-
try {
|
|
206
|
-
if (!(await filter(cached, clan)))
|
|
207
|
-
continue;
|
|
208
|
-
this.pollingClient.emit(name, cached, clan);
|
|
209
|
-
}
|
|
210
|
-
catch (error) {
|
|
211
|
-
this.pollingClient.emit(Constants_1.PollingEvents.Error, error);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
return this._clans.set(clan.tag, clan);
|
|
215
|
-
}
|
|
216
|
-
async runPlayerUpdate(tag) {
|
|
217
|
-
if (this._inMaintenance)
|
|
218
|
-
return null;
|
|
219
|
-
const player = await this.pollingClient.getPlayer(tag).catch(() => null);
|
|
220
|
-
if (!player)
|
|
221
|
-
return null;
|
|
222
|
-
const cached = this._players.get(player.tag);
|
|
223
|
-
if (!cached)
|
|
224
|
-
return this._players.set(player.tag, player);
|
|
225
|
-
for (const { name, filter } of this._pollingEvents.players) {
|
|
226
|
-
try {
|
|
227
|
-
if (!(await filter(cached, player)))
|
|
228
|
-
continue;
|
|
229
|
-
this.pollingClient.emit(name, cached, player);
|
|
230
|
-
}
|
|
231
|
-
catch (error) {
|
|
232
|
-
this.pollingClient.emit(Constants_1.PollingEvents.Error, error);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
return this._players.set(player.tag, player);
|
|
236
|
-
}
|
|
237
|
-
async runWarUpdate(tag) {
|
|
238
|
-
if (this._inMaintenance)
|
|
239
|
-
return null;
|
|
240
|
-
const clanWars = await this.pollingClient.getWars(tag).catch(() => null);
|
|
241
|
-
if (!clanWars?.length)
|
|
242
|
-
return null;
|
|
243
|
-
clanWars.forEach(async (war, index) => {
|
|
244
|
-
const key = `${tag}:${index}`;
|
|
245
|
-
const cached = this._wars.get(key);
|
|
246
|
-
if (!cached)
|
|
247
|
-
return this._wars.set(key, war);
|
|
248
|
-
for (const { name, filter } of this._pollingEvents.wars) {
|
|
249
|
-
try {
|
|
250
|
-
if (!(await filter(cached, war)))
|
|
251
|
-
continue;
|
|
252
|
-
this.pollingClient.emit(name, cached, war);
|
|
253
|
-
}
|
|
254
|
-
catch (error) {
|
|
255
|
-
this.pollingClient.emit(Constants_1.PollingEvents.Error, error);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
// check for war end
|
|
259
|
-
if (index === 1 && cached.warTag !== war.warTag) {
|
|
260
|
-
const data = await this.pollingClient.getLeagueWar({ clanTag: tag, round: 'PreviousRound' }).catch(() => null);
|
|
261
|
-
if (data && data.warTag === cached.warTag) {
|
|
262
|
-
for (const { name, filter } of this._pollingEvents.wars) {
|
|
263
|
-
try {
|
|
264
|
-
if (!(await filter(cached, data)))
|
|
265
|
-
continue;
|
|
266
|
-
this.pollingClient.emit(name, cached, data);
|
|
267
|
-
}
|
|
268
|
-
catch (error) {
|
|
269
|
-
this.pollingClient.emit(Constants_1.PollingEvents.Error, error);
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
return this._wars.set(key, war);
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
exports.PollingEventManager = PollingEventManager;
|