-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
778 lines (715 loc) · 23.9 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
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
// install and require dependencies
const axios = require('axios');
const express = require('express');
const { Server } = require("socket.io");
//create app to run requests from.
const app = express();
const http = require('http');
const server = http.createServer(app);
// create a websocket from my server
const io = new Server(server);
// nobody is waiting to join a room yet
var queue = [];
// load static files.
app.use(express.static('static'));
// game has not started yet
var gamesStarted = false;
// for bots [depracted]
var API_endpoints = [{"endpoint":'https://MultiSnake-R-500.sojs.repl.co/api/endpoint',"username":"R-500#"}];
//initialize rooms object (entire game runs off of this)
var rooms = {
"classic0":{
"snakes":{
},
"queue":[],
"blocks":[],
"apple":[10,10],
"started":false,
"points_to_next_obs": 5,
"base_points_to_next_obs":5,
"lastvisited": new Date().getTime(),
"by": 25,
"type":"normal",
"towin":10,
"color":"green" // redlight greenlight
}
}
// generate random names for users
function getName(){
var start = [
'flying',
'blue',
'pink',
'jumping',
'invisible',
'red',
'dancing',
'running',
'yellow'
];
var end = [
'Tangerine',
'Orange',
'Helicopter',
'Snake',
'Toad',
'Ninja',
'Goose',
'Banana',
'Duck'
];
var ending = start[Math.floor(Math.random()* start.length)] + end[Math.floor(Math.random()*end.length)] + Math.round(Math.random() * 100);
return ending;
}
// start game loop.
initGame()
io.on('connection', (socket) => {
//a user connected
console.log('a user connected');
// chat message
socket.on('message',(data)=>{
if(data){
if(data.room){
// make sure message is valid
data.msg = (data.msg) ? data.msg : {'content': 'Uh-Oh! Somone just tried to hack and failed!','sender': 'ROBOT','color': 'white'}
// send message to all users.
io.to(data.room).emit('message', data.msg);
}
}
});
socket.on('joining',(data)=>{
// a user joined.
// make sure that all users data is in check & assign defualt values.
var roomtojoin = (data) ? data.room || "classic0" : "classic0";
var username = (data) ? data.name || getName() : getName();
var type = (data) ? data.type || "normal" : "normal";
var by = (data) ? data.by || 25 : 25;
var towin = (data) ? data.towin || 10 : 10;
if(rooms[roomtojoin]){
// the room the snake wants to join exists
// join the channel for that specific room.
socket.join(roomtojoin)
// add the snake to the queue to join the game.
addToQueue({
"blocks": [[2,2]],
"id": Math.random().toString(36).slice(2),
"score":0,
"eating":false,
"dir":2,
"username": username,
"speed":1,
"by": by,
"towin": towin,
"type": "PLAYER"
},roomtojoin)
// assign a new timestamp to the last time the room was joined.
rooms[roomtojoin].lastvisited = new Date().getTime();
}else{
// the room to join does not exist. create it.
rooms[roomtojoin] = {
"snakes":{},
"blocks":[],
"apple":[10,10],
"started":false,
"points_to_next_obs": 5,
"base_points_to_next_obs":5,
"lastvisited": new Date().getTime(),
"type": type,
"by": by,
"towin": towin,
"queue":[],
"color": "green"
}
// join the rooms channel.
socket.join(roomtojoin)
// add the snake to the queue to join the game.
addToQueue({
"blocks": [[2,2]],
"id": Math.random().toString(36).slice(2),
"score":0,
"eating":false,
"dir":2,
"username": username,
"speed":1,
"by": by,
"towin": towin,
"type": "PLAYER"
},roomtojoin)
}
});
socket.on('speed',(data)=>{
// a user just pressed space or lifted it.
// ensure the snakes data is in check
var room = (data) ? data.room || "classic0" : "classic0";
var id = (data) ? data.id || -1 : -1;
// is he pressing space or did he just lift his fingers from it?
var press = (data) ? (data.press || false) : false;
if(rooms[room]){
// room exists
if(rooms[room].snakes[id]){
// snake exists
if(rooms[room].snakes[id] !== "dead_snake" && rooms[room].snakes[id]){
// snake is alive (depracted)
// assign snake new speed based on wether SPACE is pressed. True? speed of 2. False? speed of 1.
// if the room type is redlight greenlight, freeze the snake.
if(rooms[room].type == "redgreen"){
rooms[room].snakes[id].frozen = press;
}else{
rooms[room].snakes[id].speed = (press) ? 2 : 1;
}
}
}
}
})
socket.on('direction',(data)=>{
// a snake just changed direction
// 1 up, 2 right, 3 down, 4 left.
// ensure snakes data is in check
var room = (data) ? data.room || "classic0" : "classic0";
var id = (data) ? data.id || "-1" : "-1";
var dir = (data) ? data.dir || 1 : 1;
if(rooms[room]){
// room exists
if(rooms[room].snakes[id]){
// snake exists
if(rooms[data.room].snakes[id] !== "dead_snake" && rooms[data.room].snakes[id]){
// snake is alive [depracted]
//change snakes direction.
rooms[data.room].snakes[id].dir = data.dir;
}
}
}
})
socket.on('disconnect', () => {
// user disconnected
console.log('user disconnected');
});
});
//adds a snake to queue
function addToQueue(snake,room){
rooms[room].queue.push(
{
"snake":snake,
"room":room
}
);
}
// spawns the next snake in the queue.
function spawnFromQueue(room){
var queue = rooms[room].queue;
var snake = queue[0].snake;
var room = queue[0].room;
rooms[room].snakes[snake.id] = snake;
if(snake.type == "PLAYER"){
// only send this if the snake is a player and not a bot [depracated]
io.to(room).emit('joined',snake.id);
}
// remove the snake that from the queue
queue.shift();
}
// spawn a bot [depracted]
function spawnBot(room){
// choose an AI for it to work off of.
var chosenBot = API_endpoints[Math.floor(Math.random()*API_endpoints.length)];
// add the bot to the queue & give it a username
addToQueue({
"blocks": [[2,2]],
"id": Math.random().toString(36).slice(2),
"score":0,
"eating":false,
"dir":2,
"username": chosenBot.username + Math.round(Math.random()* 999),
"speed":1,
"by": 25,
"towin": 10,
"type": "BOT",
"endpoint":chosenBot.endpoint
},room);
}
// reset a room to defualt values.
function resetRoom(roomkey){
rooms[roomkey].apple = [10,10];
rooms[roomkey].blocks = [];
rooms[roomkey].snakes = {};
rooms[roomkey].started = false;
}
function changeColorToRed(room){
rooms[room].color = "yellow";
io.to(room).emit('color','yellow')
setTimeout(()=>{
rooms[room].color = "red";
io.to(room).emit('color','red')
setTimeout(()=>{
rooms[room].color = "green";
io.to(room).emit('color','green')
}, 2000)
}, 1000)
setTimeout(changeColorToRed,Math.max(Math.round(Math.random()*20*1000),5000),room)
}
//game loop
function initGame(){
// start loop timestamp
var delta = new Date().getTime();
// convert rooms object to array
var rooms1 = json2array(rooms);
rooms1.forEach((room)=>{
if(room.queue.length > 0){
// if snakes are in the queue, spawn one.
spawnFromQueue(room.key);
}
// if more than 1 minute has passed since someone last joined, reset the room.
if((new Date().getTime() - room.lastvisited)/1000 > 60){
resetRoom(room.key);
}else{
//convert snakes to array
var snakesArray = json2array(room.snakes);
if(snakesArray.length < 5){
// if there are not 5 players, spawn a bot in [depracted]
// uncomment line below to put bots in.
//spawnBot(room.key);
}
// if game type is redlight greenlight, get a 8% chance of the light turning red. This will be ~every 10-60 seconds (1 the odds of it happening in a minute are 0.9913. )
// use https://www.calculators.tech/probability-calculator
if(room.type == "redgreen" && room.color == "green" && !rooms[room.key].ticking){
// change the rooms color to "yellow", warning the players it is about to change to red.
// set a timeout to half a second to change the color to red.
rooms[room.key].ticking = true;
setTimeout(changeColorToRed,Math.max(Math.round(Math.random()*20*1000),5000), room.key)
}
snakesArray.forEach((snake)=>{
// loop through snakes
if(snake !== "dead_snake"){
//snake is alive
if(snake.type == "BOT"){
//snake is a bot. uncomment below to add bots in.
// var time1 = new Date().getTime()
// axios
// .post(snake.endpoint, {
// 'room':rooms[room.key],
// 'id':snake.id
// })
// .then(res => {
// if(rooms[room.key].snakes[snake.id]){
// rooms[room.key].snakes[snake.id].dir = res.data.dir;
// }
// console.log(new Date().getTime() - time1)
// })
// .catch(error => {
// });
}else{
// update the last time a person visited.
rooms[room.key].lastvisited = new Date().getTime();
}
// move the player. pass the room and the snakes id.
rooms[room.key].lastvisited = new Date().getTime();
movePlayers(room.key,snake.id);
// hand deaths and winds
checkwin(room.key, snake.id);
// clear the board if a win.
clearWinBoard(room.key, snake.id);
}
});
}
// send board ove to clients.
sendBoard(room.key);
})
// end timestamp
var delta2 = new Date().getTime();
var delta3 = delta2-delta;
// ensure consitent game loop time.
setTimeout(initGame, Math.max(100-delta3,10));
}
function checkwin(roomkey,id){
//get snake object
var snake = rooms[roomkey].snakes[id];
if(snake){
//the snake exists
// set defualt values.
var dead = false;
var win = false;
// room type is redlight greenlight and the light is red and the player is not frozen. Kill him.
if(rooms[roomkey].color == "red" && rooms[roomkey].type == "redgreen" && !snake.frozen){
dead = true;
}
snake = snake.blocks;
var snakeHead = snake[0];
if(
snakeHead[0] > rooms[roomkey].by - 2 || snakeHead[0] < 1 ||
snakeHead[1] > rooms[roomkey].by - 2 || snakeHead[1] < 1
){
// if it is in a wall block, die.
dead = true;
}
snake.forEach((snakeBlock, i)=>{
if(i !== 0){
if(snakeBlock[0] == snakeHead[0] && snakeBlock[1] == snakeHead[1]){
// the snake ran into itself. idiot.
dead = true;
}
}
});
rooms[roomkey].blocks.forEach((block, i)=>{
if(block[0] == snakeHead[0] && block[1] == snakeHead[1]){
//snake ran into a block.
dead = true;
}
});
if(rooms[roomkey].snakes[id].score == rooms[roomkey].towin && !win){
//snake got to the needed score to win. tell the clients.
io.to(roomkey).emit('win',rooms[roomkey].snakes[id]);
gamesStarted = false;
rooms[roomkey].win = true;
}
var snakesArray = json2array(rooms[roomkey].snakes);
snakesArray.forEach((otherSnake)=>{
if(otherSnake.blocks){
if(otherSnake.id !== id){
otherSnake.blocks.forEach((enemyBlock,enemyBlockNumber)=>{
if(enemyBlockNumber == 0 && enemyBlock[0] == snakeHead[0] && enemyBlock[1] == snakeHead[1]){
// the type is not tag. collision of snakes head to head. kill both.
dead = true;
io.to(roomkey).emit('death',otherSnake.id);
delete rooms[roomkey].snakes[otherSnake.id]
}else if(enemyBlock[0] == snakeHead[0] && enemyBlock[1] == snakeHead[1]){
if(rooms[roomkey].type !== "tag"){
// client snake ran into other snake. kill it.
dead = true;
}else{
// type is tag.
if(rooms[roomkey].snakes[id].it){
//if the snake is "it" kill the snake it ran into.
io.to(roomkey).emit('death',otherSnake.id);
delete rooms[roomkey].snakes[otherSnake.id]
}
}
}
})
}
}
});
if(rooms[roomkey].apple[0] == snakeHead[0] && rooms[roomkey].apple[1] == snakeHead[1]){
// the snake ate an apple
if(rooms[roomkey].type == "tag"){
if(!rooms[roomkey].it){
// room type is tag. the snake is now it.
rooms[roomkey].it = id;
}else{
// room type is tag. remove "it" from old snake that was it.
if(rooms[roomkey].snakes[rooms[roomkey].it]){
rooms[roomkey].snakes[rooms[roomkey].it].it = false;
}
}
// assign "it" to client snake
rooms[roomkey].snakes[id].it = true;
rooms[roomkey].it = id;
}
// add one to snake score. spawn new apple.
rooms[roomkey].snakes[id].score +=1;
var newAppleX, newAppleY;
var applepos = getNewApplePos(roomkey);
rooms[roomkey].apple = applepos;
if(rooms[roomkey].points_to_next_obs == 0){
// time to spawn a block in.
if(rooms[roomkey].type == "normal"){
spawnBlock(roomkey);
}
// resent block score counter.
rooms[roomkey].points_to_next_obs = rooms[roomkey].base_points_to_next_obs
}else{
// remove one from block score counter
rooms[roomkey].points_to_next_obs -=1;
}
// the snake is eating. tell the code so.
rooms[roomkey].snakes[id].eating = true;
}
if(dead == true){
// the snake is dead. tell the clients.
io.to(roomkey).emit('death',id);
delete rooms[roomkey].snakes[id]
}
}
}
// get a new position for the apple.
function getNewApplePos(room){
var newAppleX = Math.round((Math.random() * (rooms[room].by - 2 - 1)) + 1);
var newAppleY = Math.round((Math.random() * (rooms[room].by - 2 - 1)) + 1);
if(is_block_occupied([newAppleX,newAppleY],room)){
return getNewApplePos(room)
}else{
return [newAppleX,newAppleY]
}
}
function movePlayers(roomkey,id){
// move a snake.
var snake = rooms[roomkey].snakes[id];
if(snake){
if(!snake.frozen){
//the snake exists
var dir = snake.dir;
var toPop = 0;
// change speed depending on length of snake. If it is only one block long, change speed to 1.
if(snake.speed > 1){
if(snake.blocks.length > 1){
rooms[roomkey].snakes[id].score -= 1;
toPop++;
}else{
rooms[roomkey].snakes[id].speed = 1;
snake.speed = 1;
}
}
// add quantity of blocks to pop depending on speed.
toPop += snake.speed;
var speed = snake.speed;
var snake = snake.blocks;
// append block depending on direction.
switch (dir){
case 1:
for(var i = 0; i < speed; i++){
var snakeHead = rooms[roomkey].snakes[id].blocks[0];
var newCoordsX = snakeHead[0];
var newCoordsY = snakeHead[1];
newCoordsY -= 1;
rooms[roomkey].snakes[id].blocks.unshift([newCoordsX,newCoordsY]);
}
break;
case 2:
for(var i = 0; i < speed; i++){
var snakeHead = rooms[roomkey].snakes[id].blocks[0];
var newCoordsX = snakeHead[0];
var newCoordsY = snakeHead[1];
newCoordsX += 1;
rooms[roomkey].snakes[id].blocks.unshift([newCoordsX,newCoordsY]);
}
break;
case 3:
for(var i = 0; i < speed; i++){
var snakeHead = rooms[roomkey].snakes[id].blocks[0];
var newCoordsX = snakeHead[0];
var newCoordsY = snakeHead[1];
newCoordsY += 1;
rooms[roomkey].snakes[id].blocks.unshift([newCoordsX,newCoordsY]);
}
break;
case 4:
for(var i = 0; i < speed; i++){
var snakeHead = rooms[roomkey].snakes[id].blocks[0];
var newCoordsX = snakeHead[0];
var newCoordsY = snakeHead[1];
newCoordsX -= 1;
rooms[roomkey].snakes[id].blocks.unshift([newCoordsX,newCoordsY]);
}
break;
}
// pop snake depending on predifined quantity
for(var i = 0; i < toPop; i++){
if(!rooms[roomkey].snakes[id].eating){
rooms[roomkey].snakes[id].blocks.pop();
}else{
rooms[roomkey].snakes[id].eating = false;
}
}
}
}
}
// spawn a wall into a room.
function spawnBlock(room){
if(rooms[room]){
if(odds(0.75)){
// spawn block in a random location
if(!rooms[room].blocks[0]){
var blockX = Math.round((Math.random() * (rooms[room].by - 2 - 1)) + 1);
var blockY = Math.round((Math.random() * (rooms[room].by - 2 - 1)) + 1);
if(!is_block_occupied([blockX,blockY],room)){
rooms[room].blocks.push([blockX,blockY])
}else{
// block already exists. try again.
spawnBlock(room);
}
}else{
// spawn a block next to an existing block.
var side = Math.round(Math.random()*3);
var pickedblock = rooms[room].blocks[Math.floor(Math.random()*rooms[room].blocks.length)];
var newblock = [];
switch(side){
case 0:
newblock[0] = pickedblock[0];
newblock[1] = scale2board(pickedblock[1]-1,room);
if(!is_block_occupied([newblock[0],newblock[1]],room)){
rooms[room].blocks.push([newblock[0],newblock[1]]);
}else{
spawnBlock(room)
}
break;
case 1:
newblock[0] = pickedblock[0];
newblock[1] = scale2board(pickedblock[0]-1,room);
if(!is_block_occupied([newblock[0],newblock[1]],room)){
rooms[room].blocks.push([newblock[0],newblock[1]]);
}else{
spawnBlock(room)
}
break;
case 2:
newblock[0] = pickedblock[0];
newblock[1] = scale2board(pickedblock[1]+1,room);
if(!is_block_occupied([newblock[0],newblock[1]],room)){
rooms[room].blocks.push([newblock[0],newblock[1]]);
}else{
spawnBlock(room);
}
break;
case 3:
newblock[0] = pickedblock[0];
newblock[1] = scale2board(pickedblock[0]+1,room);
if(!is_block_occupied([newblock[0],newblock[1]],room)){
rooms[room].blocks.push([newblock[0],newblock[1]]);
}else{
spawnBlock(room)
}
break;
}
}
}else{
// no blocks exist yet... spawn one in a random location
var blockX = Math.round((Math.random() * (rooms[room].by - 2 - 1)) + 1);
var blockY = Math.round((Math.random() * (rooms[room].by - 2 - 1)) + 1);
if(!is_block_occupied[blockX,blockY],room){
rooms[room].blocks.push([blockX,blockY])
}else{
spawnBlock(room)
}
}
}
}
// constrain a number according to the maps walls.
function scale2board(number,room){
return Math.min(Math.max(1,number),rooms[room].by - 2)
}
// return true/false depending on an X% chance
function odds(odd){
return (Math.random() < odd)
}
// send the board over to the clients listening on a specific channel.
function sendBoard(roomkey){
io.to(roomkey).emit('board',{
"snakes":rooms[roomkey].snakes || [],
"apple":rooms[roomkey].apple || [],
"walls":rooms[roomkey].blocks || [],
"type":rooms[roomkey].type || "normal"
});
}
// is a square occupied in a specific room. Determines if it is one of the blocks infront of the spawn, has an apple on it, a snake block, or a wall block. returns true/false.
function is_block_occupied(target,room){
var foundblocks = rooms[room].blocks.find((elem)=>{
return (
(target[0] == elem[0] && target[1] == elem[1]) ||
(target[0] == rooms[room].apple[0] && target[1]==rooms[room].apple[1])
)
});
if((
([2,2][0] == target[0] && [2,2][1] == target[1]) ||
([3,2][0] == target[0] && [3,2][1] == target[0]) ||
([4,2][0] == target[0] && [4,2][1] == target[1]) ||
([5,2][0] == target[0] && [5,2][1] == target[1]) ||
([6,2][0] == target[0] && [6,2][1] == target[1]) ||
([7,2][0] == target[0] && [7,2][1] == target[1]) ||
([8,2][0] == target[0] && [8,2][1] == target[1]) ||
([9,2][0] == target[0] && [9,2][1] == target[1]) ||
([10,2][0] == target[0] && [10,2][1] == target[1]) ||
([11,2][0] == target[0] && [11,2][1] == target[1]) ||
([12,2][0] == target[0] && [12,2][1] == target[1])
)){
return true;
}else{
return (foundblocks) ? true : false;
}
}
//convert json to array.
function json2array(json){
var result = [];
var keys = Object.keys(json);
keys.forEach(function(key){
var endJSON = json[key];
endJSON.key = key
result.push(endJSON);
});
return result;
}
// somebody won. reset the board.
function clearWinBoard(roomkey,id){
var room = rooms[roomkey];
if(room.win){
rooms[roomkey].snakes = {};
rooms[roomkey].blocks = [];
rooms[roomkey].apple = [10,10];
rooms[roomkey].win = false;
}
}
// send index index over at root route.
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
// game endpoint
app.get('/play',(req,res)=>{
res.sendFile(__dirname + '/play.html');
});
// API endpoint
app.get('/api/v1/rooms',(req,res)=>{
var reems = json2array(rooms);
reems = reems.map((reem)=>{
var snakesArray = json2array(reem.snakes);
var reemSnakes = snakesArray.filter((snake)=>{
if(snake !== "dead_snake"){
return true;
}else{
return false;
}
});
return {
"room_key": reem.key,
"type":reem.type,
"lastvisited":reem.lastvisited,
"alive_snakes":reemSnakes,
"snake_quantity": reemSnakes.length,
"points_per_obstacle": reem.base_points_to_next_obs,
"apple_pos":reem.apple,
"current_obstacles":reem.obs,
"room_size":reem.by,
}
})
res.json(reems);
})
// listen.
server.listen(3000, () => {
console.log('listening on *:3000');
});
/*
Art by Marcin Glinski
__..._
..-' o.
.-' :
_..' .'__..--<
...--"" '-.
..-" __.'
.' ___...--'
: ____....---'
: .'
: : _____
: : _..--""" """--..__
: : ." ""i--.
: '.: : '.
: '--...___i---""""--..___.' :
: ""---...---"" :
'. :
'-. :
'--... .'
: ""---....._____.....---""
'. '.
'-.. '.
'. :
: .'
/ :
.' :
.' .--'
'--'
*/