Skip to content
Browse files

Implemented a config file, first steps towards persistence. Refactore…

…d slightly.
  • Loading branch information...
1 parent c6675c5 commit 6faaebd19c731edf1c00b55009fd19a4b33523d3 @supernovus committed
Showing with 82 additions and 30 deletions.
  1. +1 −0 README
  2. 0 { → doc}/LICENSE
  3. +15 −0 game.json
  4. +12 −3 lib/Room.js
  5. +52 −5 lib/World.js
  6. +2 −22 nodemoo.js
View
1 README
@@ -15,6 +15,7 @@ It's very incomplete, yet, give me time.
* Emote
* Basic navigation ("go north" or "n")
* Mixins
+ * Basic world config
TODO:
View
0 LICENSE → doc/LICENSE
File renamed without changes.
View
15 game.json
@@ -0,0 +1,15 @@
+{
+ "rooms" : {
+ "start" : {
+ "name" : "The Lobby",
+ "desc" : "Not much to see here yet. It's a big, mostly empty room.",
+ "exits" : { "north" : "bathroom" }
+ },
+ "bathroom" : {
+ "name" : "The Bathroom",
+ "desc" : "A filthy bathroom, this place stinks!",
+ "exits" : { "south" : "start" },
+ "mixin" : [ "Bathroom" ]
+ }
+ }
+}
View
15 lib/Room.js
@@ -1,6 +1,5 @@
// New-style Rooms.
-var nl = "\r\n";
var opposites = {
'north' : 'south',
'south' : 'north',
@@ -33,6 +32,16 @@ function Room (id, world, init) {
if (init.exits)
this.exits = init.exits;
+ // Any mixins that need to be added.
+ if (init.mixin)
+ for (var m in init.mixin)
+ this.mixin(init.mixin[m], init);
+
+ // If any contents, let's add them too.
+ if (init.contents) {
+ this.contents = world.loadContents(init.contents);
+ }
+
}
Room.prototype.rename = function (newname) {
@@ -184,9 +193,9 @@ Room.prototype.disconnect = function (direction) {
// Add some functions from an extension class.
// Specify the role name here.
-Room.prototype.mixin = function (rolename) {
+Room.prototype.mixin = function (rolename, init) {
var role = require('./Room/'+rolename+'.js');
- this.extend(role);
+ this.extend(role, init);
}
exports.newRoom = function (id, world, init) {
View
57 lib/World.js
@@ -1,16 +1,38 @@
+// The main library that powers The World.
+
+var fs = require('fs');
+
+// Let's add an "extend" method, with an optional "setup" component.
+Object.defineProperty(Object.prototype, "extend", {
+ enumerable: false,
+ value : function (from, init) {
+ for (var f in from)
+ { if (f == 'setup')
+ from.setup(this, init);
+ else
+ this[f] = from[f];
+ }
+ }
+});
+
var nl = "\r\n";
+// Construct the world.
function World () {
- this.clients = [];
- this.rooms = {};
- this.usercount = 1;
- this.Room = require('./Room.js');
+ this.clients = [];
+ this.rooms = {};
+ this.usercount = 1;
+ this.config = '';
+ this.Room = require('./Room.js');
+ this.Client = require('./Client.js');
}
// Add a client to the client list.
-World.prototype.addClient = function (client) {
+World.prototype.addClient = function (socket) {
+ var client = this.Client.newClient(socket, this);
this.clients.push(client);
this.usercount++;
+ return client;
}
// Delete a client from the client list.
@@ -23,12 +45,36 @@ World.prototype.delClient = function (client) {
}
}
+// Load a world configuration
+World.prototype.load = function (configfile) {
+ // Okay, let's load the file.
+ this.config = configfile;
+ var jsontext = fs.readFileSync(configfile);
+ var config = JSON.parse(jsontext);
+
+ // First, let's load rooms.
+ if (config.rooms)
+ for (var r in config.rooms)
+ this.addRoom(r, config.rooms[r]);
+
+ // TODO: cached players, and more.
+ return config;
+}
+
// Add a room
World.prototype.addRoom = function (roomid, init) {
var room = this.Room.newRoom(roomid, this, init);
this.rooms[roomid] = room;
}
+// Parse a contents JSON config, and return
+// an array of contents.
+World.prototype.loadContents = function (config) {
+ var contents = [];
+ // TODO: actually support items maybe?
+ return contents;
+}
+
// See if a client with a given name exists.
World.prototype.userExists = function (username) {
for (var c in this.clients) {
@@ -38,6 +84,7 @@ World.prototype.userExists = function (username) {
return false;
}
+// Get a specific user.
World.prototype.getUser = function (username) {
for (var c in this.clients) {
if (this.clients[c].name == username)
View
24 nodemoo.js
@@ -2,33 +2,13 @@
var net = require('net');
-// Let's add an "extend" method, with an optional "setup" component.
-Object.defineProperty(Object.prototype, "extend", {
- enumerable: false,
- value : function (from) {
- for (var f in from)
- { if (f == 'setup')
- from.setup(this);
- else
- this[f] = from[f];
- }
- }
-});
-
var world = require('./lib/World.js');
-var clib = require('./lib/Client.js');
-
-// The starting room. Rather boring.
-world.addRoom('start', {
- 'name' : 'The Lobby',
- 'desc' : "Not much to see here yet. It's a big, mostly empty room.",
-});
+var config = world.load('./game.json');
var server = net.createServer(function (socket) {
- var client = clib.newClient(socket, world);
+ var client = world.addClient(socket);
client.msg("Welcome to NodeMoo");
client.gotoRoom('start');
- world.addClient(client);
client.showPrompt();
socket.on('data', function (data) {
client.parse(data);

0 comments on commit 6faaebd

Please sign in to comment.
Something went wrong with that request. Please try again.