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.
   * @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;

    // 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.deserialize(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
    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.deserialize(item.key)
        : item.key.includes(" ")
          ? A.serialize(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;
    });

    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;
    }
  };
})();