// getAsset.js
(function () {
/* global adventurejs A */
var p = adventurejs.Game.prototype;
/**
* <strong>getAsset</strong> takes a string representing an
* asset name or id and tries to return an asset object.
* @memberOf adventurejs.Game
* @method adventurejs.Game#getAsset
* @param {String} identifier Name or ID of a game object.
* @param {Object} params Used to check for 'prefer_substance' in cases of looking for substances in containers.
* @returns {Object} A game asset.
*/
p.getAsset = function Game_getAsset(identifier, params) {
// did we get an asset?
if (identifier && "object" === typeof identifier && identifier.id) {
return identifier;
}
// did we get something other than a string?
if ("string" !== typeof identifier) {
this.game.log(
"L1051",
"warn",
"low",
"Game_getAsset received invalid identifier. ",
"Game"
);
return null;
}
identifier = identifier.toLowerCase();
if ("undefined" === typeof params) {
params = {};
}
var object;
// When parsing asset:aspect:potential we can set a parsednoun
// to a string that looks like "bowl:in:water"
// That's handled elsewhere - here we're only returning the last param
var asset_aspect_potential = identifier.split(":"); // asset:aspect:substance
let potential, lookup;
switch (asset_aspect_potential.length) {
case 3:
potential = asset_aspect_potential[2];
lookup = this.game.potential_lookup[potential];
if (lookup?.type === "platonic") {
// @TODO what do we do here with platonics?
// at the moment we're going to return the dispenser
identifier = asset_aspect_potential[0];
} else if (lookup?.type === "substance") {
// it's a substance
if (
"undefined" !== typeof params.prefer_substance &&
true === params.prefer_substance
) {
identifier = asset_aspect_potential[2];
} else {
identifier = asset_aspect_potential[0];
}
}
// const contained = this.game.world[asset_aspect_potential[2]];
// if (contained.hasClass("Platonic")) {
// // @TODO what do we do here with platonics?
// // at the moment we're going to return the dispenser
// identifier = asset_aspect_potential[0];
// } else if (contained.hasClass("Substance")) {
// // it's a substance
// if (
// "undefined" !== typeof params.prefer_substance &&
// true === params.prefer_substance
// ) {
// identifier = asset_aspect_potential[2];
// } else {
// identifier = asset_aspect_potential[0];
// }
// }
break;
case 2:
this.game.log(
"L1052",
"warn",
"high",
`Game_getAsset received invalid identifier: ${identifier}`,
"Game"
);
return null;
default:
break;
}
// what other special props might we want to return from here?
if ("player" === identifier || "_player" === identifier) {
object = this.getPlayer();
} else if ("room" === identifier || "_room" === identifier) {
object = this.getRoom();
} else {
object = this.world[A.normalize(identifier)];
}
if (typeof object !== "undefined") {
return object;
}
return null;
};
})();