Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// splitSentences.js

(function () {
  /* global AdventureJS A */

  var p = AdventureJS.Parser.prototype;

  /**
   * Calls split by periods and enqueues the results.
   * @memberOf AdventureJS.Parser
   * @method AdventureJS.Parser#splitSentences
   * @param {String} input
   * @returns {String}
   *
   * @TODO multi-turn handling for instructions given to NPCs
   * ex: Roger, go east then drop lamp
   *
   */
  p.splitSentences = function Parser_splitSentences(input) {
    this.game.log(
      "L1605",
      "log",
      "high",
      `[splitSentences.js] splitSentences() receive: ${input}`,
      "Parser"
    );

    const subject = this.input_history[0].subject;
    const this_turn = this.input_history[0];
    const last_turn = this.input_history[1];

    const sentences = input
      .split("|")
      .map((sentence) => sentence.trim())
      .filter((sentence) => sentence.length);

    if (1 === sentences.length) {
      // single clause found

      // did player enter "again" ?
      // if so, copy last turn into this turn
      if (this.game.dictionary.testVerbSynonyms(input, "again")) {
        input = last_turn.input;
        this_turn.input = input;
      }
      // else use whole input
      else input = sentences[0];
      // this_turn.was_split = false;
    } else if (sentences.length > 1) {
      // player entered multiple clauses

      this_turn.was_split = true;

      // is first item in queue "again"?
      // replace with last turn
      if (this.game.dictionary.testVerbSynonyms(sentences[0], "again")) {
        sentences[0] = last_turn.input;
      }

      // is any other item in queue "again"?
      // replace with the turn preceding it in the queue
      for (var i = 1; i < sentences.length; i++) {
        if (this.game.dictionary.testVerbSynonyms(sentences[i], "again")) {
          sentences[i] = sentences[i - 1];
        }
      }

      // use the first one now
      input = sentences.shift();

      // queue up the rest for subsequent turns
      for (var i = 0; i < sentences.length; i++) {
        this.input_queue.push({
          input: sentences[i],
          printInput: true,
          quote_tokens: this_turn.quote_tokens,
          raw_input: this_turn.raw_input,
          subject: subject,
          was_split: true,
        });
      }
    }

    this.game.log(
      "L1606",
      "log",
      "high",
      `[splitSentences.js] splitSentences() return: ${input}`,
      "Parser"
    );
    return input;
  };
})();