-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-server.js
executable file
·232 lines (200 loc) · 6 KB
/
test-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
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
let express = require('express'); // web server application
let app = express(); // webapp
let http = require('http').Server(app); // connects http library to server
let io = require('socket.io')(http); // connect websocket library to server
// let serialPort = require('serialport'); // serial library
// let readLine = serialPort.parsers.Readline; // read serial data as lines
let serverPort = 8000;
// start the serial port connection and read on newlines
// const serial = new serialPort('/dev/ttyUSB0', {
// baudRate: 115200
// });
// const parser = new readLine({
// delimiter: '\r\n'
// });
//---------------------- WEBAPP SERVER SETUP ---------------------------------//
// use express to create the simple webapp
app.use(express.static('public')); // find pages in public directory
// start the server and say what port it is on
http.listen(serverPort, function() {
console.log('listening on *:%s', serverPort);
});
app.get('/', function(req, res) {
res.sendFile('./public/index.html');
});
//----------------------------------------------------------------------------//
//------------------------------- GAME STATES --------------------------------//
// game environment variables
let boardHeight = 8;
let boardWidth = 8;
let dirs = {
U: [-1, 0],
D: [1, 0],
L: [0, -1],
R: [0, 1]
};
// connections
let clientReady = false;
let arduinoReady = false;
// game state variables
let gameInPlay = false;
let score = 0;
let foodPos = null; // when food is eaten => null
let dir = 'R'; // 'U', 'D', 'L', 'R', randomize on start?
let initialPos = [[3, 0], [3, 1], [3, 2]];
let snakeBody = [];
let snakeBodySet = new Set();
let _startGame;
let _gameLoop = () => {
_calculateNextState();
console.log('snake at', snakeBody);
_updateClient(); // update client
// _updateArduino(); // update arduino
console.log('Cant stop me now!');
};
let _numsToString = pos => {
return `${pos[0]},${pos[1]}`;
};
let _resetStates = () => {
score = 0;
foodPos = null; // when food is eaten => null
dir = 'R'; // 'U', 'D', 'L', 'R', randomize on start?
snakeBody = initialPos.slice();
console.log(snakeBody);
snakeBodySet = new Set();
};
let _updateClient = () => {
clientGameStatus = {
gameInPlay: gameInPlay,
snakeBody: snakeBody,
foodPos: foodPos,
score: score
};
io.emit('gameUpdate', clientGameStatus);
};
let _calculateNextState = () => {
let curHead;
let newHead;
let curI;
let curJ;
let dI;
let dJ;
let i;
let j;
curHead = snakeBody[snakeBody.length - 1];
[curI, curJ] = curHead;
console.log(dir);
[dI, dJ] = dirs[dir];
[i, j] = [curI + dI, curJ + dJ];
newHead = [i, j];
// check if game over
if (
i < 0 ||
i == boardHeight ||
j < 0 ||
j == boardWidth ||
(snakeBodySet.has(_numsToString(newHead)) && newHead != snakeBody[0])
) {
// game over
gameInPlay = false;
console.log('game over');
clearInterval(_startGame);
return;
}
if (foodPos && (foodPos[0] == newHead[0] && foodPos[1] == newHead[1])) {
score += 1;
foodPos = null;
} else {
let tail = snakeBody.shift();
snakeBodySet.delete(_numsToString(tail));
}
snakeBody.push(newHead);
snakeBodySet.add(_numsToString(newHead));
};
//----------------------------------------------------------------------------//
//---------------------- SERIAL COMMUNICATION --------------------------------//
// Read data that is available on the serial port and send it to the websocket
// serial.pipe(parser);
//
// parser.on('data', data => {
// // on data from the arduino
// if (data == 'up' && dir != 'U' && dir != 'D') {
// dir = 'U';
// } else if (data == 'down' && dir != 'D' && dir != 'U') {
// dir = 'D';
// } else if (data == 'left' && dir != 'L' && dir != 'R') {
// dir = 'L';
// } else if (data == 'right' && dir != 'R' && dir != 'L') {
// dir = 'R';
// } else if (data == 'press') {
// if (!arduinoReady) {
// arduinoReady = true;
//
// // if clientReady, start game
// if (clientReady) {
// gameInPlay = true;
// console.log('gameInPlay:', true);
//
// _resetStates();
// _initializeArduino();
//
// // start game, 1000 millisecs/frame
// _startGame = setInterval(_gameLoop, 1000);
// }
// }
// }
// });
//
// let _initializeArduino = () => {
// //send initial matrix to arduino and client
// let stringTosend = 'matrix:' + snakeBody.toString();
// serial.write(stringTosend, function(err) {
// if (err) {
// return console.log('Error on write: ', err.message);
// }
// console.log('message written');
// });
// };
//
// let _updateArduino = () => {
// // send game update to arduino
// var stringTosend = 'matrix:' + snakeBody.toString();
// if (foodPos) {
// stringTosend += ';foodPosition:' + foodPos.toString();
// }
// //console.log(stringTosend);
//
// serial.write(stringTosend, function(err) {
// if (err) {
// return console.log('Error on write: ', err.message);
// }
// console.log('message written');
// });
// };
//----------------------------------------------------------------------------//
//---------------------- WEBSOCKET COMMUNICATION -----------------------------//
io.on('connect', function(socket) {
// call reset to make sure the website is clean
socket.emit('reset');
clientReady = true;
console.log('connected to client');
if (clientReady) {
_resetStates();
gameInPlay = true;
console.log('gameInPlay:', true);
_startGame = setInterval(_gameLoop, 200);
}
// can only listen for foodPlaced event via socket here
// b.c. io is to many, socket is to one
// => can't use io.on('foodPlaced', ...) outside io.on('connect', ...)
// but can do io.emit('event', ...) outside io.on('connect', ...)
socket.on('foodPlaced', pos => {
foodPos = pos;
console.log('food received:', foodPos);
});
// if you get the 'disconnect' message, say the user disconnected
io.on('disconnect', function() {
// disconnect arduino
console.log('disconnected from client');
});
});