clashofclans.js 2.8.4 → 3.0.0-dev.f6ce42f

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 CHANGED
@@ -2,17 +2,6 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
- ## 2.8.3 (2022-09-13)
6
-
7
- ### Bug Fixes
8
-
9
- - Conflict with the same name of builder base and home base troops. ([#123](https://github.com/clashperk/clashofclans.js/pull/123))
10
- - Fix the issue with the `Client#getLeagueWar()` method.
11
-
12
- ### Features
13
-
14
- - Clan Capital related properties and typings.
15
-
16
5
  ## 2.8.0 (2022-07-22)
17
6
 
18
7
  ### Features
@@ -1,9 +1,8 @@
1
1
  /// <reference types="node" />
2
+ import { EventEmitter } from 'node:events';
2
3
  import { ClanSearchOptions, SearchOptions, ClientOptions, LoginOptions, OverrideOptions } from '../types';
3
- import { EVENTS, CWL_ROUNDS } from '../util/Constants';
4
+ import { CWLRounds } from '../util/Constants';
4
5
  import { RESTManager } from '../rest/RESTManager';
5
- import { EventManager } from './EventManager';
6
- import { EventEmitter } from 'events';
7
6
  import { Util } from '../util/Util';
8
7
  import { Clan, ClanMember, ClanWar, ClanWarLog, League, Location, Player, WarLeague, RankedClan, RankedPlayer, Label, SeasonRankedPlayer, GoldPassSeason, ClanWarLeagueGroup } from '../struct';
9
8
  /**
@@ -14,15 +13,11 @@ import { Clan, ClanMember, ClanWar, ClanWarLog, League, Location, Player, WarLea
14
13
  * ```
15
14
  */
16
15
  export declare class Client extends EventEmitter {
17
- /** Event Manager for the client. */
18
- events: EventManager;
19
16
  /** REST Handler of the client. */
20
17
  rest: RESTManager;
21
18
  constructor(options?: ClientOptions);
22
19
  /** Contains various general-purpose utility methods. */
23
20
  get util(): typeof Util;
24
- /** Whether the API is in maintenance break. */
25
- get inMaintenance(): boolean;
26
21
  /**
27
22
  * Initialize the client to create keys.
28
23
  * @example
@@ -57,7 +52,7 @@ export declare class Client extends EventEmitter {
57
52
  */
58
53
  getCurrentWar(clanTag: string | {
59
54
  clanTag: string;
60
- round?: keyof typeof CWL_ROUNDS;
55
+ round?: keyof typeof CWLRounds;
61
56
  }, options?: OverrideOptions): Promise<ClanWar | null>;
62
57
  /**
63
58
  * Get info about currently running CWL round.
@@ -72,7 +67,7 @@ export declare class Client extends EventEmitter {
72
67
  */
73
68
  getLeagueWar(clanTag: string | {
74
69
  clanTag: string;
75
- round?: keyof typeof CWL_ROUNDS;
70
+ round?: keyof typeof CWLRounds;
76
71
  }, options?: OverrideOptions): Promise<ClanWar | null>;
77
72
  /** Returns active wars (last 2) of the CWL group. */
78
73
  getLeagueWars(clanTag: string, options?: OverrideOptions): Promise<ClanWar[]>;
@@ -131,68 +126,4 @@ export declare class Client extends EventEmitter {
131
126
  getPlayerLabels(options?: SearchOptions): Promise<Label[]>;
132
127
  /** Get info about gold pass season. */
133
128
  getGoldPassSeason(options?: OverrideOptions): Promise<GoldPassSeason>;
134
- /**
135
- * Emits when a new season starts.
136
- *
137
- * **Parameters**
138
- *
139
- * | Name | Type | Description |
140
- * | :--: | :------: | :-------------------: |
141
- * | `id` | `string` | Id of the new season. |
142
- * @public
143
- * @event
144
- */
145
- private static newSeasonStart;
146
- /**
147
- * Emits when maintenance break starts in the API.
148
- * @public
149
- * @event
150
- */
151
- private static maintenanceStart;
152
- /**
153
- * Emits when maintenance break ends in the API.
154
- *
155
- * **Parameters**
156
- *
157
- * | Name | Type | Description |
158
- * | :--------: | :------: | :------------------------------------------------: |
159
- * | `duration` | `number` | Duration of the maintenance break in milliseconds. |
160
- * @public
161
- * @event
162
- */
163
- private static maintenanceEnd;
164
- /** @internal */
165
- on<K extends keyof ClientEvents>(event: K, listeners: (...args: ClientEvents[K]) => void): this;
166
- /** @internal */
167
- on<S extends keyof CustomEvents>(event: Exclude<S, keyof ClientEvents>, listeners: (...args: CustomEvents[S]) => void): this;
168
- /** @internal */ on<S extends string | symbol>(event: Exclude<S, keyof ClientEvents>, listeners: (...args: any[]) => void): this;
169
- /** @internal */
170
- once<K extends keyof ClientEvents>(event: K, listeners: (...args: ClientEvents[K]) => void): this;
171
- /** @internal */
172
- once<S extends keyof CustomEvents>(event: Exclude<S, keyof ClientEvents>, listeners: (...args: CustomEvents[S]) => void): this;
173
- /** @internal */ once<S extends string | symbol>(event: Exclude<S, keyof ClientEvents>, listeners: (...args: any[]) => void): this;
174
- /** @internal */
175
- emit<K extends keyof ClientEvents>(event: K, ...args: ClientEvents[K]): boolean;
176
- /** @internal */
177
- emit<S extends keyof CustomEvents>(event: Exclude<S, keyof ClientEvents>, ...args: CustomEvents[S]): this;
178
- /** @internal */ emit<S extends string | symbol>(event: Exclude<S, keyof ClientEvents>, ...args: any[]): boolean;
179
- }
180
- interface ClientEvents {
181
- [EVENTS.NEW_SEASON_START]: [id: string];
182
- [EVENTS.MAINTENANCE_START]: [];
183
- [EVENTS.MAINTENANCE_END]: [duration: number];
184
- [EVENTS.CLAN_LOOP_START]: [];
185
- [EVENTS.CLAN_LOOP_END]: [];
186
- [EVENTS.PLAYER_LOOP_START]: [];
187
- [EVENTS.PLAYER_LOOP_END]: [];
188
- [EVENTS.WAR_LOOP_START]: [];
189
- [EVENTS.WAR_LOOP_END]: [];
190
- [EVENTS.ERROR]: [error: unknown];
191
- [EVENTS.DEBUG]: [path: string, status: string, message: string];
192
- }
193
- interface CustomEvents {
194
- [key: `clan${string}`]: [oldClan: Clan, newClan: Clan];
195
- [key: `war${string}`]: [oldWar: ClanWar, newWar: ClanWar];
196
- [key: `player${string}`]: [oldPlayer: Player, newPlayer: Player];
197
129
  }
198
- export {};
@@ -1,11 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Client = void 0;
4
+ const node_events_1 = require("node:events");
4
5
  const Constants_1 = require("../util/Constants");
5
6
  const HTTPError_1 = require("../rest/HTTPError");
6
7
  const RESTManager_1 = require("../rest/RESTManager");
7
- const EventManager_1 = require("./EventManager");
8
- const events_1 = require("events");
9
8
  const Util_1 = require("../util/Util");
10
9
  const struct_1 = require("../struct");
11
10
  /**
@@ -15,21 +14,15 @@ const struct_1 = require("../struct");
15
14
  * const client = new Client({ keys: ['***'] });
16
15
  * ```
17
16
  */
18
- class Client extends events_1.EventEmitter {
17
+ class Client extends node_events_1.EventEmitter {
19
18
  constructor(options) {
20
19
  super();
21
20
  this.rest = new RESTManager_1.RESTManager({ ...options, rejectIfNotValid: true });
22
- this.events = new EventManager_1.EventManager(this);
23
21
  }
24
22
  /** Contains various general-purpose utility methods. */
25
23
  get util() {
26
24
  return Util_1.Util;
27
25
  }
28
- /** Whether the API is in maintenance break. */
29
- get inMaintenance() {
30
- // @ts-expect-error
31
- return this.events._inMaintenance;
32
- }
33
26
  /**
34
27
  * Initialize the client to create keys.
35
28
  * @example
@@ -116,7 +109,7 @@ class Client extends events_1.EventEmitter {
116
109
  */
117
110
  async getLeagueWar(clanTag, options) {
118
111
  const args = typeof clanTag === 'string' ? { clanTag } : { clanTag: clanTag.clanTag, round: clanTag.round };
119
- const state = (args.round && Constants_1.CWL_ROUNDS[args.round]) ?? 'inWar'; // eslint-disable-line
112
+ const state = (args.round && Constants_1.CWLRounds[args.round]) ?? 'inWar'; // eslint-disable-line
120
113
  const data = await this.getClanWarLeagueGroup(args.clanTag, options);
121
114
  const rounds = data.rounds.filter((round) => !round.warTags.includes('#0'));
122
115
  if (!rounds.length)
@@ -128,7 +121,7 @@ class Client extends events_1.EventEmitter {
128
121
  .flat()
129
122
  .reverse();
130
123
  const wars = await this.util.allSettled(warTags.map((warTag) => this.getClanWarLeagueRound({ warTag, clanTag: args.clanTag }, { ...options, ignoreRateLimit: true })));
131
- if (args.round && args.round in Constants_1.CWL_ROUNDS) {
124
+ if (args.round && args.round in Constants_1.CWLRounds) {
132
125
  return wars.find((war) => war.clan.tag === args.clanTag && war.state === state) ?? null;
133
126
  }
134
127
  return (wars.find((war) => war.clan.tag === args.clanTag && war.state === state) ??
@@ -147,7 +140,7 @@ class Client extends events_1.EventEmitter {
147
140
  return [await this.getClanWar(clanTag, options)];
148
141
  }
149
142
  try {
150
- return this.getLeagueWars(clanTag, options);
143
+ return await this.getLeagueWars(clanTag, options);
151
144
  }
152
145
  catch (e) {
153
146
  if (e instanceof HTTPError_1.HTTPError && [404].includes(e.status)) {
@@ -196,12 +189,12 @@ class Client extends events_1.EventEmitter {
196
189
  }
197
190
  /** Get Legend League season Ids. */
198
191
  async getLeagueSeasons(options) {
199
- const { data } = await this.rest.getLeagueSeasons(Constants_1.LEGEND_LEAGUE_ID, options);
192
+ const { data } = await this.rest.getLeagueSeasons(Constants_1.LegendLeagueId, options);
200
193
  return data.items.map((league) => league.id);
201
194
  }
202
195
  /** Get Legend League season rankings by season Id. */
203
196
  async getSeasonRankings(seasonId, options) {
204
- const { data } = await this.rest.getSeasonRankings(Constants_1.LEGEND_LEAGUE_ID, seasonId, options);
197
+ const { data } = await this.rest.getSeasonRankings(Constants_1.LegendLeagueId, seasonId, options);
205
198
  return data.items.map((entry) => new struct_1.SeasonRankedPlayer(this, entry));
206
199
  }
207
200
  /** Get list of Clan War Leagues. */
@@ -0,0 +1,157 @@
1
+ import { Clan, ClanWar, Player } from '../struct';
2
+ import { ClientOptions } from '../types';
3
+ import { PollingEvents } from '../util/Constants';
4
+ import { Client } from './Client';
5
+ /**
6
+ * Represents Clash of Clans Polling Event Client.
7
+ * ```js
8
+ * const { PollingClient } = require('clashofclans.js');
9
+ * const client = new PollingClient({ keys: ['***'] });
10
+ * ```
11
+ */
12
+ export declare class PollingClient extends Client {
13
+ private readonly _clanTags;
14
+ private readonly _playerTags;
15
+ private readonly _warTags;
16
+ private readonly _clans;
17
+ private readonly _players;
18
+ private readonly _wars;
19
+ private readonly _pollingEvents;
20
+ inMaintenance: boolean;
21
+ private _maintenanceStartTime;
22
+ constructor(options?: ClientOptions);
23
+ /** Initialize the PollingEvent Manager to start pulling the data by polling api. */
24
+ init(): Promise<string[]>;
25
+ /** Add clan tags to clan polling events. */
26
+ addClans(tags: string[] | string): this;
27
+ /** Delete clan tags from clan polling events. */
28
+ deleteClans(tags: string[] | string): this;
29
+ /** Add player tags for player polling events. */
30
+ addPlayers(tags: string[] | string): this;
31
+ /** Delete player tags from player polling events. */
32
+ deletePlayers(tags: string[] | string): this;
33
+ /** Add clan tags for war polling events. */
34
+ addWars(tags: string[] | string): this;
35
+ /** Delete clan tags from war polling events. */
36
+ deleteWars(tags: string[] | string): this;
37
+ /**
38
+ * Set your own custom clan polling event.
39
+ *
40
+ * In order to emit the custom polling event, you must have this filter function that returns a boolean.
41
+ *
42
+ * @example
43
+ * ```js
44
+ * client.pollingEvents.addClans(['#2PP', '#8QU8J9LP']);
45
+ *
46
+ * client.pollingEvents.setClanEvent({
47
+ * name: 'clanMemberUpdate',
48
+ * filter: (oldClan, newClan) => {
49
+ * return oldClan.memberCount !== newClan.memberCount;
50
+ * }
51
+ * });
52
+ *
53
+ * client.on('clanMemberUpdate', (oldClan, newClan) => {
54
+ * console.log(oldClan.memberCount, newClan.memberCount);
55
+ * });
56
+ *
57
+ * (async function () {
58
+ * await client.pollingEvents.init();
59
+ * })();
60
+ * ```
61
+ * @returns
62
+ */
63
+ setClanEvent(event: {
64
+ name: string;
65
+ filter: (oldClan: Clan, newClan: Clan) => boolean;
66
+ }): this;
67
+ /**
68
+ * Set your own custom war event.
69
+ *
70
+ * In order to emit the custom event, you must have this filter function that returns a boolean.
71
+ */
72
+ setWarEvent(event: {
73
+ name: string;
74
+ filter: (oldWar: ClanWar, newWar: ClanWar) => boolean;
75
+ }): this;
76
+ /**
77
+ * Set your own custom player event.
78
+ *
79
+ * In order to emit the custom event, you must have this filter function that returns a boolean.
80
+ */
81
+ setPlayerEvent(event: {
82
+ name: string;
83
+ filter: (oldPlayer: Player, newPlayer: Player) => boolean;
84
+ }): this;
85
+ private maintenanceHandler;
86
+ private seasonEndHandler;
87
+ private clanUpdateHandler;
88
+ private playerUpdateHandler;
89
+ private warUpdateHandler;
90
+ private runClanUpdate;
91
+ private runPlayerUpdate;
92
+ private runWarUpdate;
93
+ /**
94
+ * Emits when a new season starts.
95
+ *
96
+ * **Parameters**
97
+ *
98
+ * | Name | Type | Description |
99
+ * | :--: | :------: | :-------------------: |
100
+ * | `id` | `string` | Id of the new season. |
101
+ * @public
102
+ * @event
103
+ */
104
+ private static newSeasonStart;
105
+ /**
106
+ * Emits when maintenance break starts in the API.
107
+ * @public
108
+ * @event
109
+ */
110
+ private static maintenanceStart;
111
+ /**
112
+ * Emits when maintenance break ends in the API.
113
+ *
114
+ * **Parameters**
115
+ *
116
+ * | Name | Type | Description |
117
+ * | :--------: | :------: | :------------------------------------------------: |
118
+ * | `duration` | `number` | Duration of the maintenance break in milliseconds. |
119
+ * @public
120
+ * @event
121
+ */
122
+ private static maintenanceEnd;
123
+ /** @internal */
124
+ on<K extends keyof ClientPollingEvents>(event: K, listeners: (...args: ClientPollingEvents[K]) => void): this;
125
+ /** @internal */
126
+ on<S extends keyof CustomEvents>(event: Exclude<S, keyof ClientPollingEvents>, listeners: (...args: CustomEvents[S]) => void): this;
127
+ /** @internal */ on<S extends string | symbol>(event: Exclude<S, keyof ClientPollingEvents>, listeners: (...args: any[]) => void): this;
128
+ /** @internal */
129
+ once<K extends keyof ClientPollingEvents>(event: K, listeners: (...args: ClientPollingEvents[K]) => void): this;
130
+ /** @internal */
131
+ once<S extends keyof CustomEvents>(event: Exclude<S, keyof ClientPollingEvents>, listeners: (...args: CustomEvents[S]) => void): this;
132
+ /** @internal */ once<S extends string | symbol>(event: Exclude<S, keyof ClientPollingEvents>, listeners: (...args: any[]) => void): this;
133
+ /** @internal */
134
+ emit<K extends keyof ClientPollingEvents>(event: K, ...args: ClientPollingEvents[K]): boolean;
135
+ /** @internal */
136
+ emit<S extends keyof CustomEvents>(event: Exclude<S, keyof ClientPollingEvents>, ...args: CustomEvents[S]): this;
137
+ /** @internal */ emit<S extends string | symbol>(event: Exclude<S, keyof ClientPollingEvents>, ...args: any[]): boolean;
138
+ }
139
+ interface ClientPollingEvents {
140
+ [PollingEvents.NewSeasonStart]: [id: string];
141
+ [PollingEvents.MaintenanceStart]: [];
142
+ [PollingEvents.MaintenanceEnd]: [duration: number];
143
+ [PollingEvents.ClanLoopStart]: [];
144
+ [PollingEvents.ClanLoopEnd]: [];
145
+ [PollingEvents.PlayerLoopStart]: [];
146
+ [PollingEvents.PlayerLoopEnd]: [];
147
+ [PollingEvents.WarLoopStart]: [];
148
+ [PollingEvents.WarLoopEnd]: [];
149
+ [PollingEvents.Error]: [error: unknown];
150
+ [PollingEvents.Debug]: [path: string, status: string, message: string];
151
+ }
152
+ interface CustomEvents {
153
+ [key: `clan${string}`]: [oldClan: Clan, newClan: Clan];
154
+ [key: `war${string}`]: [oldWar: ClanWar, newWar: ClanWar];
155
+ [key: `player${string}`]: [oldPlayer: Player, newPlayer: Player];
156
+ }
157
+ export {};