Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7adbf87
Showing
5 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.<BR /> ALPHA<BR /><BR />"; | ||
var cmd = "none"; | ||
var tickTime = 10000; | ||
|
||
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
|
||
var scrambleMsg = function(msg) { | ||
var scrambledMsg = ""; | ||
for (var i=0; i<msg.length; i++) { | ||
scrambledMsg += possible.charAt(Math.floor(Math.random() * possible.length)); | ||
} | ||
return scrambledMsg; | ||
}; | ||
|
||
var translate = function(msg, origMsg) { | ||
var translatedMsg = ""; | ||
for (var i=0; i<msg.length; i++) { | ||
if (Math.floor(Math.random() * train) > 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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<H1><%= mudName %></H1> | ||
|
||
<DIV ID="mudShell"></DIV> | ||
|
||
command<BR /> | ||
<FORM NAME="mudForm" ID="mudForm"> | ||
<INPUT type="text" name="mudCmd" id="mudCmd" /> | ||
<INPUT type="submit" name="send" /> | ||
</FORM> | ||
|
||
<script src="/socket.io/socket.io.js"></script> | ||
<script type="text/javascript"> | ||
|
||
var cmd = ""; | ||
var socket = io.connect('http://localhost:3000'); | ||
var win = $("#mudShell"); | ||
|
||
socket.on('connect', function() { | ||
socket.on("serverSlogan", function (data) { | ||
win.append("MotD:<BR />"+data); | ||
}); | ||
socket.on("newTick", function (data) { | ||
console.log("Received tick"); | ||
win.append("<"+data.tickMsg+"> "); | ||
}); | ||
socket.on("message", function (data) { | ||
win.append("<BR />" + data.msg); | ||
win.animate({ scrollTop: win.prop("scrollHeight") - win.height() }, 0); | ||
}); | ||
socket.on("newClient", function (data) { | ||
console.log("A new client connected whose id is: "+data); | ||
}); | ||
}); | ||
|
||
function setServerSlogan(newSlogan) { | ||
socket.emit("setServerSlogan", newSlogan); | ||
} | ||
|
||
function sendCmd(cmd) { | ||
socket.emit("sendCmd", { cmd: cmd }); | ||
console.log("New cmd sent from client: " + cmd); | ||
} | ||
|
||
$(document).ready(function() { | ||
$("#mudForm").submit(function() { | ||
var cmd = $("#mudCmd").val(); | ||
sendCmd(cmd); | ||
$("#mudCmd").val(''); | ||
return false; | ||
}); | ||
}); | ||
|
||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<HTML> | ||
|
||
<HEAD> | ||
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> | ||
<TITLE>MUD</TITLE> | ||
<STYLE TYPE="text/css"> | ||
#mudShell { overflow-y: auto; width: 400px; height: 300px; vertical-align: bottom; | ||
border-style: solid; border-color: black; border-width: 1px; } | ||
</STYLE> | ||
</HEAD> | ||
|
||
<BODY> | ||
|
||
<%- body %> | ||
|
||
</BODY> | ||
|
||
</HTML> |