Prevent diversions after chapter has been played

This commit is contained in:
Ruben van de Ven 2019-04-28 11:34:30 +02:00
parent f72cf9b100
commit ebf0d1ebad
3 changed files with 50 additions and 4 deletions

View file

@ -633,7 +633,6 @@ class HugveyState(object):
self.isConfigured = None self.isConfigured = None
self.setStatus(self.STATE_GONE) self.setStatus(self.STATE_GONE)
def shutdown(self, definitive = False): def shutdown(self, definitive = False):
self.logger.info(f"Start shutdown sequence {definitive}") self.logger.info(f"Start shutdown sequence {definitive}")
self.eventLogger.critical(f"error: shutting down") self.eventLogger.critical(f"error: shutting down")

View file

@ -14,6 +14,7 @@ from zmq.asyncio import Context
import zmq import zmq
import wave import wave
from pythonosc import udp_client from pythonosc import udp_client
from builtins import isinstance
mainLogger = logging.getLogger("hugvey") mainLogger = logging.getLogger("hugvey")
logger = mainLogger.getChild("narrative") logger = mainLogger.getChild("narrative")
@ -521,6 +522,16 @@ class Diversion(object):
Validate if condition is met for the current story state Validate if condition is met for the current story state
Returns True when diverging Returns True when diverging
""" """
# For all diversion except repeat (which simply doesn't have the variable)
if 'notAfterMsgId' in self.params and self.params['notAfterMsgId']:
msg = story.get(self.params['notAfterMsgId'])
if msg is None:
story.logger.warn(f"Invalid message selected for diversion: {self.params['notAfterMsgId']} for {self.id}")
elif story.logHasMsg(msg):
# story.logger.warn(f"Block diversion {self.id} because of hit message {self.params['notAfterMsgId']}")
return False
r = await self.method(story, msgFrom, msgTo) r = await self.method(story, msgFrom, msgTo)
if r: if r:
self.hasHit = True self.hasHit = True
@ -784,6 +795,7 @@ class Story(object):
self.events = [] # queue of received events self.events = [] # queue of received events
self.commands = [] # queue of commands to send self.commands = [] # queue of commands to send
self.log = [] # all nodes/elements that are triggered self.log = [] # all nodes/elements that are triggered
self.msgLog = [] # hit messages
self.logger = mainLogger.getChild(f"{self.hugvey.id}").getChild("story") self.logger = mainLogger.getChild(f"{self.hugvey.id}").getChild("story")
self.currentMessage = None self.currentMessage = None
self.currentDiversion = None self.currentDiversion = None
@ -902,6 +914,7 @@ class Story(object):
self.events = [] # queue of received events self.events = [] # queue of received events
self.commands = [] # queue of commands to send self.commands = [] # queue of commands to send
self.log = [] # all nodes/elements that are triggered self.log = [] # all nodes/elements that are triggered
self.msgLog = []
self.currentReply = None self.currentReply = None
self.stats = { self.stats = {
@ -1087,6 +1100,9 @@ class Story(object):
def addToLog(self, node): def addToLog(self, node):
self.log.append((node, self.timer.getElapsed())) self.log.append((node, self.timer.getElapsed()))
if isinstance(node, Message):
self.msgLog.append(node)
if self.hugvey.recorder: if self.hugvey.recorder:
if isinstance(node, Message): if isinstance(node, Message):
self.hugvey.recorder.log('hugvey', node.text, node.id) self.hugvey.recorder.log('hugvey', node.text, node.id)
@ -1094,6 +1110,9 @@ class Story(object):
self.hugvey.recorder.log('diversion',node.id) self.hugvey.recorder.log('diversion',node.id)
if isinstance(node, Condition): if isinstance(node, Condition):
self.hugvey.recorder.log('condition',node.logInfo, node.id) self.hugvey.recorder.log('condition',node.logInfo, node.id)
def logHasMsg(self, node):
return node in self.msgLog
async def _renderer(self): async def _renderer(self):
""" """

View file

@ -382,6 +382,10 @@ class Graph {
alert('invalid type for diversion'); alert('invalid type for diversion');
} }
if(type != 'repeat') {
div['params']['notAfterMsgId'] = "";
}
this.data.push( div ); this.data.push( div );
this.updateFromData(); this.updateFromData();
this.build(); this.build();
@ -401,6 +405,27 @@ class Graph {
let divsNoResponse =[], divsRepeat = [], divsReplyContains = [], divsTimeouts = []; let divsNoResponse =[], divsRepeat = [], divsReplyContains = [], divsTimeouts = [];
for(let div of this.diversions) { for(let div of this.diversions) {
let notAfterMsgIdEl = "";
if(div['type'] != 'repeat') {
let notMsgOptions = [crel('option',"")];
let chapterMsgs = this.messages.filter( m => m.hasOwnProperty('chapterStart') && m['chapterStart'] == true);
for(let startMsg of chapterMsgs) {
let optionParams = {
'value': startMsg['@id']
};
if(div['params']['notAfterMsgId'] == startMsg['@id']) {
optionParams['selected'] = 'selected';
}
notMsgOptions.push(crel('option', optionParams , `${startMsg['text']} (${startMsg['@id']})`));
}
notAfterMsgIdEl = crel('label', 'Not when chapter has hit:',
crel('select', {'on': {
'change': (e) => div['params']['notAfterMsgId'] = e.target.value
}}, ...notMsgOptions)
);
}
if(div['type'] == 'no_response') { if(div['type'] == 'no_response') {
let returnAttrs = { let returnAttrs = {
'type': 'checkbox', 'type': 'checkbox',
@ -468,7 +493,8 @@ class Graph {
crel('select', {'on': { crel('select', {'on': {
'change': (e) => div['params']['msgId'] = e.target.value 'change': (e) => div['params']['msgId'] = e.target.value
}}, ...msgOptions) }}, ...msgOptions)
) ),
notAfterMsgIdEl
)); ));
} }
if(div['type'] == 'reply_contains') { if(div['type'] == 'reply_contains') {
@ -544,7 +570,8 @@ class Graph {
crel('select', {'on': { crel('select', {'on': {
'change': (e) => div['params']['msgId'] = e.target.value 'change': (e) => div['params']['msgId'] = e.target.value
}}, ...msgOptions) }}, ...msgOptions)
) ),
notAfterMsgIdEl
)); ));
} }
if(div['type'] == 'timeout') { if(div['type'] == 'timeout') {
@ -638,7 +665,8 @@ class Graph {
crel('select', {'on': { crel('select', {'on': {
'change': (e) => div['params']['msgId'] = e.target.value 'change': (e) => div['params']['msgId'] = e.target.value
}}, ...msgOptions) }}, ...msgOptions)
) ),
notAfterMsgIdEl
)); ));
} }
if(div['type'] == 'repeat'){ if(div['type'] == 'repeat'){