Fetch Mastery Points from the Riot API and add the champion sort preference (#76)
The tracker asks Riot for each Account's champion mastery (platform host) right after a successful sync, and after a Match fetched by id is added, so no new polling. Lists are stored per Account in one transaction and read back with Tracker::mastery (None = never fetched). A failed fetch keeps the last values and records no problem. Preferences gain champion_sort (mastery or name). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
50c0427958
commit
5ce8c777c4
9 files changed
+593
-4
No files matched your search
+73
-1
@@ -3,6 +3,7 @@ use std::sync::{Mutex, PoisonError};
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use serde::Deserialize;
|
||||
use ureq::{Agent, Body};
|
||||
|
||||
use crate::error::SourceError;
|
||||
@@ -42,6 +43,10 @@ impl RiotApi {
|
||||
}
|
||||
|
||||
fn get(&self, path_and_query: &str) -> Result<String, SourceError> {
|
||||
self.get_on(&self.host, path_and_query)
|
||||
}
|
||||
|
||||
fn get_on(&self, host: &str, path_and_query: &str) -> Result<String, SourceError> {
|
||||
let key = self
|
||||
.key
|
||||
.lock()
|
||||
@@ -55,7 +60,7 @@ impl RiotApi {
|
||||
Instant::now(),
|
||||
);
|
||||
thread::sleep(wait);
|
||||
let url = format!("{}{path_and_query}", self.host);
|
||||
let url = format!("{host}{path_and_query}");
|
||||
get(&self.agent, &url, Some(&key), |body| body.read_to_string())
|
||||
}
|
||||
}
|
||||
@@ -132,6 +137,29 @@ impl MatchHistorySource for RiotApi {
|
||||
fn fetch_match(&self, match_id: &str) -> Result<String, SourceError> {
|
||||
self.get(&format!("/lol/match/v5/matches/{}", encode(match_id)))
|
||||
}
|
||||
|
||||
/// Mastery lives on the platform host (e.g. `euw1`), not on the regional one.
|
||||
fn champion_mastery(
|
||||
&self,
|
||||
platform: &str,
|
||||
puuid: &str,
|
||||
) -> Result<Vec<(u32, u64)>, SourceError> {
|
||||
// The platform becomes part of the host: nothing but letters and digits goes through.
|
||||
if platform.is_empty() || !platform.bytes().all(|b| b.is_ascii_alphanumeric()) {
|
||||
return Err(SourceError::Other(format!(
|
||||
"Plateforme invalide : {platform}"
|
||||
)));
|
||||
}
|
||||
let host = format!(
|
||||
"https://{}.api.riotgames.com",
|
||||
platform.to_ascii_lowercase()
|
||||
);
|
||||
let path = format!(
|
||||
"/lol/champion-mastery/v4/champion-masteries/by-puuid/{}",
|
||||
encode(puuid)
|
||||
);
|
||||
decode_mastery(&self.get_on(&host, &path)?)
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_account(body: &str) -> Result<Account, SourceError> {
|
||||
@@ -143,6 +171,21 @@ fn decode_ids(body: &str) -> Result<Vec<String>, SourceError> {
|
||||
.map_err(|e| SourceError::Other(format!("Liste de Parties illisible : {e}")))
|
||||
}
|
||||
|
||||
fn decode_mastery(body: &str) -> Result<Vec<(u32, u64)>, SourceError> {
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct Entry {
|
||||
champion_id: u32,
|
||||
champion_points: u64,
|
||||
}
|
||||
let entries: Vec<Entry> = serde_json::from_str(body)
|
||||
.map_err(|e| SourceError::Other(format!("Maîtrise illisible : {e}")))?;
|
||||
Ok(entries
|
||||
.into_iter()
|
||||
.map(|e| (e.champion_id, e.champion_points))
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// Percent-encodes everything but RFC 3986 unreserved characters.
|
||||
pub(crate) fn encode(segment: &str) -> String {
|
||||
let mut out = String::new();
|
||||
@@ -194,6 +237,26 @@ mod tests {
|
||||
Err(SourceError::MissingKey)
|
||||
);
|
||||
assert_eq!(api.fetch_match("EUW1_1"), Err(SourceError::MissingKey));
|
||||
assert_eq!(
|
||||
api.champion_mastery("EUW1", "p"),
|
||||
Err(SourceError::MissingKey)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_invalid_platform_is_rejected_without_a_request() {
|
||||
// With a key, a request would be attempted (and fail on the network, not as `Other`).
|
||||
let api = RiotApi::new("RGAPI-test".into(), "europe");
|
||||
for platform in ["", "euw1.evil.example/", "eu w1", "euw1@host", "é"] {
|
||||
assert!(
|
||||
matches!(
|
||||
api.champion_mastery(platform, "p"),
|
||||
Err(SourceError::Other(_))
|
||||
),
|
||||
"{platform:?}"
|
||||
);
|
||||
}
|
||||
assert!(api.sent.lock().unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -218,6 +281,15 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decodes_the_champion_mastery_response() {
|
||||
let points =
|
||||
decode_mastery(include_str!("../tests/fixtures/riot/champion_mastery.json")).unwrap();
|
||||
assert_eq!(points, [(222, 154_320), (103, 27_500), (51, 0)]);
|
||||
assert_eq!(decode_mastery("[]").unwrap(), []);
|
||||
assert!(decode_mastery("{}").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decodes_a_match_id_list() {
|
||||
assert_eq!(
|
||||
|
||||
Reference in new issue
Block a user