From 45d3408f0cd08295256645489cd3dd551153eedc Mon Sep 17 00:00:00 2001 From: hut Date: Sat, 13 Apr 2024 00:11:32 +0200 Subject: [PATCH] cleanup --- src/chat.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index 9194681..c34fa0b 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -34,9 +34,18 @@ pub struct Chat { #[derive(Component)] pub struct Choice; +// This is the only place where any YAML interaction should be happening. #[derive(Resource)] pub struct ChatDB(Vec); impl ChatDB { + pub fn load_from_str(&mut self, yaml_string: &str) -> Result<(), ()> { + if let Ok(mut yaml_data) = YamlLoader::load_from_str(yaml_string) { + self.0.append(&mut yaml_data); + return Ok(()); + } + return Err(()); + } + pub fn get_chat_by_id(&self, id: &String) -> Result { let mut found: Option = None; for (index, object_yaml) in self.0.iter().enumerate() { @@ -54,6 +63,7 @@ impl ChatDB { } return Err(format!("No chat with the conversation ID `{id}` was found.")); } + pub fn advance_chat(&self, chat: &mut Chat, event: &mut EventWriter) { event.send(ChatEvent::DespawnAllChoices); chat.position += 1; @@ -83,9 +93,7 @@ pub enum ChatEvent { pub fn load_chats(mut chatdb: ResMut) { for chat_yaml in CHATS { - if let Ok(mut yaml_data) = YamlLoader::load_from_str(chat_yaml) { - chatdb.0.append(&mut yaml_data); - } else { + if chatdb.load_from_str(chat_yaml).is_err() { error!("Could not load chat definitions. Validate files in `src/chats/` path."); } }