From 08ec42c043947bd2a778aa4d38269444ebab695a Mon Sep 17 00:00:00 2001 From: hut Date: Sat, 13 Apr 2024 21:45:05 +0200 Subject: [PATCH] handle msg/system/warn tokens --- src/chat.rs | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index 2c7e16e..fe1701c 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -9,6 +9,7 @@ pub const CHATS: &[&str] = &[ ]; pub const TOKEN_CHAT: &str = "chat"; +pub const TOKEN_MSG: &str = "msg"; pub const TOKEN_SYSTEM: &str = "system"; pub const TOKEN_WARN: &str = "warn"; pub const TOKEN_SET: &str = "set"; @@ -26,6 +27,7 @@ pub const CHAT_SPEED_MIN_LEN: f32 = 40.0; pub const NON_CHOICE_TOKENS: &[&str] = &[ TOKEN_CHAT, + TOKEN_MSG, TOKEN_SYSTEM, TOKEN_WARN, TOKEN_SET, @@ -149,7 +151,6 @@ impl ChatDB { dbg!(self.at(chat.id, &chat.position)); match self.at(chat.id, &chat.position) { None => { - dbg!("Pop."); chat.position.pop(); popped = true; if chat.position.len() > 0 { @@ -219,16 +220,37 @@ impl ChatDB { let mut add_choices = true; match current_item { Some(Value::String(message)) => { - event.send(ChatEvent::SpawnMessage(message.to_string())); + event.send(ChatEvent::SpawnMessage(message.to_string(), hud::LogLevel::Chat)); } - Some(Value::Mapping(message)) => { - if let Some(_) = self.search_choice(Some(&Value::Mapping(message))) { + Some(Value::Mapping(map)) => { + if let Some(_) = self.search_choice(Some(&Value::Mapping(map.clone()))) { add_choices = false; } + for (key, value) in map { + let key = key.as_str(); + match (key, value) { + (Some(TOKEN_CHAT), _) => {} + (Some(TOKEN_MSG), Value::String(message)) => { + event.send(ChatEvent::SpawnMessage( + message.to_string(), hud::LogLevel::Chat)); + } + (Some(TOKEN_SYSTEM), Value::String(message)) => { + event.send(ChatEvent::SpawnMessage( + message.to_string(), hud::LogLevel::Info)); + } + (Some(TOKEN_WARN), Value::String(message)) => { + event.send(ChatEvent::SpawnMessage( + message.to_string(), hud::LogLevel::Warning)); + } + _ => { + } + } + } } None => { if chat.position.len() == 0 { - event.send(ChatEvent::SpawnMessage("Disconnected.".to_string())); + event.send(ChatEvent::SpawnMessage( + "Disconnected.".to_string(), hud::LogLevel::Info)); event.send(ChatEvent::DespawnAllChats); } } @@ -284,7 +306,7 @@ pub struct StartConversationEvent { pub enum ChatEvent { DespawnAllChoices, DespawnAllChats, - SpawnMessage(String), + SpawnMessage(String, hud::LogLevel), SpawnChoice(String, usize, ChatPos), //Script(String, String, String), } @@ -373,8 +395,18 @@ pub fn handle_chat_events( ChatEvent::DespawnAllChats => { commands.entity(chat_entity).despawn(); } - ChatEvent::SpawnMessage(message) => { - log.chat(message.into(), chat.talker.name.clone().unwrap_or(NAME_FALLBACK.to_string())); + ChatEvent::SpawnMessage(message, level) => { + match level { + hud::LogLevel::Chat => { + log.chat(message.into(), chat.talker.name.clone().unwrap_or(NAME_FALLBACK.to_string())); + } + hud::LogLevel::Info | hud::LogLevel::Notice => { + log.info(message.into()); + } + hud::LogLevel::Warning => { + log.warning(message.into()); + } + } chat.timer = now + ((message.len() as f32).max(CHAT_SPEED_MIN_LEN) * TALKER_SPEED_FACTOR * chat.talker.talking_speed / settings.chat_speed) as f64; } ChatEvent::SpawnChoice(replytext, key, goto) => {