From 2f598ad7f7021ce9463262c676f6a9a49ce624a7 Mon Sep 17 00:00:00 2001 From: Ruben van de Ven Date: Fri, 26 Apr 2019 11:14:49 +0200 Subject: [PATCH] Diversions now return to next chapter instead of next message --- hugvey/story.py | 41 ++++++++++++++++++++++++++++++++++++---- www/css/styles.css | 5 +++++ www/js/hugvey_console.js | 26 ++++++++++++++++++++----- www/scss/styles.scss | 7 +++++++ 4 files changed, 70 insertions(+), 9 deletions(-) diff --git a/hugvey/story.py b/hugvey/story.py index 4c1d802..e3fceab 100644 --- a/hugvey/story.py +++ b/hugvey/story.py @@ -42,6 +42,7 @@ class Message(object): self.id = id self.text = text self.isStart = False + self.chapterStart = False self.reply = None # self.replyTime = None self.audioFile= None @@ -64,6 +65,7 @@ class Message(object): def initFromJson(message, data, story): msg = message(data['@id'], data['text']) msg.isStart = data['beginning'] if 'beginning' in data else False + msg.chapterStart = bool(data['chapterStart']) if 'chapterStart' in data else False msg.afterrunTime = data['afterrun'] if 'afterrun' in data else 0. msg.color = data['color'] if 'color' in data else None if 'audio' in data and data['audio'] is not None: @@ -433,6 +435,7 @@ class Direction(object): class Diversion(object): """ An Diversion. Used to catch events outside of story flow. + Not sure why I'm not using subclasses here o:) """ def __init__(self, id, type: str, params: dict): @@ -511,7 +514,12 @@ class Diversion(object): return story.logger.info(f"Diverge: No response {self.id} {story.stats}") - self.returnMessage = msgTo + + # fall back to the next message to return on. + # TODO: maybe, if not chapter is found, the diversion should be + # blocked alltogether? + self.returnMessage = story.getNextChapterForMsg(msgTo) or msgTo + await story.setCurrentMessage(msg) story.currentDiversion = self return True @@ -545,7 +553,6 @@ class Diversion(object): if r is None: return - if 'notForColor' in self.params and self.params['notForColor'] and story.currentMessage.color: if story.currentMessage.color.lower() == self.params['notForColor'].lower(): story.logger.debug(f"Skip diversion {self.id} because of section color") @@ -559,7 +566,10 @@ class Diversion(object): story.logger.critical(f"Not a valid message id for diversion: {self.params['msgId']}") return - self.returnMessage = msgTo + # fall back to the next message to return on. + # TODO: maybe, if not chapter is found, the diversion should be + # blocked alltogether? + self.returnMessage = story.getNextChapterForMsg(msgTo) or msgTo await story.setCurrentMessage(msg) story.currentDiversion = self return True @@ -657,7 +667,11 @@ class Diversion(object): story.logger.critical(f"Not a valid message id for diversion: {self.params['msgId']}") return - self.returnMessage = story.currentMessage + # fall back to the currentMessage to return on. + # TODO: maybe, if not chapter is found, the diversion should be + # blocked alltogether? + self.returnMessage = story.getNextChapterForMsg(story.currentMessage, False) or story.currentMessage + await story.setCurrentMessage(msg) story.currentDiversion = self story.timer.setMark('last_diversion_timeout') @@ -1150,6 +1164,25 @@ class Story(object): return [] else: return self.directionsPerMsg[self.currentMessage.id] + + def getNextChapterForMsg(self, msg, canIncludeSelf = True, depth = 0): + if canIncludeSelf and msg.chapterStart: + self.logger.info(f"Next chapter: {msg.id}") + return msg + + if depth >= 70: + # protection against infinite loop? + return None + + if msg.id not in self.directionsPerMsg: + return None + + for direction in self.directionsPerMsg[msg.id]: + r = self.getNextChapterForMsg(direction.msgTo, True, depth+1) + if r: + return r + # none found + return None async def run(self, customStartMsgId = None): self.logger.info("Starting story") diff --git a/www/css/styles.css b/www/css/styles.css index 5eb8f25..ce49e52 100644 --- a/www/css/styles.css +++ b/www/css/styles.css @@ -136,6 +136,11 @@ img.icon { fill: #77618e; } #story .startMsg circle { fill: lightseagreen !important; } + #story .chapterStartMsg circle { + transform: scale(1.5); + fill: white !important; } + #story .chapterStartMsg text { + fill: black; } #story .orphanedMsg { fill: lightcoral !important; } #story text { diff --git a/www/js/hugvey_console.js b/www/js/hugvey_console.js index e6396bf..f9d3134 100644 --- a/www/js/hugvey_console.js +++ b/www/js/hugvey_console.js @@ -739,7 +739,6 @@ class Graph { } - let startAttributes = { 'name': msg['@id'] + '-start', // 'readonly': 'readonly', @@ -763,6 +762,19 @@ class Graph { beginningAttributes['checked'] = 'checked'; } + // chapter marker: + let chapterAttributes = { + 'name': msg['@id'] + '-chapterStart', +// 'readonly': 'readonly', + 'type': 'checkbox', + 'on': { + 'change': this.getEditEventListener() + } + } + if ( typeof msg['chapterStart'] !== 'undefined' && msg['chapterStart'] == true ) { + chapterAttributes['checked'] = 'checked'; + } + let params = {}; if(msg.hasOwnProperty('params')) { params = msg['params']; @@ -771,7 +783,6 @@ class Graph { } let audioSrcEl = crel('source', {'src': msg['audio'] ? msg['audio']['file'] : this.getAudioUrlForMsg(msg)}); -// console.log(msg['audio']); let audioSpan = crel( 'span', { @@ -829,9 +840,13 @@ class Graph { crel( 'input', startAttributes ) ), crel( 'label', - crel( 'span', 'Beginning' ), - crel( 'input', beginningAttributes ) - ), + crel( 'span', 'Beginning' ), + crel( 'input', beginningAttributes ) + ), + crel( 'label', + crel( 'span', 'Chapter start' ), + crel( 'input', chapterAttributes ) + ), crel( 'label', crel( 'span', 'Audio' ), @@ -1681,6 +1696,7 @@ class Graph { let classes = []; if ( this.selectedMsg == msg ) classes.push( 'selectedMsg' ); if ( msg['start'] == true ) classes.push( 'startMsg' ); + if ( msg.hasOwnProperty('chapterStart') && msg['chapterStart'] == true ) classes.push( 'chapterStartMsg' ); if ( this.getDirectionsFrom( msg ).length < 1 ) { classes.push( 'endMsg' ); if ( this.getDirectionsTo( msg ).length < 1 ) classes.push( 'orphanedMsg' ); diff --git a/www/scss/styles.scss b/www/scss/styles.scss index ca62a1d..404ca64 100644 --- a/www/scss/styles.scss +++ b/www/scss/styles.scss @@ -221,6 +221,13 @@ img.icon{ .endMsg circle{ // fill: lightslategray !important; } + .chapterStartMsg circle{ + transform: scale(1.5); + fill: white !important; + } + .chapterStartMsg text{ + fill: black; + } .orphanedMsg{ fill: lightcoral !important;