-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
95 lines (72 loc) · 2.32 KB
/
server.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const util = require('util');
const http = require('http')
const port = 3000
const spawn = require('child_process').spawn;
const swipl = spawn('swipl', ['matchmaking.pl']);
var express = require('express')
var bodyParser = require("body-parser");
var app = express()
var responseString = "";
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
swipl.stdout.on('data', (data) => {
responseString = responseString + `${data}`;
console.log(`${data}`);
});
swipl.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
swipl.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
swipl.stdin.setEncoding('utf-8');
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
app.post('/addRequest', function(req, res){
console.log(req.body);
var playerId = req.body.playerId;
var playerLevel = req.body.playerLevel;
var playerString = util.format("assert(player(%s))", playerId)
var playerLevelString = util.format("assert(level(%s, %s))", playerId, playerLevel)
console.log(playerString);
console.log(playerLevelString);
swipl.stdin.write(playerString + ", " + playerLevelString + ".\n");
res.send('Request accepted')
})
app.get('/listing', function(req, res){
swipl.stdin.write("listing.\n");
res.send("listed");
})
app.get('/match', function(req, res){
console.log(req.query);
responseString = "";
var size = req.query.size || 2;
var scoreThreshold = req.query.scoreThreshold || 1;
swipl.stdin.write("\n");
var queryString = util.format("match(Players, Score, %s, %s).\n", scoreThreshold, size);
swipl.stdin.write(queryString);
setTimeout(function(){
res.send(responseString)
}, 1000);
})
app.get('/', function (req, res) {
res.send('Hello World!')
})
/*
const requestHandler = (request, response) => {
console.log(request.url);
response.end('Hello Node.js Server!')
swipl.stdin.write("assert(will(rocks)).\n");
swipl.stdin.write("will(X).\n");
//swipl.stdin.write("assert(will(rocks))\n");
//swipl.stdin.end(); /// this call seems necessary, at least with plain node.js executable
}
const server = http.createServer(requestHandler)
server.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log("server is listening on ${port}")
})
*/