-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.js
77 lines (67 loc) · 2.3 KB
/
web.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
var PORT = 8001;
var HOST = null;
var sys = require("sys"),
http = require("http"),
io = require("socket.io"),
games = require("./server/play-server-index");
var server = http.createServer(function(req, res) {
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.end("Allo oui j'ecoute ?\n");
});
server.listen(Number(process.env.PORT || PORT), HOST);
console.log("port :"+process.env.PORT || PORT+" host"+HOST);
var socket = io.listen(server),
game = games.listen(socket, {});
var player = [];
var rooms = {};
var direction = ['up', 'down', 'left', 'right'];
var color = ['yellow', 'green', 'blue', 'red'];
initRoom();
function registerClient(client) {
rooms[currentRoom].push(client);
if (rooms[currentRoom].length == 2) {
launchGame(currentRoom);
initRoom();
}
}
function initRoom(){
currentRoom = Math.floor(Math.random() * 50000) + 1;
rooms[currentRoom] = [];
}
function launchGame(room) {
console.log("launching the game");
rooms[room].forEach(function(client, index) {
console.log("launching the game for client" + client + " direction " + direction[index] + " room " + room);
client.send({'event':'init','data':{
"direction": direction[index],
"color": color[index],
"room": room
}});
});
}
game.on('connection', function(client) {
console.log("Connection");
registerClient(client);
});
game.on("move",function(client,data){
console.log({
"x": data.x,
"y": data.y,
"color": data.color
});
rooms[data.room].forEach(function(c, index) {
if (c.sessionId != client.sessionId) {
game.infoMove(c,data);
}
});
});
game.on("loose",function(client,data){
rooms[data.room].forEach(function(c, index) {
if (c.sessionId != client.sessionId) {
game.infoMessage(c,"Congratulations, you win the game !!! ");
}
});
});