diff --git a/src/chats/serenity.yaml b/src/chats/serenity.yaml index 4116438..f2fed59 100644 --- a/src/chats/serenity.yaml +++ b/src/chats/serenity.yaml @@ -17,13 +17,13 @@ - chat: Icarus -- if: +- if: $talked_before then: - Oh hey, you're back! - Nice to see you again! - goto: back - Oh hey, you're awake! -- set: talked_before +- set: $talked_before - label: back - I found you drifting out cold, and thought, I better watch over you. - Took us here behind that moonlet, to shield you from the micros. diff --git a/src/var.rs b/src/var.rs index 7c22713..2913b5d 100644 --- a/src/var.rs +++ b/src/var.rs @@ -237,13 +237,30 @@ impl GameVars { self.db.insert(key.to_lowercase(), value); } - #[allow(dead_code)] - pub fn set_in_scope(&mut self, scope: &str, key: &str, value: String) { - let key: String = if key.contains(SCOPE_SEPARATOR) { - key.to_string() + pub fn set_in_scope(&mut self, fallback_scope: &str, key: &str, value: String) { + let parts: Vec<&str> = key.split(SCOPE_SEPARATOR).collect(); + let key: String = if parts.len() == 1 { + // we got a key like "foo", turn it into "$foo" + fallback_scope.to_string() + SCOPE_SEPARATOR + key + } else if parts.len() > 1 { + // we got a key with at least one "$" + // extract anything before the last "$": + let scope_part: String = parts[0..parts.len() - 2].join(SCOPE_SEPARATOR); + + if scope_part.is_empty() { + // we got a key like "$foo", just prefix the fallback scope + fallback_scope.to_string() + key + } + else { + // we got a key like "Ke$ha$foo" or "$$foo" (which is the convention for + // global variables), leave the scope intact + key.to_string() + } } else { - scope.to_string() + SCOPE_SEPARATOR + key + // we got an empty string. this is bad, but handle gracefully by prefixing scope + fallback_scope.to_string() + SCOPE_SEPARATOR }; + let key = key.to_lowercase(); self.db.insert(key, value); }