rivescript-js icon indicating copy to clipboard operation
rivescript-js copied to clipboard

run multi - conditions within one line?

Open Lewikster opened this issue 8 years ago • 2 comments
trafficstars

It would be a great feature to have down the road to run more than one conditions in one line

Currently, have to do several triggers

+ varcheck
 * <get money> == 0 => {@varcheck2}
 - {@mainmenu}


+ varcheck2
* <get mood> == sad =>{@varcheck3}
-  {@mainmenu}

+ varcheck3
* <get time> == 1200 =>{@needhelp}
 - {@mainmenu}

Would be cool to add "and" "or" to the conditional

+ varcheckboth
* <get money> == 0 && <get mood> == sad && <get time> ==1200 =>  {@needhelp}
- {@mainmenu}

+ varcheckor
* <get money> == 0 || <get mood> == sad =>  {@itsok}
- {@mainmenu}

Lewikster avatar Mar 06 '17 11:03 Lewikster

+1 😁

dcsan avatar Mar 08 '17 15:03 dcsan

This is a good idea. For implementation notes (if somebody else wants to take this on before I get around to it), if you split a string using a regular expression with a capture group, the capture group itself gets returned in the array output.

Example:

> a = "<get money> == 0 && <get mood> == sad || <get time> == 1200"
> a.split(/(&&|\|\|)/)
[ '<get money> == 0 ',
  '&&',
  ' <get mood> == sad ',
  '||',
  ' <get time> == 1200' ]

I don't think I'll care to implement grouping of conditions with parenthesis (e.g. (a || b) && c), which would add a ton of complexity; so this is my thought on how the algorithm could be implemented:

  1. Split at && and ||
  2. Step through the array; for each item that does a conditional check, do what the code currently does.
  3. If the condition is false, but the next token is an ||, keep going and check the next condition
  4. If the condition is false, and the next token is an &&, the overall condition fails and stop checking the rest of the line.

kirsle avatar Mar 08 '17 18:03 kirsle