-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
47 lines (39 loc) · 1.16 KB
/
server.js
File metadata and controls
47 lines (39 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
var app = require('http').createServer(handler);
var io = require('socket.io')(app);
var fs = require('fs');
var Bot = require('./bot');
app.listen(8001);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
Bot.init(function() {
io.on('connection', function (socket) {
socket.emit('botReady', { hello: 'world' });
var lastMove = {
leftFwd: 0,
rightFwd: 0,
leftRev: 0,
rightRev: 0
};
socket.on('botMove', function (thisMove) {
Object.keys(thisMove).forEach(function(direction){
if (thisMove[direction] !== lastMove[direction]) {
if (thisMove[direction]) {
Bot[direction](255);
} else {
Bot[direction](0);
}
}
});
lastMove = thisMove;
});
});
});