Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
// combineVerbs.js

(function () {
  /*global adventurejs A*/
  "use strict";

  var p = adventurejs.Dictionary.prototype;

  /**
   * <strong>combineVerbs</strong> is a method to allow authors
   * to consolidate verbs.
   * For example, consider move and push.
   * These verbs are almost identical and only exist here as
   * distinct verbs because it seems that "push character" has
   * a very different connotation than "move character".
   * Authors who find the distinction unhelpful
   * may want to combine them so they don't have to write custom
   * responses for both verbs.
   * What combineVerbs does under the hood is delete the
   * verb(s) provided in the first param and add their synonyms
   * to the verb provided in the second param.
   * @memberOf adventurejs.Dictionary
   * @method adventurejs.Dictionary#combineVerbs
   * @param {String} pushVerbs
   * @param {String} intoVerb
   */
  p.combineVerbs = function (pushVerbs, intoVerb) {
    // validate intoVerb
    if ("string" !== typeof intoVerb) {
      var msg =
        "combineVerbs takes a string but received unknown object " +
        intoVerb +
        ". ";
      this.game.log("warn", "critical", msg, "dictionary");
      return false;
    }
    if ("undefined" === typeof this.verbs[intoVerb]) {
      var msg = "combineVerbs received unknown verb " + intoVerb + ". ";
      this.game.log("warn", "critical", msg, "dictionary");
      return false;
    }
    // pushVerbs can take string or array, so convert to array
    if ("string" === typeof pushVerbs) {
      pushVerbs = [pushVerbs];
    }
    // copy pushVerb's synonyms to intoVerb then delete pushVerb from lookup
    for (var v = 0; v < pushVerbs.length; v++) {
      var pushVerb = pushVerbs[v]; //this.verb_lookup[ pushVerb ];
      if ("undefined" === typeof this.verbs[pushVerb]) {
        var msg = "combineVerbs received unknown verb " + pushVerb + ". ";
        this.game.log("warn", "critical", msg, "dictionary");
        continue;
      }
      for (var s = 0; s < this.verbs[pushVerb].synonyms.length; s++) {
        this.verb_lookup[intoVerb].synonyms.push(
          this.verbs[pushVerb].synonyms[s]
        );
      }
      delete this.verb_lookup[pushVerb];
      delete this.verbs[pushVerb];
    }
    return;
  };
})();