-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
41 lines (30 loc) · 870 Bytes
/
client.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
import WebSocket from 'ws';
import c from './constants.js';
function a(n) {
return Array.apply(0, new Array(n));
}
let state = a(9).map(() => a(16).map(() => 0));
let walls = a(9).map(() => a(16).map(() => 0));
let ws = new WebSocket('ws://localhost:8080/');
function send(message) {
ws.send(JSON.stringify(message));
}
ws.on('open', function() {
});
ws.on('message', function(message) {
console.log('received: %s', message);
message = JSON.parse(message);
switch (message.type) {
case 'START':
if (message.firstPlayer) {
send({ type: "STATE", state: state, walls: walls });
send({ type: "MOVE", move: { 5: [[1, 1], [1, 2]] } });
}
break;
case 'STATE':
state = message.state;
walls = message.walls;
send({ type: "MOVE", move: { 9: [[1, 3], [1, 4]], 10: [[1, 5]] } });
break;
}
});