Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// validateVerbConnections.js
(function () {
  /*global adventurejs A*/
  var p = adventurejs.Asset.prototype;
  /**
   * Validate any connections specified in the game file.
   * Assets can subscribe to verbs with
   * <code class="property">dov[verb]</code>
   * and some verbs can make connections between assets
   * that they act upon. Connections are stored in
   * <code class="property">asset.is.connected_by.verbname.to_iov["array of asset ids"]</code>.
   * Authors can preset connections in their game file,
   * so we validate any preset connections here.
   * @memberOf adventurejs.Asset
   * @method adventurejs.Asset#validateVerbConnections
   */
  p.validateVerbConnections = function Asset_validateVerbConnections() {
    if (!this.is.connected_by) return;
    var objects = ["dov", "iov"];

    let verb_keys = Object.keys(this.is.connected_by);
    for (let i = 0; i < verb_keys.length; i++) {
      let verb_name = verb_keys[i];
      let verb = this.game.dictionary.verbs[verb_name];
      if (!verb) {
        var msg = `${this.id}.is.connected_by.${verb_name} doesn't appear to be a valid verb. `;
        this.game.log("L1035", "warn", 0, msg, "Asset");
        continue;
      }
      if (!verb.makes_connections) {
        var msg = `Found ${this.id}.is.connected_by.${verb_name}, but ${verb_name}.makes_connections is false. `;
        this.game.log("L1036", "warn", 0, msg, "Asset");
        continue;
      }

      for (let obj = 0; obj < objects.length; obj++) {
        let ov = objects[obj];
        let assets = this.is.connected_by[verb_name]["to_" + ov];
        if (!assets) {
          //var msg = `Found ${this.id}.is.connected_by.${verb_name}, but ${this.id}.is.connected_by.${verb_name}.${"to_" + ov} is unset. `;
          //this.game.log("warn", 0, msg, "Asset");
          continue;
        }
        for (let a = 0; a < assets.length; a++) {
          let asset_name = assets[a];
          if (asset_name === null) continue; // null is a legit option, doesn't need additional processing
          let asset_id = A.serialize(asset_name);
          assets[a] = asset_id; // author will have used name - write id back to array
          let asset = this.game.getAsset(asset_id);
          if (!asset) {
            var msg = `Found ${this.id}.is.connected_by.${verb_name}.to_${ov}${asset_name}, but ${asset_name} doesn't appear to be a valid asset. `;
            this.game.log("L1037", "warn", 0, msg, "Asset");
            continue;
          }
          let opp_ov = ov === "dov" ? "iov" : "dov";
          if (!asset.is.connected_by[verb_name]) {
            asset.is.connected_by[verb_name] = {};
          }
          if (!asset.is.connected_by[verb_name]["to_" + opp_ov]) {
            asset.is.connected_by[verb_name]["to_" + opp_ov] = [];
          }
          if (
            !asset.is.connected_by[verb_name]["to_" + opp_ov].includes(
              this.name
            ) &&
            !asset.is.connected_by[verb_name]["to_" + opp_ov].includes(this.id)
          ) {
            asset.is.connected_by[verb_name]["to_" + opp_ov].push(this.id);
          }
        }
        // END TODO
      }
    }
  }; // validate connections
})();