Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
// setVerbSubscriptionsWithAssets.js
(function() {
	/*global adventurejs A*/ 
  "use strict";
  var p = adventurejs.Asset.prototype;	
  /**
   * Link assets that connect via verb subscriptions.
   * For example, imagine "hit target with sword", 
   * where we have 
   * target.dov.hit.with_assets:["sword"]
   * This pass will set 
   * sword.iov.hit.with_assets:["target"]
   * to ensure that both assets are in sync
   * and either can be queried.
   * @memberOf adventurejs.Asset
	 * @method adventurejs.Asset#setVerbSubscriptionsWithAssets
   * @param {String} description
   * @return {String}
   */
  p.setVerbSubscriptionsWithAssets = function Asset_setVerbSubscriptionsWithAssets()
  {
    var object = ['dov','iov'];
    for(var of = 0; of < object.length; of++) {
      var oppo = object[of===0?1:0];
      for( var verb in this[object[of]] )
      {
        if(this[object[of]][verb]?.with_assets?.length)
        {
          var with_assets = this[object[of]][verb].with_assets;
          for(var v = 0; v < with_assets.length; v++ )
          {
            //if(!with_assets[v].length) continue; // empty string?
            var asset = this.game.getAsset( with_assets[v] );
            if(!asset) { 
              /*throw error*/ 
              this.game.log( "error", 0, `${this.id}.${object[of]}.${verb}.with_assets["${with_assets[v]}"] is an invalid asset name/id` );
              continue; 
            }
            if( !this.id ) continue; // not sure why we'd hit this, but must have done
            if( !asset[oppo][verb] ) 
            {
              // if opposing asset hasn't got a verb subscription, set that now
              asset.setVerbSubscription( oppo, verb, { with_assets: [ this.id ] } );
            }
            else if( -1 === asset[oppo][verb].with_assets.indexOf(this.id) ) 
            {
              // otherwise just make sure my id is in its with_assets
              asset[oppo][verb].with_assets.push(this.id);
            }
          }
        }
      }
    }
  }
}());