Skip to content

Instantly share code, notes, and snippets.

@whiskers75
Created July 30, 2012 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save whiskers75/3209159 to your computer and use it in GitHub Desktop.
Save whiskers75/3209159 to your computer and use it in GitHub Desktop.
// CreativeMUD by whiskers75
// Licenced under the GPLv2.
var sockets = [];
var players = [];
var repl = require('repl');
var net = require('net');
var fs = require('fs');
var len = 0;
var waiting = false;
var waitingFor = -1;
var waitingType = '';
var wait = 0;
var users = [];
var nnames = [];
var regs = 0;
var items = [];
var owners = [];
var areas = [];
var file;
net.createServer(function (socket) {
socket.write('Welcome to CreativeMUD, version 0.0.1!\nThere are currently '+ len + ' players logged in.\nTo exit CreativeMUD, type \'.exit\'.\n');
sockets.push(socket);
players[sockets.indexOf(socket)] = 'none';
len = len + 1;
repl.start({
prompt:"CreativeMUD, socket "+ sockets.indexOf(socket)+"> ",
input: socket,
output: socket,
'eval': function(cmd, context, filename, callback){
cmd = cmd.replace("\n)","").replace("(","");
console.log(cmd);
file = fs.readFileSync('items', 'utf-8');
items = JSON.parse(file);
file = fs.readFileSync('areas', 'utf-8');
areas = JSON.parse(file);
file = fs.readFileSync('owners', 'utf-8');
owners = JSON.parse(file);
file = fs.readFileSync('users', 'utf-8');
users = JSON.parse(file);
if (cmd === "login") {
console.log('login');
if (waiting) {
callback(null, 'Please wait a moment and try again.');
}
if (!waiting) {
callback(null, 'Please enter PIN.');
waiting = true;
waitingFor = socket;
waitingType = 'login';
wait = 1;
}
}
if (cmd === "register") {
if (waiting) {
callback(null, 'Please wait a moment and try again.');
}
if (!waiting) {
callback(null, 'Please enter desired PIN.');
waiting = true;
waitingFor = socket;
waitingType = 'register';
wait = 1;
}
}
if (cmd === "save") {
fs.writeFileSync('items', JSON.stringify(items), 'utf-8');
fs.writeFileSync('areas', JSON.stringify(areas), 'utf-8');
fs.writeFileSync('owners', JSON.stringify(owners), 'utf-8');
fs.writeFileSync('users', JSON.stringify(users), 'utf-8');
callback(null, 'Save complete.');
}
// put new commands here...
else {
if(wait === 0) {
if (cmd !== '') {
if (waiting) {
if (waitingFor === socket) {
if (waitingType === 'login') {
players[sockets.indexOf(socket)] = users[cmd];
waiting = false;
waitingFor = -1;
waitingType = 'none';
callback(null, 'Done!');
callback(null, 'Logged in as: '+ players[sockets.indexOf(socket)]);
}
if (waitingType === 'register') {
if (users[cmd] !== undefined) {
callback(null, 'This PIN is taken. Please try again.');
}
else {
nnames[sockets.indexOf(socket)] = cmd;
waitingType = 'username';
wait = 0;
}
}
if (waitingType === 'username') {
if (wait === 0) {
regs += 1;
users[nnames[sockets.indexOf(socket)]] = regs;
callback(null, 'Registered.');
players[sockets.indexOf(socket)] = users[cmd];
waiting = false;
waitingFor = -1;
waitingType = 'none';
callback(null, 'Logged in as: '+ players[sockets.indexOf(socket)]);
}
}
}
}
}
}
}
wait = 0;
}}).on('exit', function() {
socket.end();
len = len - 1;
});
}).listen(5001);
@isaacs
Copy link

isaacs commented Jul 30, 2012

  1. You need to do cmd = cmd.trim() on line 29.
  2. line 46 should probably be else if(..., unless you want any command that isn't "register" to hit the final else block.
  3. Line 48, while(waiting) { if(!waiting) {... makes no sense. The while will already exit if it's not waiting, that's how while works. And why are you looping in this way anyway? Node is single-threaded, that's an infinite loop that will just cause your program to lock up. You're probably doing it wrong.
  4. Line 75 (and others), you don't need parens around strings.

@nlf
Copy link

nlf commented Jul 30, 2012

Just a reminder that in addition to .trim() on line 29, you need to remove the parentheses from the string first

Your string is '(login\n)' not 'login\n' so .trim() won't remove the \n if you don't, as it's not at the beginning or end of the string.

@whiskers75
Copy link
Author

Note to self: this removes parentheses: cmd = cmd.replace(/^(|\n_)\n_$/g, '')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment