Fix the final champ select review findings (#13)
This commit was merged in pull request #51.
This commit is contained in:
1 parent
cf15eeeb8f
commit
304d958c72
12 files changed
+612
-375
No files matched your search
@@ -2,7 +2,7 @@
|
||||
// Each test binary uses its own subset of these helpers.
|
||||
#![allow(dead_code)]
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::path::Path;
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||
use std::sync::mpsc::{Receiver, Sender};
|
||||
@@ -10,9 +10,9 @@ use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
|
||||
use arena_tracker_core::{
|
||||
Account, ChampSelect, Champion, ChampionCatalog, ChampionSource, ClientError, ClientEvent,
|
||||
Date, GameClientSource, GamePhase, Gameflow, MatchHistorySource, Season, SourceError, Tracker,
|
||||
decode_gameflow,
|
||||
Account, ChampSelect, ChampSelectReader, Champion, ChampionCatalog, ChampionSource,
|
||||
ClientError, ClientEvent, Date, GameClientSource, GamePhase, Gameflow, MatchHistorySource,
|
||||
Season, SourceError, Tracker, decode_gameflow,
|
||||
};
|
||||
use serde_json::Value;
|
||||
|
||||
@@ -562,6 +562,9 @@ pub fn recorded_game_id() -> u64 {
|
||||
#[derive(Clone, Default)]
|
||||
pub struct FakeClient {
|
||||
state: Arc<Mutex<ClientState>>,
|
||||
/// Session and champion-list bodies the fake's champ select reader asked for.
|
||||
session_reads: Arc<AtomicUsize>,
|
||||
list_reads: Arc<AtomicUsize>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -570,15 +573,20 @@ struct ClientState {
|
||||
gameflow: Gameflow,
|
||||
tracks: HashMap<String, u32>,
|
||||
waits: Vec<Duration>,
|
||||
/// The raw bodies the client answers for the current draft.
|
||||
/// The raw bodies the client answers for the current champ select.
|
||||
champ_select: Option<RawChampSelect>,
|
||||
/// Reads the champ select the one way the live client does.
|
||||
champ_select_reader: ChampSelectReader,
|
||||
/// Session bodies the client reports as events, applied before `wait_for_event` returns.
|
||||
events: VecDeque<Value>,
|
||||
/// A raw gameflow body, replayed through the same parser as the live client.
|
||||
raw_gameflow: Option<String>,
|
||||
gameflow_error: Option<ClientError>,
|
||||
champ_select_error: Option<ClientError>,
|
||||
}
|
||||
|
||||
/// The raw client responses of a draft, replayed through the same parsing as the live client.
|
||||
/// The raw client responses of a champ select, replayed through the same reader as the live
|
||||
/// client.
|
||||
#[derive(Clone)]
|
||||
pub struct RawChampSelect {
|
||||
pub pickable: String,
|
||||
@@ -659,6 +667,20 @@ impl FakeClient {
|
||||
self.state.lock().unwrap().champ_select = Some(raw);
|
||||
}
|
||||
|
||||
/// Reports one of the captured session bodies as the client's champ select event.
|
||||
pub fn send_champ_select_event(&self, session: &str) {
|
||||
let session = serde_json::from_str(session).unwrap_or(Value::Null);
|
||||
self.state.lock().unwrap().events.push_back(session);
|
||||
}
|
||||
|
||||
/// How many session and champion-list bodies the champ select reader asked the client for.
|
||||
pub fn champ_select_reads(&self) -> (usize, usize) {
|
||||
(
|
||||
self.session_reads.load(Ordering::SeqCst),
|
||||
self.list_reads.load(Ordering::SeqCst),
|
||||
)
|
||||
}
|
||||
|
||||
/// The next gameflow or champ select read fails with this, until cleared.
|
||||
pub fn fail_gameflow(&self, error: Option<ClientError>) {
|
||||
self.state.lock().unwrap().gameflow_error = error;
|
||||
@@ -694,10 +716,14 @@ impl GameClientSource for FakeClient {
|
||||
if let Some(error) = &state.gameflow_error {
|
||||
return Err(error.clone());
|
||||
}
|
||||
match &state.raw_gameflow {
|
||||
Some(body) => decode_gameflow(200, body),
|
||||
None => Ok(state.gameflow.clone()),
|
||||
}
|
||||
let flow = match &state.raw_gameflow {
|
||||
Some(body) => decode_gameflow(200, body)?,
|
||||
None => state.gameflow.clone(),
|
||||
};
|
||||
state
|
||||
.champ_select_reader
|
||||
.reset_unless_arena_champ_select(&flow);
|
||||
Ok(flow)
|
||||
}
|
||||
|
||||
fn champ_select(&self) -> Result<ChampSelect, ClientError> {
|
||||
@@ -706,12 +732,22 @@ impl GameClientSource for FakeClient {
|
||||
return Err(error.clone());
|
||||
}
|
||||
let raw = state.champ_select.as_ref().ok_or(ClientError::Absent)?;
|
||||
let session = serde_json::from_str(&raw.session)
|
||||
.map_err(|error| ClientError::Other(error.to_string()))?;
|
||||
let pickable = ChampSelect::pickable_champions(&raw.pickable, &raw.disabled)?;
|
||||
let anvil = serde_json::from_str(&raw.anvil)
|
||||
.map_err(|error| ClientError::Other(error.to_string()))?;
|
||||
ChampSelect::from_session(&session, pickable, anvil)
|
||||
let sessions = self.session_reads.clone();
|
||||
let lists = self.list_reads.clone();
|
||||
state.champ_select_reader.read(
|
||||
|| {
|
||||
sessions.fetch_add(1, Ordering::SeqCst);
|
||||
Ok(raw.session.clone())
|
||||
},
|
||||
|| {
|
||||
lists.fetch_add(1, Ordering::SeqCst);
|
||||
Ok((
|
||||
raw.pickable.clone(),
|
||||
raw.disabled.clone(),
|
||||
Some(raw.anvil.clone()),
|
||||
))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn official_progress(&self, track_name: &str) -> Result<Option<u32>, ClientError> {
|
||||
@@ -721,7 +757,14 @@ impl GameClientSource for FakeClient {
|
||||
}
|
||||
|
||||
fn wait_for_event(&self, timeout: Duration) -> ClientEvent {
|
||||
self.state.lock().unwrap().waits.push(timeout);
|
||||
ClientEvent::Other
|
||||
let mut state = self.state.lock().unwrap();
|
||||
state.waits.push(timeout);
|
||||
match state.events.pop_front() {
|
||||
Some(session) => {
|
||||
state.champ_select_reader.set_session(session);
|
||||
ClientEvent::ChampSelect
|
||||
}
|
||||
None => ClientEvent::Other,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user