Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
475 lines (409 loc) · 17.6 KB
/
Copy pathindex.js
File metadata and controls
475 lines (409 loc) · 17.6 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
app.get('/', function(req, res) {
res.sendFile(__dirname + '/game.html');
});
app.get('/css/style.css', function(req, res) {
res.sendFile(__dirname + '/css/style.css');
});
app.get('/js/canv.js', function(req, res) {
res.sendFile(__dirname + '/js/canv.js');
});
app.get('/favicon.ico', function(req, res) {
res.sendFile(__dirname + '/favicon.ico');
});
var field_height = 2000; //y
var field_width = 2000; //x
var border_max_value = 15; //МАКС количесвто препядствий
var border_max_lenght = 10; //Их МАКС мак длина
var food_max = 50;
var food_value = Math.floor(Math.random() * (food_max - 5) + 5);
var border_value = Math.floor(Math.random() * (border_max_value - 5) + 5); //Количество препядствий ИНИЦИАЛИЗАЦИЯ
var border_lenght; //Длина конкретного борта
var border = [];
var food = [];
function isInteger(num) {
return (num ^ 0) === num;
}
function getRandomArbitrary(min, max) {
var norm = Math.floor(Math.random() * (max - min) + min);
if (isInteger(norm / 20)) return norm;
else return getRandomArbitrary(min, max);
}
var arr_sq = [];
function clear_space() {
try {
for (var ind = 0; ind <= field_height; ind += 20) {
for (var inde = 0; inde <= field_width; inde += 20) {
if ((ind == 0) || (inde == 0) || (ind == field_height - 20) || (inde == field_width - 20)) {
arr_sq[ind.toString() + "_" + inde.toString()] = {
id: ind.toString() + "_" + inde.toString(),
x: ind,
y: inde,
busy_id: "-1",
food: false,
border: true,
};
} else {
arr_sq[ind.toString() + "_" + inde.toString()] = {
id: ind.toString() + "_" + inde.toString(),
x: ind,
y: inde,
busy_id: "-1",
food: false,
border: false,
}
}
}
}
arr_sq["2000_2000"] = {
field_height: field_height,
field_width: field_width
};
console.log(arr_sq["0_0"]);
console.log("Ячейки успешно инициализированы");
} catch (e) {
console.log(e);
}
}
function border_add() {
try {
border_lenght = Math.floor(Math.random() * (border_max_lenght - 5) + 5); //Длина
border[0] = {
x: getRandomArbitrary(0, field_height - border_max_lenght),
y: getRandomArbitrary(0, field_width - border_max_lenght),
};
for (let ind = 1; ind <= border_lenght; ind++) {
border[ind] = {
x: border[ind - 1].x + 20,
y: border[0].y,
};
}
for (let ind = 0; ind <= border_lenght; ind++) {
if (arr_sq[border[ind].x + "_" + border[ind].y].border == false)
arr_sq[border[ind].x + "_" + border[ind].y].border = true;
}
//
console.log(border);
} catch (e) {
console.log("Промах с конкретным бортом");
border_add();
}
}
function food_add() {
try {
var x = getRandomArbitrary(0, field_height - 550);
var y = getRandomArbitrary(0, field_width - 550);
if (arr_sq[x + "_" + y].border == false) {
arr_sq[x + "_" + y].food = true;
} else food_add();
console.log(arr_sq[x + "_" + y].food);
return x + "_" + y;
} catch (e) {
console.log(e);
food_add();
}
}
function food_init() {
try {
for (let ind = 0; ind <= food_value; ind++) {
var x = getRandomArbitrary(0, field_height - 400);
var y = getRandomArbitrary(0, field_width - 400);
if (arr_sq[x + "_" + y].border == false)
arr_sq[x + "_" + y].food = true;
else food_add();
}
console.log(arr_sq[x + "_" + y].food);
} catch (e) {
console.log(e);
}
}
function border_init() {
try {
for (let i = 0; i < border_value; i++) {
border_add();
}
} catch (e) {
border_add();
console.log("Промахнулся с границей");
}
}
//Инициализация
clear_space(); //Расчерчиваем
border_init(); //Добавляем борты
food_init(); //Раскладываем еду
var player = [];
io.on('connection', function(socket) {
var player_num = 0;
try {
console.log('Установлено новое подключние!');
console.log("ID= " + socket.id);
socket.on('player_num_req', function(msg) {
try {
console.log("Пользователь запросил ID");
io.emit('player_num_ans', socket.id);
player_num++;
console.log("Количествео игроков: " + player_num);
} catch (e) {
io.emit("updatepg", 1);
console.log("Поймал ошибку: " + e);
}
});
socket.on('add_user', function(msg) {
try {
player[msg.id] = msg;
console.log(player[msg.id]);
console.log("Добавлен новый пользователь ID=" + msg.id);
io.emit('send border', arr_sq["2000_2000"]);
for (let ind = 0; ind < field_height; ind += 20) {
for (let inde = 0; inde < field_width; inde += 20) {
if (arr_sq[ind + "_" + inde].border == true) {
io.emit('draw', { fillStyle: '#FF0000', x: arr_sq[ind + "_" + inde].x, y: arr_sq[ind + "_" + inde].y });
}
if (arr_sq[ind + "_" + inde].food == true) {
io.emit('draw', { fillStyle: '#00FF00', x: arr_sq[ind + "_" + inde].x, y: arr_sq[ind + "_" + inde].y });
}
if (arr_sq[ind + "_" + inde].busy_id !== "-1") {
io.emit('draw', { fillStyle: '#FFFF00', x: arr_sq[ind + "_" + inde].x, y: arr_sq[ind + "_" + inde].y });
}
}
}
console.log("Поле игрока очищено");
went(msg.id);
} catch (e) {
io.emit("updatepg", 1);
console.log("Поймал ошибку: " + e);
}
});
socket.on('login', function(ans) {
if (ans.pass == 222)
player[ans.id].admin = true;
});
socket.on("windowResized", function() {
try {
socket.emit('send border', arr_sq["2000_2000"]);
for (let ind = 0; ind < field_height; ind += 20) {
for (let inde = 0; inde < field_width; inde += 20) {
if (arr_sq[ind + "_" + inde].border == true) {
io.emit('draw', { fillStyle: '#FF0000', x: arr_sq[ind + "_" + inde].x, y: arr_sq[ind + "_" + inde].y });
}
if (arr_sq[ind + "_" + inde].food == true) {
io.emit('draw', { fillStyle: '#00FF00', x: arr_sq[ind + "_" + inde].x, y: arr_sq[ind + "_" + inde].y });
}
if (arr_sq[ind + "_" + inde].busy_id !== "-1") {
io.emit('draw', { fillStyle: '#FFFF00', x: arr_sq[ind + "_" + inde].x, y: arr_sq[ind + "_" + inde].y });
}
}
}
} catch (e) {
console.log(e);
}
})
socket.on('player_click', function(ans) {
try {
console.log("ID [" + ans.id + "] кликнул X=" + ans.x + " Y=" + ans.y);
if (player[ans.id].admin == true) {
arr_sq[ans.x + "_" + ans.y].border = true;
io.emit('draw', {
id: ans.x,
x: ans.x,
y: ans.y,
fillStyle: "red"
});
}
} catch (e) {
socket.emit("updatepg", 1);
console.log("Поймал ошибку: " + e);
}
});
socket.on('player_rghtclick', function(ans) {
try {
if (player[ans.id].admin == true) {
console.log("ID [" + ans.id + "] кликнул X=" + ans.x + " Y=" + ans.y);
player[ans.id].x = ans.x;
player[ans.id].y = ans.y;
}
} catch (e) {
socket.emit("updatepg", 1);
console.log("Поймал ошибку: " + e);
}
});
socket.on('color_change', function(ans) {
try {
console.log("ID [" + ans.id + "] сменил сцвет " + ans.color);
player[ans.id].color_body = ans.color;
} catch (e) {
io.emit("updatepg", 1);
console.log("Поймал ошибку: " + e);
}
});
socket.on('player_eraser', function(ans) {
try {
if (player[ans.id].admin == true) {
console.log("[ " + ans.id + " ] Стер X=" + ans.x + " Y=" + ans.y);
arr_sq[ans.x + "_" + ans.y].border = false;
io.emit('eraser', {
id: ans.id,
x: ans.x,
y: ans.y,
});
}
} catch (e) {
socket.emit("updatepg", 1);
console.log("Поймал ошибку: " + e);
}
});
socket.on('clear', function(ans) {
try {
io.emit('send border', arr_sq["2000_2000"]);
for (let ind = 0; ind <= field_height; ind += 20) {
for (let inde = 0; inde <= field_width; inde += 20) {
if (arr_sq[ind + "_" + inde].border == true) {
io.emit('draw', { fillStyle: '#FF0000', x: arr_sq[ind + "_" + inde].x, y: arr_sq[ind + "_" + inde].y });
}
if (arr_sq[ind + "_" + inde].food == true) {
io.emit('draw', { fillStyle: '#00FF00', x: arr_sq[ind + "_" + inde].x, y: arr_sq[ind + "_" + inde].y });
}
if (arr_sq[ind + "_" + inde].busy_id !== "-1") {
io.emit('draw', { fillStyle: '#FFFF00', x: arr_sq[ind + "_" + inde].x, y: arr_sq[ind + "_" + inde].y });
}
}
}
console.log("Поле очищено");
} catch (e) {
console.log(e);
}
});
socket.on('key_press', function(keyp) {
try {
console.log("ID [" + keyp.id + "] нажал клавишу " + keyp.par);
if ((player[keyp.id].side !== "top") && (player[keyp.id].side !== "down")) {
if ((keyp.par == 87) || (keyp.par == 38)) {
player[keyp.id].side = "top";
went(keyp.id);
}
if ((keyp.par == 83) || (keyp.par == 40)) {
player[keyp.id].side = "down";
went(keyp.id);
}
}
if ((player[keyp.id].side !== "left") && (player[keyp.id].side !== "right")) {
if ((keyp.par == 65) || (keyp.par == 37)) {
player[keyp.id].side = "left";
went(keyp.id);
}
if ((keyp.par == 68) || (keyp.par == 39)) {
player[keyp.id].side = "right";
went(keyp.id);
}
}
if (keyp.par == 70) {
if (player[keyp.id].admin == true) {
let new_Food = food_add();
io.emit('draw', { fillStyle: '#00FF00', x: arr_sq[new_Food].x, y: arr_sq[new_Food].y });
}
}
} catch (e) {
socket.emit("updatepg", 1);
console.log("Поймал ошибку: " + e);
}
});
function went(id) {
try {
clearInterval(player[id].timer);
player[id].timer = setInterval(function go() {
for (let index = 0; index <= player[id].sq_size; index++) {
player[id].lastX[index] = player[id].lastX[index + 1];
player[id].lastY[index] = player[id].lastY[index + 1];
}
io.emit('eraser', { id: id, x: player[id].lastX[0], y: player[id].lastY[0] });
try {
arr_sq[player[id].lastX[0] + "_" + player[id].lastY[0]].busy_id = '-1';
} catch (e) {
console.log(player[id].lastX[0] + "_" + player[id].lastY[0]);
console.log(arr_sq[player[id].lastX[0] + "_" + player[id].lastY[0]]);
console.log(e);
}
switch (player[id].side) {
case "top":
player[id].y -= 20;
break;
case "down":
player[id].y += 20;
break;
case "left":
player[id].x -= 20;
break;
case "right":
player[id].x += 20;
break;
}
if ((arr_sq[player[id].x + "_" + player[id].y].busy_id == "-1") || (arr_sq[player[id].x + "_" + player[id].y].busy_id == id)) {
if (arr_sq[player[id].x + "_" + player[id].y].border == false) {
if (arr_sq[player[id].x + "_" + player[id].y].food == true) {
player[id].sq_size++;
io.emit("laeder", {
id: player[id].id,
name: player[id].name,
sq_size: player[id].sq_size,
});
let new_Food = food_add();
io.emit('draw', { fillStyle: '#00FF00', x: arr_sq[new_Food].x, y: arr_sq[new_Food].y });
}
player[id].lastX[player[id].sq_size] = player[id].x;
player[id].lastY[player[id].sq_size] = player[id].y;
arr_sq[player[id].lastX[player[id].sq_size] + "_" + player[id].lastY[player[id].sq_size]].busy_id = id;
io.emit("draw", {
fillStyle: player[id].color_head,
id: player[id].id,
x: player[id].x,
y: player[id].y,
head: true
});
for (let index = 1; index < player[id].sq_size; index++) {
io.emit("draw", {
fillStyle: player[id].color_body,
id: player[id].id,
x: player[id].lastX[index],
y: player[id].lastY[index],
head: false
});
}
} else {
console.log("Ударился лбом");
clearInterval(player[id].timer);
}
} else {
console.log("Он врезался в змейку");
clearInterval(player[id].timer);
}
}, 250)
} catch (e) {
console.log(e);
}
}
socket.on('disconnect', function(e) {
try {
clearInterval(player[socket.id].timer);
io.emit('eraser', { id: socket.id, x: player[socket.id].x, y: player[socket.id].y });
arr_sq[player[socket.id].x + "_" + player[socket.id].y].busy_id = '-1';
for (let ind = 0; ind < player[socket.id].sq_size; ind++) {
io.emit('eraser', { id: socket.id, x: player[socket.id].lastX[ind], y: player[socket.id].lastY[ind] });
arr_sq[player[socket.id].lastX[ind] + "_" + player[socket.id].lastY[ind]].busy_id = '-1';
}
console.log('Кто-то ушел, осталось: ' + player.length);
} catch (e) {
socket.emit("updatepg", 1);
console.log("Поймал ошибку: " + e);
}
});
} catch (e) {
socket.emit("updatepg", 1);
console.log("Поймал ошибку: " + e);
}
});
var port = 3003;
http.listen(port, function() {
console.log('listening on:' + port);
})