PPCC

Pixel Place Compile Client

当前为 2022-04-24 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/443844/1043551/PPCC.js

  1. // ==UserScript==
  2. // @name PPCC
  3. // @description Pixel Place Compile Client
  4. // @version 1.4.6
  5. // @author 0vC4
  6. // @namespace https://greasyfork.org/users/670183
  7. // @match https://pixelplace.io/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=pixelplace.io
  9. // @license MIT
  10. // @grant none
  11. // @run-at document-start
  12. // ==/UserScript==
  13.  
  14.  
  15.  
  16.  
  17.  
  18. const PPCC = (() => {
  19. return {
  20. speed: 30,
  21. compile(client, PPML, CWSS, timer=window) {
  22. const speed = this.speed;
  23. Object.assign(client, {
  24. ws: null,
  25. map: {},
  26.  
  27. onclick: null,
  28.  
  29. last: [0, 0, 255],
  30. lock: false,
  31. _pixelQueue: [],
  32. _posQueue: 0,
  33. safeEmit(x, y, pixel) {
  34. this._pixelQueue.push(x, y, pixel);
  35. },
  36. send(data) {
  37. CWSS.send.call(client.ws, data);
  38. },
  39. liveQueue(client, ws) {
  40. const t = timer.setInterval(() => {
  41. while (client._posQueue < client._pixelQueue.length) {
  42. client._posQueue += 3;
  43. const [x, y, pixel] = client._pixelQueue.slice(client._posQueue-3, client._posQueue);
  44. if (client.map.get(x,y) === 255 || pixel === 255) continue;
  45. if (client.map.get(x,y) === pixel) continue;
  46. CWSS.send.call(ws, `42["p",[${x},${y},${pixel},1]]`);
  47. return;
  48. }
  49. if (client.lock && client._posQueue > client._pixelQueue.length-1) {
  50. client._posQueue = 0;
  51. return;
  52. }
  53. client._posQueue = 0;
  54. client._pixelQueue = [];
  55. }, 1e3/speed);
  56. ws.addEventListener('close', () => timer.clearInterval(t));
  57. },
  58. });
  59.  
  60.  
  61.  
  62. PPML.onload = map => {
  63. Object.assign(client.map, map);
  64. client.map.pixels = new Uint8Array(map.pixels);
  65. };
  66.  
  67.  
  68.  
  69. CWSS.setHook({
  70. priority: 0,
  71. init() {
  72. if (client.ws) return arguments;
  73. client.ws = this;
  74. return arguments;
  75. },
  76. open() {
  77. if (client.ws !== this) return arguments;
  78. client.liveQueue(client, client.ws);
  79. return arguments;
  80. },
  81.  
  82. message({data}) {
  83. if (client.ws != this) return arguments;
  84.  
  85. const message = JSON.parse(data.split(/(?<=^\d+)(?=[^\d])/)[1] || '[]');
  86. if (!message.length) return arguments;
  87.  
  88. const [event, json] = message;
  89. if (event == 'canvas' || event == 'p') json.map(p => client.map.set(...p));
  90.  
  91. return arguments;
  92. },
  93.  
  94. send(data) {
  95. if (client.ws != this) return arguments;
  96.  
  97. const message = JSON.parse(data.split(/(?<=^\d+)(?=[^\d])/)[1] || '[]');
  98. if (!message.length) return arguments;
  99.  
  100. const [event, json] = message;
  101. if (event == 'p') {
  102. const [x, y, pixel] = json;
  103. client.last = [x, y, pixel];
  104. if (client.onclick && client.onclick(x, y, pixel) === false) return;
  105. }
  106.  
  107. return arguments;
  108. }
  109. });
  110. }
  111. };
  112. })();
  113. // 0vC4#7152