Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
// ParsedNoun.js
(function () {
  /*global adventurejs A*/
  "use strict";

  /**
   * @class adventurejs.ParsedNoun
   * @param {Object|adventurejs.Asset} asset
   * @ajsinternal
   * @ajsnavheading FrameworkReference
   * @summary Special class used to store metadata about an asset
   * and lists of all possible matches for that asset.
   * @classdesc
   * <p>
   * <strong>ParsedNoun</strong> is a special class constructed by
   * {@link adventurejs.Parser#parseNoun|Parser.parseNoun()},
   * and used to store metadata about an asset and lists of all
   * possible matches for that asset. This is an internal class
   * that authors should not need to construct.
   * </p>
   */
  class ParsedNoun {
    constructor(asset) {
      // @TODO should be able to do this but ParsedNoun has no context for game
      // if( asset && "string" === typeof asset )
      // {
      //   asset = this.game.getAsset( asset );
      // }
      if (!asset) asset = null;

      /**
       *
       * @var {String} adventurejs.ParsedNoun#
       * @default
       */
      this.class = "ParsedNoun";

      /**
       *
       * @var {Boolean} adventurejs.ParsedNoun#exclude
       * @default false
       */
      this.exclude = false;

      /**
       *
       * @var {String} adventurejs.ParsedNoun#plural
       * @default ""
       */
      this.plural = "";

      /**
       *
       * @var {Boolean} adventurejs.ParsedNoun#isPlural
       * @default false
       */
      this.isPlural = false;

      /**
       *
       * @var {Boolean} adventurejs.ParsedNoun#isGroup
       * @default false
       */
      this.isGroup = false;

      /**
       *
       * @var {String} adventurejs.ParsedNoun#singular
       * @default ""
       */
      this.singular = "";

      /**
       *
       * @var {String} adventurejs.ParsedNoun#type
       * @default ""
       */
      this.type = "";

      /**
       *
       * @var {String} adventurejs.ParsedNoun#input
       * @default ""
       */
      this.input = asset === null ? "" : asset.name;

      /**
       *
       * @var {String} adventurejs.ParsedNoun#serialized_input
       * @default ""
       */
      this.serialized_input = asset === null ? "" : asset.id;

      /**
       *
       * @var {String} adventurejs.ParsedNoun#deserialized_input
       * @default ""
       */
      this.deserialized_input = asset === null ? "" : asset.name;

      /**
       *
       * @var {String} adventurejs.ParsedNoun#qualified_object_id
       * @default
       */
      this.qualified_object_id = asset === null ? undefined : asset.id;

      /**
       *
       * @var {Object} adventurejs.ParsedNoun#matches
       * @default { all:[], direction:false, qualified:[], qualifiedIndex:0, unambiguous:""}
       */
      this.matches = {
        all: [],
        direction: false,
        substance: false,
        qualified: [],

        // qualifiedIndex exists because there is a scenario where
        // a verb can act without disambiguation upon one asset
        // out of a group returned by the parser
        qualifiedIndex: 0,

        // we set unamiguous but never really rely on it due to the
        // scenario described above - safer to use ParsedNoun.asset_id
        unambiguous: "",
      };

      if (null !== asset) {
        this.matches.all = [asset.id];
        this.matches.qualified = [asset.id];
        this.matches.qualifiedIndex = 0;
        this.matches.unambiguous = asset.id;
      }

      /**
       *
       * @var {String} adventurejs.ParsedNoun#preposition
       * @default ""
       */
      this.preposition = "";

      /**
       *
       * @var {Boolean} adventurejs.ParsedNoun#is_assumed
       * @default false
       */
      this.is_assumed = false;

      return this;
    }

    /**
     * Getter function looks for a qualified object id, and if it
     * doesn't find one, it returns the first qualified match.
     * @var {Getter} adventurejs.ParsedNoun#object_id
     * @returns {String}
     */
    get object_id() {
      if ("undefined" !== typeof this.qualified_object_id) {
        return this.qualified_object_id;
      } else if (this.matches.qualified.length > 0) {
        return this.matches.qualified[this.matches.qualifiedIndex];
      } else return null;
    }

    /**
     * Provides a chainable shortcut method for setting a number of properties on the instance.
     * @method adventurejs.Atom#set
     * @memberOf adventurejs.Atom
     * @param {Object} props A generic object containing properties to copy to the Object instance.
     * @returns {Object} Returns the instance the method is called on (useful for chaining calls.)
     * @chainable
     */
    set(props) {
      return A.deepSet.call(this.game, props, this);
    }
  }

  adventurejs.ParsedNoun = ParsedNoun;
})();