Skip to content

Commit

Permalink
Pretext handling (#9)
Browse files Browse the repository at this point in the history
* Logic to preserve text generated before first set of choices.

* Additional changes so that functions in pretext are processed.
  • Loading branch information
rabidgremlin committed May 25, 2018
1 parent 84da631 commit 957443d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 34 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=4.2.0
version=4.2.1-SNAPSHOT

# These are place holder values. Real values should be set in user's home gradle.properties
# and are only needed when signing and uploading to central maven repo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ public BotResponse respond(Session session, Context context, String messageText)
// call hook so additional things can be applied to story after state has been restored
afterStoryStateLoaded(story);

// get to right place in story
story.continueMaximally();
// get to right place in story, capture any pretext
String preText = processStory(session, currentResponse, story, null, false).toString();

// build expected intents set
HashSet<String> expectedIntents = new HashSet<String>();
Expand Down Expand Up @@ -249,7 +249,7 @@ public BotResponse respond(Session session, Context context, String messageText)
if (knotName != null)
{
story.choosePathString(knotName);
getResponseText(session, currentResponse, story, intentMatch, false);
getResponseText(session, currentResponse, story, intentMatch, false, preText);
foundMatch = true;
}
else
Expand All @@ -266,7 +266,7 @@ public BotResponse respond(Session session, Context context, String messageText)
log.debug("Choosing: {}", c.getText());
story.chooseChoiceIndex(choiceIndex);

getResponseText(session, currentResponse, story, intentMatch, true);
getResponseText(session, currentResponse, story, intentMatch, true, preText);

foundMatch = true;
break;
Expand Down Expand Up @@ -316,7 +316,7 @@ public BotResponse respond(Session session, Context context, String messageText)
// jump to confused knot
story.choosePathString(confusedKnotName);
// continue story
getResponseText(session, currentResponse, story, intentMatch, false);
getResponseText(session, currentResponse, story, intentMatch, false, preText);
// reset failed count
failedToUnderstandCount = 0;
}
Expand Down Expand Up @@ -352,59 +352,101 @@ public BotResponse respond(Session session, Context context, String messageText)
}
}

private void getResponseText(Session session, CurrentResponse currentResponse, Story story, IntentMatch intentMatch, boolean skipfirst)
private void getResponseText(Session session, CurrentResponse currentResponse, Story story, IntentMatch intentMatch, boolean skipfirst, String preText)
throws StoryException, Exception
{
// reset reprompt, hint and quick replies
currentResponse.setReprompt(null);
currentResponse.setHint(null);
currentResponse.setResponseQuickReplies(null);

StringBuffer response = new StringBuffer();
// get the story output and build the reponse
StringBuffer response = processStory(session, currentResponse, story, intentMatch, skipfirst);

// add any pretext if we have it
preText = StringUtils.chomp(preText).trim(); // remove any trailing \n and trim to ensure we actually have some content
if (StringUtils.isNotBlank(preText))
{
response.insert(0,"\n");
response.insert(0,preText);
}

currentResponse.setResponseText(response.toString());
}

/** Processes the story until the next set of choices, triggering any InkFunctions along the way.
*
* @param session The current session.
* @param currentResponse The current response.
* @param story The current story.
* @param intentMatch The current intent match.
* @param skipfirst True if first lien should be skipped. Required as ink always replays choice.
* @return String buffer containing output.
* @throws StoryException Thrown if there is an error.
* @throws Exception Thrown if there is an error.
*/
private StringBuffer processStory(Session session, CurrentResponse currentResponse, Story story, IntentMatch intentMatch, boolean skipfirst) throws StoryException, Exception
{
StringBuffer response = new StringBuffer();

boolean first = true;
while (story.canContinue())
{
String line = story.Continue();

// skip first line as ink replays choice first
if (first && skipfirst)
{
first = false;
continue;
}

log.debug("Line {}", line);

String trimmedLine = line.trim();

if (trimmedLine.startsWith("::"))
{
String functionName = trimmedLine.split(" ")[0].substring(2).trim();
String param = trimmedLine.substring(functionName.length() + 2).trim();

InkBotFunction function = inkBotFunctions.get(functionName.toLowerCase());
if (function != null)
{
function.execute(currentResponse, session, intentMatch, story, param);
}
else
{
log.warn("Did not find function named {}", functionName);
}
}
else
{
response.append(line);
}
processStoryLine(line,response,currentResponse, session, intentMatch, story);
}

// chop off last \n
if (response.length() > 0 && response.charAt(response.length() - 1) == '\n')
{
response.setLength(response.length() - 1);
}

currentResponse.setResponseText(response.toString());

return response;
}

/** Processes a story line triggering any InkFunctions that are found.
*
* @param line The story line to process.
* @param response The response to populate.
* @param currentResponse The current response.
* @param session The current session.
* @param intentMatch The current intent match.
* @param story The current story.
*/
private void processStoryLine(String line, StringBuffer response, CurrentResponse currentResponse, Session session, IntentMatch intentMatch, Story story)
{
log.debug("Line {}", line);

String trimmedLine = line.trim();

if (trimmedLine.startsWith("::"))
{
String functionName = trimmedLine.split(" ")[0].substring(2).trim();
String param = trimmedLine.substring(functionName.length() + 2).trim();

InkBotFunction function = inkBotFunctions.get(functionName.toLowerCase());
if (function != null)
{
function.execute(currentResponse, session, intentMatch, story, param);
}
else
{
log.warn("Did not find function named {}", functionName);
}
}
else
{
response.append(line);
}
}

/**
Expand Down

0 comments on commit 957443d

Please sign in to comment.