ed_journals/modules/state/models/resolvers/
game_state_resolver.rspub mod game_commander_entry;
use std::collections::HashMap;
use serde::Serialize;
use crate::logs::{LogEvent, LogEventContent};
use crate::state::models::feed_result::FeedResult;
use crate::state::resolvers::game_state_resolver::game_commander_entry::GameCommanderEntry;
use crate::state::traits::state_resolver::StateResolver;
use crate::state::LogState;
#[derive(Serialize, Default)]
pub struct GameStateResolver {
pub commanders: HashMap<String, GameCommanderEntry>,
current_commander_id: Option<String>,
}
impl StateResolver<LogEvent> for GameStateResolver {
fn feed(&mut self, input: &LogEvent) -> FeedResult {
match &input.content {
LogEventContent::Commander(commander) => {
self.current_commander_id = Some(commander.fid.to_string());
if !self.commanders.contains_key(&commander.fid) {
self.commanders.insert(
commander.fid.to_string(),
GameCommanderEntry {
name: commander.name.to_string(),
log_state: LogState::default(),
},
);
}
}
_ => {
let Some(current) = self.current_commander_mut() else {
return FeedResult::Later;
};
current.log_state.feed(input);
}
}
FeedResult::Accepted
}
fn flush_inner(&mut self) {
for commander in self.commanders.values_mut() {
commander.log_state.flush();
}
}
}
impl GameStateResolver {
pub fn current_commander(&self) -> Option<&GameCommanderEntry> {
self.current_commander_id
.as_ref()
.and_then(|commander_id| self.commanders.get(commander_id))
}
pub fn current_commander_mut(&mut self) -> Option<&mut GameCommanderEntry> {
self.current_commander_id
.as_ref()
.and_then(|commander_id| self.commanders.get_mut(commander_id))
}
}
#[cfg(test)]
mod tests {
use crate::logs::blocking::LogDirReader;
use crate::state::GameState;
use std::collections::HashSet;
use std::env::current_dir;
use std::time::Instant;
#[test]
fn state_is_correct() {
let dir_path = current_dir().unwrap().join("test-files").join("journals");
let log_dir = LogDirReader::open(dir_path);
let mut state = GameState::default();
let instant = Instant::now();
for entry in log_dir {
state.feed(&entry.unwrap());
}
state.flush();
dbg!(instant.elapsed().as_nanos());
for commander in state.commanders.values() {
for system in commander.log_state.systems.values() {
for body in system.planet_state.values() {
let mut genuses = HashSet::new();
for species in &body.scanned_species {
let inserted = genuses.insert(species.genus());
if !inserted {
panic!("Not here!");
}
}
}
}
}
}
}