Tell why a champ select makes a champion unavailable (#74)
Split the excluded set into banned and taken_by_ally, add ChampSelect::unavailability (banned, taken_by_ally, not_offered; never for the player's own pick) and make the anvil list an Option so an unreadable list is not read as an empty one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9c13e14072
commit
bd4de1ec05
6 files changed
+302
-59
No files matched your search
+221
-41
@@ -17,14 +17,31 @@ pub(crate) const ANVIL_EVENT: &str =
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ChampSelect {
|
||||
pub current: Option<u32>,
|
||||
/// The five anvil champions; empty when the client reports none. The client calls them
|
||||
/// crowd favorites.
|
||||
pub anvil_champions: Vec<u32>,
|
||||
/// The five anvil champions, in the order the client gives them; the client calls them
|
||||
/// crowd favorites. `None` when the client gave no readable list for this champ select,
|
||||
/// `Some` of an empty list when it said there are none: its list request fails (HTTP
|
||||
/// error, unreadable body) when the list is absent, answers `[]` for a known empty list,
|
||||
/// and its Update events carry a list while a Delete carries `null`.
|
||||
pub anvil_champions: Option<Vec<u32>>,
|
||||
/// The champions the champ select offers, before its bans and the other players' picks.
|
||||
/// Shared with the client's cache instead of cloned on every session event.
|
||||
pickable: Arc<HashSet<u32>>,
|
||||
/// Banned or taken by another player in this champ select.
|
||||
excluded: HashSet<u32>,
|
||||
/// Banned in this champ select: the completed ban actions and both teams' ban lists.
|
||||
banned: HashSet<u32>,
|
||||
/// Confirmed by another player of the local team in this champ select. A pre-pick (the
|
||||
/// client's pick intent) is not part of it.
|
||||
taken_by_ally: HashSet<u32>,
|
||||
}
|
||||
|
||||
/// Why the champ select does not let the local player pick a champion. Reasons add up; a
|
||||
/// champion available to pick has none. Whether the champion is Completed is not part of it.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct Unavailability {
|
||||
pub banned: bool,
|
||||
pub taken_by_ally: bool,
|
||||
/// The client does not offer the champion (not pickable, or disabled), and neither a ban
|
||||
/// nor an ally explains it.
|
||||
pub not_offered: bool,
|
||||
}
|
||||
|
||||
impl PartialEq for ChampSelect {
|
||||
@@ -62,19 +79,24 @@ fn ids_in(value: &Value) -> Option<Vec<u32>> {
|
||||
|
||||
impl ChampSelect {
|
||||
/// A champ select with nothing banned or taken, for callers with a ready pool.
|
||||
pub fn new(current: Option<u32>, pickable: HashSet<u32>, anvil_champions: Vec<u32>) -> Self {
|
||||
pub fn new(
|
||||
current: Option<u32>,
|
||||
pickable: HashSet<u32>,
|
||||
anvil_champions: Option<Vec<u32>>,
|
||||
) -> Self {
|
||||
Self {
|
||||
current,
|
||||
anvil_champions,
|
||||
pickable: Arc::new(pickable),
|
||||
excluded: HashSet::new(),
|
||||
banned: HashSet::new(),
|
||||
taken_by_ally: HashSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn from_session(
|
||||
session: &Value,
|
||||
pickable: Arc<HashSet<u32>>,
|
||||
anvil_champions: Vec<u32>,
|
||||
anvil_champions: Option<Vec<u32>>,
|
||||
) -> Result<Self, ClientError> {
|
||||
let local = session["localPlayerCellId"]
|
||||
.as_i64()
|
||||
@@ -86,13 +108,14 @@ impl ChampSelect {
|
||||
.ok_or(ClientError::Absent)?;
|
||||
let current =
|
||||
champion_id(&me["championId"]).or_else(|| champion_id(&me["championPickIntent"]));
|
||||
let mut excluded = HashSet::new();
|
||||
let mut taken_by_ally = HashSet::new();
|
||||
let mut banned = HashSet::new();
|
||||
for player in team
|
||||
.iter()
|
||||
.filter(|p| p["cellId"] != local && p["team"] == me["team"])
|
||||
{
|
||||
if let Some(id) = champion_id(&player["championId"]) {
|
||||
excluded.insert(id);
|
||||
taken_by_ally.insert(id);
|
||||
}
|
||||
}
|
||||
// In Arena the confirmed bans were in actions while both team ban lists were empty.
|
||||
@@ -102,20 +125,21 @@ impl ChampSelect {
|
||||
&& action["completed"] == true
|
||||
&& let Some(id) = champion_id(&action["championId"])
|
||||
{
|
||||
excluded.insert(id);
|
||||
banned.insert(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
for key in ["myTeamBans", "theirTeamBans"] {
|
||||
if let Some(bans) = session["bans"][key].as_array() {
|
||||
excluded.extend(bans.iter().filter_map(champion_id));
|
||||
banned.extend(bans.iter().filter_map(champion_id));
|
||||
}
|
||||
}
|
||||
Ok(Self {
|
||||
current,
|
||||
anvil_champions,
|
||||
pickable,
|
||||
excluded,
|
||||
banned,
|
||||
taken_by_ally,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -127,14 +151,33 @@ impl ChampSelect {
|
||||
/// Whether the local player can still pick this champion. Their own pick always stays
|
||||
/// pickable.
|
||||
pub fn can_pick(&self, id: u32) -> bool {
|
||||
self.current == Some(id) || (self.pickable.contains(&id) && !self.excluded.contains(&id))
|
||||
self.current == Some(id)
|
||||
|| (self.pickable.contains(&id)
|
||||
&& !self.banned.contains(&id)
|
||||
&& !self.taken_by_ally.contains(&id))
|
||||
}
|
||||
|
||||
/// Why the local player cannot pick this champion. The player's own pick, hovered or
|
||||
/// locked, is never unavailable.
|
||||
pub fn unavailability(&self, id: u32) -> Unavailability {
|
||||
if self.current == Some(id) {
|
||||
return Unavailability::default();
|
||||
}
|
||||
let banned = self.banned.contains(&id);
|
||||
let taken_by_ally = self.taken_by_ally.contains(&id);
|
||||
Unavailability {
|
||||
banned,
|
||||
taken_by_ally,
|
||||
not_offered: !banned && !taken_by_ally && !self.pickable.contains(&id),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether both champ selects offer the same champions, whatever the current pick. The
|
||||
/// shared set makes the common case a pointer comparison.
|
||||
pub fn same_pickable(&self, other: &Self) -> bool {
|
||||
(Arc::ptr_eq(&self.pickable, &other.pickable) || self.pickable == other.pickable)
|
||||
&& self.excluded == other.excluded
|
||||
&& self.banned == other.banned
|
||||
&& self.taken_by_ally == other.taken_by_ally
|
||||
}
|
||||
|
||||
/// A champion still to complete in the Challenge and pickable in this champ select. The
|
||||
@@ -196,7 +239,11 @@ impl ChampSelectReader {
|
||||
}
|
||||
let lists = lock(&self.lists);
|
||||
let lists = lists.as_ref().expect("set just above");
|
||||
ChampSelect::from_session(&session, lists.pickable(), lists.anvil().to_vec())
|
||||
ChampSelect::from_session(
|
||||
&session,
|
||||
lists.pickable(),
|
||||
lists.anvil().map(<[u32]>::to_vec),
|
||||
)
|
||||
}
|
||||
|
||||
/// Applies the client's pickable list event.
|
||||
@@ -227,7 +274,8 @@ pub(crate) struct ChampSelectLists {
|
||||
/// The client's pickable list, before the disabled champions are removed.
|
||||
offered: HashSet<u32>,
|
||||
disabled: HashSet<u32>,
|
||||
anvil: Vec<u32>,
|
||||
/// `None` until the client gives a readable list.
|
||||
anvil: Option<Vec<u32>>,
|
||||
/// `offered` minus `disabled`, rebuilt only when either list changes.
|
||||
pickable: Arc<HashSet<u32>>,
|
||||
}
|
||||
@@ -243,12 +291,10 @@ impl ChampSelectLists {
|
||||
disabled: ids(disabled)?,
|
||||
..Self::default()
|
||||
};
|
||||
if let Some(body) = anvil
|
||||
&& let Ok(value) = parse(body)
|
||||
&& let Some(ids) = ids_in(&value)
|
||||
{
|
||||
lists.anvil = ids;
|
||||
}
|
||||
// A missing or unreadable body leaves the list unknown; `[]` is a known empty list.
|
||||
lists.anvil = anvil
|
||||
.and_then(|body| parse(body).ok())
|
||||
.and_then(|value| ids_in(&value));
|
||||
lists.rebuild();
|
||||
Ok(lists)
|
||||
}
|
||||
@@ -261,8 +307,8 @@ impl ChampSelectLists {
|
||||
self.pickable.clone()
|
||||
}
|
||||
|
||||
pub(crate) fn anvil(&self) -> &[u32] {
|
||||
&self.anvil
|
||||
pub(crate) fn anvil(&self) -> Option<&[u32]> {
|
||||
self.anvil.as_deref()
|
||||
}
|
||||
|
||||
fn set_pickable(&mut self, data: &Value) {
|
||||
@@ -281,7 +327,7 @@ impl ChampSelectLists {
|
||||
|
||||
fn set_anvil(&mut self, data: &Value) {
|
||||
if let Some(ids) = ids_in(data) {
|
||||
self.anvil = ids;
|
||||
self.anvil = Some(ids);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -295,43 +341,177 @@ mod tests {
|
||||
fn the_lists_change_only_for_the_events_that_concern_them() {
|
||||
let mut lists = ChampSelectLists::from_bodies("[1,2,3]", "[3]", Some("[9,8]")).unwrap();
|
||||
assert_eq!(&*lists.pickable(), &HashSet::from([1, 2]));
|
||||
assert_eq!(lists.anvil(), [9, 8]);
|
||||
assert_eq!(lists.anvil(), Some(&[9, 8][..]));
|
||||
|
||||
// A Delete carries no data: it changes nothing.
|
||||
lists.set_pickable(&Value::Null);
|
||||
lists.set_anvil(&Value::Null);
|
||||
assert_eq!(&*lists.pickable(), &HashSet::from([1, 2]));
|
||||
assert_eq!(lists.anvil(), [9, 8]);
|
||||
assert_eq!(lists.anvil(), Some(&[9, 8][..]));
|
||||
|
||||
lists.set_pickable(&json!([1, 4]));
|
||||
assert_eq!(&*lists.pickable(), &HashSet::from([1, 4]));
|
||||
assert_eq!(lists.anvil(), [9, 8]);
|
||||
assert_eq!(lists.anvil(), Some(&[9, 8][..]));
|
||||
|
||||
lists.set_disabled(&json!([1]));
|
||||
assert_eq!(&*lists.pickable(), &HashSet::from([4]));
|
||||
|
||||
lists.set_anvil(&json!([5, 6]));
|
||||
assert_eq!(lists.anvil(), [5, 6]);
|
||||
assert_eq!(lists.anvil(), Some(&[5, 6][..]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_anvil_champions_are_read_once_when_the_champ_select_starts() {
|
||||
assert!(
|
||||
ChampSelectLists::from_bodies("[]", "[]", None)
|
||||
let anvil = |body| {
|
||||
ChampSelectLists::from_bodies("[]", "[]", body)
|
||||
.unwrap()
|
||||
.anvil()
|
||||
.is_empty()
|
||||
);
|
||||
assert_eq!(
|
||||
ChampSelectLists::from_bodies("[]", "[]", Some("[1,2]"))
|
||||
.unwrap()
|
||||
.anvil(),
|
||||
[1, 2]
|
||||
);
|
||||
.map(<[u32]>::to_vec)
|
||||
};
|
||||
// No answer, or one that is not a list: the client gave no list.
|
||||
assert_eq!(anvil(None), None);
|
||||
assert_eq!(anvil(Some("not json")), None);
|
||||
assert_eq!(anvil(Some(r#"{"error":"x"}"#)), None);
|
||||
// An empty list is a list: the client says there are none.
|
||||
assert_eq!(anvil(Some("[]")), Some(vec![]));
|
||||
assert_eq!(anvil(Some("[1,2]")), Some(vec![1, 2]));
|
||||
// The client's order is kept.
|
||||
assert_eq!(anvil(Some("[9,1,5]")), Some(vec![9, 1, 5]));
|
||||
// A malformed anvil answer only loses the anvil list, not the pickable one.
|
||||
let lists = ChampSelectLists::from_bodies("[7]", "[]", Some("not json")).unwrap();
|
||||
assert_eq!(&*lists.pickable(), &HashSet::from([7]));
|
||||
assert!(lists.anvil().is_empty());
|
||||
assert_eq!(lists.anvil(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_anvil_event_gives_a_list_from_unknown_and_a_delete_keeps_the_value() {
|
||||
let mut lists = ChampSelectLists::from_bodies("[]", "[]", None).unwrap();
|
||||
lists.set_anvil(&Value::Null);
|
||||
assert_eq!(lists.anvil(), None);
|
||||
lists.set_anvil(&json!([4, 2]));
|
||||
assert_eq!(lists.anvil(), Some(&[4, 2][..]));
|
||||
lists.set_anvil(&Value::Null);
|
||||
assert_eq!(lists.anvil(), Some(&[4, 2][..]));
|
||||
// An empty list from an event is a known empty list.
|
||||
lists.set_anvil(&json!([]));
|
||||
assert_eq!(lists.anvil(), Some(&[][..]));
|
||||
}
|
||||
|
||||
fn session(
|
||||
local_intent: u32,
|
||||
ally_id: u32,
|
||||
ally_intent: u32,
|
||||
actions: Value,
|
||||
bans: Value,
|
||||
) -> Value {
|
||||
json!({
|
||||
"localPlayerCellId": 16,
|
||||
"myTeam": [
|
||||
{"cellId": 16, "team": 1, "championId": 0, "championPickIntent": local_intent},
|
||||
{"cellId": 17, "team": 1, "championId": ally_id, "championPickIntent": ally_intent},
|
||||
{"cellId": 18, "team": 2, "championId": 77, "championPickIntent": 0},
|
||||
],
|
||||
"actions": actions,
|
||||
"bans": bans,
|
||||
})
|
||||
}
|
||||
|
||||
fn champ_select(current_session: Value) -> ChampSelect {
|
||||
ChampSelect::from_session(
|
||||
¤t_session,
|
||||
Arc::new(HashSet::from([1, 2, 3, 4, 5, 6])),
|
||||
Some(vec![1, 2]),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn reasons(banned: bool, taken_by_ally: bool, not_offered: bool) -> Unavailability {
|
||||
Unavailability {
|
||||
banned,
|
||||
taken_by_ally,
|
||||
not_offered,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_reason_that_applies_is_given() {
|
||||
let pick = champ_select(session(
|
||||
0,
|
||||
2,
|
||||
0,
|
||||
json!([[
|
||||
{"type": "ban", "championId": 1, "completed": true},
|
||||
{"type": "ban", "championId": 3, "completed": false},
|
||||
{"type": "pick", "championId": 4, "completed": true},
|
||||
]]),
|
||||
json!({"myTeamBans": [5], "theirTeamBans": [2]}),
|
||||
));
|
||||
// A completed ban, either team's ban list.
|
||||
assert_eq!(pick.unavailability(1), reasons(true, false, false));
|
||||
assert_eq!(pick.unavailability(5), reasons(true, false, false));
|
||||
// Banned and taken by an ally add up; a ban in progress or a pick action count for
|
||||
// nothing; a player of the other team is not an ally.
|
||||
assert_eq!(pick.unavailability(2), reasons(true, true, false));
|
||||
assert_eq!(pick.unavailability(3), Unavailability::default());
|
||||
assert_eq!(pick.unavailability(4), Unavailability::default());
|
||||
assert_eq!(pick.unavailability(77), reasons(false, false, true));
|
||||
// Not in the client's pickable list.
|
||||
assert_eq!(pick.unavailability(99), reasons(false, false, true));
|
||||
assert_eq!(pick.unavailability(6), Unavailability::default());
|
||||
// `can_pick` agrees with the reasons.
|
||||
for id in [1, 2, 3, 4, 5, 6, 77, 99] {
|
||||
assert_eq!(
|
||||
pick.can_pick(id),
|
||||
pick.unavailability(id) == Unavailability::default(),
|
||||
"champion {id}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_ally_pre_pick_takes_nothing_but_the_confirmed_pick_does() {
|
||||
let hover = champ_select(session(0, 0, 3, json!([]), json!({})));
|
||||
assert_eq!(hover.unavailability(3), Unavailability::default());
|
||||
let locked = champ_select(session(0, 3, 3, json!([]), json!({})));
|
||||
assert_eq!(locked.unavailability(3), reasons(false, true, false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_disabled_champion_is_not_offered() {
|
||||
let lists = ChampSelectLists::from_bodies("[1,2,3]", "[3]", None).unwrap();
|
||||
let pick = ChampSelect::from_session(
|
||||
&session(0, 0, 0, json!([]), json!({})),
|
||||
lists.pickable(),
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(pick.unavailability(3), reasons(false, false, true));
|
||||
assert_eq!(pick.unavailability(1), Unavailability::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_players_own_pick_is_never_greyed() {
|
||||
// Hovered then locked, though the data also bans it, takes it and does not offer it.
|
||||
let banned = json!([[{"type": "ban", "championId": 99, "completed": true}]]);
|
||||
let intent = champ_select(session(99, 99, 0, banned.clone(), json!({})));
|
||||
assert_eq!(intent.current, Some(99));
|
||||
assert_eq!(intent.unavailability(99), Unavailability::default());
|
||||
let mut locked = session(0, 99, 0, banned, json!({"myTeamBans": [99]}));
|
||||
locked["myTeam"][0]["championId"] = json!(99);
|
||||
let locked = champ_select(locked);
|
||||
assert_eq!(locked.unavailability(99), Unavailability::default());
|
||||
assert!(locked.can_pick(99));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn two_champ_selects_differ_by_their_bans_and_ally_picks() {
|
||||
let plain = champ_select(session(0, 0, 0, json!([]), json!({})));
|
||||
let banned = champ_select(session(0, 0, 0, json!([]), json!({"myTeamBans": [3]})));
|
||||
let taken = champ_select(session(0, 3, 0, json!([]), json!({})));
|
||||
assert!(!plain.same_pickable(&banned));
|
||||
assert!(!plain.same_pickable(&taken));
|
||||
assert!(!banned.same_pickable(&taken));
|
||||
assert_ne!(plain, banned);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -179,7 +179,7 @@ impl GameClientSource for LeagueClient {
|
||||
|| {
|
||||
let pickable = self.get_ok(PICKABLE)?;
|
||||
let disabled = self.get_ok(DISABLED)?;
|
||||
// The anvil list is a bonus: a failed read leaves it empty until an event
|
||||
// The anvil list is a bonus: a failed read leaves it unknown until an event
|
||||
// delivers it.
|
||||
let anvil = self.get_ok(ANVIL_CHAMPIONS).ok();
|
||||
Ok((pickable, disabled, anvil))
|
||||
@@ -710,7 +710,7 @@ mod tests {
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(pick.current, Some(16));
|
||||
assert_eq!(pick.anvil_champions, [9, 8]);
|
||||
assert_eq!(pick.anvil_champions, Some(vec![9, 8]));
|
||||
assert!(!pick.can_pick(1));
|
||||
assert!(pick.can_pick(2));
|
||||
}
|
||||
@@ -755,7 +755,7 @@ mod tests {
|
||||
.unwrap()
|
||||
};
|
||||
let pick = read();
|
||||
assert_eq!(pick.anvil_champions, [9]);
|
||||
assert_eq!(pick.anvil_champions, Some(vec![9]));
|
||||
assert!(pick.can_pick(1));
|
||||
assert!(!pick.can_pick(2));
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ mod statistics;
|
||||
mod tracker;
|
||||
mod update_package;
|
||||
|
||||
pub use champ_select::{ChampSelect, ChampSelectReader};
|
||||
pub use champ_select::{ChampSelect, ChampSelectReader, Unavailability};
|
||||
pub use champion_search::ChampionSearch;
|
||||
pub use champions::{Champion, ChampionCatalog, ChampionSource, DataDragon};
|
||||
pub use client::{ClientError, ClientEvent, ClientSnapshot, GameClientSource, GamePhase, Gameflow};
|
||||
|
||||
Reference in new issue
Block a user