// ==UserScript==
// @name PPCC
// @description Pixel Place Compile Client
// @version 1.5.1
// @author 0vC4
// @namespace https://gf.qytechs.cn/users/670183
// @match https://pixelplace.io/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=pixelplace.io
// @license MIT
// @grant none
// @run-at document-start
// ==/UserScript==
const PPCC = (() => {
const log = console.log;
const PPCC = {};
PPCC.config = ({packetSpeed, timer}) => {
Object.assign(PPCC, {
packetSpeed,
timer,
});
};
PPCC.compile = (client, PPML, CWSS) => {
const {speed, timer} = PPCC;
Object.assign(client, {
_id: 0,
_posSocket: 0,
sockets: [],
getSocket() {
let i = 0;
let ws = null;
while (i++ < this.sockets.length) {
if (this._posSocket > this.sockets.length-1) this._posSocket = 0;
const _ws = this.sockets[this._posSocket++];
if (!_ws._can) continue;
ws = _ws;
break;
}
return ws;
},
ws: null,
map: {},
onclick: null,
last: [0, 0, 255],
lock: false,
_pixelQueue: [],
_posQueue: 0,
_queueId: 0,
safeEmit(x, y, p) {
client._pixelQueue.push([x, y, p, client._queueId++]);
}
});
let progress = false;
timer.setInterval(() => {
if (progress) return;
progress = true;
while (client._posQueue < client._pixelQueue.length) {
const [x, y, p, i] = client._pixelQueue[client._posQueue++];
if (p === 255 || client.map.get(x,y) === 255) continue;
if (client.map.get(x,y) === p) continue;
const ws = client.getSocket();
if (!ws) {
client._posQueue--;
progress = false;
return;
}
CWSS.send.call(ws, `42["p",[${x},${y},${p},1]]`);
ws._can = false;
continue;
}
if (client.lock && client._posQueue > client._pixelQueue.length-1) {
client._posQueue = 0;
progress = false;
return;
}
client._posQueue = 0;
client._pixelQueue = [];
client._queueId = 0;
progress = false;
});
PPML.onload = map => {
Object.assign(client.map, map);
client.map.pixels = new Uint8Array(map.pixels);
};
PPCC.hook = {
priority: 0,
init() {
if (client.ws) return arguments;
client.ws = this;
return arguments;
},
open() {
client.sockets.push(this);
this.id = client._id++;
this.addEventListener('close', () => {
timer.clearInterval(this._inter);
client.sockets.splice(client.sockets.indexOf(this),1);
});
this._can = true;
this._inter = timer.setInterval(() => {
this._can = true;
}, 1e3/speed);
return arguments;
},
message({data}) {
if (client.ws != this) return arguments;
const message = JSON.parse(data.split(/(?<=^\d+)(?=[^\d])/)[1] || '[]');
if (!message.length) return arguments;
const [event, json] = message;
if (event == 'canvas' || event == 'p') json.map(p => client.map.set(...p));
return arguments;
},
send(data) {
if (client.ws != this) return arguments;
const message = JSON.parse(data.split(/(?<=^\d+)(?=[^\d])/)[1] || '[]');
if (!message.length) return arguments;
const [event, json] = message;
if (event == 'p') {
const [x, y, pixel] = json;
client.last = [x, y, pixel];
if (client.onclick && client.onclick(x, y, pixel) === false) return;
}
return arguments;
}
};
CWSS.setHook(PPCC.hook);
};
return PPCC;
})();
// 0vC4#7152