-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArenaManager.js
38 lines (35 loc) · 1.23 KB
/
ArenaManager.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
import {WebSocket} from 'ws';
import {EventEmitter} from 'events';
export default class ArenaManager extends EventEmitter {
constructor() {
super();
this.lastSeen = [];
this.ws = new WebSocket(`ws://localhost:7844`);
this.ws.on('open', () => {
console.log('⏺️ Opened arena websocket!');
this.emit('connected');
});
this.ws.on('message', data => {
const message = data.toString()
try {
const tags = JSON.parse(message);
for(const tag of tags) {
const existing = this.lastSeen.find(t => t.id === tag.id);
if(existing === undefined) {
this.lastSeen.push(tag);
} else {
existing.x = tag.x;
existing.y = tag.y;
if(existing.in && !tag.in) {
this.emit('leave', tag.id);
}
existing.in = tag.in;
}
if(!tag.in) {
this.emit('gone', tag.id);
}
}
} catch(ignored) {}
});
}
}