// setVerbSubscriptionsWithConnection.js
(function() {
/*global adventurejs A*/
"use strict";
var p = adventurejs.Asset.prototype;
/**
* Make any connections specified in the game file.
* Assets can subscribe to verbs with
* <code class="property">dov[verb]</code>
* and some verbs make connections between assets
* that they act upon, which are recorded in
* <code class="property">dov[verb].with_params.connections</code>.
* Authors can preset connections in their game file, and
* in case they only set one of two connected assets, we want
* to ensure that both assets are marked as connected,
* so we check that here.
* @memberOf adventurejs.Asset
* @method adventurejs.Asset#setVerbSubscriptionsWithConnection
*/
p.setVerbSubscriptionsWithConnection = function Asset_setVerbSubscriptionsWithConnection()
{
//console.warn(this.id,'setVerbSubscriptionsWithConnection');
let objects = [
['dov','iov'],
['iov','dov']
];
for(var pair = 0; pair < objects.length; pair++ ) {
let object = objects[pair];
// check all this asset's direct/indirect verb subscriptions
for(var verb in this[object[0]])
{
let averb = this[object[0]][verb];
// does this verb make connections?
if( averb?.with_params?.connections?.length )
{
// does this asset have connections set from game file?
for( var i = 0; i < averb.with_params.connections.length; i++ )
{
// author probably used names, not ids, so serialize
averb.with_params.connections[i] = A.serialize(averb.with_params.connections[i]);
// get connected asset
let connection = this.game.getAsset(averb.with_params.connections[i]);
if(connection)
{
// verify that connection is also subscribed to verb
if(!connection[object[1]][verb])
{
// if not, subscribe it
if( object[1] === 'dov' ) connection.setDOV(verb);
else connection.setIOV(verb);
}
// is this id not found in connection?
if(-1 === connection[object[1]][verb].with_params.connections.indexOf(this.id) )
{
// player would've set name, not id, so also check for that
let namepos = connection[object[1]][verb].with_params.connections.indexOf(this.name);
if(-1 !== namepos )
{
// replace this name in connection with this id
connection[object[1]][verb].with_params.connections[namepos] = this.id;
}
else
{
// add this id to connection
connection[object[1]][verb].with_params.connections.push(this.id);
}
}
}
}
}
}
}
} // set connections
}());