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

(function () {
  /* global AdventureJS A */

  var p = AdventureJS.Game.prototype;

  /**
   *
   * @method AdventureJS.Game#getPrintableObjectList
   * @memberOf AdventureJS.Game
   * @param {Object} objects An array of object ID strings.
   * @param {Object} exclusions An array of of object ID strings to exclude.
   * @returns {String} A formatted string.
   */
  p.getPrintableObjectList = function Game_getPrintableObjectList({
    article,
    objects,
    exclusions,
  }) {
    this.game.log(
      "L1074",
      "log",
      "high",
      `[getPrintableObjectList.js] getPrintableObjectList() receive:\n${objects.map((a) => a.id).join(", ")}`,
      "Game"
    );
    let list = "";
    let definite = false;
    let fungibles = {};
    let assets = {};
    let names = [];

    if ("definite" === article) {
      definite = true;
    }

    // remove any exclusions from objects list
    if ("undefined" !== typeof exclusions) {
      // convert params string to array if needed
      if ("string" === typeof exclusions) {
        exclusions = [exclusions];
      }
      objects = objects.filter(function (objects) {
        return exclusions.indexOf(objects) < 0;
      });
    }

    // convert object IDs to assets
    // and note any fungibles
    objects.map((object) => {
      const asset = this.getAsset(object);
      if (asset.is.fungible) {
        fungibles[asset.class] = fungibles[asset.class] || [];
        fungibles[asset.class].push(asset);
      }
      assets[asset.id] = asset;
      // return asset;
    });

    // if there are multiples of any type of fungibles,
    // remove them from the assets list and write a
    // collective name to names
    Object.entries(fungibles).forEach((collection) => {
      if (Object.entries(collection[1]).length > 1) {
        let name = "";
        Object.entries(collection[1]).forEach((item) => {
          const asset = item[1];
          name = asset.plural;
          if (asset.print_bold) name = `<strong>${name}</strong>`;
          if (asset.print_italic) name = `<em>${name}</em>`;
          if (asset.print_style) {
            name = `<span class="${asset.print_style}">${name}</span>`;
          }
          delete assets[item[1].id];
        });
        let article = "a";
        switch (Object.entries(collection[1]).length) {
          case 2:
            article = "a couple of";
            break;
          case 3:
            article = "several";
            break;
          default:
            article = "some";
            break;
        }
        names.push(`${article} ${name}`);
      }
    });

    Object.entries(assets).forEach((item) => {
      const asset = item[1];
      let name = definite ? asset.definite_name : asset.indefinite_name;
      if (asset.print_bold) name = `<strong>${name}</strong>`;
      if (asset.print_italic) name = `<em>${name}</em>`;
      if (asset.print_style) {
        name = `<span class="${asset.print_style}">${name}</span>`;
      }
      names.push(name);
    });

    for (var i = 0; i < names.length; i++) {
      if (i > 0 && i < names.length - 1) {
        list += ", ";
      }
      if (names.length > 1 && i === names.length - 1) {
        list += " and ";
      }
      list += names[i];
    }

    return list;
  };
})();