Files
arena-tracker/crates/core/tests/common/payloads.rs
T

74 lines
2.4 KiB
Rust

use super::*;
pub const SELF_PUUID: &str = "fixture-puuid-self";
const BASE: &str = include_str!("../fixtures/riot/match_1750_EUW1_9000000006.json");
/// The six recorded Riot payloads.
pub fn recorded_matches() -> Vec<String> {
[
include_str!("../fixtures/riot/match_1700_EUW1_9000000001.json"),
include_str!("../fixtures/riot/match_1700_EUW1_9000000002.json"),
include_str!("../fixtures/riot/match_1740_EUW1_9000000004.json"),
include_str!("../fixtures/riot/match_1740_EUW1_9000000005.json"),
include_str!("../fixtures/riot/match_1750_EUW1_9000000003.json"),
include_str!("../fixtures/riot/match_1750_EUW1_9000000006.json"),
]
.map(String::from)
.to_vec()
}
pub fn recorded_account() -> Account {
serde_json::from_str(include_str!("../fixtures/riot/account.json")).unwrap()
}
/// A payload cloned from a recorded one, with the self participant patched.
pub fn payload(
match_id: &str,
queue_id: u32,
champion_id: u32,
champion_name: &str,
placement: u8,
win: bool,
started_at_ms: i64,
) -> String {
let mut v: Value = serde_json::from_str(BASE).unwrap();
v["metadata"]["matchId"] = match_id.into();
v["info"]["queueId"] = queue_id.into();
v["info"]["gameStartTimestamp"] = started_at_ms.into();
let me = v["info"]["participants"]
.as_array_mut()
.unwrap()
.iter_mut()
.find(|p| p["puuid"] == SELF_PUUID)
.unwrap();
me["championId"] = champion_id.into();
me["championName"] = champion_name.into();
me["placement"] = placement.into();
me["win"] = win.into();
v.to_string()
}
pub fn with_game_mode(payload: &str, game_mode: &str) -> String {
let mut v: Value = serde_json::from_str(payload).unwrap();
v["info"]["gameMode"] = game_mode.into();
v.to_string()
}
/// The payload with these fields missing from every participant but the Account.
pub fn without_fields_for_others(payload: &str, fields: &[&str]) -> String {
let mut v: Value = serde_json::from_str(payload).unwrap();
for p in v["info"]["participants"].as_array_mut().unwrap() {
if p["puuid"] != SELF_PUUID {
for field in fields {
p.as_object_mut().unwrap().remove(*field);
}
}
}
v.to_string()
}
pub fn played_by(payload: &str, puuid: &str) -> String {
payload.replace(SELF_PUUID, puuid)
}