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.
   * @memberOf adventurejs.Game
   * @method adventurejs.Game#getDescription
   * @param {Object} params
   * @return {String}
   */
  p.getDescription = function Game_getDescription(params) {
    var asset = params.asset;
    var identifier = params.identifier;
    var description;
    if (!identifier || identifier === "at") identifier = "look";

    if ("string" === typeof asset) {
      asset = this.game.getAsset(asset);
      if (!asset) return "";
    }

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

    if (identifier === "at") {
      identifier = "look";
    }

    // 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);
      }

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

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