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

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

  var p = adventurejs.Game.prototype;

  /**
   *
   * @memberOf adventurejs.Game
   * @method adventurejs.Game#getCurrentRoomExits
   * @returns {String} A formatted list of exits.
   */
  p.getCurrentRoomExits = function Game_getCurrentRoomExits() {
    // loop through exits, which are nested objects
    // we need index/length to handle our punctuation logic
    // but we don't get that by enumerating properties
    // so instead we use object.keys
    var room = this.world[this.world._currentRoom];
    var exits = Object.keys(room.exits);
    var exitID, exit, destinationID, destination;
    var closed = [];
    var output = "";

    //console.log( exits );
    for (var i = exits.length - 1; i > -1; i--) {
      exitID = room.exits[exits[i]];
      exit = this.world[exitID]; // EtoS
      var destinationID = this.world[exit.destinationID];
      if ("undefined" === typeof destinationID) {
        exits.splice(i, 1);
      }
      if (exit.is.closed) {
        closed.push(exit);
        exits.splice(i, 1);
      }
    }

    if (!exits.length && !closed.length) {
      // no exits
      return false;
    }

    // this will need to be more sophisticated,
    // checking against visibility and knowledge
    if (exits.length) output += "$(We) can go ";

    for (var i = 0; i < exits.length; i++) {
      //console.log( "EXITS",this.world[ this.world._currentRoom ].exits[exits[i]].name );
      exitID = room.exits[exits[i]];
      exit = this.world[exitID];
      destination = this.world[exit.destinationID];

      // last item in the list
      if (exits.length > 1 && i === exits.length - 1) {
        output += "or ";
      }

      // depending on settings, we're going to want
      // a custom for_exits_list
      // just the direction name
      // or the direction name + name of destination
      var for_exits_list = A.getSAF.call(
        this.game,
        exit.descriptions.for_exits_list
      );

      if (for_exits_list) {
        output += `<span class="custom-exit-name">${for_exits_list}</span>`;
        //output += for_exits_list;
      } else {
        // get exit name and if logic permits,
        // also get preposition, article, destination name
        output += `<span class="extended-exit-name">${this.getExtendedExitName(
          exit,
          destination
        )}</span>`;
      } // else

      if (i === exits.length - 1) {
        output += ". ";
      } else {
        output += ", ";
      }
    }

    if (closed.length) {
      output += `${closed.length > 1 ? "Ways " : "A way "} `;
      for (var i = 0; i < closed.length; i++) {
        let lookup = this.game.dictionary.directionLookup[closed[i].direction];
        if (i > 0 && i < closed.length - 1) {
          output += ", ";
        }
        if (closed.length > 1 && i === closed.length - 1) {
          output += " and ";
        }
        if (i === 0 && lookup.article) output += `to ${lookup.article} `;
        output += closed[i].direction;
      }
      output += `${closed.length > 1 ? " are" : " is"} blocked. `;
    }

    return `<span class="room_exits">${output}</span>`;
  };
})();