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

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

  var p = AdventureJS.Parser.prototype;

  /**
   * <p>
   * In order to handle sentences with multiple clauses
   * connected by conjunctions 'and' or 'but', we convert
   * them to symbols we can use down the line.
   * </p>
   * @memberOf AdventureJS.Parser
   * @method AdventureJS.Parser#stripConjunctions
   * @param {String} input_string Player input string.
   * @returns {String}
   */
  p.stripConjunctions = function Parser_stripConjunctions(input_string) {
    this.game.log(
      "L1571",
      "log",
      "high",
      `[stripConjunctions.js] stripConjunctions() receive: ${input_string}`,
      "Parser"
    );
    let this_turn = this.input_history[0];

    /* *
     * Convert " and " to "&" for later noun parsing
     * We've already removed instances of " and then "
     * and we're assuming that any other use of "and" is in
     * the form of "verb noun and noun".
     * We also look for ", and" as in "take x, and y"
     * which seems less likely to be input, but not impossible.
     * Another possibility: "look between noun and noun" - may need a lookBetweenAnd verb
     * "put noun between noun and noun" - might need a putBetweenAnd verb
     */
    input_string = input_string.replace(/, and /g, "&");
    input_string = input_string.replace(/ and /g, "&");

    /* *
     * Also convert ", " to "&", for instances of:
     *  take sword, shield
     *
     * There is an additional comma check in splitByThens,
     * because we assume English speakers will input things like
     *  take sword, then go north
     * But in that case we want to strip the comma.
     */
    input_string = input_string.replace(/, /g, "&");

    /* *
     * Convert " but " and  " except " to " -". Used
     * to omit specific items from collections.
     * Example:
     *  take all but sword
     * We save this to tokens for printing back the player's input
     * @TODO correctly parse
     * "take all from knapsack but coinpurse"
     * where but follows the direct object
     * currently this is parsed as
     * > take all from knapsack -coinpurse
     *
     * @TODO this only handles one "but" or "except"
     * which is not very robust
     */
    input_string = input_string.replace(/\s(but|except)\s/, (_, word) => {
      let context = `parser.stripConjunctions() found '${word}' and replaced it with ' -'`;
      this_turn.tokens[" -"] = {
        key: " -",
        source: ` ${word} `,
        context: context,
      };
      return " -";
    });

    this.game.log(
      "L1513",
      "log",
      "high",
      `[stripConjunctions.js] stripConjunctions() return:\n${input_string}`,
      "Parser"
    );
    return input_string;
  };
})();