Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// getPrintableListOfContentsAt.js
(function () {
  /* global adventurejs A */
  var p = adventurejs.Tangible.prototype;
  /**
   * Get a printable list of assets within a specified
   * Aspect of this asset, for example
   * to append to asset description when player inputs
   * "look in this". Returns a string.
   * @memberOf adventurejs.Tangible
   * @method adventurejs.Tangible#getPrintableListOfContentsAt
   * @param {String|Object} aspect
   * @param {Object} params
   * @return {String}
   * @todo reconsider this logic because it assumes that aspect can be
   * not enabled while substance within it can be enabled
   * and that is not how I see it right now
   */
  p.getPrintableListOfContentsAt =
    function Tangible_getPrintableListOfContentsAt(aspect, params) {
      if ("string" === typeof aspect) aspect = this.aspects[aspect];
      if (!aspect) return "";

      this.game.log(
        "L1480",
        "log",
        "high",
        `[getPrintableListOfContentsAt.js] ${this.id}.getPrintableListOfContentsAt("${aspect.name}")`,
        "Tangible"
      );

      const vessel = aspect.vessel;

      // neither things nor substances enabled
      // @todo reconsider this logic
      if (
        !this.hasAspectAt(aspect.name) &&
        !this.hasVesselAtAspect(aspect.name)
      ) {
        return "";
      }

      // only things enabled but no things to list
      if (
        this.hasAspectAt(aspect.name) &&
        !this.hasVesselAtAspect(aspect.name) &&
        0 === this.getCountOfListableContentsAt(aspect.name)
      ) {
        return "";
      }

      // only substances enabled but empty
      if (
        !this.hasAspectAt(aspect.name) &&
        this.hasVesselAtAspect(aspect.name) &&
        0 >= vessel.getVolume()
      ) {
        return "";
      }

      // some valid contexts call without params
      params = params || {};
      params.caller = params.caller || "examine";

      var output = "";
      var listableObjects = [];
      var contents = A.clone.call(this.game, aspect.contents);
      var empty = true;
      var substance = "";

      if (
        this.hasVesselAtAspect(aspect.name) &&
        0 < vessel.getVolume() &&
        vessel.substance_id
      ) {
        // shouldn't be necessary
        empty = false;
        vessel.is.known = true; // ??

        substance = this.game.world[vessel.substance_id]; //.name;

        if (vessel.is.emitting) {
          output += `${substance.Name} spills out of ${this.articlename}. `;
        } else if (!params.exclude_substance) {
          // we have a lookup table for percent values but it rounds down
          // and if percent is between 0 and 0.1 we treat it like 0.1
          const percent =
            vessel.percent_of_maxvolume > 0 && vessel.percent_of_maxvolume < 0.1
              ? "0.1"
              : vessel.percent_of_maxvolume.toFixed(1);

          output += `${
            this.Articlename
          } is ${this.game.dictionary.getStringLookup(
            "substance_percents",
            percent
          )} of ${substance.name}. `;
        }
      }

      for (var i = 0; i < contents.length; i++) {
        var object = this.game.getAsset(contents[i]);

        if (object.is.hidden) {
          continue;
        }

        if (!object.is.listed) {
          continue;
        }

        listableObjects.push(object);
      }

      // if( 0 === listableObjects.length
      // && empty
      // && "examine" === params.caller )
      // {
      //   output += " There isn't anything "
      //   + this.game.dictionary.getStringLookup( "prepositions", aspect.name )
      //   + " "
      //   + this.definite_article
      //   + " "
      //   + this.name
      //   + ". ";
      // }

      if (listableObjects.length > 0) {
        const preposition = this.game.dictionary.getStringLookup(
          "prepositions",
          aspect.name
        );
        output += ` ${A.propercase(preposition)} ${
          substance ? "the " + substance.name : this.articlename
        } {we} see `;
        output += this.game.getPrintableObjectList({
          objects: listableObjects,
        });
        output += ". ";
      }
      if (output.length) output = `<span class="ajs-aspect">${output}</span>`;
      return output;
    };
})();