Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Some refactoring, and changes to exit listings.

  • Loading branch information...
commit c9849e21e71dcfa2436173fbd4be830b854c4cb6 1 parent 044ff73
@supernovus authored
Showing with 108 additions and 42 deletions.
  1. +21 −2 game.json
  2. +14 −15 lib/Client.js
  3. +67 −22 lib/Room.js
  4. +3 −3 lib/World.js
  5. +3 −0  nodemoo.js
View
23 game.json
@@ -4,14 +4,33 @@
"rooms" : {
"lobby" : {
"name" : "The Lobby",
- "desc" : "Not much to see here yet. It's a big, mostly empty room.",
- "exits" : { "north" : "bathroom" }
+ "desc" : "Not much to see here yet. It's a big, mostly empty room.\nThere are several doors though.",
+ "exits" : {
+ "north" : "bathroom",
+ "east" : "kitchen",
+ "west" : "dining"
+ }
},
"bathroom" : {
"name" : "The Bathroom",
"desc" : "A filthy bathroom, this place stinks!",
"exits" : { "south" : "lobby" },
"mixin" : [ "Bathroom" ]
+ },
+ "kitchen" : {
+ "name" : "The Kitchen",
+ "desc" : "A fairly clean kitchen, with a nice stove.",
+ "exits" : { "west" : "lobby" }
+ },
+ "dining" : {
+ "name" : "The Dining Room",
+ "desc" : "A very large dining room, with a huge table in the middle.",
+ "exits" : { "east" : "lobby", "north" : "library" }
+ },
+ "library" : {
+ "name" : "The Library",
+ "desc" : "A massive library filled with thousands of books.",
+ "exits" : { "south" : "dining" }
}
}
}
View
29 lib/Client.js
@@ -1,16 +1,15 @@
var nl = "\r\n";
-function Client (socket, world, username) {
+function Client (socket, username) {
this.socket = socket;
this.connected = true;
- this.world = world;
this.name = username;
this.visited = {}; // rooms we've been to.
this.inventory = []; // our stuff.
}
Client.prototype.gotoRoom = function (roomid) {
- var room = this.world.getRoom(roomid);
+ var room = world.getRoom(roomid);
if (room) {
this.room = room;
if (!this.visited[roomid])
@@ -38,11 +37,11 @@ Client.prototype.showPrompt = function () {
Client.prototype.say = function (message) {
var umsg = "You say \""+message+"\"";
var omsg = this.name + " says \"" + message + "\"";
- this.world.message(this, umsg, omsg, this.room);
+ world.message(this, umsg, omsg, this.room);
}
Client.prototype.whisper = function (to, message) {
- var target = this.world.getUser(to);
+ var target = world.getUser(to);
if (target)
{ this.msg("You whisper \""+message+"\" to "+to);
target.msg(nl+this.name + " whispers \""+message+"\" to you.");
@@ -55,11 +54,11 @@ Client.prototype.whisper = function (to, message) {
Client.prototype.emote = function (message) {
var umsg = "* "+message;
var omsg = this.name + " " + message;
- this.world.message(this, umsg, omsg, this.room);
+ world.message(this, umsg, omsg, this.room);
}
Client.prototype.rename = function (newname) {
- if (this.world.userExists(newname)) {
+ if (world.userExists(newname)) {
this.msg("Sorry, that name has already been taken.");
}
else {
@@ -67,14 +66,14 @@ Client.prototype.rename = function (newname) {
this.name = newname;
var umsg = "You have changed your name to: " + newname;
var omsg = oldname + " has changed their name to: " + newname;
- this.world.message(this, umsg, omsg);
+ world.message(this, umsg, omsg);
}
}
// Commands for all clients.
Client.prototype.parse = function (data) {
var matches;
- var str = this.world.dataString(data);
+ var str = world.dataString(data);
if (str.match(/^exit$/)) return this.logout();
else if (this.room.parse(this, str)) true; // We found a Room call.
@@ -90,16 +89,16 @@ Client.prototype.parse = function (data) {
else if (matches = str.match(/^call me ([\w\s'\.]+)/)) {
this.rename(matches[1]);
}
- else if (matches = str.match(/^make room (\w+) (called)? ([\w\s'\.]+)?/)) {
+ else if (matches = str.match(/^make room (\w+)\s?(?:called)?\s?([\w\s'\.]+)?/)) {
var roomid = matches[1];
- var roomname = matches[3];
+ var roomname = matches[2];
var init = {};
if (roomname)
init.name = roomname;
- if (this.world.roomExists(roomid))
+ if (world.roomExists(roomid))
this.msg("Sorry, a room called "+roomid+" already exists.");
else {
- this.world.addRoom(roomid, init);
+ world.addRoom(roomid, init);
this.gotoRoom(roomid);
}
}
@@ -111,8 +110,8 @@ Client.prototype.parse = function (data) {
this.showPrompt();
}
-exports.newClient = function (socket, world, username) {
- return new Client(socket, world, username);
+exports.newClient = function (socket, username) {
+ return new Client(socket, username);
}
// End of library.
View
89 lib/Room.js
@@ -13,10 +13,9 @@ var opposites = {
'southeast' : 'northwest'
};
-function Room (id, world, init) {
+function Room (id, init) {
// Required items.
this.id = id;
- this.world = world;
// The room's name.
if (init.name)
@@ -26,7 +25,7 @@ function Room (id, world, init) {
// The room's description.
if (init.desc)
- this.description = init.desc;
+ this.desc = init.desc;
// Any exits from this room.
if (init.exits)
@@ -49,7 +48,7 @@ Room.prototype.rename = function (newname) {
}
Room.prototype.describe = function (newdesc) {
- this.description = newdesc;
+ this.desc = newdesc;
}
// Commands applicable to Rooms in general.
@@ -107,7 +106,7 @@ Room.prototype.parse = function (client, str) {
this.mixin(matches[1]);
return true;
}
- else if (str.match(/^show parsers$/)) {
+ else if (str.match(/^show room parsers$/)) {
client.msg("Extra parsers in this room:");
for (var p in this.parsers) {
var parser = this.parsers[p];
@@ -128,16 +127,23 @@ Room.prototype.parse = function (client, str) {
return false;
}
-// Feel free to override the prototype for look().
-// The default just returns the 'description' member.
+// Look at a room, list it's contents,
+// exits and who is in it.
Room.prototype.look = function (client) {
var desc;
- if (this.description)
- desc = this.description;
+ if (this.desc)
+ desc = this.desc;
else
desc = "You are in " + this.name;
client.msg(desc);
- // Okay, now list the contents of the room, as long as they aren't implicit.
+
+ this.listContents(client);
+ this.listClients(client);
+ this.listExits(client);
+}
+
+// List the contents of the room, as long as they aren't implicit.
+Room.prototype.listContents = function (client) {
if (this.contents) {
for (var c in this.contents) {
var item = this.contents[c];
@@ -148,21 +154,53 @@ Room.prototype.look = function (client) {
client.msg("There is a "+item.name+" here.");
}
}
- // Next, list other people in this room.
- for (var c in client.world.clients)
- { var tc = client.world.clients[c];
- if (tc === client) continue;
- if (tc.room !== this) continue;
+}
+
+// List the logged in users in this room.
+Room.prototype.listClients = function (client) {
+ for (var c in world.clients)
+ { var tc = world.clients[c];
+ if (tc === client) continue; // Skip ourselves.
+ if (!tc.connected) continue; // Skip logged out users.
+ if (tc.room !== this) continue; // Skip users not in this room.
var cname = tc.name;
client.msg(cname + " is here.");
}
- // Finally, list any visible exits.
+}
+
+// List any exits from this room.
+Room.prototype.listExits = function (client) {
if (this.exits) {
- client.msg("You can go: ");
- for (var e in this.exits) {
- client.msg(" " + e);
+ if (!this.exitlist)
+ this.buildExitList();
+ var ways = this.exitlist.length;
+ if (ways == 1)
+ client.msg("You can go "+this.exitlist[0]+".");
+ else if (ways == 2)
+ client.msg("You can go "+this.exitlist.join(" and ")+".");
+ else {
+ var message = "You can go ";
+ for (var e in this.exitlist)
+ { if (e == ways-1)
+ message += "and ";
+ message += this.exitlist[e];
+ if (e != ways-1)
+ message += ", ";
+ }
+ message += ".";
+ client.msg(message);
}
}
+ else
+ client.msg("There are no obvious exits from here.");
+}
+
+// Build a flat list of exits for use in listExits.
+Room.prototype.buildExitList = function () {
+ if (!this.exits) return; // We can't do anything without exits.
+ this.exitlist = [];
+ for (var e in this.exits)
+ this.exitlist.push(e);
}
Room.prototype.go = function (client, direction) {
@@ -173,22 +211,29 @@ Room.prototype.go = function (client, direction) {
client.msg("You can't go that way.");
}
+// Build a connection to another room.
+// If the connection is a known direction, then
+// a corresponding connection will be made in the
+// target room.
Room.prototype.connect = function (direction, target, nosync) {
if (!this.exits)
this.exits = {};
this.exits[direction] = target;
if (!nosync && direction in opposites) {
- var troom = this.world.getRoom(target);
+ var troom = world.getRoom(target);
var odir = opposites[direction];
if (troom)
troom.connect(odir, this.id, true);
}
+ this.buildExitList();
}
+// Deletes a connection. This does NOT delete any corresponding connections.
Room.prototype.disconnect = function (direction) {
if (!this.exists)
return;
delete(this.exits[direction]);
+ this.buildExitList();
}
// Add some functions from an extension class.
@@ -198,7 +243,7 @@ Room.prototype.mixin = function (rolename, init) {
this.extend(role, init);
}
-exports.newRoom = function (id, world, init) {
- return new Room(id, world, init);
+exports.newRoom = function (id, init) {
+ return new Room(id, init);
}
View
6 lib/World.js
@@ -38,7 +38,7 @@ World.prototype.dataString = function (data) {
World.prototype.login = function (socket) {
socket.write(this.config.welcome+nl);
socket.write("Enter your username: ");
- var world = this;
+ // Below we use "world" instead of "this" for disabiguity.
var loginHandler = function (data) {
var username = world.dataString(data);
var client = world.getUser(username);
@@ -63,7 +63,7 @@ World.prototype.login = function (socket) {
// Add a new client to the client list.
World.prototype.newClient = function (socket, username) {
- var client = this.Client.newClient(socket, this, username);
+ var client = this.Client.newClient(socket, username);
this.clients.push(client);
this.usercount++;
client.gotoRoom(this.config.start);
@@ -99,7 +99,7 @@ World.prototype.load = function (configfile) {
// Add a room
World.prototype.addRoom = function (roomid, init) {
- var room = this.Room.newRoom(roomid, this, init);
+ var room = this.Room.newRoom(roomid, init);
this.rooms[roomid] = room;
}
View
3  nodemoo.js
@@ -2,6 +2,9 @@
var net = require('net');
var world = require('./lib/World.js');
+// Let's make "world" available everywhere:
+global.world = world;
+
// Load the game config.
world.load('./game.json');
Please sign in to comment.
Something went wrong with that request. Please try again.