Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
// oops.js
// OK 09 2023
(function () {
  /*global adventurejs A*/
  "use strict";

  /**
   * @augments {adventurejs.Verb}
   * @class oops
   * @ajsnode game.dictionary.verbs.oops
   * @ajsconstruct MyGame.createVerb({ "name": "oops", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading UtilityVerbs
   * @summary Verb meaning replace unknown word with new input.
   * @tutorial Scripting_VerbSubscriptions
   * @tutorial Verbs_VerbAnatomy
   * @tutorial Verbs_VerbProcess
   * @tutorial Verbs_ModifyVerbs
   * @tutorial Verbs_WriteVerbs
   * @classdesc
   * <pre class="display border outline">
   * <span class="input">&gt; sacrifice virgin on walter/span>
   * I don't know of anything called 'walter'.
   * <span class="input">&gt; oops alter</span>
   * You sacrifice the virgin on the alter, thereby opening the
   * seventh seal and damning all of mankind to a thousand years
   * of runny eggs, unstylish Crocs knockoffs, and community theater.
   * </pre>
   * <p>
   * <strong>Oops</strong> allows players to correct a mistake in
   * the previous turn.
   * </p>
   */
  A.Preverbs.oops = {
    name: "oops",

    /**
     * @ajsverbstructures
     * @memberof exits
     */
    accepts_structures: ["verb noun"],

    /**
     * @memberof oops
     * @ajsverbphrase
     * phrase1:
     * {
     *   accepts_noun: true,
     *   requires_noun: true,
     * },
     */
    phrase1: {
      accepts_noun: true,
      requires_noun: true,
    },

    let_verb_handle_disambiguation: true,
    let_verb_handle_remaining_input: true,
    msgNoObject: "Oops?",

    do: function () {
      var input = this.game.getInput();
      // everything after 'oops '
      var replacementWord = input.input.substring(4).trim();
      this.game.log("log", "high", "oops " + replacementWord, "verbs");

      if ("object" === typeof replacementWord) {
        replacementWord = replacementWord.input;
      }

      var lastInput = this.game.parser.input_history[1].parsed_input;
      if (!this.game.parser.input_history[1].unknown_word) {
        var msg = "Oops indeed.";
        this.handleFailure(msg);
        return false;
      }

      var lastInputUnknownWord = this.game.parser.input_history[1].unknown_word;

      // TODO disambiguation
      if ("undefined" !== typeof lastInputUnknownWord) {
        lastInput = lastInput.replace(lastInputUnknownWord, replacementWord);
        this.game.parser.input_queue.push({
          input: lastInput,
          printInput: false,
        });
        return;
      }

      // TODO disambiguation
      if (!replacementWord) {
        var msg = "Oops found nothing to oops.";
        this.handleFailure(msg);
        return false;
      }

      var msg = `Oops found ${replacementWord}. `;
      this.handleFailure(msg);
      return true;
    },
  };
})();