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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
mod structs;
pub use structs::*;

use etterna::*;

use crate::extension_traits::*;
use crate::Error;

/// The kind of ranges that EO can process. Ranges can never be empty! They must have one or more
/// elements
pub trait EoRange {
	#[doc(hidden)]
	/// The length must not be zero
	fn start_length(&self) -> (u32, u32);
}

impl EoRange for std::ops::Range<u32> {
	fn start_length(&self) -> (u32, u32) {
		match self.end.saturating_sub(self.start) {
			0 => panic!("Range cannot be empty or negative: {:?}", self),
			length => (self.start, length),
		}
	}
}

impl EoRange for std::ops::RangeInclusive<u32> {
	fn start_length(&self) -> (u32, u32) {
		match (self.end() + 1).saturating_sub(*self.start()) {
			0 => panic!("Range cannot be empty or negative: {:?}", self),
			length => (*self.start(), length),
		}
	}
}

impl EoRange for std::ops::RangeToInclusive<u32> {
	fn start_length(&self) -> (u32, u32) {
		(0, self.end + 1)
	}
}

impl EoRange for std::ops::RangeTo<u32> {
	fn start_length(&self) -> (u32, u32) {
		match self.end {
			0 => panic!("Range cannot be empty: {:?}", self),
			length => (0, length),
		}
	}
}

impl EoRange for std::ops::RangeFull {
	fn start_length(&self) -> (u32, u32) {
		// EO interprets a zero length as a full range
		(0, 0)
	}
}

pub struct Session {
	// Rate limiting stuff
	last_request: std::sync::Mutex<std::time::Instant>, // could replace this was smth like a AtomicInstant
	request_cooldown: std::time::Duration,

	timeout: Option<std::time::Duration>,
}

impl Session {
	pub fn new(
		request_cooldown: std::time::Duration,
		timeout: Option<std::time::Duration>,
	) -> Self {
		Self {
			request_cooldown,
			timeout,
			last_request: std::sync::Mutex::new(std::time::Instant::now() - request_cooldown),
		}
	}

	fn request(
		&self,
		method: &str,
		path: &str,
		request_callback: impl Fn(ureq::Request) -> ureq::Response,
	) -> Result<String, Error> {
		// UNWRAP: propagate panics
		crate::rate_limit(
			&mut *self.last_request.lock().unwrap(),
			self.request_cooldown,
		);

		let mut request = ureq::request(method, &format!("https://etternaonline.com/{}", path));
		if let Some(timeout) = self.timeout {
			request.timeout(timeout);
		}
		let response = request_callback(request).into_string()?;

		if response.trim().is_empty() {
			return Err(Error::EmptyServerResponse);
		}

		Ok(response)
	}

	/// Panics if the provided range is empty or negative
	pub fn packlist(&self, range_to_retrieve: impl EoRange) -> Result<Vec<PackEntry>, Error> {
		let (start, length) = range_to_retrieve.start_length();

		let json = self.request("POST", "pack/packlist", |mut r| {
			r.send_form(&[
				("start", &start.to_string()),
				("length", &length.to_string()),
			])
		})?;
		let json: serde_json::Value = serde_json::from_str(&json)?;

		json["data"]
			.array()?
			.iter()
			.map(|json| {
				Ok(PackEntry {
					average_msd: json["average"].attempt_get("average_msd", |j| {
						Some(j.as_str()?.extract("\" />", "</span>")?.parse().ok()?)
					})?,
					datetime: json["date"]
						.attempt_get("datetime", |j| Some(j.as_str()?.to_owned()))?,
					size: json["size"].attempt_get("size", |j| Some(j.as_str()?.parse().ok()?))?,
					name: json["packname"].attempt_get("name", |j| {
						Some(j.as_str()?.extract(">", "</a>")?.to_owned())
					})?,
					id: json["packname"].attempt_get("id", |j| {
						Some(j.as_str()?.extract("pack/", "\"")?.parse().ok()?)
					})?,
					num_votes: json["r_avg"].attempt_get("num_votes", |j| {
						Some(j.as_str()?.extract("title='", " votes")?.parse().ok()?)
					})?,
					average_vote: json["r_avg"].attempt_get("average_vote", |j| {
						Some(j.as_str()?.extract("votes'>", "</div>")?.parse().ok()?)
					})?,
					download_link: json["download"].attempt_get("download_link", |j| {
						Some(j.as_str()?.extract("href=\"", "\">")?.to_owned())
					})?,
				})
			})
			.collect()
	}

	/// Panics if the provided range is empty or negative
	pub fn leaderboard(
		&self,
		range_to_retrieve: impl EoRange,
	) -> Result<Vec<LeaderboardEntry>, Error> {
		let (start, length) = range_to_retrieve.start_length();

		let json = self.request("POST", "leaderboard/leaderboard", |mut r| {
			r.send_form(&[
				("start", &start.to_string()),
				("length", &length.to_string()),
			])
		})?;
		let json: serde_json::Value = serde_json::from_str(&json)?;

		json["data"]
			.array()?
			.iter()
			.map(|json| {
				Ok(LeaderboardEntry {
					rank: json["rank"].attempt_get("rank int", |j| {
						Some(j.as_str()?.trim_start_matches('#').parse().ok()?)
					})?,
					username: json["username"].attempt_get("leaderboard username", |j| {
						Some(j.as_str()?.extract("/user/", "\"")?.to_owned())
					})?,
					country: (|| {
						Some(Country {
							code: json["username"]
								.as_str()?
								.extract("/img/flags/", ".svg")?
								.to_owned(),
							name: json["username"]
								.as_str()?
								.extract("title=\"", "\"")?
								.to_owned(),
						})
					})(),
					avatar: json["username"].attempt_get("leaderboard username", |j| {
						Some(j.as_str()?.extract("/avatars/", "\"")?.to_owned())
					})?,
					rating: etterna::Skillsets8 {
						overall: json["player_rating"].f32_()?,
						stamina: json["Stamina"].f32_()?,
						stream: json["Stream"].f32_()?,
						jumpstream: json["Jumpstream"].f32_()?,
						handstream: json["Handstream"].f32_()?,
						jackspeed: json["JackSpeed"].f32_()?,
						chordjack: json["Chordjack"].f32_()?,
						technical: json["Technical"].f32_()?,
					},
				})
			})
			.collect()
	}

	/// Panics if the provided range is empty or negative
	pub fn user_scores(
		&self,
		user_id: u32,
		range_to_retrieve: impl EoRange,
		song_name_search_query: Option<&str>,
		sort_criterium: UserScoresSortBy,
		sort_direction: SortDirection,
		include_invalid: bool,
	) -> Result<UserScores, Error> {
		let (start, length) = range_to_retrieve.start_length();

		let json = self.request(
			"POST",
			if include_invalid {
				"score/userScores"
			} else {
				"valid_score/userScores"
			},
			|mut r| {
				r.send_form(&[
					("start", &start.to_string()),
					("length", &length.to_string()),
					("userid", &user_id.to_string()),
					(
						"order[0][dir]",
						match sort_direction {
							SortDirection::Ascending => "asc",
							SortDirection::Descending => "desc",
						},
					),
					(
						"order[0][column]",
						match sort_criterium {
							UserScoresSortBy::SongName => "0",
							UserScoresSortBy::Rate => "1",
							UserScoresSortBy::SsrOverall => "2",
							UserScoresSortBy::SsrOverallNerfed => "3",
							UserScoresSortBy::Wifescore => "4",
							UserScoresSortBy::Date => "5",
							UserScoresSortBy::Stream => "6",
							UserScoresSortBy::Jumpstream => "7",
							UserScoresSortBy::Handstream => "8",
							UserScoresSortBy::Stamina => "9",
							UserScoresSortBy::Jacks => "10",
							UserScoresSortBy::Chordjacks => "11",
							UserScoresSortBy::Technical => "12",
							UserScoresSortBy::ChordCohesion => "13",
							UserScoresSortBy::Scorekey => "",
						},
					),
					("search[value]", song_name_search_query.unwrap_or("")),
				])
			},
		)?;
		let json: serde_json::Value = serde_json::from_str(&json)?;

		let scores = json["data"]
			.array()?
			.iter()
			.map(|json| {
				Ok(UserScore {
					song_name: json["songname"].attempt_get("song name", |j| {
						Some(j.as_str()?.extract("\">", "</a>")?.to_owned())
					})?,
					song_id: json["songname"].attempt_get("song id", |j| {
						Some(j.as_str()?.extract("song/view/", "\"")?.parse().ok()?)
					})?,
					// scorekey: json["scorekey"].parse()?, // this disappeared
					rate: json["user_chart_rate_rate"].parse()?,
					wifescore: json["wifescore"].attempt_get("wifescore", |j| {
						Some(etterna::Wifescore::from_percent(
							j.as_str()?
								.extract("<span class=", "</span>")?
								.extract(">", "%")?
								.parse()
								.ok()?,
						)?)
					})?,
					judgements: json["wifescore"].attempt_get("judgements", |j| {
						let string = j.as_str()?;
						Some(etterna::TapJudgements {
							marvelouses: string.extract("Marvelous: ", "<br")?.parse().ok()?,
							perfects: string.extract("Perfect: ", "<br")?.parse().ok()?,
							greats: string.extract("Great: ", "<br")?.parse().ok()?,
							goods: string.extract("Good: ", "<br")?.parse().ok()?,
							bads: string.extract("Bad: ", "<br")?.parse().ok()?,
							misses: string.extract("Miss: ", "<br")?.parse().ok()?,
						})
					})?,
					date: json["datetime"].string()?,
					has_chord_cohesion: json["nocc"].attempt_get("'Off' or 'On'", |j| {
						match j.as_str()? {
							"On" => Some(true),
							"Off" => Some(false),
							_ => None,
						}
					})?,
					validity_dependant: if json["Overall"].str_()?.contains("Invalid Score") {
						None
					} else {
						Some(ValidUserScoreInfo {
							scorekey: json["Overall"].attempt_get("scorekey", |j| {
								Some(
									j.as_str()?.extract("score/view/", "\"")?[..41]
										.parse()
										.ok()?,
								)
							})?,
							user_id: json["Overall"].attempt_get("user id", |j| {
								Some(
									j.as_str()?.extract("score/view/", "\"")?[41..]
										.parse()
										.ok()?,
								)
							})?,
							// The following are zero if the score is invalid
							ssr: etterna::Skillsets8 {
								overall: json["Overall"].attempt_get("overall", |j| {
									Some(j.as_str()?.extract("\">", "<")?.parse().ok()?)
								})?,
								stream: json["stream"].parse()?,
								jumpstream: json["jumpstream"].parse()?,
								handstream: json["handstream"].parse()?,
								stamina: json["stamina"].parse()?,
								jackspeed: json["jackspeed"].parse()?,
								chordjack: json["chordjack"].parse()?,
								technical: json["technical"].parse()?,
							},
							ssr_overall_nerfed: json["Nerf"].f32_()?,
						})
					},
				})
			})
			.collect::<Result<Vec<UserScore>, Error>>()?;

		Ok(UserScores {
			entries_before_search_filtering: json["recordsTotal"].u32_()?,
			entries_after_search_filtering: json["recordsFiltered"].u32_()?,
			scores,
		})
	}

	pub fn user_details(&self, username: &str) -> Result<UserDetails, Error> {
		let response = self.request("GET", &format!("user/{}", username), |mut r| r.call())?;

		if response.contains("Looks like the page you want, aint here.")
			|| response.contains("disallowed characters") // if username has funky chars
			|| response.contains("\"errors\":[]") // if username is empty
			|| response.is_empty()
		// or idek
		{
			return Err(Error::UserNotFound);
		}

		Ok(UserDetails {
			user_id: (|| response.as_str().extract("'userid': '", "'")?.parse().ok())()
				.ok_or_else(|| {
					Error::InvalidDataStructure("No userid found in user page".to_owned())
				})?,
		})
	}

	/// `all_rates` - if true, show users' scores for all rates instead of just their best score
	pub fn chart_leaderboard(
		&self,
		chartkey: impl AsRef<str>,
		range_to_retrieve: impl EoRange,
		user_name_search_query: Option<&str>,
		sort_criterium: ChartLeaderboardSortBy,
		sort_direction: SortDirection,
		all_rates: bool,
		include_invalid: bool,
	) -> Result<ChartLeaderboard, Error> {
		let (start, length) = range_to_retrieve.start_length();

		let json = self.request(
			"POST",
			if include_invalid {
				"score/chartOverallScores"
			} else {
				"valid_score/chartOverallScores"
			},
			|mut r| {
				r.send_form(&[
					("start", &start.to_string()),
					("length", &length.to_string()),
					("chartkey", chartkey.as_ref()),
					("top", if all_rates { "" } else { "true" }),
					(
						"order[0][dir]",
						match sort_direction {
							SortDirection::Ascending => "asc",
							SortDirection::Descending => "desc",
						},
					),
					(
						"order[0][column]",
						match sort_criterium {
							ChartLeaderboardSortBy::Username => "1",
							ChartLeaderboardSortBy::SsrOverall => "2",
							ChartLeaderboardSortBy::Rate => "4",
							ChartLeaderboardSortBy::Wife => "5",
							ChartLeaderboardSortBy::Date => "6",
							ChartLeaderboardSortBy::Marvelouses => "7",
							ChartLeaderboardSortBy::Perfects => "8",
							ChartLeaderboardSortBy::Greats => "9",
							ChartLeaderboardSortBy::Goods => "10",
							ChartLeaderboardSortBy::Bads => "11",
							ChartLeaderboardSortBy::Misses => "12",
							ChartLeaderboardSortBy::MaxCombo => "13",
							ChartLeaderboardSortBy::Scorekey => "",
						},
					),
					("search[value]", user_name_search_query.unwrap_or("")),
				])
			},
		)?;
		let json: serde_json::Value = serde_json::from_str(&json)?;

		Ok(ChartLeaderboard {
			entries_before_search_filtering: json["recordsTotal"].u32_()?,
			entries_after_search_filtering: json["recordsFiltered"].u32_()?,
			entries: json["data"]
				.array()?
				.iter()
				.map(|json| {
					Ok(ChartLeaderboardEntry {
						// turns out this is actually not a rank but just an index, i.e. if you sort by
						// date, rank #1 would be the latest score, not the best score. _That_ kind of rank
						// is pretty useless so let's not parse it to not confuse users about what this is
						// rank: json.attempt_get("rank string", |json| {
						// 	let s = json["rank"].as_str()?;
						// 	if &s[0..1] != "#" { return None; }
						// 	Some(s[1..].parse::<u32>().ok()? - 1)
						// })?,
						date: json["date"].string()?,
						judgements: TapJudgements {
							marvelouses: json["marv"].parse()?,
							perfects: json["perfect"].parse()?,
							greats: json["great"].parse()?,
							goods: json["good"].parse()?,
							bads: json["bad"].parse()?,
							misses: json["miss"].parse()?,
						},
						max_combo: json["combo"].parse()?,
						rate: json["rate"].parse()?,
						ssr_overall: json["score"].attempt_get("SSR from score html", |json| {
							Some(json.as_str()?.extract("\">", "<")?.parse().ok()?)
						})?,
						ssr_overall_nerfed: json["nerf"].f32_()?,
						scorekey: json["score"]
							.attempt_get("scorekey from score html", |json| {
								Some(json.as_str()?.extract("view/", "\"")?[..41].parse().ok()?)
							})?,
						user_id: json["score"].attempt_get("scorekey from score html", |json| {
							Some(json.as_str()?.extract("view/", "\"")?[41..].parse().ok()?)
						})?,
						username: json["username"]
							.attempt_get("username from username html", |json| {
								Some(json.as_str()?.extract("user/", "\"")?.to_owned())
							})?,
						wifescore: json["wife"].attempt_get(
							"wifescore from wife html",
							|json| {
								Some(Wifescore::from_percent(
									json.as_str()?.extract(">", "%")?.parse::<f32>().ok()?,
								)?)
							},
						)?,
					})
				})
				.collect::<Result<Vec<_>, Error>>()?,
		})
	}
}