From 417b3d93617d95d563021279c8fcc5b886e15b70 Mon Sep 17 00:00:00 2001 From: Ruben van de Ven Date: Thu, 13 Jun 2019 22:40:32 +0200 Subject: [PATCH] Condition variableEqual: check if 2 variables are the same --- hugvey/story.py | 19 +++++++++++++++++++ www/js/hugvey_console.js | 5 +++++ 2 files changed, 24 insertions(+) diff --git a/hugvey/story.py b/hugvey/story.py index ef2924b..4d9fba8 100644 --- a/hugvey/story.py +++ b/hugvey/story.py @@ -324,6 +324,8 @@ class Condition(object): condition.method = condition._hasVariable if data['type'] == "diversion": condition.method = condition._hasDiverged + if data['type'] == "variableEquals": + condition.method = condition._variableEquals if 'vars' in data: condition.vars = data['vars'] @@ -380,7 +382,24 @@ class Condition(object): self.vars['variable'] ) return r + + def _variableEquals(self, story) -> bool: + v1 = story.variableValues[self.vars['variable1']] if story.hasVariableSet(self.vars['variable1']) else None + v2 = story.variableValues[self.vars['variable2']] if story.hasVariableSet(self.vars['variable2']) else None + if v1: + story.logger.debug(f"Variable {self.vars['variable1']}: {v1}") + if v2: + story.logger.debug(f"Variable {self.vars['variable2']}: {v2}") + if 'notEq' in self.vars and self.vars['notEq']: + # inverse: + r = (v1 != v2) + else: + r = (v1 == v2) + + story.logger.info("'{}' {} '{}' ({})".format(v1, '==' if v1 == v2 else '!=', v2, r)) + return r + def _hasDiverged(self, story) -> bool: if not story.lastMsgFinishTime: return False diff --git a/www/js/hugvey_console.js b/www/js/hugvey_console.js index a08dbce..8e97669 100644 --- a/www/js/hugvey_console.js +++ b/www/js/hugvey_console.js @@ -1355,6 +1355,11 @@ class Graph { 'variable': { 'value': '','placeholder': "Variable name (without $)" }, 'notSet': { "label": "Not set", 'value': '', 'title': "Match if the variable is _not_ set.", 'type':'checkbox' }, }, + 'variableEquals': { + 'variable1': { 'value': '','placeholder': "Variable name (without $)" }, + 'variable2': { 'value': '','placeholder': "Variable name (without $)" }, + 'notEq': { "label": "Not equal", 'value': '', 'title': "Match if the variables are _not_ equal.", 'type':'checkbox' }, + }, 'diversion': { 'diversionId': { 'tag': 'select', 'value': '','placeholder': "Variable name (without $)", 'options': this.diversions.map((d) => d['@id']) }, 'inverseMatch': { "label": "Match if not done", 'value': '', 'title': "Match if the diversion has _not_ been done.", 'type':'checkbox' },