From ee1a2e39f2fbb75aaa8ffd300a8886e3a3d8440b Mon Sep 17 00:00:00 2001 From: Ruben van de Ven Date: Sun, 16 Jun 2019 17:44:13 +0200 Subject: [PATCH] messagePlayed condition --- hugvey/story.py | 28 +++++++++++++++++++++++++++- www/js/hugvey_console.js | 6 +++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/hugvey/story.py b/hugvey/story.py index 4ccabab..9fb4213 100644 --- a/hugvey/story.py +++ b/hugvey/story.py @@ -314,7 +314,7 @@ class Condition(object): condition = conditionClass(data['@id']) condition.type = data['type'] condition.originalJsonString = json.dumps(data) - + #: :type condition: Condition # TODO: should Condition be subclassed? if data['type'] == "replyContains": condition.method = condition._hasMetReplyContains @@ -324,6 +324,8 @@ class Condition(object): condition.method = condition._hasVariable if data['type'] == "diversion": condition.method = condition._hasDiverged + if data['type'] == "messagePlayed": + condition.method = condition._hasPlayed if data['type'] == "variableEquals": condition.method = condition._variableEquals @@ -422,6 +424,30 @@ class Condition(object): ) return r + + def _hasPlayed(self, story) -> bool: + if not story.lastMsgFinishTime: + return False + + msg = story.get(self.vars['msgId']) + if not msg: + story.logger.critical(f"Condition on non-existing message: {self.vars['msgId']}") + + #: :type msg: Message + r = msg.isFinished() + if r: + story.logger.debug(f"Msg {self.vars['msgId']} has been played.") + + if 'inverseMatch' in self.vars and self.vars['inverseMatch']: + # inverse: + r = not r + + self.logInfo = "Has {} played msg {}".format( + 'not' if 'inverseMatch' in self.vars and self.vars['inverseMatch'] else '', + self.vars['msgId'] + ) + + return r def _hasMetReplyContains(self, story) -> bool: """ diff --git a/www/js/hugvey_console.js b/www/js/hugvey_console.js index 8e97669..d223c9c 100644 --- a/www/js/hugvey_console.js +++ b/www/js/hugvey_console.js @@ -1361,8 +1361,12 @@ class Graph { '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']) }, + 'diversionId': { 'tag': 'select', 'value': '', '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' }, + }, + 'messagePlayed': { + 'msgId': { 'value': '', 'label':'Message ID', 'placeholder': "(eg. en-njsm9b0ni)" }, + 'inverseMatch': { "label": "Match if not played", 'value': '', 'title': "Match if the message has _not_ been played.", 'type':'checkbox' }, } }; }