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

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

  var p = AdventureJS.Display.prototype;

  /**
   * Print the player's input back to game display.
   * On parseSentence, we begin breaking the input down
   * into parts in such a way that there's no guarantee
   * that we have the original input, so we rebuild it
   * @memberOf AdventureJS.Display
   * @method AdventureJS.Display#printInput
   * @param {String} input An arbitrary string.
   */
  p.printInput = function Display_printInput(input) {
    const this_turn = this.game.parser.input_history[0];

    input = input || this_turn.parsed_input || this_turn.input;

    // --------------------------------------------------
    // replace "-this" or "-this&that"
    // with "but this" or "but this and that"
    // --------------------------------------------------

    // this version DOES denormalize underscored_names
    // input = input.replace(/-\w+(?:&\w+)*/g, (match) => {
    //   const words = match
    //     .slice(1) // remove leading -
    //     .split("&") // ["brass_key", "glass_key"]
    //     .map((word) => A.FX.denormalize(word));

    //   return "but " + words.join(" and ");
    // });

    // this version DOES NOT denormalize underscored_names
    // however the token pass below operates on our exclusions
    input = input.replace(
      /-\w+(?:&\w+)*/g,
      (match) => "but " + match.slice(1).replace(/&/g, " and ")
    );

    // --------------------------------------------------
    // break down ambiguous words, ie
    // brass_key=melted_brass_key,tiny_brass_key,giant_brass_key
    // this is a thing we do in joinCompoundPhrases.js
    // @TODO change this to a token scheme
    // --------------------------------------------------
    if (input.includes("_") || input.includes("=")) {
      input = input
        .split(" ")
        .map((word) => {
          return A.FX.denormalize(word.split("=")[0]);
        })
        .join(" ");
    }

    // --------------------------------------------------
    // strip stacked verbs, ie take&wear
    // which is a result of player inputting
    // something like "take and wear jacket"
    // @TODO change this to a token scheme
    // --------------------------------------------------
    if (input.includes("&")) {
      input = input
        .split(" ")
        .map((word) => {
          return word.split("&")[0];
        })
        .join(" ");
    }

    // --------------------------------------------------
    // reverse tokens
    // this includes exclusions like "all but this and that"
    // --------------------------------------------------
    for (let key in this_turn.tokens) {
      const item = this_turn.tokens[key];
      // if our token was serialized, also check deserialized
      // and vice versa
      const alt = item.key.includes("_")
        ? A.FX.denormalize(item.key)
        : item.key.includes(" ")
          ? A.FX.normalize(item.key)
          : "";
      input = input.replace(item.key, item.source);
      if (alt) {
        input = input.replace(alt, item.source);
      }
    }

    // --------------------------------------------------
    // if player input a number get that value
    // @TODO need better number token scheme
    // to match quote_tokens
    // --------------------------------------------------
    input = input.replace(
      /global_number|global number/,
      this.game.world.global_number.values[0]
    );

    // --------------------------------------------------
    // reverse quote tokens
    // --------------------------------------------------
    input = input.replace(/\$\d/g, (match) => {
      return this_turn.quote_tokens[match]
        ? this_turn.quote_tokens[match]
        : match;
    });

    // --------------------------------------------------
    // print it
    // --------------------------------------------------
    if ("undefined" !== typeof input) {
      this.print(`> ${input}`, "ajs-player-input");
      return;
    }
    if (this.game.parser.getInputCount() > 0) {
      this.print(`> ${this.game.parser.getLastInput()}`, "ajs-player-input");
      return;
    }
  };
})();