Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// getDescription.js

(function () {
  /* global adventurejs A */
  var p = adventurejs.Game.prototype;
  /**
   * Get a description, such as "look in book", where
   * "in" has been defined as a key at
   * asset.descriptions.in.
   * "look" is always the default description.
   * <br><br>
   * This is scoped to Game rather than Asset because there are
   * cases that handle simple unclassed objects with description
   * properties, notably: properties of room_scenery.
   * @memberOf adventurejs.Game
   * @method adventurejs.Game#getDescription
   * @param {String} identifier
   * @return {String|Boolean}
   */
  p.getDescription = function Game_getDescription({
    asset,
    identifier = "look",
  }) {
    let description;
    if (identifier === "at") identifier = "look";

    if (asset.proxy) {
      let proxy = this.game.getAsset(asset.proxy);
      let proxy_description = proxy.getDescription(identifier);
      if (proxy_description) return proxy_description;
    }

    // does object have descriptions at all?
    if (!asset.descriptions) {
      return asset.description || "";
    }

    if (asset.descriptions[identifier]) {
      description = asset.descriptions[identifier];
    } else if (
      asset.descriptions["look"] &&
      asset.descriptions["look"][identifier]
    ) {
      description = asset.descriptions["look"][identifier];
    } else if (asset.descriptions["look"]) {
      description = asset.descriptions["look"];
    } else if (!asset.descriptions) {
      description = asset.description;
    }

    if (description) {
      if (
        Array.isArray(description) ||
        "string" === typeof description ||
        "function" === typeof description
      ) {
        return A.getSAF.call(this.game, description, asset);
      }

      //
      if ("object" === typeof description && description.default) {
        return A.getSAF.call(this.game, description.default, asset);
      }
    }

    // no description found
    return "";
  };
})();