From 9cd1cf19e2f3abadf5935fd2585c4c8d0fac383d Mon Sep 17 00:00:00 2001 From: hut Date: Sat, 13 Apr 2024 12:24:56 +0200 Subject: [PATCH] WIP choice spawning --- src/chat.rs | 57 +++++++++++++++++++++++++++++++++++++++- src/chats/serenity.yaml | 3 +-- src/chats/startrans.yaml | 6 ++--- 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index ccaf18c..64fd2ea 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -9,11 +9,30 @@ pub const CHATS: &[&str] = &[ ]; pub const TOKEN_CHAT: &str = "chat"; +pub const TOKEN_SYSTEM: &str = "system"; +pub const TOKEN_WARN: &str = "warn"; +pub const TOKEN_SET: &str = "set"; +pub const TOKEN_IF: &str = "if"; +pub const TOKEN_GOTO: &str = "goto"; +pub const TOKEN_LABEL: &str = "label"; +pub const TOKEN_SCRIPT: &str = "script"; +//pub const TOKEN_TIMEOUT: &str = "TIMEOUT"; pub const LETTERS_PER_SECOND: f32 = 17.0; pub const TALKER_SPEED_FACTOR: f32 = settings::DEFAULT_CHAT_SPEED / LETTERS_PER_SECOND; pub const CHAT_SPEED_MIN_LEN: f32 = 40.0; pub const NAME_FALLBACK: &str = "Unknown"; +pub const NON_CHOICE_TOKENS: &[&str] = &[ + TOKEN_CHAT, + TOKEN_SYSTEM, + TOKEN_WARN, + TOKEN_SET, + TOKEN_IF, + TOKEN_GOTO, + TOKEN_LABEL, + TOKEN_SCRIPT, +]; + pub struct ChatPlugin; impl Plugin for ChatPlugin { fn build(&self, app: &mut App) { @@ -70,6 +89,21 @@ impl ChatDB { return Err(format!("No chat with the conversation ID `{id}` was found.")); } + fn search_choice(&self, yaml: &Yaml) -> Option { + let non_choice_tokens = NON_CHOICE_TOKENS.to_vec(); + if let Some(hash) = yaml.as_hash() { + for key in hash.keys() { + if let Yaml::String(key) = key { + if non_choice_tokens.contains(&key.as_str()) { + continue; + } + return Some(key.into()); + } + } + } + return None; + } + pub fn advance_chat(&self, chat: &mut Chat, event: &mut EventWriter) { event.send(ChatEvent::DespawnAllChoices); let conv = &self.0[chat.id].as_vec(); @@ -77,15 +111,36 @@ impl ChatDB { return; } let conv = conv.unwrap(); + + // Handle next entry in the chat list + let mut is_skipping_through = false; chat.position += 1; if chat.position >= conv.len() { event.send(ChatEvent::DisplayMessage("Disconnected.".to_string())); event.send(ChatEvent::DespawnAllChats); return; } - if let Some(message) = conv[chat.position].as_str() { + else if let Some(_) = self.search_choice(&conv[chat.position]) { + is_skipping_through = true; + } + else if let Some(message) = conv[chat.position].as_str() { event.send(ChatEvent::DisplayMessage(message.to_string())); } + + // Check if the immediately following entries are choices + let mut pos = chat.position + 1; + loop { + if is_skipping_through || pos >= conv.len() { + break; + } + if let Some(choice) = self.search_choice(&conv[pos]) { + event.send(ChatEvent::DisplayMessage(choice)); + } + else { + break; + } + pos += 1; + } } } diff --git a/src/chats/serenity.yaml b/src/chats/serenity.yaml index 73a13dd..5cadda7 100644 --- a/src/chats/serenity.yaml +++ b/src/chats/serenity.yaml @@ -41,11 +41,10 @@ - Whatever... I didn't ask for this.: - goto: didntask - TIMEOUT: - - How are you feeling? - TIMEOUT: - - How are you feeling? +- How are you feeling? - label: howru - I feel quite cozy, this space suit feels like a second skin.: - Hah, it does, doesn't it? diff --git a/src/chats/startrans.yaml b/src/chats/startrans.yaml index 3f68216..75d5199 100644 --- a/src/chats/startrans.yaml +++ b/src/chats/startrans.yaml @@ -9,7 +9,7 @@ - chat: ClippyTransSerenity - Welcome at StarTrans Cargo Services! You have reached Serenity Station. - "Ready for a trip? Available bus stops: Oscillation Station, Metis Prime Station" -- set busstop: serenity +- set: busstop serenity - label: startransbusstop - goto: EXIT - label: interesting @@ -23,7 +23,7 @@ - chat: ClippyTransMetis - Welcome at StarTrans Cargo Services! You have reached Metis Prime Station. - "Ready for a trip? Available bus stops: Oscillation Station, Serenity Station" -- set busstop: metis +- set: busstop metis - label: startransbusstop - goto: EXIT - label: interesting @@ -38,7 +38,7 @@ - chat: ClippyTransOscillation - Welcome at StarTrans Cargo Services! You have reached Oscillation Station. - "Ready for a trip? Available bus stops: Serenity Station, Metis Prime Station" -- set busstop: oscillation +- set: busstop oscillation - label: startransbusstop - goto: EXIT - label: interesting