Newer
Older
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
import { computeErasureIntervals, combineErasureIntervals } from "./erasure.js"
import { connect } from "./room.js"
import XMPP from "./connection/XMPP2.js"
const DEFAULT_ROOM = "imperial"
let room = null
let secureLine = null
let divulgedUpTo = new Map();
function eraseEverythingAtPosition(x, y, radius, room) {
const mousePos = [x, y]
room.getPaths().forEach((points, pathID) => {
const prevPathIntervals =
(room.erasureIntervals || { [pathID]: {} })[pathID] || {}
const newPathIntervals = computeErasureIntervals(
points,
mousePos,
radius,
prevPathIntervals,
)
const erasureIntervalsForPath = combineErasureIntervals(
prevPathIntervals,
newPathIntervals,
)
Object.keys(erasureIntervalsForPath).forEach((pointID) =>
room.extendErasureIntervals(
pathID,
pointID,
erasureIntervalsForPath[pointID],
),
)
})
}
const onRoomConnect = (room_) => {
room = room_
secureLine = new XMPP();
room.addEventListener("addOrUpdatePath", ({ detail: { id, points } }) => {
if (points.length === 0) {
return;
}
let upTo = divulgedUpTo.get(id);
if (upTo === undefined) {
upTo = 0;
}
if (upTo === 0) {
const point = points[0];
const colour = point[3];
const R = parseInt(colour.substring(1, 3), 16);
const G = parseInt(colour.substring(3, 5), 16);
const B = parseInt(colour.substring(5, 7), 16);
secureLine.sneakilySendTheOtherTeamOur(JSON.stringify({
"type": "ADD",
"identifier": id,
"weight": point[2],
"colour": [R, G, B],
"start": [point[0], point[1]]
}));
upTo++;
}
let batch = []
for (; upTo !== points.length; upTo++) {
const point = points[upTo];
batch.push([point[0], point[1]]);
}
if (batch.length !== 0) {
secureLine.sneakilySendTheOtherTeamOur(JSON.stringify({
"type": "APPEND",
"identifier": id,
"points": batch
}));
}
divulgedUpTo.set(id, upTo);
}),
room.addEventListener(
"removedIntervalsChange",
({ detail: { id, intervals, points } }) => {
room.erasureIntervals[id] = combineErasureIntervals(
room.erasureIntervals[id] || {},
intervals,
)
},
)
}
const tryRoomConnect = async (roomID) => {
return await connect(roomID)
.then(onRoomConnect)
.catch((err) => alert(`Error connecting to a room:\n${err}`))
}
const pathIDsByPointerID = new Map()
window.addEventListener("unload", () => {
if (room) {
room.disconnect()
}
})
tryRoomConnect(DEFAULT_ROOM)