Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

realized that using the this keyword to reference the execution conte…

…xt for the function was totally unecessary. Kind of embarassing, I'm a better JS programmer than that
  • Loading branch information...
commit 07bac6b7a7fd5736b5b7557b106d0770b75efe78 1 parent c025fa3
@jessehattabaugh authored
Showing with 190 additions and 197 deletions.
  1. +71 −94 public/client.js
  2. +119 −103 server.js
View
165 public/client.js
@@ -6,152 +6,129 @@
// closure for callbacks
var self = this;
+
+ var plid = null;
+ var password = null;
// initialize socket
- this.socket = io.connect();
+ var socket = io.connect();
- this.socket.on('who are you', function (data) {
+ socket.on('who are you', function (data) {
console.log("prompting for player id");
do{
- var plid = prompt("What is your name?", "Joe");
+ plid = prompt("What is your name?", "Joe");
} while(!plid)
// todo: validate length
- socket.emit('my name is', {plid:plid});
+ socket.emit('my name is', {plid: plid});
});
- this.socket.on('what is your password', function (data) {
+ socket.on('what is your password', function (data) {
console.log("prompting for password");
do{
- var password = prompt(data);
+ password = prompt(data);
} while(!password)
socket.emit('here is my password', {password:password});
});
- this.x = 0;
- this.y = 0;
+ var x = 0;
+ var y = 0;
// todo: the player needs a concept of which direction it's facing
- this.socket.on('welcome', function (pos) {
+ var chunks = {}; // dictionary of chunks where chuId is key and canvas is val
+
+ var chunkSize = 64;
+
+ function enrollment(){
+ var chuX = Math.floor( x / chunkSize );
+ var chuY = Math.floor( y / chunkSize );
+ var chuId = 'chunks:' + chuX + '/' + chuY;
+
+ if(!(chuId in chunks)){
+ console.log('enrolling in chunk '+chuId);
+ socket.emit('enroll', {chuId:chuId});
+ }
+
+ // leave chunk channels that are now too far away
+
+ // todo: call this function when screen is resized
+ };
+
+ socket.on('welcome', function (pos) {
console.log("login successfull");
- //console.dir(pos);
- self.x = pos[0];
- self.y = pos[1];
- self.enroll();
+ console.dir(pos);
+ x = pos[0];
+ y = pos[1];
+ enrollment();
// todo: show canvas
});
+
+ socket.on('enrolled', function(data){
+ console.log("you enrolled in chunk "+data.chuId);
+ //console.dir(data);
+ self.chunks[data.chuId] = {};
+ //todo: retrieve chunk pixel data from db and draw to a canvas
+ });
- this.moving = false; // whether or not we are in the process of moving
+ // todo: this.socket.on('unenrolled')
- this.move = function(x,y){
- if (!this.moving){
- this.moving = true;
+ var moving = false; // whether or not we are in the process of moving
+
+ function move(x,y){
+ if (!moving){
+ moving = true;
// todo: if server never replies to this movement, no more
// movements will be possible, fix that, maybe timeout?
console.log('moving to '+x+'/'+y);
- this.socket.emit('move', [x,y]);
+ socket.emit('move', [x,y]);
}
- };
+ }
// handle movement keys
- $('body').on('keydown', this, function(e){
+ $('body').on('keydown', function(e){
// todo: handle diagonals
// todo: don't capture EVERYTHING it's annoying
- var from_x = parseInt(e.data.x);
- var from_y = parseInt(e.data.y);
+ var from_x = parseInt(x);
+ var from_y = parseInt(y);
if(e.which == 37 || e.which == 65){ // west
- e.data.move(from_x-1, from_y);
+ move(from_x-1, from_y);
} else if(e.which == 38 || e.which == 87){ //north
- e.data.move(from_x, from_y-1);
+ move(from_x, from_y-1);
} else if(e.which == 39 || e.which == 68){ // east
- e.data.move(from_x+1, from_y);
+ move(from_x+1, from_y);
} else if(e.which == 40 || e.which == 83){ // south
- e.data.move(from_x, from_y+1);
+ move(from_x, from_y+1);
} else {
console.log('key:'+e.which);
}
return false;
});
- this.chunks = {}; // dictionary of chunks where chuId is key and canvas is val
-
- var chunkSize = 64;
- this.enroll = function(){
- var chuX = Math.floor( this.x / chunkSize );
- var chuY = Math.floor( this.y / chunkSize );
- var chuId = chuX + '/' + chuY;
-
- if(!(chuId in this.chunks)){
- console.log('enrolling in chunk '+chuId);
- self.socket.emit('enroll', {chuId:chuId});
- }
-
- // if player crossed a chunk boundary
- // leave chunk channels that are now too far away
- // join chunk channels that are now in range
-
- // todo:
- // todo: call this function when screen is resized
- }
- this.socket.on('you moved', function(to){
- console.log("you moved");
- //console.dir(to);
- self.x = to[0];
- self.y = to[1];
- self.moving = false;
- self.enroll(); // todo: only call this when crossing a chunk boundary
+ socket.on('movement', function(data){
+ console.log(plid);
+ if(data.plid == plid){
+ console.log("you moved");
+ x = to[0];
+ y = to[1];
+ moving = false;
+ enrollment(); // todo: only call this when crossing a chunk boundary
+ } else {
+ console.log(data.plid+" moved");
+ }
});
- this.socket.on('invalid move', function(data){
+ socket.on('invalid move', function(data){
console.log("can not move there");
console.dir(data);
- self.moving = false;
+ moving = false;
});
- this.socket.on('joined chunk', function(data){
- console.log("you joined chunk x/y");
- console.dir(data);
- });
- // todo: this.socket.on()
- this.socket.on('someone logged in', function(data){
- console.log("someone logged in");
- console.dir(data);
- });
-
- this.socket.on('someone logged out', function(data){
- console.log("someone logged outw");
- console.dir(data);
- });
-
- this.socket.on('someone joined a chunk', function(data){
- console.log("someone joined a chunk");
- console.dir(data);
- });
-
- this.socket.on('someone left a chunk', function(data){
- console.log("someone left a chunk");
- console.dir(data);
- });
-
- this.socket.on('someone moved', function(data){
- console.log("player moved");
- console.dir(data);
-
-
- // todo: if user moves off chunk remove them from list
- });
-
- this.socket.on('disconnect', function(){
+ socket.on('disconnect', function(){
console.log("You are disconnected");
// todo: hide controls
});
-
- this.say = function(msg){
- // todo: send a message over the socket
- };
}());
-//todo: I'd prefer to use the new operator
View
222 server.js
@@ -30,115 +30,131 @@ io.sockets.on('connection', function(socket) {
// todo: send a unique username
//socket.broadcast.emit('player joined');
-
-});
-
-io.sockets.on('my name is', function handleLogin(data) {
- console.log("They say their name is " + data.plid);
- socket.set('plid', data.plid, function() {
- db.hget('players:' + data.plid, 'password', function(err, password) {
- if (password) {
- message = 'Enter your password';
- } else {
- message = 'Set your password';
- }
- socket.emit('what is your password', message);
- });
- });
-
- console.log('my_name_is:' + data.plid);
-});
-
-io.sockets.on('here is my password', function handlePassword(data) {
- console.log("Recieved a password");
- socket.get('plid', function(err, plid) {
- db.hget('players:' + plid, 'password', function(err, password) {
- if (password) {
- if (data.password == password) {
- db.hmget('players:' + plid, 'x', 'y', function(err, coords) {
- socket.emit('welcome', coords || [0, 0]);
- });
- } else {
- socket.emit('what is your password', 'Incorrect Password, Try Again');
- }
- } else {
- db.hset('players:' + plid, 'password', data.password, function setNewPassword(err, res) {
- socket.set('authenticated', true, function() {
- socket.emit('welcome', [0, 0]);
- });
- });
- }
- });
- });
-});
-
-io.sockets.on('enroll', function(data){
- console.log('enrolling in a chunk');
- console.dir(data);
-
- if(!(chuId in pubClients)){
- pubClients[chuId] = new redis.pub(function(){
-
- });
-
- }
-
- // add the player to the chunk room
- // todo: open pub clients if necessary
- // todo: open sub clients if necessary
- // todo: join chunk rooms
-});
-
-chunkSize = 64;
+ socket.on('my name is', function handleLogin(data) {
+ console.log("They say their name is " + data.plid);
+ socket.set('plid', data.plid, function() {
+ db.hget('players:' + data.plid, 'password', function(err, password) {
+ if (password) {
+ message = 'Enter your password';
+ } else {
+ message = 'Set your password';
+ }
+ socket.emit('what is your password', message);
+ });
+ });
+
+ console.log('my_name_is:' + data.plid);
+ });
-function chuidFor(x, y) {
- // returns the chunk id for the coords
- var chuX = Math.floor(x / chunkSize);
- var chuY = Math.floor(y / chunkSize);
- return 'chunks:' + chuX + '/' + chuY;
-}
+ socket.on('here is my password', function handlePassword(data) {
+ console.log("Recieved a password");
+ socket.get('plid', function(err, plid) {
+ db.hget('players:' + plid, 'password', function(err, password) {
+ if (password) {
+ if (data.password == password) {
+ db.hmget('players:' + plid, 'x', 'y', function(err, coords) {
+ socket.emit('welcome', coords || [0, 0]);
+ });
+ } else {
+ socket.emit('what is your password', 'Incorrect Password, Try Again');
+ }
+ } else {
+ db.hset('players:' + plid, 'password', data.password, function setNewPassword(err, res) {
+ socket.set('authenticated', true, function() {
+ socket.emit('welcome', [0, 0]);
+ });
+ });
+ }
+ });
+ });
+ });
+
+ subClients = {};
+
+ socket.on('enroll', function(data){
+ console.log('enrolling in a chunk');
+ console.dir(data);
+ var chuId = data.chuId;
+ if(!(chuId in subClients)){
+ var chunkClient = redis.createClient();
+ chunkClient.on('message', function(chan, msg){
+ console.log("chunk("+chan+") recieved a message");
+ console.dir(msg);
+ var data = JSON.parse(msg);
+ //todo: validate existence of data.message and data.data
+ io.sockets.in(chan).emit(data.message, data.data);
+ });
+ chunkClient.subscribe(chuId);
+ subClients[chuId] = chunkClient;
+ }
+ socket.join(chuId);
+ socket.emit('enrolled', {chuId: chuId});
+ });
+
+ socket.on('unenroll', function(data){
+ console.log('leaving a chunk');
+ });
+
+ chunkSize = 64;
+
+ function chuidFor(x, y) {
+ // returns the chunk id for the coords
+ var chuX = Math.floor(x / chunkSize);
+ var chuY = Math.floor(y / chunkSize);
+ return 'chunks:' + chuX + '/' + chuY;
+ }
-io.sockets.on('move', function handleMove(to) {
+ socket.on('move', function handleMove(to) {
- console.log("Player movement");
- console.dir(to);
+ console.log("Player movement");
+ console.dir(to);
- socket.get('plid', function(err, plid) {
- db.hmget('players:' + plid, 'x', 'y', function(err, from) {
-
- from_x = parseInt(from[0]) || 0;
- from_y = parseInt(from[1]) || 0;
- // these might be empty if player has never moved
- //todo: more random initial spawn point
-
- to_x = parseInt(to[0]) || 0;
- to_y = parseInt(to[1]) || 0;
-
- from_chuid = chuidFor(from_x, from_y);
- to_chuid = chuidFor(to_x, to_y);
-
- function taxiDistance(x1, y1, x2, y2){
- return Math.abs(x1-x2) + Math.abs(y1-y2)
- }
-
-
- if (taxiDistance(from_x, from_y, to_x, to_y) <= 1) {
- // can only move one tile at a time
- //todo: check for other players, objects etc
+ socket.get('plid', function(err, plid) {
+ db.hmget('players:' + plid, 'x', 'y', function(err, from) {
- db.hmset('players:' + plid, 'x', to_x, 'y', to_y);
- socket.emit('you moved', to);
- } else {
- socket.emit('invalid move', to);
- console.log(from_x, from_y, to_x, to_y);
- }
- });
- });
-});
+ from_x = parseInt(from[0]) || 0;
+ from_y = parseInt(from[1]) || 0;
+ // these might be empty if player has never moved
+ //todo: more random initial spawn point
+
+ to_x = parseInt(to[0]) || 0;
+ to_y = parseInt(to[1]) || 0;
+
+ function taxiDistance(x1, y1, x2, y2){
+ return Math.abs(x1-x2) + Math.abs(y1-y2)
+ }
+
+ if (taxiDistance(from_x, from_y, to_x, to_y) <= 1) {
+ // can only move one tile at a time
+ // todo: check for other players, objects etc
+
+ db.hmset('players:' + plid, 'x', to_x, 'y', to_y);
+
+ //socket.emit('you moved', to);
+ var chuId = chuidFor(to_x, to_y);
+ console.log('publishing a message to '+chuId+' channel');
+ db.publish(chuId, JSON.stringify({
+ message: 'movement',
+ data: {
+ plid: plid,
+ from_x: from_x,
+ from_y: from_y,
+ to_x: to_x,
+ to_y: to_y
+ }
+ }));
+ } else {
+ socket.emit('invalid move', to);
+ console.log(from_x, from_y, to_x, to_y);
+ }
+ });
+ });
+ });
-io.sockets.on('disconnect', function() {
- console.log("Connection stopped.");
- io.sockets.emit('player left');
+ socket.on('disconnect', function() {
+ console.log("Connection stopped.");
+ io.sockets.emit('player left');
+ });
});
Please sign in to comment.
Something went wrong with that request. Please try again.