// getRoomExits.js
(function () {
/* global adventurejs A */
var p = adventurejs.Game.prototype;
/**
*
* @memberOf adventurejs.Game
* @method adventurejs.Game#getRoomExits
* @returns {String} A formatted list of exits.
* @param {Object} room A room to get exits of. Defaults to current room.
*/
p.getRoomExits = function Game_getRoomExits(
room = this.world[this.world._room]
) {
// 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 exits = Object.keys(room.exits);
var exitID, exit, destinationID, destination;
var closed = [];
var output = "";
if (room.hasDescription("exits")) {
return `<span class="ajs-room-exits">${A.getSAF.call(
this.game,
room.descriptions.exits
)}</span>`;
}
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++) {
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="ajs-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="ajs-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.direction_lookup[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="ajs-room-exits">${output}</span>`;
};
})();