From 00df7bc7119c14b08b7e92ebe8d1f1434c4c2032 Mon Sep 17 00:00:00 2001 From: hut Date: Sun, 14 Apr 2024 19:23:43 +0200 Subject: [PATCH] implement "if" statements with 2 operands (comparing for equality) --- src/chat.rs | 2 +- src/chats/startrans.yaml | 12 ++++++------ src/var.rs | 25 ++++++++++++++++++++++++- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index c5afdfc..c0e5b19 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -679,7 +679,7 @@ pub fn handle_chat_events( } } ChatEvent::GotoIf(condition, goto) => { - if vars.evaluate(condition, &chat.talker.actor_id) { + if vars.evaluate_condition(condition, &chat.talker.actor_id) { chat.position = goto.clone(); } } diff --git a/src/chats/startrans.yaml b/src/chats/startrans.yaml index 46feec6..a01a89f 100644 --- a/src/chats/startrans.yaml +++ b/src/chats/startrans.yaml @@ -7,7 +7,7 @@ - chat: ClippyTransSerenity -- set: busstop serenity +- set: $busstop serenity - Welcome at StarTrans Cargo Services! You have reached Serenity Station. - "Ready for a trip? Available bus stops: Oscillation Station, Metis Prime Station" - label: startransbusstop @@ -21,7 +21,7 @@ - chat: ClippyTransMetis -- set: busstop metis +- set: $busstop metis - Welcome at StarTrans Cargo Services! You have reached Metis Prime Station. - "Ready for a trip? Available bus stops: Oscillation Station, Serenity Station" - label: startransbusstop @@ -36,7 +36,7 @@ - chat: ClippyTransOscillation -- set: busstop oscillation +- set: $busstop oscillation - Welcome at StarTrans Cargo Services! You have reached Oscillation Station. - "Ready for a trip? Available bus stops: Serenity Station, Metis Prime Station" - label: startransbusstop @@ -57,21 +57,21 @@ - Can I take a spacecraft with me?: - Absolutely. - goto: startransbusstop -- if: busstop != "oscillation" +- if: $busstop != oscillation Take me to Oscillation Station, please.: - StarTrans wishes you a pleasant journey. - script: cryofadeout - sleep: 5 - script: cryotrip oscillation - goto: EXIT -- if: busstop != "metis" +- if: $busstop != metis Take me to Metis Prime Station, please.: - StarTrans wishes you a pleasant journey. - script: cryofadeout - sleep: 5 - script: cryotrip metisprime - goto: EXIT -- if: busstop != "serenity" +- if: $busstop != serenity Take me to Serenity Station, please.: - StarTrans wishes you a pleasant journey. - script: cryofadeout diff --git a/src/var.rs b/src/var.rs index 93f727a..d30dfb3 100644 --- a/src/var.rs +++ b/src/var.rs @@ -237,6 +237,7 @@ impl GameVars { // This method ensures that the variable name contains a scope separator, // and if a scope is missing, it prefixes the fallback scope. + // Should NOT be used on non-variable values, like plain strings. // // Some examples, assuming fallback_scope="Clippy", SCOPE_SEPARATOR="$": // @@ -278,9 +279,11 @@ impl GameVars { self.db.insert(key, value); } - pub fn evaluate(&self, condition: &str, scope: &str) -> bool { + pub fn evaluate_condition(&self, condition: &str, scope: &str) -> bool { let parts: Vec<&str> = condition.split(" ").collect(); if parts.len() == 1 { + // Got something like "if $somevar:". + // Check whether the variable evaluates to true. let part = parts[0]; if part.contains(SCOPE_SEPARATOR) { let part = Self::normalize_varname(scope, part); @@ -290,6 +293,26 @@ impl GameVars { else { return Self::evaluate_str_as_bool(part); } + + } else if parts.len() == 2 { + // Got something like "if $something somethingelse" + // Check whether the two are identical. + let mut left: String = parts[0].to_string(); + if left.contains(SCOPE_SEPARATOR) { + left = self + .get(Self::normalize_varname(scope, left.as_str()).as_str()) + .unwrap_or("".to_string()); + } + let mut right: String = parts[1].to_string(); + if right.contains(SCOPE_SEPARATOR) { + right = self + .get(Self::normalize_varname(scope, right.as_str()).as_str()) + .unwrap_or("".to_string()); + } + dbg!(&left); + dbg!(&right); + dbg!(left == right); + return left == right; } else { return false; }