PPCC

Pixel Place Compile Client

目前为 2022-04-23 提交的版本,查看 最新版本

此脚本不应直接安装,它是供其他脚本使用的外部库。如果你需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/443844/1043319/PPCC.js

  1. // ==UserScript==
  2. // @name PPCC
  3. // @description Pixel Place Compile Client
  4. // @version 1.3.2
  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. compile(client, PPML, CWSS, WorkerTimer) {
  21. Object.assign(client, {
  22. ws: null,
  23. map: {},
  24.  
  25. onclick: null,
  26.  
  27. last: [0, 0, 255],
  28. _pixelQueue: [],
  29. _posQueue: 0,
  30. safeEmit(x, y, pixel) {
  31. this._pixelQueue.push(x, y, pixel);
  32. },
  33. send(data) {
  34. CWSS.send.call(client.ws, data);
  35. }
  36. });
  37.  
  38.  
  39.  
  40. PPML.onload = map => {
  41. Object.assign(client.map, map);
  42. client.map.pixels = new Uint8Array(map.pixels);
  43. };
  44.  
  45.  
  46.  
  47. WorkerTimer.setInterval(() => {
  48. while (client._posQueue < client._pixelQueue.length) {
  49. const [x, y, pixel] = client._pixelQueue.slice(client._posQueue, client._posQueue + 3);
  50. client._posQueue += 3;
  51. if (client.map.get(x,y) === 255 || pixel === 255) continue;
  52. if (client.map.get(x,y) === pixel) continue;
  53. CWSS.send.call(client.ws, `42["p",[${x},${y},${pixel},1]]`);
  54. return;
  55. }
  56. client._posQueue = 0;
  57. client._pixelQueue = [];
  58. }, 1e3/30);
  59.  
  60.  
  61.  
  62. CWSS.setHook({
  63. priority: 0,
  64. init() {
  65. if (client.ws) return arguments;
  66. client.ws = this;
  67. return arguments;
  68. },
  69.  
  70. message({data}) {
  71. if (client.ws != this) return arguments;
  72.  
  73. const message = JSON.parse(data.split(/(?<=^\d+)(?=[^\d])/)[1] || '[]');
  74. if (!message.length) return arguments;
  75.  
  76. const [event, json] = message;
  77. if (event == 'canvas' || event == 'p') json.map(p => client.map.set(...p));
  78.  
  79. return arguments;
  80. },
  81.  
  82. send(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 == 'p') {
  90. const [x, y, pixel] = json;
  91. client.last = [x, y, pixel];
  92. if (client.onclick && client.onclick(x, y, pixel) === false) return;
  93. }
  94.  
  95. return arguments;
  96. }
  97. });
  98. }
  99. };
  100. })();
  101. // 0vC4#7152