// doVerbAction.js
(function () {
/* global adventurejs A */
var p = adventurejs.Asset.prototype;
/**
* Check if this asset has a general verb action or reaction
* for the specified event,
* or if the asset has an action for the specific asset.
* If either form
* is found, pass it to getStringOrArrayOrFunction and
* return the results. If the result returns false or null
* it will terminate the calling verb's default behavior.
* @memberOf adventurejs.Asset
* @method adventurejs.Asset#doVerbAction
* @param {string} action
* @param {string} asset2 We use asset.name here instead of asset.id
* in support of authors, because we're never asking them to deal in IDs,
* only names. Hooks will only be defined by authors, so we let them use
* asset.name as their identifier. We do however make an effort to see if
* an id has been passed instead of a name, because Ivan.
* @param {string} asset3
* @param {object} params Arbitrary parameter object.
* @return {Boolean}
*/
p.doVerbAction = function Asset_doVerbAction({
action,
asset2,
asset3,
params = {},
type = "Hook",
} = {}) {
this.game.log(
"L1476",
"log",
"high",
`[doVerbAction.js] ${this.id}.${action}()`,
type
);
this.game.debug(
`D1415 | ${type} | <span class="ajs-actor">${this.id}.</span><span class="ajs-action">${action}</span><span class="ajs-action">(</span><span class="ajs-target">${""}</span><span class="ajs-action">)</span>`
);
if (asset2) {
this.game.log(
"L1456",
"log",
"high",
`[doVerbAction.js] ${this.id}.${action}(${asset2.id ? asset2.id : asset2})`,
type
);
this.game.debug(
`D1420 | ${type} | <span class="ajs-actor">${this.id}.</span><span class="ajs-action">${action}</span><span class="ajs-action">(</span><span class="ajs-target">${asset2.id ? asset2.id : asset2}</span><span class="ajs-action">)</span>`
);
}
if (asset3) {
this.game.log(
"L1459",
"log",
"high",
`[doVerbAction.js] ${this.id}.${action}(${asset3.id ? asset3.id : asset3})`,
type
);
this.game.debug(
`D1442 | ${type} | <span class="ajs-actor">${this.id}.</span><span class="ajs-action">${action}</span><span class="ajs-action">(</span><span class="ajs-target">${asset3.id ? asset3.id : asset3}</span><span class="ajs-action">)</span>`
);
}
function camelToSnake(str) {
return str.replace(/([A-Z])/g, "_$1").toLowerCase();
}
if (!action) return;
const action_ = camelToSnake(action);
if (!this[action] && !this[action_]) return;
const input = this.game.getInput();
if (asset2) {
// if an asset was passed, disregard assets retrieved from input
asset2 = asset2.class ? asset2 : this.game.getAsset(asset2);
if (asset3) {
asset3 = asset3.class ? asset3 : this.game.getAsset(asset3);
}
} else {
asset2 = input.getAsset(2);
asset3 = input.getAsset(3);
if (!asset2) asset2 = this.game.getPlayer();
}
this.game.log(
"L1038",
"log",
"high",
`[doVerbAction.js] ${this.id}.doVerbAction > ${action} ${asset2 ? asset2.id : ""} ${asset3 ? asset3.id : ""}`,
"OnAction"
);
this.game.debug(
`D1383 | VerbAction | ${this.id}.${action} ${asset2 ? asset2.id : ""} ${asset3 ? asset3.id : ""}`
);
// look for a generic hook method
if ("function" === typeof this[action]) {
return this[action].call(this, params);
}
// if no method was found, see if there's an alt string
if (
"string" === typeof this[action_] ||
Array.isArray(this[action_]) ||
"function" === typeof this[action_]
) {
return A.getSAF.call(this.game, this[action_]);
}
// see if hook is an object containing other properties,
// where we expect to find asset names
if (this[action] && "function" === typeof this[action][asset2.name]) {
input.appendTurn(this[action][asset2.name].call(this, params));
return;
}
// if no method was found, see if there's an alt string
if (
this[action_] &&
asset2 &&
this[action_][asset2.name] &&
("string" === typeof this[action_][asset2.name] ||
Array.isArray(this[action_][asset2.name]) ||
"function" === typeof this[action_][asset2.name])
) {
// return A.getSAF.call(this.game, this[action_][asset2.name]);
input.appendTurn(A.getSAF.call(this.game, this[action_][asset2.name]));
return;
}
if (
this[action] &&
asset2 &&
this[action][asset2.name] &&
asset3 &&
"function" === typeof this[action][asset2.name][asset3.name]
) {
return this[action][asset2.name][asset3.name].call(this, params);
}
// if no method was found, see if there's an alt string
if (
this[action_] &&
asset2 &&
this[action_][asset2.name] &&
asset3 &&
("string" === typeof this[action_][asset2.name][asset3.name] ||
Array.isArray(this[action_][asset2.name][asset3.name]) ||
"function" === typeof this[action_][asset2.name][asset3.name])
) {
input.appendTurn(
A.getSAF.call(this.game, this[action_][asset2.name][asset3.name])
);
return;
}
return;
};
})();