Permalink
Browse files

Lots of progress but still lots to go

  • Loading branch information...
1 parent ec9e72d commit 4edbb6fedbfac0ee65d23f8b4cd38ed238349382 @robrighter committed Jan 3, 2012
Showing with 120 additions and 47 deletions.
  1. +53 −2 server.js
  2. BIN static/images/sprites/protocoldroid2.png
  3. +66 −42 static/js/script.js
  4. +1 −3 views/index.jade
View
@@ -5,6 +5,9 @@ var connect = require('connect')
, easyoauth = require('easy-oauth')
, port = (process.env.PORT || 8081);
+
+var crypto = require('crypto');
+var keylookups = {};
//Setup Express
var server = express.createServer();
@@ -44,6 +47,8 @@ var io = io.listen(server);
io.sockets.on('connection', function(socket){
console.log('Client Connected');
socket.on('move_character', function(data){
+ data.character = keylookups[data.requestkey];
+ delete data.requestkey;
socket.broadcast.emit('server_move_character',data);
socket.emit('server_move_character',data);
});
@@ -53,6 +58,32 @@ io.sockets.on('connection', function(socket){
});
+// Website functions /////////////////////////////////////
+var hashsalt = Math.random() + 'seedin' + Math.random();
+var characters = {
+ hubot: {
+ image: "/images/sprites/protocoldroid2.png",
+ location: {
+ x: 50,
+ y: 50
+ }
+ }
+}
+
+function createHash(tohash, callback){
+ crypto.pbkdf2(tohash,hashsalt, 1, 10, function(err, derivedkey){
+ keylookups[derivedkey] = tohash;
+ callback(derivedkey);
+ });
+}
+
+function notifyClientsAboutNewCharacter(handle){
+ //TODO: FIX THIS
+ console.log('GOT HERE!!!!!!!!!!!!!!!!!!!!!!!!!!');
+ io.sockets.emit('server_add_character', { handel: handle, details: characters[handle] });
+ //io.sockets.broadcast.emit('add_character', characters[handle]);
+}
+
///////////////////////////////////////////
// Routes //
///////////////////////////////////////////
@@ -62,14 +93,34 @@ io.sockets.on('connection', function(socket){
server.get('/', function(req,res){
res.render('index.jade', {
locals : {
- title : 'Your Page Title'
+ title : 'Rob Righter, Javascript, Node.JS, IOS, Hubot and other such things.'
,description: 'Your Page Description'
- ,author: 'Your Name'
+ ,author: 'Rob Righter'
,analyticssiteid: 'XXXXXXX'
}
});
});
+server.get('/ajax/characters', function(req,res){
+ res.send(characters);
+})
+
+server.post('/ajax/initiate-character', function(req,res){
+ //todo: CHECK THE AUTH AND MAKE SURE THEY ARE OAUTH WITH THIS TWITTER HANDLE
+ characters[req.body.handle] = {
+ image: req.body.image,
+ location: {
+ x: 100,
+ y: 100
+ }
+ };
+ console.log('GOT A NEW CHARACTER: ' + req.body.handle);
+ console.log(characters[req.body.handle]);
+ notifyClientsAboutNewCharacter(req.body.handle);
+ createHash(req.body.handle, function(hash){
+ res.send({ requestkey: hash });
+ });
+});
//A Route for Creating a 500 Error (Useful to keep around)
server.get('/500', function(req, res){
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -2,52 +2,70 @@
*/
$(document).ready(function() {
- //////////////////////////////////////
- var authcallback = function(data){
- $('.details').html('<p>You are all signed in as <strong>'
- +data.user.username+
- '</strong><br>...and here are some details:'
- +JSON.stringify(data.user)+
- '</p><a href="/logout">logout</a>').fadeIn('slow');
- }
-
- $('#oauthbutt').click(function(){
- openEasyOAuthBox('twitter',authcallback);
- });
-
- /////////////////////////////////////////
+
var scene = sjs.Scene({w:$(window).width(), h:$(window).height(), autoPause: false});
- var sps = {
- lando: scene.Sprite("/images/sprites/lando.png"),
- luke: scene.Sprite("/images/sprites/luke.png")
- }
+ var sps = {};
+ var input = sjs.Input(scene);
+ var inmotion = false;
+ var socket;
+ var requestkey = '';
- _.each(_.keys(sps), function(item){
- sp = sps[item];
- sp.size(32, 48);
- sp.offset(0, 0);
- sp.move(100, 100);
- sp.update();
+ //setup the oauth button
+ $('#oauthbutt').click(function(){
+ openEasyOAuthBox('twitter',function(oauth){
+ $.post("/ajax/initiate-character", {
+ handle: oauth.user.username,
+ image: '/images/sprites/weddingguy02.png'
+ }, function(data){
+ requestkey = data.requestkey;
+ });
+ setupTalkInterface(oauth.user.username);
+
+ });
});
-
- var socket = io.connect();
-
- socket.on('server_move_character', function(data){
- sps[data.character].xv = data.xv;
- sps[data.character].yv = data.yv;
- if(data.direction){
- spriteAnimateDirection(sps[data.character], data.direction);
- }
+
+ //load up the characters
+ $.get('/ajax/characters', function(data) {
+ _.each(_.keys(data), function(key){
+ addCharacter(key, data[key]);
+ });
+ //now go ahead and setup the socket
+ setupSocket();
+ //now setup the ticker
+ setupTicker();
});
-
- //"/images/sprites/lando.png" 128x192 32x48
+ function addCharacter(handle, chardict){
+ sps[handle] = scene.Sprite(chardict.image);
+ sps[handle].size(32,48);
+ sps[handle].offset(0,0);
+ sps[handle].move(chardict.location.x,chardict.location.y);
+ sps[handle].update();
+ }
+
+ function setupSocket(){
+ socket = io.connect();
+ socket.on('server_move_character', function(data){
+ console.log(data);
+ console.log(sps);
+ sps[data.character].xv = data.xv;
+ sps[data.character].yv = data.yv;
+ if(data.direction){
+ spriteAnimateDirection(sps[data.character], data.direction);
+ }
+ });
+
+ socket.on('server_add_character', function(data){
+ console.log('GOT AN ADD CHARACTER!!!!!!!');
+ console.log(data);
+ addCharacter(data.character, data.details);
+ });
+ }
- var image = 'lando.png';
- var inmotion = false;
- var input = sjs.Input(scene);
- var ticker = scene.Ticker(gameTick, {tickDuration: 130});
- ticker.run();
+ function setupTicker(){
+ var ticker = scene.Ticker(gameTick, {tickDuration: 130});
+ ticker.run();
+ }
function updateAllSprites(){
_.each(_.keys(sps), function(item){
@@ -84,11 +102,11 @@ $(document).ready(function() {
if(character.direction){
inmotion = true;
- socket.emit('move_character', {character: 'lando', xv: character.xv, yv: character.yv, direction: character.direction });
+ socket.emit('move_character', {requestkey: requestkey, xv: character.xv, yv: character.yv, direction: character.direction });
}
else if(inmotion){
inmotion=false;
- socket.emit('move_character', {character: 'lando', xv: 0, yv: 0, direction: null });
+ socket.emit('move_character', {requestkey: requestkey, xv: 0, yv: 0, direction: null });
}
updateAllSprites();
}
@@ -110,5 +128,11 @@ $(document).ready(function() {
sprite[prop][direction] = sprite[prop][direction]%4;
sprite.offset(sprite[prop][direction]*width, spritekey[direction]);
}
+
+ function setupTalkInterface(user){
+ $('#controlbox').slideUp('slow', function(){
+ $('#controlbox').html("<div class='username'>@"+user+"</div>"+"<input type='text' id='talkbox'><button id='#saybutt'>say it.</button>").slideDown('slow');
+ });
+ }
});
View
@@ -16,6 +16,4 @@
span
&#9733
li
- a(href='http://www.linkedin.com/in/robrighter')='linkedin'
-.details
- | these are details
+ a(href='http://www.linkedin.com/in/robrighter')='linkedin'

0 comments on commit 4edbb6f

Please sign in to comment.