Skip to content

Commit

Permalink
Add pre filtering to the dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
TrueDoctor committed Mar 12, 2022
1 parent 80cddaa commit d2f42d0
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,23 @@ impl NS2Stats {

pub fn player_ids_sorted(&self) -> Vec<SteamId> {
let mut ids = self.player_ids().into_iter().collect::<Vec<_>>();
ids.sort();
ids.sort_unstable();
ids
}

pub fn player_name<'a>(&'a self, id: &'a SteamId) -> &'a str {
self.player_stats(id)
.next()
.expect("Player with given steam id was not found")
.unwrap_or_else(|| panic!("Player with given steam id {id} was not found"))
.player_name
.as_str()
}

pub fn kill_feed(&self) -> impl Iterator<Item = &KillFeed> {
self.games.iter().flat_map(|game| game.kill_feed.iter())
self.games
.iter()
.filter(|x| x.round_info.round_length > 800. && x.player_stats.len() > 4)
.flat_map(|game| game.kill_feed.iter())
}

pub fn kd(&self, player: u32) -> (u32, u32) {
Expand All @@ -67,29 +70,47 @@ impl NS2Stats {
_ => (),
}
}
let mut to_remove: Vec<u32> = self
.player_ids()
.iter()
.map(|id| (id, kds.iter().filter_map(|((_, id2), deaths)| (id == id2).then(|| deaths)).sum::<u32>()))
.filter_map(|(&id, deaths)| (deaths < 100).then(|| id))
.collect();
to_remove.push(0);
for id in dbg!(to_remove) {
kds.retain(|&(id1, id2), _| id != id1 && id != id2);
}

let mut ids: Vec<_> = kds.keys().map(|(id, _)| *id).collect();
ids.sort_unstable();
ids.dedup();
dbg!(&ids);

//dbg!(&kds);
let mut scores = HashMap::new();
let mut ids = self.player_ids_sorted();
for (i, player1) in ids.iter().enumerate() {
for player2 in &ids[i..] {
let p1_k = *kds.get(&(*player1, *player2)).unwrap_or(&0);
let p2_k = *kds.get(&(*player2, *player1)).unwrap_or(&0);
let kd = p1_k as f32 / p2_k as f32;
if player1 == player2 {
scores.insert((*player1, *player2), 0.);
} else if kd.is_finite() && p1_k + p2_k > 60 && (1. / kd).is_finite() {
} else if kd.is_finite() && p1_k + p2_k > 20 && (1. / kd).is_finite() {
scores.insert((*player1, *player2), 1. / kd);
scores.insert((*player2, *player1), kd);
} else {
scores.insert((*player1, *player2), 0.);
scores.insert((*player2, *player1), 0.);
}
}
}
dbg!(scores.len());
ids.retain(|id| scores.keys().any(|(kid, _)| kid == id));
let mut encounter_count: Vec<_> = ids.iter().map(|&id| (id, scores.keys().filter(|(kid, _)| *kid == id).count())).collect();
encounter_count.sort_by_key(|&(_, count)| count);
let dimension = dbg!(&encounter_count).last().unwrap_or(&(0, 0)).1;
let ids: Vec<_> = encounter_count
.into_iter()
.filter_map(|(id, count)| (count > dimension - 4).then(|| id))
.collect();
//dbg!(&scores);
//dbg!(&ids);
dbg!(&ids);
let mut results = Vec::new();
for player1 in &ids {
for player2 in &ids {
Expand Down Expand Up @@ -134,6 +155,7 @@ impl NS2Stats {
.flat_map(|res| res.map(|e| e.path()))
.filter_map(|path| (path.is_file() && path.extension().expect("Found file without extension") == "json").then(|| std::fs::read_to_string(&path)))
.flatten()
.skip(30)
.map(|path| types::GameStats::from_json(&path).map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("{e}"))))
.collect::<std::io::Result<Vec<_>>>()
}
Expand Down

0 comments on commit d2f42d0

Please sign in to comment.