From 15a3ddc5dd002bb5bdef0949a1de11e3e57a2f49 Mon Sep 17 00:00:00 2001 From: Ruben van de Ven Date: Fri, 29 Mar 2019 14:11:48 +0100 Subject: [PATCH] Fix #26 - Condition: Variable --- hugvey/story.py | 23 +++++++++++++++++++++++ www/js/hugvey_console.js | 4 ++++ 2 files changed, 27 insertions(+) diff --git a/hugvey/story.py b/hugvey/story.py index bbafac1..e59bfb8 100644 --- a/hugvey/story.py +++ b/hugvey/story.py @@ -140,6 +140,7 @@ class Message(object): return self.audioFile self.logger.debug(f"Fetching audio for {self.getText()}") +# return "test"; async with self.filenameFetchLock: client = AsyncHTTPClient() queryString = urllib.parse.urlencode({ @@ -227,6 +228,8 @@ class Condition(object): condition.method = condition._hasMetReplyContains if data['type'] == "timeout": condition.method = condition._hasMetTimeout + if data['type'] == "variable": + condition.method = condition._hasVariable if 'vars' in data: condition.vars = data['vars'] @@ -265,6 +268,20 @@ class Condition(object): story.stats['consecutiveSilentTimeouts'] += 1 return True + + def _hasVariable(self, story) -> bool: + if not story.lastMsgFinishTime: + return False + + r = story.hasVariableSet(self.vars['variable']) + if r: + story.logger.debug(f"Variable {self.vars['variable']} is set.") + + if 'notSet' in self.vars and self.vars['notSet']: + # inverse: + r = not r + + return r def _hasMetReplyContains(self, story) -> bool: """ @@ -569,8 +586,14 @@ class Story(object): self.logger.warn(f"Set variable that is not needed in the story: {name}") self.variableValues[name] = value + if name not in self.variables: + return + for message in self.variables[name]: message.setVariable(name, value) + + def hasVariableSet(self, name) -> bool: + return name in self.variableValues and self.variableValues is not None def setStoryData(self, story_data): """ diff --git a/www/js/hugvey_console.js b/www/js/hugvey_console.js index 6419d3f..af0a4cc 100644 --- a/www/js/hugvey_console.js +++ b/www/js/hugvey_console.js @@ -938,6 +938,10 @@ class Graph { 'delays.2.waitTime': { 'type': 'number', 'value': 0, 'min': 0, 'step': 0.1, 'label': 'Delay 3 - time', 'unit': "s" }, 'regex': { 'value': '','placeholder': "match any input" }, 'instantMatch': { 'value': '', 'title': "When matched, don't wait for reply to finish. Instantly take this direction.", 'type':'checkbox' }, + }, + 'variable': { + 'variable': { 'value': '','placeholder': "Variable name (without $)" }, + 'notSet': { "label": "Not set", 'value': '', 'title': "Match if the variable is _not_ set.", 'type':'checkbox' }, } }; }