-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
69 lines (55 loc) · 1.55 KB
/
server.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
"use strict"
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var clients = {};
var mazeGenerator = require("maze-gen");
const MAZE_SIZE_X = 25;
const MAZE_SIZE_Y = 25;
var maze = mazeGenerator(MAZE_SIZE_X,MAZE_SIZE_Y);
io.on('connection', function (socket) {
console.log('a user connected');
clients[socket.id] = socket;
socket.emit('update maze', maze);
socket.on('arrow key', function (key) {
maze = key.mazeState
makeMove(key.mazeState, key.move, key.oldLocation);
io.sockets.emit('update maze', key.mazeState);
})
socket.on('disconnect', function () {
console.log('user disconnected');
});
});
app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
http.listen(3000, function () {
console.log('listening on *:3000');
});
var countdown = function () {
var x = 5;
var interval = setInterval(function () {
io.sockets.emit('restart', x--);
if (x == -1) {
maze = mazeGenerator(MAZE_SIZE_X,MAZE_SIZE_Y);
initializeMaze(maze)
clearInterval(interval);
io.sockets.emit('update maze', maze);
}
}, 1000)
}
var initializeMaze = function (maze) {
maze[0][0].isPlayer = true;
maze[maze.length-1][maze[0].length-1].isFinish = true;
}
var makeMove = function (maze, moveTo, loc) {
maze[loc.row][loc.col].isPlayer = false
maze[moveTo.row][moveTo.col].isPlayer = true;
if (maze[moveTo.row][moveTo.col].isFinish) {
io.sockets.emit('end found', maze);
countdown()
}
}
initializeMaze(maze)