prefix the actor id as scope for variables like "$foo" (-> "icarus$foo")

This commit is contained in:
yuni 2024-04-14 18:09:14 +02:00
parent b4ff95c3be
commit a13264a404
2 changed files with 24 additions and 7 deletions

View file

@ -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.

View file

@ -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) {
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 "<scope>$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);
}