Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
// get.js

(function () {
  /*global adventurejs A*/
  "use strict";

  var p = adventurejs.Game.prototype;

  /**
   * <strong>getAsset</strong> or shortcut <strong>get</strong>
   * takes a string representing an asset name or id and tries
   * to return an asset object.
   * @memberOf adventurejs.Game
   * @method adventurejs.Game#getAsset
   * @param {String} identifier Name or ID of a game object.
   * @param {Object} params Used to check for 'prefer_substance' in cases of looking for substances in containers.
   * @returns {String} An object ID.
   */
  p.getAsset = p.$ = function Game_getAsset(identifier, params) {
    // did we get an asset?
    if (identifier && "object" === typeof identifier && identifier.id) {
      return identifier;
    }

    // did we get something other than a string?
    if ("string" !== typeof identifier) {
      this.game.log(
        "warn",
        "low",
        "Game_getAsset received invalid identifier. ",
        "Game"
      );
      return false;
    }

    if ("undefined" === typeof params) {
      params = {};
    }

    var object;

    // When parsing asset:aspect:substance we can set a parsednoun
    // to a string that looks like "bowl:in:water"
    // That's handled elsewhere - here we're only returning the last param
    var asset_aspect_substance = identifier.split(":"); // asset:aspect:substance
    switch (asset_aspect_substance.length) {
      case 3:
        if (
          "undefined" !== typeof params.prefer_substance &&
          true === params.prefer_substance
        ) {
          identifier = asset_aspect_substance[2];
        } else {
          identifier = asset_aspect_substance[0];
        }
        break;
      case 2:
        console.warn("game.getAsset received " + identifier);
        return null;
      default:
        break;
    }

    // what other special props might we want to return from here?

    if ("player" === identifier) {
      object = this.world.getPlayer();
    } else if ("currentRoom" === identifier) {
      object = this.world.getCurrentRoom();
    } else {
      object = this.world[A.serialize(identifier)];
    }

    if (typeof object !== "undefined") {
      return object;
    }

    return false;
  };
})();