From 7adbf878bd07481cf37c7770a014cca5bd7bd49c Mon Sep 17 00:00:00 2001 From: Ben Turner Date: Tue, 20 Mar 2012 14:15:17 -0400 Subject: [PATCH] first commit --- .gitignore | 1 + README | 5 ++ app.js | 115 ++++++++++++++++++++++++++++++++++++++++++++++ views/index.html | 53 +++++++++++++++++++++ views/layout.html | 18 ++++++++ 5 files changed, 192 insertions(+) create mode 100644 .gitignore create mode 100644 README create mode 100644 app.js create mode 100644 views/index.html create mode 100644 views/layout.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/README b/README new file mode 100644 index 0000000..5af8068 --- /dev/null +++ b/README @@ -0,0 +1,5 @@ +Testing out socket.io by trying to make a mini-MUD (text-based game) +via a web client. + +TODO: +-scramble/translate messages based on linguist skills diff --git a/app.js b/app.js new file mode 100644 index 0000000..bab2567 --- /dev/null +++ b/app.js @@ -0,0 +1,115 @@ +var express = require('express'); +var ejs = require('ejs'); // EJS (Embedded JavaScript) https://github.com/visionmedia/ejs +var app = express.createServer(express.logger()); + +/*********** SERVER CONFIGURATION *****************/ +app.configure(function() { + app.set('view engine','ejs'); + app.set('views',__dirname+ '/views'); + app.set('view options',{layout:true}); + app.register('html',require('ejs')); + app.use(express.static(__dirname + '/static')); + app.use(express.bodyParser()); + app.use(express.logger()); + app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); +}); + +var currentServerSlogan = "Welcome to Ben's socket.io MUD experiment.
ALPHA

"; +var cmd = "none"; +var tickTime = 10000; + +var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + +var scrambleMsg = function(msg) { + var scrambledMsg = ""; + for (var i=0; i 2) { + translatedMsg += origMsg[i]; + } + else { + translatedMsg += msg[i]; + } + } + return translatedMsg; +}; + +var skillTranslation = 0; +var train = function(skill) { + return skill++; +}; + +var io = require('socket.io').listen(app); +io.configure('production', function() { + io.set('log level', 1); +}); + +io.sockets.on('connection', function (socket) { + + newTick = function() { + var tickMsg = "testing"; + socket.emit("newTick", { tickMsg: tickMsg }); + console.log("Fired"); + setTimeout(newTick, tickTime); + }; + + setTimeout(newTick, tickTime); + + socket.emit("serverSlogan", currentServerSlogan); + socket.broadcast.emit("newClient", socket.id); + + socket.on('private message', function (from, msg) { + console.log('I received a private message by ', from, ' saying ', msg); + }); + + socket.on("setServerSlogan", function (data) { + currentServerSlogan = data; // set it + socket.broadcast.emit("serverSlogan", currentServerSlogan); // tell everyone else the new slogan + }); + + socket.on("sendCmd", function (cmd) { + var command = cmd.cmd; + var timestamp = new Date(); + console.log(timestamp); + var hours = timestamp.getHours(); + var minutes = timestamp.getMinutes(); + if (minutes < 10) { + minutes = "0" + minutes; + } + + if (command === "d" || command === "down") { + socket.emit("message", { msg: "[" + hours + ":" + minutes + "] User " + socket.id + " has headed " + command + "." }); + } + else if (/gossip /).test(command) { + var msg = command.substring(7,command.length); + socket.emit("message", { msg: "[" + hours + ":" + minutes + "] User " + socket.id + " gossips, \"" + scrambleMsg(msg); + "\"" }); + } + //console.log(msg); + //socket.emit("message", msg); + }); + + socket.on('disconnect', function () { + socket.broadcast.emit('user disconnected'); + }); + +}); + +app.get('/', function(request, response) { + + var data = { mudName: "Ben's Test socket.io MUD" }; + + response.render("index.html", data); +}); + +// Make server turn on and listen at defined PORT (or port 3000 if is not defined). +var port = process.env.PORT || 3000; +app.listen(port, function() { + console.log("Listening on " + port); +}); \ No newline at end of file diff --git a/views/index.html b/views/index.html new file mode 100644 index 0000000..15c8245 --- /dev/null +++ b/views/index.html @@ -0,0 +1,53 @@ +

<%= mudName %>

+ +
+ +command
+
+ + +
+ + + \ No newline at end of file diff --git a/views/layout.html b/views/layout.html new file mode 100644 index 0000000..6fc6c37 --- /dev/null +++ b/views/layout.html @@ -0,0 +1,18 @@ + + + + +MUD + + + + + +<%- body %> + + + + \ No newline at end of file