Now fetch only unqiue items with variable_storage if checkbox is set

This commit is contained in:
Ruben van de Ven 2019-08-30 21:55:55 +02:00
parent 69a858b251
commit 6f4cd6aae4
3 changed files with 6 additions and 3 deletions

View file

@ -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:

View file

@ -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

View file

@ -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' },