Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Lots of progress but still lots to go

  • Loading branch information...
commit 4edbb6fedbfac0ee65d23f8b4cd38ed238349382 1 parent ec9e72d
Rob Righter authored January 02, 2012
55  server.js
@@ -5,6 +5,9 @@ var connect = require('connect')
5 5
     , easyoauth = require('easy-oauth')
6 6
     , port = (process.env.PORT || 8081);
7 7
     
  8
+    
  9
+var crypto = require('crypto');
  10
+var keylookups = {};
8 11
 
9 12
 //Setup Express
10 13
 var server = express.createServer();
@@ -44,6 +47,8 @@ var io = io.listen(server);
44 47
 io.sockets.on('connection', function(socket){
45 48
   console.log('Client Connected');
46 49
   socket.on('move_character', function(data){
  50
+    data.character = keylookups[data.requestkey];
  51
+    delete data.requestkey; 
47 52
     socket.broadcast.emit('server_move_character',data);
48 53
     socket.emit('server_move_character',data);
49 54
   });
@@ -53,6 +58,32 @@ io.sockets.on('connection', function(socket){
53 58
 });
54 59
 
55 60
 
  61
+// Website functions /////////////////////////////////////
  62
+var hashsalt = Math.random() + 'seedin' + Math.random();
  63
+var characters = {
  64
+  hubot: {
  65
+    image: "/images/sprites/protocoldroid2.png",
  66
+    location: {
  67
+      x: 50,
  68
+      y: 50
  69
+    }
  70
+  }
  71
+}
  72
+
  73
+function createHash(tohash, callback){
  74
+  crypto.pbkdf2(tohash,hashsalt, 1, 10, function(err, derivedkey){
  75
+    keylookups[derivedkey] = tohash;
  76
+    callback(derivedkey);
  77
+  });
  78
+}
  79
+
  80
+function notifyClientsAboutNewCharacter(handle){
  81
+  //TODO: FIX THIS
  82
+  console.log('GOT HERE!!!!!!!!!!!!!!!!!!!!!!!!!!');
  83
+  io.sockets.emit('server_add_character', { handel: handle, details: characters[handle] });
  84
+  //io.sockets.broadcast.emit('add_character', characters[handle]);
  85
+}
  86
+
56 87
 ///////////////////////////////////////////
57 88
 //              Routes                   //
58 89
 ///////////////////////////////////////////
@@ -62,14 +93,34 @@ io.sockets.on('connection', function(socket){
62 93
 server.get('/', function(req,res){
63 94
   res.render('index.jade', {
64 95
     locals : { 
65  
-              title : 'Your Page Title'
  96
+              title : 'Rob Righter, Javascript, Node.JS, IOS, Hubot and other such things.'
66 97
              ,description: 'Your Page Description'
67  
-             ,author: 'Your Name'
  98
+             ,author: 'Rob Righter'
68 99
              ,analyticssiteid: 'XXXXXXX' 
69 100
             }
70 101
   });
71 102
 });
72 103
 
  104
+server.get('/ajax/characters', function(req,res){
  105
+  res.send(characters);
  106
+})
  107
+
  108
+server.post('/ajax/initiate-character', function(req,res){
  109
+  //todo: CHECK THE AUTH AND MAKE SURE THEY ARE OAUTH WITH THIS TWITTER HANDLE
  110
+  characters[req.body.handle] = {
  111
+    image: req.body.image,
  112
+    location: {
  113
+      x: 100,
  114
+      y: 100
  115
+    }
  116
+  };
  117
+  console.log('GOT A NEW CHARACTER: ' + req.body.handle);
  118
+  console.log(characters[req.body.handle]);
  119
+  notifyClientsAboutNewCharacter(req.body.handle);
  120
+  createHash(req.body.handle, function(hash){
  121
+    res.send({ requestkey: hash });
  122
+  });
  123
+});
73 124
 
74 125
 //A Route for Creating a 500 Error (Useful to keep around)
75 126
 server.get('/500', function(req, res){
BIN  static/images/sprites/protocoldroid2.png
108  static/js/script.js
@@ -2,52 +2,70 @@
2 2
 */
3 3
 
4 4
 $(document).ready(function() {
5  
-  //////////////////////////////////////
6  
-  var authcallback = function(data){
7  
-      $('.details').html('<p>You are all signed in as <strong>'
8  
-            +data.user.username+
9  
-            '</strong><br>...and here are some details:'
10  
-            +JSON.stringify(data.user)+
11  
-            '</p><a href="/logout">logout</a>').fadeIn('slow');
12  
-    }
13  
-
14  
-    $('#oauthbutt').click(function(){
15  
-      openEasyOAuthBox('twitter',authcallback);
16  
-    });
17  
-      
18  
-  /////////////////////////////////////////   
  5
+  
19 6
   var scene = sjs.Scene({w:$(window).width(), h:$(window).height(), autoPause: false});
20  
-  var sps = {
21  
-    lando: scene.Sprite("/images/sprites/lando.png"),
22  
-    luke: scene.Sprite("/images/sprites/luke.png")
23  
-  }
  7
+  var sps = {};
  8
+  var input = sjs.Input(scene);
  9
+  var inmotion = false;
  10
+  var socket;
  11
+  var requestkey = '';
24 12
   
25  
-  _.each(_.keys(sps), function(item){
26  
-    sp = sps[item];
27  
-    sp.size(32, 48);
28  
-    sp.offset(0, 0);
29  
-    sp.move(100, 100);
30  
-    sp.update();
  13
+  //setup the oauth button
  14
+  $('#oauthbutt').click(function(){
  15
+    openEasyOAuthBox('twitter',function(oauth){
  16
+      $.post("/ajax/initiate-character", { 
  17
+        handle: oauth.user.username,
  18
+        image: '/images/sprites/weddingguy02.png'
  19
+      }, function(data){
  20
+        requestkey = data.requestkey;
  21
+      });
  22
+      setupTalkInterface(oauth.user.username);
  23
+      
  24
+    });
31 25
   });
32  
-
33  
-  var socket = io.connect();
34  
-
35  
-  socket.on('server_move_character', function(data){
36  
-   sps[data.character].xv = data.xv;
37  
-   sps[data.character].yv = data.yv;
38  
-   if(data.direction){
39  
-     spriteAnimateDirection(sps[data.character], data.direction);
40  
-   }
  26
+  
  27
+  //load up the characters
  28
+  $.get('/ajax/characters', function(data) {
  29
+    _.each(_.keys(data), function(key){
  30
+      addCharacter(key, data[key]);
  31
+    });
  32
+    //now go ahead and setup the socket
  33
+    setupSocket();
  34
+    //now setup the ticker
  35
+    setupTicker();
41 36
   });
42  
-
43  
-  //"/images/sprites/lando.png" 128x192 32x48
44 37
   
  38
+  function addCharacter(handle, chardict){
  39
+    sps[handle] = scene.Sprite(chardict.image);
  40
+    sps[handle].size(32,48);
  41
+    sps[handle].offset(0,0);
  42
+    sps[handle].move(chardict.location.x,chardict.location.y);
  43
+    sps[handle].update();
  44
+  }
  45
+  
  46
+  function setupSocket(){
  47
+    socket = io.connect();
  48
+    socket.on('server_move_character', function(data){
  49
+     console.log(data);
  50
+     console.log(sps);
  51
+     sps[data.character].xv = data.xv;
  52
+     sps[data.character].yv = data.yv;
  53
+     if(data.direction){
  54
+       spriteAnimateDirection(sps[data.character], data.direction);
  55
+     }
  56
+    });
  57
+    
  58
+    socket.on('server_add_character', function(data){
  59
+      console.log('GOT AN ADD CHARACTER!!!!!!!');
  60
+      console.log(data);
  61
+      addCharacter(data.character, data.details);
  62
+    });
  63
+  }
45 64
 
46  
-  var image = 'lando.png';
47  
-  var inmotion = false;
48  
-  var input = sjs.Input(scene);
49  
-  var ticker = scene.Ticker(gameTick, {tickDuration: 130});
50  
-  ticker.run();
  65
+  function setupTicker(){
  66
+    var ticker = scene.Ticker(gameTick, {tickDuration: 130});
  67
+    ticker.run(); 
  68
+  }
51 69
   
52 70
   function updateAllSprites(){
53 71
     _.each(_.keys(sps), function(item){
@@ -84,11 +102,11 @@ $(document).ready(function() {
84 102
 
85 103
     if(character.direction){
86 104
       inmotion = true;
87  
-      socket.emit('move_character', {character: 'lando', xv: character.xv, yv: character.yv, direction: character.direction });
  105
+      socket.emit('move_character', {requestkey: requestkey, xv: character.xv, yv: character.yv, direction: character.direction });
88 106
     }
89 107
     else if(inmotion){
90 108
       inmotion=false;
91  
-      socket.emit('move_character', {character: 'lando', xv: 0, yv: 0, direction: null });
  109
+      socket.emit('move_character', {requestkey: requestkey, xv: 0, yv: 0, direction: null });
92 110
     }
93 111
     updateAllSprites();          
94 112
   }
@@ -110,5 +128,11 @@ $(document).ready(function() {
110 128
     sprite[prop][direction] = sprite[prop][direction]%4;
111 129
     sprite.offset(sprite[prop][direction]*width, spritekey[direction]);
112 130
   }
  131
+  
  132
+  function setupTalkInterface(user){
  133
+    $('#controlbox').slideUp('slow', function(){
  134
+      $('#controlbox').html("<div class='username'>@"+user+"</div>"+"<input type='text' id='talkbox'><button id='#saybutt'>say it.</button>").slideDown('slow');
  135
+    });
  136
+  }
113 137
 });
114 138
 
4  views/index.jade
@@ -16,6 +16,4 @@
16 16
       span
17 17
         &#9733
18 18
     li
19  
-      a(href='http://www.linkedin.com/in/robrighter')='linkedin'
20  
-.details
21  
-  | these are details
  19
+      a(href='http://www.linkedin.com/in/robrighter')='linkedin'

0 notes on commit 4edbb6f

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