clashofclans.js 2.0.0-dev.30ea324 → 2.0.0-dev.3a1efb5
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 +1 -1
- package/README.md +5 -5
- package/dist/client/Client.d.ts +52 -22
- package/dist/client/Client.js +61 -37
- package/dist/client/EventManager.d.ts +46 -20
- package/dist/client/EventManager.js +97 -51
- package/dist/rest/HTTPError.d.ts +17 -1
- package/dist/rest/HTTPError.js +26 -7
- package/dist/rest/RESTManager.d.ts +24 -0
- package/dist/rest/RequestHandler.d.ts +84 -3
- package/dist/rest/RequestHandler.js +43 -16
- package/dist/rest/Throttler.d.ts +0 -4
- package/dist/struct/Clan.d.ts +2 -1
- package/dist/struct/Clan.js +2 -2
- package/dist/struct/ClanMember.d.ts +2 -1
- package/dist/struct/ClanMember.js +2 -2
- package/dist/struct/ClanWar.d.ts +6 -4
- package/dist/struct/ClanWar.js +17 -3
- package/dist/struct/ClanWarLeagueGroup.d.ts +3 -2
- package/dist/struct/ClanWarLeagueGroup.js +10 -9
- package/dist/struct/ClanWarLog.d.ts +2 -2
- package/dist/struct/ClanWarLog.js +2 -2
- package/dist/struct/Player.d.ts +2 -1
- package/dist/struct/Player.js +2 -2
- package/dist/struct/PlayerClan.d.ts +2 -1
- package/dist/struct/PlayerClan.js +2 -2
- package/dist/types/index.d.ts +3 -5
- package/dist/util/Constants.d.ts +5 -0
- package/dist/util/Constants.js +6 -1
- package/dist/util/Util.js +3 -1
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -8,7 +8,7 @@ This new version is a complete TypeScript rewrite to convert everything from pla
|
|
|
8
8
|
|
|
9
9
|
### Features
|
|
10
10
|
|
|
11
|
-
- EventManager and Custom Events ([#37](https://github.com/clashperk/clashofclans.js/issues/37)) ([5027ae6](https://github.com/clashperk/clashofclans.js/commit/5027ae663a8e07175e17384c7e5706f4a1a7afb4))
|
|
11
|
+
- EventManager and Custom Events ([#37](https://github.com/clashperk/clashofclans.js/issues/37)) ([5027ae6](https://github.com/clashperk/clashofclans.js/commit/5027ae663a8e07175e17384c7e5706f4a1a7afb4)) ([984451d](https://github.com/clashperk/clashofclans.js/commit/30ea3240c11866008d0dae514468c0fdbb34ffd0))
|
|
12
12
|
- Internal Caching Options ([#53](https://github.com/clashperk/clashofclans.js/issues/53))
|
|
13
13
|
- Email Password Login ([#31](https://github.com/clashperk/clashofclans.js/issues/31)) ([4153cd3](https://github.com/clashperk/clashofclans.js/commit/4153cd37ea0e1c71550b9e892105b84d5a407e23))
|
|
14
14
|
- API Date Parser and Request Retries ([#26](https://github.com/clashperk/clashofclans.js/issues/26)) ([94585f3](https://github.com/clashperk/clashofclans.js/commit/94585f3a84a7175b2d07872f9eb9e42372b95e12))
|
package/README.md
CHANGED
|
@@ -39,14 +39,14 @@ const client = new Client();
|
|
|
39
39
|
```js
|
|
40
40
|
const { Client, BatchThrottler } = require('clashofclans.js');
|
|
41
41
|
const client = new Client({
|
|
42
|
+
cache: true,
|
|
42
43
|
retryLimit: 1,
|
|
43
|
-
restRequestTimeout:
|
|
44
|
-
throttler: new BatchThrottler(
|
|
44
|
+
restRequestTimeout: 5000,
|
|
45
|
+
throttler: new BatchThrottler(20)
|
|
45
46
|
});
|
|
46
47
|
|
|
47
|
-
client.events.addClans(['#8P2QG08P']);
|
|
48
|
-
client.events.
|
|
49
|
-
type: 'CLAN',
|
|
48
|
+
client.events.addClans(['#8QU8J9LP', '#8P2QG08P']);
|
|
49
|
+
client.events.setClanEvent({
|
|
50
50
|
name: 'clanDescriptionChange',
|
|
51
51
|
filter: (oldClan, newClan) => {
|
|
52
52
|
return oldClan.description !== newClan.description;
|
package/dist/client/Client.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { ClanSearchOptions, SearchOptions, ClientOptions, InitOptions, OverrideOptions } from '../rest/RequestHandler';
|
|
3
|
-
import { EVENTS } from '../util/Constants';
|
|
3
|
+
import { EVENTS, CWL_ROUNDS } from '../util/Constants';
|
|
4
4
|
import { RESTManager } from '../rest/RESTManager';
|
|
5
5
|
import { EventManager } from './EventManager';
|
|
6
6
|
import { EventEmitter } from 'events';
|
|
@@ -21,6 +21,8 @@ export declare class Client extends EventEmitter {
|
|
|
21
21
|
constructor(options?: ClientOptions);
|
|
22
22
|
/** Contains various general-purpose utility methods. */
|
|
23
23
|
get util(): typeof Util;
|
|
24
|
+
/** Whether the API is in maintenance break. */
|
|
25
|
+
get inMaintenance(): boolean;
|
|
24
26
|
/**
|
|
25
27
|
* Initialize the client to create keys.
|
|
26
28
|
* @example
|
|
@@ -40,12 +42,38 @@ export declare class Client extends EventEmitter {
|
|
|
40
42
|
getClanMembers(clanTag: string, options?: SearchOptions): Promise<ClanMember[]>;
|
|
41
43
|
/** Get clan war log. */
|
|
42
44
|
getClanWarLog(clanTag: string, options?: SearchOptions): Promise<ClanWarLog[]>;
|
|
43
|
-
/** Get info about currently running war (
|
|
44
|
-
getClanWar(clanTag: string, options?: OverrideOptions): Promise<ClanWar
|
|
45
|
-
/**
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
45
|
+
/** Get info about currently running war (normal or friendly) in the clan. */
|
|
46
|
+
getClanWar(clanTag: string, options?: OverrideOptions): Promise<ClanWar>;
|
|
47
|
+
/**
|
|
48
|
+
* Get info about currently running war in the clan.
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* await client.getCurrentWar('#8QU8J9LP');
|
|
52
|
+
* ```
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* await client.getCurrentWar({ clanTag: '#8QU8J9LP', round: 'PREVIOUS_ROUND' });
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
getCurrentWar(clanTag: string | {
|
|
59
|
+
clanTag: string;
|
|
60
|
+
round?: keyof typeof CWL_ROUNDS;
|
|
61
|
+
}, options?: OverrideOptions): Promise<ClanWar | null>;
|
|
62
|
+
/**
|
|
63
|
+
* Get info about currently running CWL round.
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* await client.getLeagueWar('#8QU8J9LP');
|
|
67
|
+
* ```
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* await client.getLeagueWar({ clanTag: '#8QU8J9LP', round: 'PREVIOUS_ROUND' });
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
getLeagueWar(clanTag: string | {
|
|
74
|
+
clanTag: string;
|
|
75
|
+
round?: keyof typeof CWL_ROUNDS;
|
|
76
|
+
}, options?: OverrideOptions): Promise<ClanWar | null>;
|
|
49
77
|
private _getCurrentLeagueWars;
|
|
50
78
|
private _getClanWars;
|
|
51
79
|
/** Get information about clan war league. */
|
|
@@ -54,7 +82,7 @@ export declare class Client extends EventEmitter {
|
|
|
54
82
|
getClanWarLeagueRound(warTag: string | {
|
|
55
83
|
warTag: string;
|
|
56
84
|
clanTag?: string;
|
|
57
|
-
}, options?: OverrideOptions): Promise<ClanWar
|
|
85
|
+
}, options?: OverrideOptions): Promise<ClanWar>;
|
|
58
86
|
/** Get information about a player by tag. */
|
|
59
87
|
getPlayer(playerTag: string, options?: OverrideOptions): Promise<Player>;
|
|
60
88
|
/** Verify Player API token that can be found from the Game settings. */
|
|
@@ -88,8 +116,8 @@ export declare class Client extends EventEmitter {
|
|
|
88
116
|
*
|
|
89
117
|
* **Parameters**
|
|
90
118
|
*
|
|
91
|
-
* | Name |
|
|
92
|
-
* | :--: |
|
|
119
|
+
* | Name | Type | Description |
|
|
120
|
+
* | :--: | :------: | :-------------------: |
|
|
93
121
|
* | `id` | `string` | Id of the new season. |
|
|
94
122
|
* @public
|
|
95
123
|
* @event
|
|
@@ -106,8 +134,8 @@ export declare class Client extends EventEmitter {
|
|
|
106
134
|
*
|
|
107
135
|
* **Parameters**
|
|
108
136
|
*
|
|
109
|
-
* |
|
|
110
|
-
* |
|
|
137
|
+
* | Name | Type | Description |
|
|
138
|
+
* | :--------: | :------: | :------------------------------------------------: |
|
|
111
139
|
* | `duration` | `number` | Duration of the maintenance break in milliseconds. |
|
|
112
140
|
* @public
|
|
113
141
|
* @event
|
|
@@ -115,15 +143,21 @@ export declare class Client extends EventEmitter {
|
|
|
115
143
|
private static maintenanceEnd;
|
|
116
144
|
/** @internal */
|
|
117
145
|
on<K extends keyof ClientEvents>(event: K, listeners: (...args: ClientEvents[K]) => void): this;
|
|
146
|
+
/** @internal */
|
|
147
|
+
on<S extends keyof CustomEvents>(event: Exclude<S, keyof ClientEvents>, listeners: (...args: CustomEvents[S]) => void): this;
|
|
118
148
|
/** @internal */ on<S extends string | symbol>(event: Exclude<S, keyof ClientEvents>, listeners: (...args: any[]) => void): this;
|
|
119
149
|
/** @internal */
|
|
120
150
|
once<K extends keyof ClientEvents>(event: K, listeners: (...args: ClientEvents[K]) => void): this;
|
|
151
|
+
/** @internal */
|
|
152
|
+
once<S extends keyof CustomEvents>(event: Exclude<S, keyof ClientEvents>, listeners: (...args: CustomEvents[S]) => void): this;
|
|
121
153
|
/** @internal */ once<S extends string | symbol>(event: Exclude<S, keyof ClientEvents>, listeners: (...args: any[]) => void): this;
|
|
122
154
|
/** @internal */
|
|
123
155
|
emit<K extends keyof ClientEvents>(event: K, ...args: ClientEvents[K]): boolean;
|
|
156
|
+
/** @internal */
|
|
157
|
+
emit<S extends keyof CustomEvents>(event: Exclude<S, keyof ClientEvents>, ...args: CustomEvents[S]): this;
|
|
124
158
|
/** @internal */ emit<S extends string | symbol>(event: Exclude<S, keyof ClientEvents>, ...args: any[]): boolean;
|
|
125
159
|
}
|
|
126
|
-
|
|
160
|
+
interface ClientEvents {
|
|
127
161
|
[EVENTS.NEW_SEASON_START]: [id: string];
|
|
128
162
|
[EVENTS.MAINTENANCE_START]: [];
|
|
129
163
|
[EVENTS.MAINTENANCE_END]: [duration: number];
|
|
@@ -135,13 +169,9 @@ export interface ClientEvents {
|
|
|
135
169
|
[EVENTS.WAR_LOOP_END]: [];
|
|
136
170
|
[EVENTS.ERROR]: [error: unknown];
|
|
137
171
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
172
|
+
interface CustomEvents {
|
|
173
|
+
[key: `clan${string}`]: [oldClan: Clan, newClan: Clan];
|
|
174
|
+
[key: `war${string}`]: [oldWar: ClanWar, newWar: ClanWar];
|
|
175
|
+
[key: `player${string}`]: [oldPlayer: Player, newPlayer: Player];
|
|
142
176
|
}
|
|
143
|
-
export
|
|
144
|
-
readonly PREVIOUS_WAR: "warEnded";
|
|
145
|
-
readonly CURRENT_WAR: "inWar";
|
|
146
|
-
readonly NEXT_WAR: "preparation";
|
|
147
|
-
};
|
|
177
|
+
export {};
|
package/dist/client/Client.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.Client = void 0;
|
|
4
4
|
const Constants_1 = require("../util/Constants");
|
|
5
|
+
const HTTPError_1 = require("../rest/HTTPError");
|
|
5
6
|
const RESTManager_1 = require("../rest/RESTManager");
|
|
6
7
|
const EventManager_1 = require("./EventManager");
|
|
7
|
-
const HTTPError_1 = require("../rest/HTTPError");
|
|
8
8
|
const events_1 = require("events");
|
|
9
9
|
const Util_1 = require("../util/Util");
|
|
10
10
|
const struct_1 = require("../struct");
|
|
@@ -25,6 +25,11 @@ class Client extends events_1.EventEmitter {
|
|
|
25
25
|
get util() {
|
|
26
26
|
return Util_1.Util;
|
|
27
27
|
}
|
|
28
|
+
/** Whether the API is in maintenance break. */
|
|
29
|
+
get inMaintenance() {
|
|
30
|
+
// @ts-expect-error
|
|
31
|
+
return this.events._inMaintenance;
|
|
32
|
+
}
|
|
28
33
|
/**
|
|
29
34
|
* Initialize the client to create keys.
|
|
30
35
|
* @example
|
|
@@ -62,30 +67,52 @@ class Client extends events_1.EventEmitter {
|
|
|
62
67
|
const { data } = await this.rest.getClanWarLog(clanTag, options);
|
|
63
68
|
return data.items.map((entry) => new struct_1.ClanWarLog(this, entry));
|
|
64
69
|
}
|
|
65
|
-
/** Get info about currently running war (
|
|
70
|
+
/** Get info about currently running war (normal or friendly) in the clan. */
|
|
66
71
|
async getClanWar(clanTag, options) {
|
|
67
|
-
const { data, maxAge } = await this.rest.getCurrentWar(clanTag, options);
|
|
68
|
-
if (data.state === 'notInWar')
|
|
69
|
-
|
|
72
|
+
const { data, maxAge, path, status } = await this.rest.getCurrentWar(clanTag, options);
|
|
73
|
+
if (data.state === 'notInWar') {
|
|
74
|
+
throw new HTTPError_1.HTTPError(HTTPError_1.NotInWarError, status, path, maxAge);
|
|
75
|
+
}
|
|
70
76
|
return new struct_1.ClanWar(this, data, { clanTag, maxAge });
|
|
71
77
|
}
|
|
72
|
-
/**
|
|
78
|
+
/**
|
|
79
|
+
* Get info about currently running war in the clan.
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* await client.getCurrentWar('#8QU8J9LP');
|
|
83
|
+
* ```
|
|
84
|
+
* @example
|
|
85
|
+
* ```ts
|
|
86
|
+
* await client.getCurrentWar({ clanTag: '#8QU8J9LP', round: 'PREVIOUS_ROUND' });
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
73
89
|
async getCurrentWar(clanTag, options) {
|
|
90
|
+
const args = typeof clanTag === 'string' ? { clanTag } : { clanTag: clanTag.clanTag, round: clanTag.round };
|
|
74
91
|
try {
|
|
75
|
-
|
|
76
|
-
return data ?? (await this.getLeagueWar(clanTag));
|
|
92
|
+
return await this.getClanWar(args.clanTag, options);
|
|
77
93
|
}
|
|
78
94
|
catch (e) {
|
|
79
|
-
if (e instanceof HTTPError_1.HTTPError && e.status
|
|
80
|
-
return this.getLeagueWar(clanTag);
|
|
95
|
+
if (e instanceof HTTPError_1.HTTPError && [200, 403].includes(e.status)) {
|
|
96
|
+
return this.getLeagueWar({ clanTag: args.clanTag, round: args.round }, options);
|
|
81
97
|
}
|
|
98
|
+
throw e;
|
|
82
99
|
}
|
|
83
|
-
return null;
|
|
84
100
|
}
|
|
85
|
-
/**
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
101
|
+
/**
|
|
102
|
+
* Get info about currently running CWL round.
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* await client.getLeagueWar('#8QU8J9LP');
|
|
106
|
+
* ```
|
|
107
|
+
* @example
|
|
108
|
+
* ```ts
|
|
109
|
+
* await client.getLeagueWar({ clanTag: '#8QU8J9LP', round: 'PREVIOUS_ROUND' });
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
async getLeagueWar(clanTag, options) {
|
|
113
|
+
const args = typeof clanTag === 'string' ? { clanTag } : { clanTag: clanTag.clanTag, round: clanTag.round };
|
|
114
|
+
const state = (args.round && Constants_1.CWL_ROUNDS[args.round]) ?? 'inWar'; // eslint-disable-line
|
|
115
|
+
const data = await this.getClanWarLeagueGroup(args.clanTag, options);
|
|
89
116
|
const rounds = data.rounds.filter((round) => !round.warTags.includes('#0'));
|
|
90
117
|
if (!rounds.length)
|
|
91
118
|
return null;
|
|
@@ -95,29 +122,30 @@ class Client extends events_1.EventEmitter {
|
|
|
95
122
|
.map((round) => round.warTags)
|
|
96
123
|
.flat()
|
|
97
124
|
.reverse();
|
|
98
|
-
const wars = await this.util.allSettled(warTags.map((warTag) => this.getClanWarLeagueRound({ warTag, clanTag }, { ignoreRateLimit: true })));
|
|
99
|
-
|
|
125
|
+
const wars = await this.util.allSettled(warTags.map((warTag) => this.getClanWarLeagueRound({ warTag, clanTag: args.clanTag }, { ...options, ignoreRateLimit: true })));
|
|
126
|
+
if (args.round && args.round in Constants_1.CWL_ROUNDS) {
|
|
127
|
+
return wars.find((war) => war.state === state) ?? null;
|
|
128
|
+
}
|
|
129
|
+
return wars.find((war) => war.state === state) ?? wars.at(0) ?? null;
|
|
100
130
|
}
|
|
101
131
|
async _getCurrentLeagueWars(clanTag, options) {
|
|
102
132
|
const data = await this.getClanWarLeagueGroup(clanTag, options);
|
|
103
133
|
// @ts-expect-error
|
|
104
|
-
return data._getCurrentWars(clanTag);
|
|
134
|
+
return data._getCurrentWars(clanTag, options);
|
|
105
135
|
}
|
|
106
136
|
async _getClanWars(clanTag, options) {
|
|
107
|
-
const date = new Date().
|
|
137
|
+
const date = new Date().getUTCDate();
|
|
138
|
+
if (!(date >= 1 && date <= 10)) {
|
|
139
|
+
return [await this.getClanWar(clanTag, options)];
|
|
140
|
+
}
|
|
108
141
|
try {
|
|
109
|
-
|
|
110
|
-
if (!(date >= 1 && date <= 10))
|
|
111
|
-
return data ? [data] : [];
|
|
112
|
-
return data ? [data] : await this._getCurrentLeagueWars(clanTag);
|
|
142
|
+
return this._getCurrentLeagueWars(clanTag, options);
|
|
113
143
|
}
|
|
114
144
|
catch (e) {
|
|
115
|
-
if (
|
|
116
|
-
return [];
|
|
117
|
-
if (e instanceof HTTPError_1.HTTPError && e.status === 403) {
|
|
118
|
-
return this._getCurrentLeagueWars(clanTag);
|
|
145
|
+
if (e instanceof HTTPError_1.HTTPError && [404].includes(e.status)) {
|
|
146
|
+
return [await this.getClanWar(clanTag, options)];
|
|
119
147
|
}
|
|
120
|
-
|
|
148
|
+
throw e;
|
|
121
149
|
}
|
|
122
150
|
}
|
|
123
151
|
/** Get information about clan war league. */
|
|
@@ -128,9 +156,10 @@ class Client extends events_1.EventEmitter {
|
|
|
128
156
|
/** Get information about CWL round by WarTag. */
|
|
129
157
|
async getClanWarLeagueRound(warTag, options) {
|
|
130
158
|
const args = typeof warTag === 'string' ? { warTag } : { warTag: warTag.warTag, clanTag: warTag.clanTag };
|
|
131
|
-
const { data, maxAge } = await this.rest.getClanWarLeagueRound(args.warTag, options);
|
|
132
|
-
if (data.state === 'notInWar')
|
|
133
|
-
|
|
159
|
+
const { data, maxAge, status, path } = await this.rest.getClanWarLeagueRound(args.warTag, options);
|
|
160
|
+
if (data.state === 'notInWar') {
|
|
161
|
+
throw new HTTPError_1.HTTPError(HTTPError_1.NotInWarError, status, path, maxAge);
|
|
162
|
+
}
|
|
134
163
|
return new struct_1.ClanWar(this, data, { warTag: args.warTag, clanTag: args.clanTag, maxAge });
|
|
135
164
|
}
|
|
136
165
|
/** Get information about a player by tag. */
|
|
@@ -206,8 +235,3 @@ class Client extends events_1.EventEmitter {
|
|
|
206
235
|
}
|
|
207
236
|
}
|
|
208
237
|
exports.Client = Client;
|
|
209
|
-
exports.CWLRound = {
|
|
210
|
-
PREVIOUS_WAR: 'warEnded',
|
|
211
|
-
CURRENT_WAR: 'inWar',
|
|
212
|
-
NEXT_WAR: 'preparation'
|
|
213
|
-
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { Clan, ClanWar, Player } from '../struct';
|
|
2
|
+
import { Client } from './Client';
|
|
3
|
+
/** Represents Event Manager of the {@link Client}. */
|
|
3
4
|
export declare class EventManager {
|
|
4
5
|
private readonly client;
|
|
5
6
|
private readonly _clanTags;
|
|
@@ -13,25 +14,29 @@ export declare class EventManager {
|
|
|
13
14
|
private _maintenanceStartTime;
|
|
14
15
|
constructor(client: Client);
|
|
15
16
|
/** Initialize the Event Manager to start pulling. */
|
|
16
|
-
init(): Promise<
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
init(): Promise<string[]>;
|
|
18
|
+
/** Add clan tags to clan events. */
|
|
19
|
+
addClans(tags: string[] | string): this;
|
|
20
|
+
/** Delete clan tags from clan events. */
|
|
21
|
+
deleteClans(tags: string[] | string): this;
|
|
22
|
+
/** Add player tags for player events. */
|
|
23
|
+
addPlayers(tags: string[] | string): this;
|
|
24
|
+
/** Delete player tags from player events. */
|
|
25
|
+
deletePlayers(tags: string[] | string): this;
|
|
26
|
+
/** Add clan tags for war events. */
|
|
27
|
+
addWars(tags: string[] | string): this;
|
|
28
|
+
/** Delete clan tags from war events. */
|
|
29
|
+
deleteWars(tags: string[] | string): this;
|
|
23
30
|
/**
|
|
24
|
-
* Set your own custom event.
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* @param event.filter - Filter of this event. Must return a boolean value.
|
|
31
|
+
* Set your own custom clan event.
|
|
32
|
+
*
|
|
33
|
+
* In order to emit the custom event, you must have this filter function that returns a boolean.
|
|
28
34
|
*
|
|
29
35
|
* @example
|
|
30
36
|
* ```js
|
|
31
|
-
* client.events.addClans(['#2PP', '']);
|
|
37
|
+
* client.events.addClans(['#2PP', '#8QU8J9LP']);
|
|
32
38
|
*
|
|
33
|
-
* client.events.
|
|
34
|
-
* type: 'CLAN',
|
|
39
|
+
* client.events.setClanEvent({
|
|
35
40
|
* name: 'clanMemberUpdate',
|
|
36
41
|
* filter: (oldClan, newClan) => {
|
|
37
42
|
* return oldClan.memberCount !== newClan.memberCount;
|
|
@@ -40,14 +45,35 @@ export declare class EventManager {
|
|
|
40
45
|
*
|
|
41
46
|
* client.on('clanMemberUpdate', (oldClan, newClan) => {
|
|
42
47
|
* console.log(oldClan.memberCount, newClan.memberCount);
|
|
43
|
-
* })
|
|
48
|
+
* });
|
|
49
|
+
*
|
|
50
|
+
* (async function () {
|
|
51
|
+
* await client.events.init();
|
|
52
|
+
* })();
|
|
44
53
|
* ```
|
|
45
54
|
* @returns
|
|
46
55
|
*/
|
|
47
|
-
|
|
48
|
-
|
|
56
|
+
setClanEvent(event: {
|
|
57
|
+
name: string;
|
|
58
|
+
filter: (oldClan: Clan, newClan: Clan) => boolean;
|
|
59
|
+
}): this;
|
|
60
|
+
/**
|
|
61
|
+
* Set your own custom war event.
|
|
62
|
+
*
|
|
63
|
+
* In order to emit the custom event, you must have this filter function that returns a boolean.
|
|
64
|
+
*/
|
|
65
|
+
setWarEvent(event: {
|
|
66
|
+
name: string;
|
|
67
|
+
filter: (oldWar: ClanWar, newWar: ClanWar) => boolean;
|
|
68
|
+
}): this;
|
|
69
|
+
/**
|
|
70
|
+
* Set your own custom player event.
|
|
71
|
+
*
|
|
72
|
+
* In order to emit the custom event, you must have this filter function that returns a boolean.
|
|
73
|
+
*/
|
|
74
|
+
setPlayerEvent(event: {
|
|
49
75
|
name: string;
|
|
50
|
-
filter: (
|
|
76
|
+
filter: (oldPlayer: Player, newPlayer: Player) => boolean;
|
|
51
77
|
}): this;
|
|
52
78
|
private maintenanceHandler;
|
|
53
79
|
private seasonEndHandler;
|
|
@@ -4,7 +4,7 @@ exports.EventManager = void 0;
|
|
|
4
4
|
const HTTPError_1 = require("../rest/HTTPError");
|
|
5
5
|
const Constants_1 = require("../util/Constants");
|
|
6
6
|
const Util_1 = require("../util/Util");
|
|
7
|
-
/** Represents Event Manager of the {@link Client}
|
|
7
|
+
/** Represents Event Manager of the {@link Client}. */
|
|
8
8
|
class EventManager {
|
|
9
9
|
constructor(client) {
|
|
10
10
|
this.client = client;
|
|
@@ -31,54 +31,65 @@ class EventManager {
|
|
|
31
31
|
this.warUpdateHandler();
|
|
32
32
|
return Promise.resolve(this.client.eventNames());
|
|
33
33
|
}
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
/** Add clan tags to clan events. */
|
|
35
|
+
addClans(tags) {
|
|
36
|
+
for (const tag of Array.isArray(tags) ? tags : [tags]) {
|
|
36
37
|
this._clanTags.add(this.client.util.parseTag(tag));
|
|
37
38
|
}
|
|
38
39
|
return this;
|
|
39
40
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
/** Delete clan tags from clan events. */
|
|
42
|
+
deleteClans(tags) {
|
|
43
|
+
for (const tag of Array.isArray(tags) ? tags : [tags]) {
|
|
44
|
+
const key = this.client.util.parseTag(tag);
|
|
45
|
+
this._clans.delete(key);
|
|
46
|
+
this._clanTags.delete(key);
|
|
43
47
|
}
|
|
44
48
|
return this;
|
|
45
49
|
}
|
|
46
|
-
|
|
47
|
-
|
|
50
|
+
/** Add player tags for player events. */
|
|
51
|
+
addPlayers(tags) {
|
|
52
|
+
for (const tag of Array.isArray(tags) ? tags : [tags]) {
|
|
48
53
|
this._playerTags.add(this.client.util.parseTag(tag));
|
|
49
54
|
}
|
|
50
55
|
return this;
|
|
51
56
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
57
|
+
/** Delete player tags from player events. */
|
|
58
|
+
deletePlayers(tags) {
|
|
59
|
+
for (const tag of Array.isArray(tags) ? tags : [tags]) {
|
|
60
|
+
const key = this.client.util.parseTag(tag);
|
|
61
|
+
this._players.delete(key);
|
|
62
|
+
this._playerTags.delete(key);
|
|
55
63
|
}
|
|
56
64
|
return this;
|
|
57
65
|
}
|
|
58
|
-
|
|
59
|
-
|
|
66
|
+
/** Add clan tags for war events. */
|
|
67
|
+
addWars(tags) {
|
|
68
|
+
for (const tag of Array.isArray(tags) ? tags : [tags]) {
|
|
60
69
|
this._warTags.add(this.client.util.parseTag(tag));
|
|
61
70
|
}
|
|
62
71
|
return this;
|
|
63
72
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
73
|
+
/** Delete clan tags from war events. */
|
|
74
|
+
deleteWars(tags) {
|
|
75
|
+
for (const tag of Array.isArray(tags) ? tags : [tags]) {
|
|
76
|
+
const key = this.client.util.parseTag(tag);
|
|
77
|
+
this._wars.delete(`${key}:${1}`);
|
|
78
|
+
this._wars.delete(`${key}:${2}`);
|
|
79
|
+
this._warTags.delete(key);
|
|
67
80
|
}
|
|
68
81
|
return this;
|
|
69
82
|
}
|
|
70
83
|
/**
|
|
71
|
-
* Set your own custom event.
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
* @param event.filter - Filter of this event. Must return a boolean value.
|
|
84
|
+
* Set your own custom clan event.
|
|
85
|
+
*
|
|
86
|
+
* In order to emit the custom event, you must have this filter function that returns a boolean.
|
|
75
87
|
*
|
|
76
88
|
* @example
|
|
77
89
|
* ```js
|
|
78
|
-
* client.events.addClans(['#2PP', '']);
|
|
90
|
+
* client.events.addClans(['#2PP', '#8QU8J9LP']);
|
|
79
91
|
*
|
|
80
|
-
* client.events.
|
|
81
|
-
* type: 'CLAN',
|
|
92
|
+
* client.events.setClanEvent({
|
|
82
93
|
* name: 'clanMemberUpdate',
|
|
83
94
|
* filter: (oldClan, newClan) => {
|
|
84
95
|
* return oldClan.memberCount !== newClan.memberCount;
|
|
@@ -87,27 +98,46 @@ class EventManager {
|
|
|
87
98
|
*
|
|
88
99
|
* client.on('clanMemberUpdate', (oldClan, newClan) => {
|
|
89
100
|
* console.log(oldClan.memberCount, newClan.memberCount);
|
|
90
|
-
* })
|
|
101
|
+
* });
|
|
102
|
+
*
|
|
103
|
+
* (async function () {
|
|
104
|
+
* await client.events.init();
|
|
105
|
+
* })();
|
|
91
106
|
* ```
|
|
92
107
|
* @returns
|
|
93
108
|
*/
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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._events.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._events.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._events.players.push(event);
|
|
111
141
|
return this;
|
|
112
142
|
}
|
|
113
143
|
async maintenanceHandler() {
|
|
@@ -116,7 +146,7 @@ class EventManager {
|
|
|
116
146
|
const res = await this.client.rest.getClans({ maxMembers: Math.floor(Math.random() * 40) + 10, limit: 1 });
|
|
117
147
|
if (res.status === 200 && this._inMaintenance) {
|
|
118
148
|
this._inMaintenance = Boolean(false);
|
|
119
|
-
const duration =
|
|
149
|
+
const duration = Date.now() - this._maintenanceStartTime.getTime();
|
|
120
150
|
this._maintenanceStartTime = null;
|
|
121
151
|
this.client.emit(Constants_1.EVENTS.MAINTENANCE_END, duration);
|
|
122
152
|
}
|
|
@@ -153,7 +183,7 @@ class EventManager {
|
|
|
153
183
|
for (const tag of this._playerTags)
|
|
154
184
|
await this.runPlayerUpdate(tag);
|
|
155
185
|
this.client.emit(Constants_1.EVENTS.PLAYER_LOOP_END);
|
|
156
|
-
setTimeout(this.playerUpdateHandler.bind(this),
|
|
186
|
+
setTimeout(this.playerUpdateHandler.bind(this), 10000);
|
|
157
187
|
}
|
|
158
188
|
async warUpdateHandler() {
|
|
159
189
|
this.client.emit(Constants_1.EVENTS.WAR_LOOP_START);
|
|
@@ -171,9 +201,9 @@ class EventManager {
|
|
|
171
201
|
const cached = this._clans.get(clan.tag);
|
|
172
202
|
if (!cached)
|
|
173
203
|
return this._clans.set(clan.tag, clan);
|
|
174
|
-
for (const { name,
|
|
204
|
+
for (const { name, filter } of this._events.clans) {
|
|
175
205
|
try {
|
|
176
|
-
if (!
|
|
206
|
+
if (!filter(cached, clan))
|
|
177
207
|
continue;
|
|
178
208
|
this.client.emit(name, cached, clan);
|
|
179
209
|
}
|
|
@@ -192,9 +222,9 @@ class EventManager {
|
|
|
192
222
|
const cached = this._players.get(player.tag);
|
|
193
223
|
if (!cached)
|
|
194
224
|
return this._players.set(player.tag, player);
|
|
195
|
-
for (const { name,
|
|
225
|
+
for (const { name, filter } of this._events.players) {
|
|
196
226
|
try {
|
|
197
|
-
if (!
|
|
227
|
+
if (!filter(cached, player))
|
|
198
228
|
continue;
|
|
199
229
|
this.client.emit(name, cached, player);
|
|
200
230
|
}
|
|
@@ -211,14 +241,14 @@ class EventManager {
|
|
|
211
241
|
const clanWars = await this.client._getClanWars(tag).catch(() => null);
|
|
212
242
|
if (!clanWars?.length)
|
|
213
243
|
return null;
|
|
214
|
-
clanWars.forEach((war,
|
|
215
|
-
const key =
|
|
244
|
+
clanWars.forEach(async (war, index) => {
|
|
245
|
+
const key = `${tag}:${index}`;
|
|
216
246
|
const cached = this._wars.get(key);
|
|
217
247
|
if (!cached)
|
|
218
248
|
return this._wars.set(key, war);
|
|
219
|
-
for (const { name,
|
|
249
|
+
for (const { name, filter } of this._events.wars) {
|
|
220
250
|
try {
|
|
221
|
-
if (!
|
|
251
|
+
if (!filter(cached, war))
|
|
222
252
|
continue;
|
|
223
253
|
this.client.emit(name, cached, war);
|
|
224
254
|
}
|
|
@@ -226,6 +256,22 @@ class EventManager {
|
|
|
226
256
|
this.client.emit(Constants_1.EVENTS.ERROR, error);
|
|
227
257
|
}
|
|
228
258
|
}
|
|
259
|
+
// check for war end
|
|
260
|
+
if (index === 1 && cached.warTag !== war.warTag) {
|
|
261
|
+
const data = await this.client.getLeagueWar({ clanTag: tag, round: 'PREVIOUS_ROUND' }).catch(() => null);
|
|
262
|
+
if (data && data.warTag === cached.warTag) {
|
|
263
|
+
for (const { name, filter } of this._events.wars) {
|
|
264
|
+
try {
|
|
265
|
+
if (!filter(cached, data))
|
|
266
|
+
continue;
|
|
267
|
+
this.client.emit(name, cached, data);
|
|
268
|
+
}
|
|
269
|
+
catch (error) {
|
|
270
|
+
this.client.emit(Constants_1.EVENTS.ERROR, error);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
229
275
|
return this._wars.set(key, war);
|
|
230
276
|
});
|
|
231
277
|
}
|