Skip to content

Commit

Permalink
Error handling onFailure callback which allows commands to output err…
Browse files Browse the repository at this point in the history
…or messages to players. Useful for materialize, reload, etc.
  • Loading branch information
Yuffster committed Jan 14, 2013
1 parent dcbf7eb commit 0834f35
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 34 deletions.
12 changes: 8 additions & 4 deletions classes/Living.js
Expand Up @@ -159,22 +159,22 @@ Living = new Class({
return this.room;
},

setLocation: function(path) {
setLocation: function(path,opts) {
//If we've been passed a room object, use that.
if (path.addLiving) {
this.setRoom(path);
return true;
}
var room = this.world.getRoom(path);
var room = this.world.getRoom(path,opts);
if (room && room.addLiving) {
this.setRoom(room);
return true;
} else return false;
},

//moveTo includes tracking which players are where.
moveTo: function(path) {
if (this.setLocation(path)) {
moveTo: function(path,opts) {
if (this.setLocation(path,opts)) {
return true;
} else return false;
},
Expand Down Expand Up @@ -474,6 +474,10 @@ Living = new Class({
if (this.talkMenu) this.world.enterMenu(this.talkMenu, player, this);
},

sendError: function(e) {
this.send("ERROR: "+e+"\n\n"+e.stack);
},

/**
* Fun things to implement if we have time later.
*/
Expand Down
38 changes: 21 additions & 17 deletions classes/World.js
Expand Up @@ -139,7 +139,9 @@ World = new Class({
});
},

getRoom: function(path) {
getRoom: function(path,opts) {

opts = opts || {};

if (typeOf(path)=='object') {
path = path.to;
Expand All @@ -148,7 +150,7 @@ World = new Class({
if (path===undefined) console.trace();

if (!this.rooms[path]) {
var room = this.loadModule(this.roomPath+path);
var room = this.loadModule(this.roomPath+path, opts);
if (!room) { log_error("Room not found for "+path); }
if (room) {
this.rooms[path] = new room(this, path);
Expand All @@ -161,15 +163,16 @@ World = new Class({
} return this.rooms[path];
},

getCommand: function(command) {
getCommand: function(command,opts) {
opts = opts || {};
//No argument passed? No command for you!
if (!command) return;
var that = this;
if (!this.commands[command]) {
/* First check the world, then check the engine. */
var world = that.worldPath+this.commandPath+command;
var engine = that.enginePath+this.commandPath+command;
var com = this.loadModule([world,engine], {rootPath:true});
var com = this.loadModule([world,engine], {rootPath:true,onFailure:opts.onFailure});
var file_path = com.file_path;
if (!com) {
that.commands[command] = false;
Expand Down Expand Up @@ -203,11 +206,11 @@ World = new Class({
}
},

loadItem: function(path) {
loadItem: function(path,opts) {
if (!path) { return; }
if (!this.items[path]) {
var file = this.itemPath+path;
var obj = this.loadModule(file);
var obj = this.loadModule(file,opts);
if (!obj) { return false; }
this.items[path] = obj;
}
Expand Down Expand Up @@ -276,7 +279,7 @@ World = new Class({
},

loadModule: function(path, opts) {

/**
* If an array of paths is passed, each path will be checked one after
* another until either one succeeds or they all fail.
Expand Down Expand Up @@ -306,7 +309,9 @@ World = new Class({
} catch (e) {
if (fallbacks.length) {
return this.loadModule(fallbacks, opts);
} return false;
}
if (opts.onFailure) opts.onFailure(e);
return false;
}

},
Expand All @@ -319,7 +324,6 @@ World = new Class({

reloadEngineModule: function(file_path) {
var path = this.enginePath+file_path,bkup;
console.log(path.color('green'));
if (require.cache[path+'.js']) {
bkup = require.cache[path+'.js'];
delete(require.cache[path+'.js']);
Expand All @@ -333,24 +337,24 @@ World = new Class({
}
},

reloadCommand: function(command) {
com = this.getCommand(command);
reloadCommand: function(command, opts) {
com = this.getCommand(command, opts);
if (!com) return;
delete(this.commands[command]);
delete(require.cache[com.file_path+'.js']);
return true;
},

reloadItem: function(object) {
reloadItem: function(object, opts) {
delete(this.items[object.game_path]);
this.reloadModule(object.file_path);
this.reloadModule(object.file_path, opts);
return this.loadItem(object.game_path);
},

reloadRoom: function(object) {
reloadRoom: function(object, opts) {
var dislodgedPlayers = object.getPlayers();
delete(this.rooms[object.game_path]);
var success = this.reloadModule(object.file_path),
var success = this.reloadModule(object.file_path, opts),
newRoom = this.getRoom(object.game_path);
if (!success) {
//Let the caller know to send a message to the players about why
Expand All @@ -364,12 +368,12 @@ World = new Class({
}
},

reloadNPC: function(object) {
reloadNPC: function(object, opts) {
var success = this.reloadModule(object.file_path);
if (!success) return false;
delete(this.npcs[object.game_path]);
var room = object.get('room'),
replacement = this.loadNPC(object.game_path);
replacement = this.loadNPC(object.game_path, opts);
if (replacement) {
room.removeLiving(object);
room.addLiving(replacement);
Expand Down
7 changes: 6 additions & 1 deletion commands/materialize.js
Expand Up @@ -3,7 +3,12 @@ module.exports = new Class({
Extends: Command,

execute: function(path) {
var item = this.world.loadItem(path);
var player = this,
item = this.world.loadItem(path, {
onFailure: function(e) {
player.sendError(e);
}
});
if (!item) return "Invalid path: "+path;
this.emit("%You pull%s "+item.get('indefinite')+' out of thin air.');
this.addItem(item);
Expand Down
9 changes: 7 additions & 2 deletions commands/move.js
Expand Up @@ -3,9 +3,14 @@ module.exports = new Class({
Extends: Command,

execute: function(direction) {
var room = this.getRoom();
var room = this.getRoom(),
player = this;
if (room && room.exits[direction]) {
var success = this.moveTo(room.exits[direction]);
var success = this.moveTo(room.exits[direction], {
onFailure: function(e) {
player.sendError(e);
}
});
if (!success) return "You can't go that way."
this.emit("%You leave%s "+direction+".");
this.force('look');
Expand Down
42 changes: 32 additions & 10 deletions commands/reload.js
Expand Up @@ -15,7 +15,10 @@ module.exports = new Class({
//We need to know if this object is in the room or in an inventory.
var holder = object.getContainer(),
//Attempt to get a new item to replace it.
replacement = this.world.reloadItem(object),
error = false,
replacement = this.world.reloadItem(object, {
onFailure: function(e) { holder.sendError(e); }
}),
//We'll store some text here to append to the destruction message.
andThen;

Expand All @@ -29,10 +32,8 @@ module.exports = new Class({
} else {
andThen = "A spark ignites in its place for a moment before "+
"fading away with a disappointing fizz.";
if (holder.send) holder.send("The object couldn't be reloaded. "+
"Please check for errors.");
}

this.emit("%You wave%s %your hands about in an arcane manner.");

if (holder instanceof Living) {
Expand All @@ -43,11 +44,22 @@ module.exports = new Class({
"smoke. "+andThen);
}

if (error) {
if (holder.send) {
holder.send("The object couldn't be reloaded. "+
"Please check for errors.");
holder.send(error);
}
}

},

reload_room: function() {

var success = this.world.reloadRoom(this.get('room')),
var player = this,
success = this.world.reloadRoom(this.get('room'), {
onFailure: function(e) { player.sendError(e); }
}),
andThen;

if (success) {
Expand All @@ -56,7 +68,7 @@ module.exports = new Class({
} else {
andThen = "Nothing seems to happen.";
}

this.emit("%You snap%s %your fingers. "+andThen);

},
Expand All @@ -79,7 +91,10 @@ module.exports = new Class({
return false;
}

var replacement = this.world.reloadNPC(l);
var player = this,
replacement = this.world.reloadNPC(l, {
onFailure: function(e) { player.sendError(e); }
});

if (replacement) {
replacement.emit(
Expand All @@ -96,12 +111,19 @@ module.exports = new Class({
},

reload_something: function(words) {
var andThen = "Nothing happens.", success = false;
if (this.world.reloadEngineModule(words)) {

var player = this,
andThen = "Nothing happens.",
success = false,
opts = {
onFailure: function(e) { player.sendError(e); }
};

if (this.world.reloadEngineModule(words, opts)) {
andThen = "The core mechanics of the universe seem to be "+
"different somehow.";
success = true;
} else if (this.world.reloadCommand(words)) {
} else if (this.world.reloadCommand(words, opts)) {
andThen = "The rules of '"+words+"' seem to have changed.";
this.send(andThen);
success = true;
Expand Down

0 comments on commit 0834f35

Please sign in to comment.