Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// hasVerbMaxConnections.js
(function () {
  /*global adventurejs A*/
  var p = adventurejs.Asset.prototype;
  /**
   * Get whether the asset's specific verb connections are at
   * the maximum set by verb.with_params.max_connections.
   * @memberOf adventurejs.Asset
   * @method adventurejs.Asset#hasVerbMaxConnections
   * @param {String} verb The name of a verb.
   * @param {String} to_ov Connection to direct or indirect objects of verb.
   * @return {Boolean}
   * @TODO dov/iov or both
   */
  p.hasVerbMaxConnections = function Asset_hasMaxVerbConnections(verb, to_ov) {
    // no verb? is max
    // verb doesn't make connections? is max
    // this not subscribed to verb? is max
    // this can't make connections? is max
    if (
      !verb ||
      !this.game.dictionary.verbs[verb] ||
      !this.game.dictionary.verbs[verb].makes_connections ||
      (!this.dov[verb] && !this.iov[verb]) ||
      !this.is.connected_by
    ) {
      return true;
    }

    // verb doesn't set max connections? not max
    if (
      "undefined" ===
      typeof this.game.dictionary.verbs[verb].with_params.max_connections
    ) {
      return false;
    }

    // no connections made yet?
    if (!this.is.connected_by[verb]) {
      return false;
    }

    if (to_ov === "dov" && this.isIOV(verb)) to_ov = "to_dov";
    if (to_ov === "iov" && this.isDOV(verb)) to_ov = "to_iov";

    if (to_ov !== "to_dov" && to_ov !== "to_iov") {
      if (this.isDOV(verb)) to_ov = "to_iov";
      else if (this.isIOV(verb)) to_ov = "to_dov";
    }

    if (to_ov === "to_dov") {
      if (this.iov[verb].with_params.max_connections === -1) return false;
      if (this.is.connected_by[verb].to_dov) {
        return (
          this.is.connected_by[verb].to_dov.length >=
          this.iov[verb].with_params.max_connections
        );
      } else return true;
    }

    if (to_ov === "to_iov") {
      if (this.dov[verb].with_params.max_connections === -1) return false;
      if (this.is.connected_by[verb].to_iov) {
        return (
          this.is.connected_by[verb].to_iov.length >=
          this.dov[verb].with_params.max_connections
        );
      } else return true;
    }

    // default
    return true;
  };
})();