66 lines
1.7 KiB
Rust
66 lines
1.7 KiB
Rust
use serde::Deserialize;
|
|
|
|
use crate::model::{Format, Match};
|
|
|
|
#[derive(Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct Payload {
|
|
metadata: PayloadMetadata,
|
|
info: PayloadInfo,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct PayloadMetadata {
|
|
match_id: String,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct PayloadInfo {
|
|
queue_id: u32,
|
|
game_mode: String,
|
|
game_start_timestamp: i64,
|
|
/// Read one by one: only the Account's own entry has to be complete.
|
|
participants: Vec<serde_json::Value>,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct Participant {
|
|
champion_id: u32,
|
|
champion_name: String,
|
|
placement: u8,
|
|
win: bool,
|
|
}
|
|
|
|
/// The Match (with its raw queue id) the Account played in a Riot match-v5 payload, or
|
|
/// `None` when the payload is not an Arena Match of this Account.
|
|
pub(super) fn decode_match(
|
|
raw: &str,
|
|
puuid: &str,
|
|
) -> Result<Option<(Match, u32)>, serde_json::Error> {
|
|
let payload: Payload = serde_json::from_str(raw)?;
|
|
let info = payload.info;
|
|
if info.game_mode != "CHERRY" {
|
|
return Ok(None);
|
|
}
|
|
let Some(format) = Format::from_queue(info.queue_id) else {
|
|
return Ok(None);
|
|
};
|
|
let Some(me) = info.participants.into_iter().find(|p| p["puuid"] == puuid) else {
|
|
return Ok(None);
|
|
};
|
|
let me: Participant = serde_json::from_value(me)?;
|
|
let m = Match {
|
|
match_id: payload.metadata.match_id,
|
|
format,
|
|
champion_id: me.champion_id,
|
|
champion_name: me.champion_name,
|
|
placement: me.placement,
|
|
win: me.win,
|
|
started_at_ms: info.game_start_timestamp,
|
|
};
|
|
Ok(Some((m, info.queue_id)))
|
|
}
|