forked from cwthompson/mouse_ai_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
77 lines (55 loc) · 1.59 KB
/
app.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
const express = require('express');
const app = express();
const serv = require('http').Server(app);
const mazeArea = require('./maze.js');
const m = require('./mouse.js');
const b = require('./brain.js');
app.set('view engine', 'pug');
app.use(express.static(__dirname + '/public'));
serv.listen(3000);
app.get('/', (req, res) => {
res.render('index', {
showCoordinates: false, // set to true to display room coordinates
mazeHash: maze.getMazeHash(),
maze: maze.getRooms(),
mouse: mouse,
showCoordinates: false
});
});
app.get('/maze/:id', (req, res) => {
maze.load = true;
maze.hash = req.params.id;
maze.init();
mouse.number = 0;
mouse.updateLocation(maze.getStartZone());
res.render('maze', {
maze: maze.getRooms(),
mouse: mouse,
showCoordinates: false
});
});
// - setup -----------------------------------
var brain;
var maze = mazeArea.getMaze();
var mouse = m.getMouse();
mouse.updateLocation(maze.getStartZone());
// - socket -----------------------------------
var io = require('socket.io')(serv,{});
io.sockets.on('connection', function(socket){
brain = b.getBrain(socket, mouse, maze);
socket.on('startMouse',function(data){
mouse.steps = 0;
mouse.resetMouse();
mouse.updateLocation(maze.getStartZone());
brain.start();
});
socket.on('stopMouse',function(data){
brain.stop();
});
socket.on('loadMaze',function(data){
maze.load = true;
maze.hash = data.hash;
maze.loadArea();
brain.stop();
});
});