forked from samuelthomas2774/nxapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplatnet2-xrank.ts
243 lines (192 loc) · 8.54 KB
/
splatnet2-xrank.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//
// X Rank seasons
//
// X Rank seasons are calculated by the web app, not retrieved from the API.
//
export interface Season {
/** Season ID use by the API, e.g. "220501T00_220601T00" */
id: string;
/** Season ID use by the web app, e.g. "2022_05" */
key: string;
index: number;
start: Date;
end: Date;
complete: boolean;
}
export function* getAllSeasons(sort_ascending = false) {
let season = sort_ascending ? getFirstSeason() : getCurrentSeason();
while (season) {
yield season;
season = sort_ascending ? getNextSeason(season) : getPreviousSeason(season);
}
}
export function getSeason(year: number, month: number): Season | null
export function getSeason(start: Date): Season | null
export function getSeason(id: string | number): Season | null
export function getSeason(year: number | Date | string, month?: number): Season | null {
if (year instanceof Date) {
month = year.getUTCMonth() + 1;
year = year.getUTCFullYear();
}
if (typeof year === 'string') {
// 180401T00_180601T00
const match = year.match(/^(\d{2,})(0\d|1[012])01T00_(\d{2,})(0\d|1[012])01T00$/);
if (!match) throw new Error('Invalid season ID');
year = 2000 + parseInt(match[1]);
month = parseInt(match[2]);
const nextyear = month === 12 ? year + 1 : year;
const nextmonth = year === 2018 && month === 4 ? 6 :
month === 12 ? 1 : month + 1;
if (nextyear !== (2000 + parseInt(match[3])) || nextmonth !== parseInt(match[4])) throw new Error('Invalid season ID');
if (year === 2018 && month === 5) throw new Error('Invalid season ID');
}
const start = typeof month === 'number' ? new Date(Date.UTC(year, month - 1)) :
getSeasonStartDateByIndex(year);
if (start.getUTCFullYear() === 2018 && start.getUTCMonth() === 3) {
start.setUTCMonth(4);
}
if (Date.now() < start.getTime()) return null;
if (start.getUTCFullYear() < 2018 || (start.getUTCFullYear() === 2018 && start.getUTCMonth() < 3)) return null;
const end = new Date(Date.UTC(
start.getUTCFullYear() + (start.getUTCMonth() === 11 ? 1 : 0),
start.getUTCMonth() === 11 ? 0 : start.getUTCMonth() + 1,
));
const id = toSeasonId(start.getUTCFullYear(), start.getUTCMonth() + 1);
const key = start.getUTCFullYear() + '_' + ('' + (start.getUTCMonth() + 1)).padStart(2, '0');
if (start.getUTCFullYear() === 2018 && start.getUTCMonth() === 4) {
start.setUTCMonth(3);
}
return {
id,
key,
index: getSeasonIndex(start.getUTCFullYear(), start.getUTCMonth() + 1),
start,
end,
complete: Date.now() > end.getTime(),
};
}
export function getFirstSeason() {
return getSeason(2018, 5);
}
export function getCurrentSeason() {
return getSeason(new Date());
}
export function getNextSeason(season: Season | number): Season | null
export function getNextSeason(year: number, month: number): Season | null
export function getNextSeason(season: Season | number, month?: number): Season | null {
const current_start =
typeof season === 'number' && typeof month === 'number' ?
new Date(Date.UTC(season, month)) :
typeof season === 'number' ? getSeasonStartDateByIndex(season) :
new Date(season.start.getTime());
if (current_start.getUTCFullYear() === 2018 && current_start.getUTCMonth() === 3) {
current_start.setUTCMonth(4);
}
const start = new Date(Date.UTC(
current_start.getUTCFullYear() + (current_start.getUTCMonth() === 11 ? 1 : 0),
current_start.getUTCMonth() === 11 ? 0 : current_start.getUTCMonth() + 1,
));
if (Date.now() < start.getTime()) return null;
const end = new Date(Date.UTC(
start.getUTCFullYear() + (start.getUTCMonth() === 11 ? 1 : 0),
start.getUTCFullYear() === 2018 && start.getUTCMonth() === 3 ? 5 :
start.getUTCMonth() === 11 ? 0 : start.getUTCMonth() + 1,
));
const id = toSeasonId(start.getUTCFullYear(), start.getUTCMonth() + 1);
const key = start.getUTCFullYear() + '_' + ('' + (start.getUTCMonth() + 1)).padStart(2, '0');
return {
id,
key,
index: getSeasonIndex(start.getUTCFullYear(), start.getUTCMonth() + 1),
start,
end,
complete: Date.now() > end.getTime(),
};
}
export function getPreviousSeason(season: Season | number): Season | null
export function getPreviousSeason(year: number, month: number): Season | null
export function getPreviousSeason(season: Season | number, month?: number): Season | null {
const current_start =
typeof season === 'number' && typeof month === 'number' ?
new Date(Date.UTC(season, month)) :
typeof season === 'number' ? getSeasonStartDateByIndex(season) :
new Date(season.start.getTime());
const start = new Date(Date.UTC(
current_start.getUTCFullYear() - (current_start.getUTCMonth() === 0 ? 1 : 0),
current_start.getUTCMonth() === 0 ? 11 : current_start.getUTCMonth() - 1,
));
if (start.getUTCFullYear() < 2018 || (start.getUTCFullYear() === 2018 && start.getUTCMonth() < 3)) return null;
const end = new Date(Date.UTC(
start.getUTCFullYear() + (start.getUTCMonth() === 11 ? 1 : 0),
start.getUTCFullYear() === 2018 && start.getUTCMonth() === 3 ? 5 :
start.getUTCMonth() === 11 ? 0 : start.getUTCMonth() + 1,
));
const id = toSeasonId(start.getUTCFullYear(), start.getUTCMonth() + 1);
const key = start.getUTCFullYear() + '_' + ('' + (start.getUTCMonth() + 1)).padStart(2, '0');
if (start.getUTCFullYear() === 2018 && start.getUTCMonth() === 4) {
start.setUTCMonth(3);
}
return {
id,
key,
index: getSeasonIndex(start.getUTCFullYear(), start.getUTCMonth() + 1),
start,
end,
complete: Date.now() > end.getTime(),
};
}
export function getSeasonIndex(year: number, month: number) {
const nextyear = month === 12 ? year + 1 : year;
if (year < 2000) throw new Error('Invalid season ID');
if (nextyear >= 2100) throw new Error('Invalid season ID');
if (month < 1) throw new Error('Invalid season ID');
if (month > 12) throw new Error('Invalid season ID');
if (year < 2018) throw new Error('Invalid season ID');
if (year === 2018 && month < 4) throw new Error('Invalid season ID');
const now = new Date();
if (year > now.getUTCFullYear()) throw new Error('Invalid season ID');
if (year === now.getUTCFullYear() && month > (now.getUTCMonth() + 1)) throw new Error('Invalid season ID');
if (year === 2018 && month === 4) month = 5;
const i = year * 12 + (month - 1);
return i - 24220;
}
export function getSeasonStartDateByIndex(index: number) {
if (index < 0) throw new Error('Invalid season index');
if (index === 0) return new Date(Date.UTC(2018, 3));
const i = index + 24220;
const year = Math.floor(i / 12);
const month = (i % 12) + 1;
if (year < 2018) throw new Error('Invalid season index');
if (year === 2018 && month < 4) throw new Error('Invalid season index');
const now = new Date();
if (year > now.getUTCFullYear()) throw new Error('Invalid season index');
if (year === now.getUTCFullYear() && month > (now.getUTCMonth() + 1)) throw new Error('Invalid season index');
return new Date(Date.UTC(year, month - 1));
}
export function toSeasonId(year: number, month: number) {
if (year === 2018 && month === 4) month = 5;
const nextyear = month === 12 ? year + 1 : year;
const nextmonth = month === 12 ? 1 : month + 1;
if (year < 2000) throw new Error('Invalid season ID');
if (nextyear >= 2100) throw new Error('Invalid season ID');
if (month < 1) throw new Error('Invalid season ID');
if (month > 12) throw new Error('Invalid season ID');
if (year < 2018) throw new Error('Invalid season ID');
if (year === 2018 && month < 4) throw new Error('Invalid season ID');
const now = new Date();
if (year > now.getUTCFullYear()) throw new Error('Invalid season ID');
if (year === now.getUTCFullYear() && month > (now.getUTCMonth() + 1)) throw new Error('Invalid season ID');
if (year === 2018 && month === 5) month = 4;
return ('' + (year - 2000)).padStart(2, '0') +
('' + month).padStart(2, '0') +
'01T00_' +
('' + (nextyear - 2000)).padStart(2, '0') +
('' + nextmonth).padStart(2, '0') +
'01T00';
}
export enum Rule {
SPLAT_ZONES = 'splat_zones',
TOWER_CONTROL = 'tower_control',
RAINMAKER = 'rainmaker',
CLAM_BLITZ = 'clam_blitz',
}