clashofclans.js 3.5.3-dev.96712e2 → 3.5.4-dev.074d698

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.
@@ -54,6 +54,16 @@ export declare class Util extends null {
54
54
  endTime: Date;
55
55
  seasonId: string;
56
56
  };
57
+ static getTournamentWindow(timestamp?: Date): {
58
+ id: string;
59
+ startTime: Date;
60
+ endTime: Date;
61
+ };
62
+ static getTournamentWindowById(id: string): {
63
+ id: string;
64
+ startTime: Date;
65
+ endTime: Date;
66
+ };
57
67
  static allSettled<T>(values: Promise<T>[]): Promise<T[]>;
58
68
  static delay(ms: number): Promise<unknown>;
59
69
  }
package/dist/util/Util.js CHANGED
@@ -195,6 +195,32 @@ class Util extends null {
195
195
  const endTime = new Date(startTime.getTime() + seasonDuration);
196
196
  return { startTime, endTime, seasonId };
197
197
  }
198
+ static getTournamentWindow(timestamp) {
199
+ const now = timestamp ?? new Date();
200
+ const day = now.getUTCDay(); // 0=Sun, 1=Mon, ..., 6=Sat
201
+ const hour = now.getUTCHours();
202
+ // Days since last Monday
203
+ let daysSinceMonday = (day + 6) % 7; // e.g., if Mon -> 0, Tue -> 1, etc.
204
+ // If today is Monday but before 5 AM, move back one full week
205
+ if (day === 1 && hour < 5) {
206
+ daysSinceMonday = 7;
207
+ }
208
+ // Last Monday 5:00 AM UTC
209
+ const lastMonday = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate() - daysSinceMonday, 5, 0, 0, 0));
210
+ // Next Monday 5:00 AM UTC (7 days after last Monday)
211
+ const nextMonday = new Date(Date.UTC(lastMonday.getUTCFullYear(), lastMonday.getUTCMonth(), lastMonday.getUTCDate() + 7, 5, 0, 0, 0));
212
+ const year = lastMonday.getUTCFullYear();
213
+ const month = String(lastMonday.getUTCMonth() + 1).padStart(2, '0');
214
+ const dayOfMonth = String(lastMonday.getUTCDate()).padStart(2, '0');
215
+ return {
216
+ id: `${year}-${month}-${dayOfMonth}`,
217
+ startTime: lastMonday,
218
+ endTime: nextMonday
219
+ };
220
+ }
221
+ static getTournamentWindowById(id) {
222
+ return this.getTournamentWindow(new Date(`${id}T05:00:00.000Z`));
223
+ }
198
224
  static async allSettled(values) {
199
225
  return (await Promise.allSettled(values))
200
226
  .filter((res) => res.status === 'fulfilled')
package/dist/util.spec.js CHANGED
@@ -140,4 +140,68 @@ describe('util', () => {
140
140
  expect(endTime.toISOString()).toBe(expectedEndTime);
141
141
  expect(seasonId).toBe('2026-02');
142
142
  });
143
+ // tournament window
144
+ it('should get the correct tournament window for a Wednesday', async () => {
145
+ const timestamp = new Date('2024-03-20T10:00'); // Wednesday
146
+ const { endTime, startTime } = Util_1.Util.getTournamentWindow(timestamp);
147
+ const expectedStartTime = new Date('2024-03-18T05:00').toISOString(); // Last Monday 5 AM UTC
148
+ const expectedEndTime = new Date('2024-03-25T05:00').toISOString(); // Next Monday 5 AM UTC
149
+ expect(startTime.toISOString()).toBe(expectedStartTime);
150
+ expect(endTime.toISOString()).toBe(expectedEndTime);
151
+ });
152
+ it('should get the correct tournament window for a Monday after 5 AM', async () => {
153
+ const timestamp = new Date('2024-03-18T10:00'); // Monday after 5 AM
154
+ const { endTime, startTime } = Util_1.Util.getTournamentWindow(timestamp);
155
+ const expectedStartTime = new Date('2024-03-18T05:00').toISOString(); // Last Monday 5 AM UTC
156
+ const expectedEndTime = new Date('2024-03-25T05:00').toISOString(); // Next Monday 5 AM UTC
157
+ expect(startTime.toISOString()).toBe(expectedStartTime);
158
+ expect(endTime.toISOString()).toBe(expectedEndTime);
159
+ });
160
+ it('should get the correct tournament window for a Monday before 5 AM', async () => {
161
+ const timestamp = new Date('2024-03-18T04:00'); // Monday before 5 AM
162
+ const { endTime, startTime } = Util_1.Util.getTournamentWindow(timestamp);
163
+ const expectedStartTime = new Date('2024-03-11T05:00').toISOString(); // Last Monday 5 AM UTC (a week earlier)
164
+ const expectedEndTime = new Date('2024-03-18T05:00').toISOString(); // Next Monday 5 AM UTC
165
+ expect(startTime.toISOString()).toBe(expectedStartTime);
166
+ expect(endTime.toISOString()).toBe(expectedEndTime);
167
+ });
168
+ it('should get the correct tournament window for a Sunday', async () => {
169
+ const timestamp = new Date('2024-03-17T10:00'); // Sunday
170
+ const { endTime, startTime } = Util_1.Util.getTournamentWindow(timestamp);
171
+ const expectedStartTime = new Date('2024-03-11T05:00').toISOString(); // Last Monday 5 AM UTC
172
+ const expectedEndTime = new Date('2024-03-18T05:00').toISOString(); // Next Monday 5 AM UTC
173
+ expect(startTime.toISOString()).toBe(expectedStartTime);
174
+ expect(endTime.toISOString()).toBe(expectedEndTime);
175
+ });
176
+ it('should pass 5am Monday', async () => {
177
+ const timestamp = new Date('2025-10-06T05:00');
178
+ const { endTime, startTime } = Util_1.Util.getTournamentWindow(timestamp);
179
+ const expectedStartTime = new Date('2025-10-06T05:00').toISOString(); // Last Monday 5 AM UTC
180
+ const expectedEndTime = new Date('2025-10-13T05:00').toISOString(); // Next Monday 5 AM UTC
181
+ expect(startTime.toISOString()).toBe(expectedStartTime);
182
+ expect(endTime.toISOString()).toBe(expectedEndTime);
183
+ });
184
+ it('should pass 4:59am Monday', async () => {
185
+ const timestamp = new Date('2025-10-06T04:59');
186
+ const { endTime, startTime } = Util_1.Util.getTournamentWindow(timestamp);
187
+ const expectedStartTime = new Date('2025-09-29T05:00').toISOString();
188
+ const expectedEndTime = new Date('2025-10-06T05:00').toISOString();
189
+ expect(startTime.toISOString()).toBe(expectedStartTime);
190
+ expect(endTime.toISOString()).toBe(expectedEndTime);
191
+ });
192
+ it('should pass End of year', async () => {
193
+ const timestamp = new Date('2025-12-30T05:00');
194
+ const { endTime, startTime } = Util_1.Util.getTournamentWindow(timestamp);
195
+ const expectedStartTime = new Date('2025-12-29T05:00').toISOString();
196
+ const expectedEndTime = new Date('2026-01-05T05:00').toISOString();
197
+ expect(startTime.toISOString()).toBe(expectedStartTime);
198
+ expect(endTime.toISOString()).toBe(expectedEndTime);
199
+ });
200
+ it('should pass End of year (ID)', async () => {
201
+ const { endTime, startTime } = Util_1.Util.getTournamentWindowById('2025-12-30');
202
+ const expectedStartTime = new Date('2025-12-29T05:00').toISOString();
203
+ const expectedEndTime = new Date('2026-01-05T05:00').toISOString();
204
+ expect(startTime.toISOString()).toBe(expectedStartTime);
205
+ expect(endTime.toISOString()).toBe(expectedEndTime);
206
+ });
143
207
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clashofclans.js",
3
- "version": "3.5.3-dev.96712e2",
3
+ "version": "3.5.4-dev.074d698",
4
4
  "description": "JavaScript library for interacting with the Clash of Clans API",
5
5
  "author": "https://clashofclans.js.org",
6
6
  "license": "MIT",