-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
200 lines (174 loc) · 5.79 KB
/
index.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
var express = require('express');
const app = express();
var http = require('http').Server(app);
var path = require('path');
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;
var maze = require('./maze');
app.use(express.static(path.join(__dirname, 'static')));
function randomId(checkList) {
let newId = 0;
do {
newId = Math.floor(Math.random()*1000);
} while (newId in checkList);
return newId;
}
let users = {};
let games = {};
io.on('connection', function(socket) {
console.log(socket.id);
const userId = randomId(users);
let gameId;
users[userId] = {
_id: userId,
socket: socket,
};
socket.emit('setId', userId);
socket.on('disconnect', function() {
console.log('disconnect');
console.log(`User ${userId} disconnected.`);
delete users[userId];
console.log(games, gameId);
if (gameId != undefined && gameId in games) {
for (let index in games[gameId].playersIds) {
let uid = games[gameId].playersIds[index];
if (userId !== uid){
let user = users[uid];
let userSocket = user.socket;
console.log(`Sending disconnected emit to ${userSocket}.`);
userSocket.emit('disconnected', userId);
}
}
delete games[gameId];
gameId = undefined;
}
});
socket.on('setName', function(name) {
console.log(`setName: ${name}`);
users[userId].name = name;
});
socket.on('createGame', function() {
console.log('createGame');
gameId = randomId(games);
games[gameId] = {
playersIds: [ userId ]
};
users[userId].gameId = gameId;
socket.join(gameId);
socket.emit('createGameCallback', {
_id: gameId,
user: {
_id: userId,
name: users[userId].name
}
});
});
socket.on('joinGame', function(id) {
console.log(`joinGame: ${id}`);
if (id in games) {
games[id].playersIds.push(userId);
gameId = id;
users[userId].gameId = id;
socket.to(id).emit('addCollab', {
_id: userId,
name: users[userId].name
});
socket.join(id);
let collaborators = [];
for (const index in games[id].playersIds) {
const uid = games[id].playersIds[index];
const u = users[uid];
collaborators = [ ...collaborators, { _id: u._id, name: u.name }];
}
socket.emit('joinGameCallback', { _id: id, collaborators: collaborators });
} else {
socket.emit('joinGameCallback', false);
}
});
socket.on('exitGame', function() {
console.log('exitGame');
if (gameId in games) {
// remove user from game
const index = games[gameId].playersIds.indexOf(userId);
if (index > -1) {
games[gameId].playersIds.splice(index, 1);
}
}
socket.leave(gameId);
socket.to(gameId).emit('removeCollab', userId);
gameId = 0;
users[userId].gameId = 0;
socket.emit('exitGameCallback', false);
});
socket.on('startGame', function() {
const D_dimensions = games[gameId].playersIds.length + 1;
games[gameId].game = maze.build_game(9, D_dimensions);
let dim = 0;
for (let index in games[gameId].playersIds) {
const gameUserId = games[gameId].playersIds[index];
let dim_x = dim % D_dimensions;
let dim_y = (dim + 1) % D_dimensions;
users[gameUserId].dim_x = dim_x;
users[gameUserId].dim_y = dim_y;
let gameUser = users[gameUserId];
let userSocket = gameUser.socket;
let userInfo = newInfo(gameUser, games[gameId].game);
userSocket.emit('gameStarted', userInfo);
++dim;
}
});
socket.on('movement', function(movement){
makeMovementAndNewInfo(users[userId], games[gameId], movement);
});
});
function makeMovementAndNewInfo(user, game, movement) {
let dim_x = user.dim_x;
let dim_y = user.dim_y;
game.game.position[dim_x] += movement[0];
game.game.position[dim_y] += movement[1];
let emission = finalPosition(game.game.position, game.game.goal);
for (let index in game.playersIds) {
let user = users[game.playersIds[index]];
let userSocket = user.socket;
let userInfo = newInfo(user, game.game);
userSocket.emit('gameUpdated', userInfo);
if (emission) userSocket.emit('finalPosition');
}
}
function finalPosition(position, goal) {
for (let i = 0; i < position.length; ++i) {
if (position[i] !== goal[i]) return false;
}
return true;
}
function newInfo(user, game) {
let dim_x = user.dim_x;
let dim_y = user.dim_y;
let position = game.position;
let goal = game.goal;
return {
blocks: game.t,
dimensions: game.d,
map: maze.get_projection(game.maze, position, dim_x, dim_y),
position: [position[dim_x], position[dim_y]],
goal: {
position: [goal[dim_x], goal[dim_y]],
same_proj: isOnSameProjection(position, goal, dim_x, dim_y, game.d)
},
overall: {
player: position,
goal: goal
},
coordinates: [dim_x, dim_y]
};
}
function isOnSameProjection(position, goal, dim_x, dim_y, d) {
for (let i = 0; i < d; ++i) {
if (i !== dim_x && i !== dim_y && position[i] !== goal[i])
return false;
}
return true;
}
http.listen(port, function(){
console.log(`listening on *:${port}`);
});