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

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

  var p = adventurejs.Parser.prototype;

  /**
   * Each clause separated by 'then' is a distinct input.
   * We treat clauses as unrelated without depencencies
   * and stack each into a queue.
   * <br><br>
   * Example: "take sword then go north"
   * @memberOf adventurejs.Parser
   * @method adventurejs.Parser#splitByThens
   * @param {String} input
   * @returns {String}
   */
  p.splitByThens = function Parser_splitByThens(input) {
    this.game.log(
      "L1568",
      "log",
      "high",
      `[splitByThens.js] splitByThens() receive: ${input}`,
      "Parser"
    );
    let split_input = input.split(" then ");
    /**
     * We also do a comma test here because English
     * speakers may be inclined to use a comma
     * before "then", as in:
     *
     *  take sword, then go north
     *
     * We want to strip that comma if we find it, but note that
     * in parseInput.js, commas are being substitued
     * as a shorthand for " and ", so we have two different
     * comma searches in an effort to catch either case.
     */
    for (var i = 0; i < split_input.length; i++) {
      split_input[i] = split_input[i].trim();
      if ("," === split_input[i].charAt(split_input[i].length - 1)) {
        split_input[i] = split_input[i].slice(0, -1);
      }
    }

    // try to catch the form of "take then wear eyeglasses"
    if (split_input.length > 1 && split_input[0].split(" ").length === 1) {
      // we got a split where the language preceding the then
      // is only one word. Is that word a verb?
      const potential_verb = this.parseVerb(split_input[0]);
      if (potential_verb) {
        // we found a verb - can that verb be intransitive?
        // this isn't conclusive but it's the best we got
        const dictionary_verb = this.game.getVerb(potential_verb);
        if (dictionary_verb && !dictionary_verb.accepts_structures.verb) {
          // get first word from split_input[1]
          const next_potential_verb = this.parseVerb(
            split_input[1].split(" ")[0]
          );
          const next_dictionary_verb = this.game.getVerb(next_potential_verb);
          if (next_dictionary_verb) {
            // yes, it appears that player input
            // "verb1 then verb2 something"
            // we can't make anything of verb1 alone
            // so copy second clause to first clause
            // then restore verb1 to first clause
            // we should wind up with
            // [ "verb1 something", "verb2 something" ]
            split_input[0] = split_input[1].replace(
              next_potential_verb,
              potential_verb
            );
          }
        }
      }
    }
    this.game.log(
      "L1569",
      "log",
      "high",
      `[splitByThens.js] splitByThens() return: ${String(split_input)}`,
      "Parser"
    );
    return split_input;
  };
})();