From a572959df3e6328a312ffba1bffd041f1de9c5bd Mon Sep 17 00:00:00 2001 From: hut Date: Fri, 12 Apr 2024 23:03:46 +0200 Subject: [PATCH] implement looking up chat id --- src/chat.rs | 74 +++++++++++++++++++++++++++++++++++----- src/chats/serenity.yaml | 12 ++----- src/chats/startrans.yaml | 8 +++++ src/commands.rs | 3 +- src/defs.txt | 16 ++++----- 5 files changed, 85 insertions(+), 28 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index 938510c..b0f2853 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -1,45 +1,101 @@ extern crate yaml_rust; use bevy::prelude::*; +use yaml_rust::{Yaml, YamlLoader}; +use crate::{audio}; pub const CHATS: &[&str] = &[ include_str!("chats/serenity.yaml"), include_str!("chats/startrans.yaml"), ]; +pub const TOKEN_CHAT: &str = "chat"; + pub struct ChatPlugin; impl Plugin for ChatPlugin { fn build(&self, app: &mut App) { app.add_systems(Startup, load_chats); + app.add_systems(Update, ( + handle_new_conversations, + )); app.add_event::(); app.insert_resource(ChatDB(Vec::new())); } } +#[derive(Resource)] +pub struct ChatDB(Vec); +impl ChatDB { + pub fn get_chat_by_id(&self, id: &String) -> Result { + let mut found: Option = None; + for (index, object_yaml) in self.0.iter().enumerate() { + if let Some(object_vec) = object_yaml.as_vec() { + if object_vec.len() == 0 { + continue; + } + let first_item = &object_vec[0]; + if let Some(hash) = first_item.as_hash() { + if let Some(chat_id_yaml) = hash.get(&Yaml::String(TOKEN_CHAT.to_string())) { + if let Some(chat_id) = chat_id_yaml.as_str() { + if chat_id != id { + continue; + } + if found.is_some() { + return Err("Found multiple chats with the same id!".to_string()); + } + found = Some(index as u32); + } + } + } + } else { + warn!("Non-list YAML object found while processing chat specs"); + } + } + if let Some(result) = found { + return Ok(result); + } + return Err(format!("No chat with the conversation ID `{id}` was found.")); + } +} + #[derive(Component)] #[derive(Clone)] pub struct Talker { - pub pronoun: String, pub conv_id: String, + pub name: Option, + pub pronoun: Option, } -impl Default for Talker { fn default() -> Self { Self { - pronoun: "they/them".to_string(), - conv_id: "undefined".to_string(), -}}} #[derive(Event)] pub struct StartConversationEvent { pub talker: Talker, } -#[derive(Resource)] -pub struct ChatDB(Vec); - pub fn load_chats(mut chatdb: ResMut) { for chat_yaml in CHATS { - if let Ok(mut yaml_data) = yaml_rust::YamlLoader::load_from_str(chat_yaml) { + if let Ok(mut yaml_data) = YamlLoader::load_from_str(chat_yaml) { chatdb.0.append(&mut yaml_data); } else { error!("Could not load chat definitions. Validate files in `src/chats/` path."); } } } + +pub fn handle_new_conversations( + //mut commands: Commands, + mut er_conv: EventReader, + mut ew_sfx: EventWriter, + chatdb: Res, + //time: Res