Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// allowVerbWithAsset.js
(function () {
  /* global adventurejs A */
  var p = adventurejs.Asset.prototype;
  /**
   * <strong>allowVerbWithAsset</strong> is a method
   * to check whether this asset is subscribed to act
   * as an object with the specified verb and object.
   * @memberOf adventurejs.Asset
   * @method adventurejs.Asset#allowVerbWithAsset
   * @param {Object} options - An object of options.
   * @param {String} options.verb The name of a verb.
   * @param {Object} options.asset A game asset.
   * @param {String} [options.ov="dov"] Direct or indirect object of verb (default is "dov").
   * @returns {Boolean}
   */
  p.allowVerbWithAsset = function Asset_allowVerbWithAsset({
    verb,
    asset,
    ov = "dov",
  }) {
    ov = ov === "dov" || ov === "iov" ? ov : "dov";
    const rov = ov === "dov" ? "iov" : "dov";
    if ("string" === typeof asset) asset = this.game.getAsset(asset);
    if (!asset || !asset.id) return false;
    if ("undefined" !== typeof verb.name) {
      if ("undefined" === typeof this.game.dictionary.verbs.verb) return false;
    } else {
      if (!verb || !this.game.dictionary.verbs[verb]) return false;
      verb = this.game.dictionary.verbs[verb];
    }
    if (!this[ov][verb.name]?.enabled) return false;

    // does this asset's verb subscription explicitly allow that asset?
    if (this[ov][verb.name].with_assets.includes(asset.id)) {
      return true;
    }

    // does this asset's verb subscription  explicitly deny that asset?
    if (this[ov][verb.name].without_assets.includes(asset.id)) {
      return false;
    }

    // is that asset's class in this[ov][verb].with_classes ?
    let classes = this[ov][verb.name].with_classes;
    for (let i = 0; i < classes.length; i++) {
      if (asset instanceof adventurejs[classes[i]]) {
        return true;
      }
    }

    // is that asset's class in this[ov][verb].with_classes ?
    classes = this[ov][verb.name].without_classes;
    for (let i = 0; i < classes.length; i++) {
      if (asset instanceof adventurejs[classes[i]]) {
        return false;
      }
    }

    // is that asset reverse subscribed?
    if (asset[rov][verb.name]) {
      // does that asset's verb subscription explicitly allow this asset?
      if (asset[rov][verb.name].with_assets.includes(this.id)) {
        return true;
      }

      // does that asset's verb subscription  explicitly deny this asset?
      if (asset[rov][verb.name].without_assets.includes(this.id)) {
        return false;
      }

      // is this asset's class in that[rov][verb].with_classes ?
      classes = asset[rov][verb.name].with_classes;
      for (let i = 0; i < classes.length; i++) {
        if (this instanceof adventurejs[classes[i]]) {
          return true;
        }
      }

      classes = asset[rov][verb.name].without_classes;
      for (let i = 0; i < classes.length; i++) {
        if (this instanceof adventurejs[classes[i]]) {
          return false;
        }
      }
    }

    // if this verb allows for iov on iov,
    // (as in a case like "write 'foo' on paper with pen")
    // is that asset subscribed with the same ov?
    if (verb.allow_iov_on_iov && ov === "iov") {
      // does that asset's verb subscription explicitly allow this asset?
      if (asset[ov][verb.name].with_assets.includes(this.id)) {
        return true;
      }

      // does that asset's verb subscription  explicitly deny this asset?
      if (asset[ov][verb.name].without_assets.includes(this.id)) {
        return false;
      }

      // is this asset's class in that[ov][verb].with_classes ?
      classes = asset[ov][verb.name].with_classes;
      for (let i = 0; i < classes.length; i++) {
        if (this instanceof adventurejs[classes[i]]) {
          return true;
        }
      }

      classes = asset[ov][verb.name].without_classes;
      for (let i = 0; i < classes.length; i++) {
        if (this instanceof adventurejs[classes[i]]) {
          return false;
        }
      }
    }

    return false;
  };
})();