Created
April 28, 2012 18:36
-
-
Save possan/2521175 to your computer and use it in GitHub Desktop.
Harassing some sifteo cubes with javascript
This file contains hidden or 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
function Simulator(){ | |
var ret = {} | |
var _callback = undefined; | |
var _queue = []; | |
var _counter = 0; | |
ret.fireCallback = function(json) { | |
// console.log('Fire event '+json); | |
if( _callback ) _callback(json); | |
} | |
ret.queueEvent = function(json) { | |
// console.log('Queue event '+json); | |
if( typeof(json) == 'string' ) | |
_queue.push(json); | |
else | |
_queue.push(JSON.stringify(json)); | |
} | |
ret.popEvent = function() { | |
if( _queue.length < 1 ) | |
return undefined; | |
var r = _queue[0]; | |
_queue.splice(0,1); | |
return r; | |
} | |
ret.handleEvent = function(json) { | |
json = ''+json; | |
var obj = undefined; | |
try { | |
obj = JSON.parse(json); | |
} catch( e ) { | |
// console.log('unable to parse: '+json); | |
} | |
if( typeof(obj) == 'undefined' ) | |
return; | |
// console.log('got',obj); | |
if(obj.method) { | |
switch(obj.method){ | |
case 'link.emptyEvent': | |
break; | |
/* | |
case 'sound.stoppedEvent':break; | |
case 'cube.tiltEvent':break; | |
case 'cube.touchEvent':break; | |
case 'cube.tiltEvent':break; | |
*/ | |
case 'cube.buttonEvent': | |
if(obj.params[1] == true ) { | |
ret.queueEvent({"method":"sound.play","params":["gliss",1.0,1.0,1],"id":_counter}); | |
_counter++; | |
} | |
break; | |
default: | |
console.log('Client got event',obj.method,obj.params); | |
break; | |
} | |
} | |
} | |
ret.popAndSendEvent = function() { | |
var pop; | |
while( pop = ret.popEvent() ) | |
{ | |
_callback(pop); | |
} | |
} | |
ret.setCallback = function(callback){ | |
_callback = callback; | |
} | |
ret.connect = function(){ | |
console.log('Client connected'); | |
ret.fireCallback('{"method":"app.init","id":0}'); | |
} | |
ret.disconnect = function(){ | |
console.log('Client disconnected'); | |
} | |
ret.tick = function(){ | |
var r=Math.round(Math.random()*2); | |
var r2=Math.round(Math.random()*4); | |
var r3=Math.round(Math.random()*100); | |
var r4=Math.round(Math.random()*100); | |
var r5=Math.round(Math.random()*100); | |
var r6=Math.round(Math.random()*100); | |
var r7=Math.round(Math.random()*100); | |
var r8=Math.round(Math.random()*100); | |
var id=Math.round(Math.random()*4); | |
var r9=1+Math.round(Math.random()*20); | |
switch(r) { | |
case 0: | |
// lazy,omg,happy | |
for(var q=0; q<r9; q++ ){ | |
ret.queueEvent('{"method":"cube.fillRect","params":['+id+','+r3+','+r4+','+r5+','+r6+','+r9+'],"id":'+_counter+'}'); | |
_counter++; | |
} | |
ret.queueEvent('{"method":"cube.paint","params":['+id+',0],"id":'+_counter+'}'); | |
_counter++; | |
break; | |
case 1: | |
// lazy,omg,happy | |
for(var q=0; q<r9; q++ ){ | |
var fn = (['omg','happy','lazy','lazy'])[r2]; | |
ret.queueEvent('{"method":"cube.image","params":['+id+',"'+fn+'",'+r3+','+r4+','+r7+','+r8+','+r5+','+r6+',1,0],"id":'+_counter+'}'); | |
_counter++; | |
} | |
ret.queueEvent('{"method":"cube.paint","params":['+id+',0],"id":'+_counter+'}'); | |
_counter++; | |
break; | |
} | |
} | |
return ret; | |
} | |
var net = require('net'); | |
net.createServer(function (socket) { | |
var sim = new Simulator(); | |
socket.name = socket.remoteAddress + ":" + socket.remotePort | |
sim.setCallback( function(data) { | |
// console.log('Sending json: '+data); | |
socket.write(''+data); | |
} ); | |
sim.connect(); | |
sim.queueEvent('{"method":"cube.fill","params":[3,0],"id":1}'); | |
var timer = setInterval(function(){ | |
sim.tick(); | |
sim.popAndSendEvent(); | |
},100); | |
socket.on( 'data', function(data) { | |
var json = ''+data; | |
sim.handleEvent(''+json); | |
sim.popAndSendEvent(); | |
} ); | |
socket.on( 'end', function() { | |
sim.disconnect(); | |
clearInterval(timer); | |
} ); | |
}).listen(7777); | |
console.log("Up and running\n"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment