-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (56 loc) · 1.95 KB
/
server.js
File metadata and controls
64 lines (56 loc) · 1.95 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var gomoku = require("./minimax.js");
var validator = require('validator');
app.use(express.static('ressources'));
var usersonline = 0;
io.on('connection', function(socket){
socket.on('minimaxrequest', function (data) {
var i;
//Tests for sanity of the field-array
var saneinput = true;
for (i=0; i<225; i++) {
if (!validator.isInt(String(data.currentfield[i]))) {
saneinput = false;
}
}
if (saneinput) {
console.time('timer');
var play = gomoku.main(data.currentfield, data.scorefield);
console.timeEnd('timer');
socket.emit('minimaxanswer', { move: play });
console.log("------roundover-----");
}
else {
console.log("insane input");
console.log(data.currentfield);
}
});
socket.on('coop_request', function() {
function coopdisconnection() {
usersonline --;
console.log("Users in or waiting for Online-Mode: " + String(usersonline));
}
usersonline ++;
console.log("Users in or waiting for Online-Mode: " + String(usersonline));
socket.on('disconnect', coopdisconnection );
//if user clicks on another Mode while waiting for Coop
socket.on('coop_disconnect', function() {
coopdisconnection();
socket.removeListener('disconnect', coopdisconnection);
});
});
//Retransmit Moves in Online-Mode
socket.on('onlinemove', function(move) {
socket.broadcast.emit('onlinemove', move);
});
//Send number of players online every second to everyone
setInterval(function() {
socket.emit('onlinestate', {data: usersonline});
}, 1000);
});
http.listen(80, function(){
console.log('listening on *:80');
});