combine classstat data

This commit is contained in:
Robin Appelman 2023-03-04 21:45:17 +01:00
commit 9d1fbf1320
2 changed files with 23 additions and 8 deletions

View file

@ -83,6 +83,12 @@ impl Class {
} }
} }
impl Default for Class {
fn default() -> Self {
Class::Scout
}
}
impl FromStr for Class { impl FromStr for Class {
type Err = (); type Err = ();

View file

@ -17,8 +17,13 @@ pub struct ClassStats {
#[derive(Default)] #[derive(Default)]
pub struct ClassStatsHandler { pub struct ClassStatsHandler {
active: bool, active: bool,
classes: BTreeMap<SubjectId, Class>, data: BTreeMap<SubjectId, ClassStatData>,
deaths: BTreeMap<SubjectId, ClassMap<u8>>, }
#[derive(Default)]
pub struct ClassStatData {
class: Class,
deaths: ClassMap<u8>,
} }
impl ClassStatsHandler { impl ClassStatsHandler {
@ -26,8 +31,12 @@ impl ClassStatsHandler {
subject subject
.id() .id()
.ok() .ok()
.and_then(|id| self.classes.get(&id)) .and_then(|id| self.data.get(&id))
.copied() .map(|data| data.class)
}
fn data_mut(&mut self, id: SubjectId) -> &mut ClassStatData {
self.data.entry(id).or_default()
} }
} }
@ -59,7 +68,7 @@ impl EventHandler for ClassStatsHandler {
match event { match event {
GameEvent::Spawned(SpawnEvent { class: Some(class) }) GameEvent::Spawned(SpawnEvent { class: Some(class) })
| GameEvent::RoleChange(RoleChangeEvent { class: Some(class) }) => { | GameEvent::RoleChange(RoleChangeEvent { class: Some(class) }) => {
self.classes.insert(subject, *class); self.data_mut(subject).class = *class;
} }
GameEvent::RoundStart => { GameEvent::RoundStart => {
self.active = true; self.active = true;
@ -72,8 +81,8 @@ impl EventHandler for ClassStatsHandler {
subject_data.kills[target_class] += 1; subject_data.kills[target_class] += 1;
} }
if let Ok(target) = kill.target.id() { if let Ok(target) = kill.target.id() {
if let Some(subject_class) = self.classes.get(&subject) { if let Some(subject_class) = self.data.get(&subject).map(|data| data.class) {
self.deaths.entry(target).or_default()[*subject_class] += 1; self.data_mut(target).deaths[subject_class] += 1;
} }
} }
} }
@ -102,7 +111,7 @@ impl EventHandler for ClassStatsHandler {
subject: &SubjectData, subject: &SubjectData,
mut data: Self::PerSubjectData, mut data: Self::PerSubjectData,
) -> Self::PerSubjectOutput { ) -> Self::PerSubjectOutput {
data.deaths = self.deaths.remove(&subject.id()).unwrap_or_default(); data.deaths = self.data.remove(&subject.id()).unwrap_or_default().deaths;
data data
} }
} }