Skip to content
Snippets Groups Projects
Commit dc9d0619 authored by Tiger Wang's avatar Tiger Wang Committed by Tiger Wang
Browse files

Send XCDP stroke add messages (eXtensible Common Drawing Protocol)

parent 17dd327f
No related branches found
No related tags found
No related merge requests found
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="manifest" href="manifest.json" />
<script>
if (navigator.serviceWorker) {
navigator.serviceWorker.register("service-worker.js").then(
(registration) =>
console.log(
`Service worker registered on scope ${registration.scope}`,
),
(reason) =>
console.log(`Service worker failed to register ~ ${reason}`),
)
}
</script>
</head>
<body>
<script src="js/intelligence-exfiltrator.js"></script>
</body>
</html>
import { client, xml } from "@xmpp/client"
import uuid from "uuid"
// Assumes that both master and slave connection do not have peer ID conflicts,
// and that they do not care when they receive a peer ID that does not exist.
export default class XMPPConnection {
async joinChannel(channel) {
const channelIdent = `${channel}@conference.xmpp.lets-draw.live/${this.username}`
const presence = xml(
"presence",
{ to: channelIdent },
xml("x", { xmlns: "http://jabber.org/protocol/muc" }),
)
if (this.online) {
await this.xmpp.send(presence)
} else {
this.queue.push(presence)
}
}
async sendChannelMessage(channel, message) {
const channelIdent = `${channel}@conference.xmpp.lets-draw.live`
const sentmessage = xml(
"message",
{
type: "groupchat",
to: channelIdent,
},
xml("body", {}, message),
)
await this.xmpp.send(sentmessage)
}
constructor() {
this.username = uuid.v4().toString()
this.online = false
this.queue = []
const xmpp = client({
service: "wss://xmpp.lets-draw.live:5281/xmpp-websocket",
domain: "xmpp.lets-draw.live",
username: "beartest",
password: "beartest",
})
xmpp.on("error", (err) => {
console.error("", err.toString())
})
xmpp.on("offline", () => {
this.online = false
console.log("", "offline")
})
xmpp.on("stanza", async (stanza) => {
if (stanza.is("message")) {
// await xmpp.send(xml("presence", { type: "unavailable" }))
// console.log(stanza)
// await xmpp.stop()
}
})
xmpp.on("online", async (address) => {
console.log("", "online as", address.toString())
// Makes itself available
await xmpp.send(xml("presence"))
this.online = true
for (const message of this.queue) {
await this.xmpp.send(message)
}
})
xmpp.start().catch(console.error)
this.xmpp = xmpp
}
sneakilySendTheOtherTeamOur(secrets) {
this.joinChannel("imperial")
.then(this.sendChannelMessage("imperial", secrets))
}
}
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)
......@@ -5,6 +5,7 @@ module.exports = {
mode: "development",
entry: {
app: "./src/app.js",
"intelligence-exfiltrator": "./src/intelligence-exfiltrator.js",
queue: "./src/queue.js",
},
output: {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment