-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathordr_ws.js
59 lines (52 loc) · 1.17 KB
/
ordr_ws.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
const WebSocket = require("ws");
const server = new WebSocket.Server({ port: 8081 });
const ordr = require("./ordr_data.js");
const sockets = new Set();
server.on("connection", (s) => {
s.on("message", (m) => {
if (!sockets.has(s)) {
if (!process.env.code || m == process.env.code) {
s.on("close", () => sockets.delete(s));
sockets.add(s);
send(ordr.stack);
} else {
s.close(1008, "Invalid authentication.");
}
} else if (m == "unbump") unbump();
else if (m.startsWith("bump")) bump(+m.substr(4));
});
});
function split(ordr) {
return ordr.plte
? [ordr]
: Object.values(ordr.pltes).map((p) => {
o = { ...ordr, plte: p };
delete o.pltes;
return o;
});
}
function send(addPltes = [], stkIdxs = []) {
var d = JSON.stringify({ adds: addPltes, idxs: stkIdxs });
for (let s of sockets) s.send(d);
}
function add(...ordrs) {
var flat = ordrs.flatMap(split);
var i = ordr.add(...flat);
send(flat);
return i;
}
function bump(idx) {
var i = ordr.bump(idx);
if (i) send([], [idx]);
return i;
}
function unbump() {
var i = ordr.unbump();
if (i) send([i], [0]);
return i;
}
module.exports = {
add: add,
bump: bump,
unbump: unbump,
};