forked from anthive/js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.js
36 lines (32 loc) · 1.01 KB
/
run.js
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
var http = require('http');
var url = require('url');
const actions = ["move","eat","load","unload"]
const directions = ["up","down","right","left"]
http.createServer(function(req, res) {
res.writeHead(200, {
'Content-Type': 'application/json'
});
if (req.method === 'POST') {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
let response ={}
//Hive object from request payload
let hive = JSON.parse(body)
//Loop through ants and give orders
for (let antId in hive.ants) {
let random = Math.floor(Math.random() * 4);
response[antId] = {
"act":actions[random],
"dir":directions[random]
}
}
console.log("Orders:",response)
res.end(JSON.stringify(response));
});
} else {
res.end("only POST allowed");
}
}).listen(7070);