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

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

  var p = adventurejs.Parser.prototype;

  /**
   * Look for numbers, and replace with the GlobalNumber
   * placeholder class.
   * <br><br>
   * Example:<br>
   * type 10 on keyboard<br>
   * Becomes:<br>
   * type global_number on keyboard<br>
   * @memberOf adventurejs.Parser
   * @method adventurejs.Parser#parseNumbers
   * @param {String} input Player input.
   * @returns {String}
   */
  p.parseNumbers = function Parser_parseNumbers(input) {
    this.game.log(
      "L1558",
      "log",
      "high",
      `[parseNumbers.js] parseNumbers() receive: ${input}`,
      "Parser"
    );
    //this.game.log( "log", "medium", "parseNumbers receive: " + input , 'Parser' );
    this.game.world.global_number.set({ values: [] });

    // Match standalone numbers using word boundaries
    const re = /\b\d+\b/g;

    let match;
    let counter = 0;

    while ((match = re.exec(input))) {
      this.game.log(
        "L1535",
        "log",
        "high",
        `parseNumbers > found number ${counter}: ${match[0]}`,
        "Parser"
      );

      // Replace only the first occurrence of the number string
      input = input.replace(match[0], "global_number");

      // Save the number
      let num = match[0];

      // Store in both input strings and global number object
      this.game.getInput().strings.push(num);
      this.game.world.global_number.values.push(num);

      counter++;
    }

    this.game.log(
      "L1536",
      "log",
      "high",
      `[parseNumbers.js] parseNumbers() return:\n${input}`,
      "Parser"
    );
    return input;
  };
})();