Better naming and some documentation of command process.

This commit is contained in:
projectmoon 2024-01-17 09:31:38 +01:00
parent bdf5b4da08
commit 773f3a1a47
2 changed files with 9 additions and 6 deletions

View File

@ -13,7 +13,6 @@ use std::convert::TryFrom;
use strum::VariantNames; use strum::VariantNames;
type EventConversionResult = std::result::Result<CommandEvent, EventConversionError>; type EventConversionResult = std::result::Result<CommandEvent, EventConversionError>;
type RefEventConversionResult<'a> = std::result::Result<&'a CommandEvent, EventConversionError>;
impl CommandEvent { impl CommandEvent {
pub fn new(raw_event: RawCommandEvent) -> EventConversionResult { pub fn new(raw_event: RawCommandEvent) -> EventConversionResult {
@ -84,9 +83,9 @@ pub async fn convert_raw_execution(
Err(err) => Either::Right(err), Err(err) => Either::Right(err),
}); });
// Coherence check of converted events. // Coherence validation of converted events.
let (successes, incoherent_events): (Vec<_>, Vec<_>) = stream::iter(converted.into_iter()) let (successes, incoherent_events): (Vec<_>, Vec<_>) = stream::iter(converted.into_iter())
.then(|event| check_event_coherence(db, event)) .then(|event| validate_event_coherence(db, event))
.collect::<Vec<_>>() .collect::<Vec<_>>()
.await .await
.into_iter() .into_iter()
@ -123,7 +122,7 @@ fn deserialize_recognized_event(
"change_scene" => Ok(CommandEvent::ChangeScene { "change_scene" => Ok(CommandEvent::ChangeScene {
scene_key: raw_event scene_key: raw_event
.parameter .parameter
.strip_prefix("scenes/") .strip_prefix("scenes/") // Mini coherence check
.map(String::from) .map(String::from)
.unwrap_or(raw_event.parameter) .unwrap_or(raw_event.parameter)
.clone(), .clone(),
@ -166,7 +165,7 @@ fn deserialize_take_damage(
} }
} }
async fn check_event_coherence<'a>( async fn validate_event_coherence<'a>(
db: &Database, db: &Database,
event: CommandEvent, event: CommandEvent,
) -> std::result::Result<CommandEvent, EventCoherenceFailure> { ) -> std::result::Result<CommandEvent, EventCoherenceFailure> {
@ -182,6 +181,8 @@ async fn check_event_coherence<'a>(
} }
} }
/// The event was converted from the raw response properly, but the
/// information contained in the response is not valid.
fn invalid_converted_event(event: CommandEvent) -> Option<EventCoherenceFailure> { fn invalid_converted_event(event: CommandEvent) -> Option<EventCoherenceFailure> {
match event { match event {
CommandEvent::ChangeScene { .. } => Some(EventCoherenceFailure::TargetDoesNotExist(event)), CommandEvent::ChangeScene { .. } => Some(EventCoherenceFailure::TargetDoesNotExist(event)),
@ -189,6 +190,9 @@ fn invalid_converted_event(event: CommandEvent) -> Option<EventCoherenceFailure>
} }
} }
/// The event was converted from the raw response properly, but
/// something went wrong with attempting to check the coherence of the
/// converted event.
fn invalid_converted_event_because_err( fn invalid_converted_event_because_err(
event: CommandEvent, event: CommandEvent,
err: anyhow::Error, err: anyhow::Error,

View File

@ -44,7 +44,6 @@ impl GameLoop {
async fn execute_command(&mut self, cmd: &str) -> Result<CommandExecution> { async fn execute_command(&mut self, cmd: &str) -> Result<CommandExecution> {
let stage = &self.state.current_scene; let stage = &self.state.current_scene;
let cached_command = self.db.load_cached_command(cmd, &stage.scene).await?; let cached_command = self.db.load_cached_command(cmd, &stage.scene).await?;
let execution = if let Some(cached) = cached_command { let execution = if let Some(cached) = cached_command {