From 6f4cd6aae49d8288ce0f37159e6e10e6307b15fd Mon Sep 17 00:00:00 2001 From: Ruben van de Ven Date: Fri, 30 Aug 2019 21:55:55 +0200 Subject: [PATCH] Now fetch only unqiue items with variable_storage if checkbox is set --- hugvey/story.py | 3 ++- hugvey/variablestore.py | 5 +++-- www/js/hugvey_console.js | 1 + 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/hugvey/story.py b/hugvey/story.py index 572ef17..f22591a 100644 --- a/hugvey/story.py +++ b/hugvey/story.py @@ -498,7 +498,8 @@ class Condition(object): return False number = int(self.vars['number']) - varValues = story.hugvey.command.variableStore.getLastOfName(self.vars['var_name'], story.language_code, number) + unique = bool(self.vars['unique']) if 'unique' in self.vars else False + varValues = story.hugvey.command.variableStore.getLastOfName(self.vars['var_name'], story.language_code, number, unique) self.hasRan = True if len(varValues) < number: diff --git a/hugvey/variablestore.py b/hugvey/variablestore.py index c4d8c1f..7adf110 100644 --- a/hugvey/variablestore.py +++ b/hugvey/variablestore.py @@ -64,10 +64,11 @@ class VariableStore: self.conn.commit() c.close() - def getLastOfName(self, name, languageCode, n = 10): + def getLastOfName(self, name, languageCode, n = 10, unique = False): cur = self.conn.cursor() logging.debug(f"Get last {n} stored variables of {name} for {languageCode}") - cur.execute("SELECT val FROM variables WHERE language_code = ? AND name = ? ORDER BY createdAt DESC LIMIT ?", (languageCode, name, n)) + distinct = "DISTINCT" if unique else "" + cur.execute(f"SELECT {distinct} val FROM variables WHERE language_code = ? AND name = ? ORDER BY createdAt DESC LIMIT ?", (languageCode, name, n)) values = [v[0] for v in cur.fetchall()] cur.close() return values \ No newline at end of file diff --git a/www/js/hugvey_console.js b/www/js/hugvey_console.js index 2f654b3..18b82fb 100644 --- a/www/js/hugvey_console.js +++ b/www/js/hugvey_console.js @@ -1634,6 +1634,7 @@ class Graph { // when matched, variable will be accessible as {store_name_1} 'var_name': { 'label': "Variable name", 'type': 'text', 'description': "When matched, variable will be accessible as $stored_VARNAME_1, $stored_VARNAME_2.. etc (use the name given here instead of VARNAME)" }, 'number': { 'label': "Nr. of items to get", 'type': 'number', 'value': 5, 'min': 0, 'step': 1 }, + 'unique': { 'label': "Unique items", 'type': 'checkbox', 'title': "If checked, every word is returned only once, eg. love, dream, love, returns love, dream"}, }, 'replyContains': { 'delays.0.minReplyDuration': { 'type': 'number', 'value': 0, 'min': 0, 'step': 0.1, 'label': 'Delay 1 - reply duration', 'unit': "s", 'readonly': 'readonly' },