-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_pong_websocket.ts
125 lines (95 loc) · 4.01 KB
/
run_pong_websocket.ts
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import {loadImage, createCanvas} from 'canvas';
import { BitmapClient } from './client';
const startIndex = 262144;
const page = 2;
async function main() {
// Function to convert image to black and white and map it to a binary array
async function convertToBlackAndWhite(imagePath: string, targetWidth: number, startIndex=0, threshold = 128) {
const image = await loadImage(imagePath);
const targetHeight = Math.floor(image.height * (targetWidth / image.width)) + 16;
const canvas = createCanvas(targetWidth, targetHeight);
const ctx = canvas.getContext('2d');
ctx.drawImage(image, 0, 0, targetWidth, targetHeight);
const imageData = ctx.getImageData(0, 0, targetWidth, targetHeight);
const binaryPixels = new Array(targetWidth * targetHeight).fill(0);
const gridWidth = 60;
const startRow = Math.floor(startIndex / gridWidth);
const startCol = startIndex % gridWidth;
for (let row = 0; row < targetHeight; row++) {
for (let col = 0; col < targetWidth; col++) {
const pixelIndex = row * targetWidth + col;
const gridRow = startRow + Math.floor(pixelIndex / gridWidth);
const gridCol = (startCol + col) % gridWidth;
const originalIndex = (row * targetWidth + col) * 4;
const grayscale = (imageData.data[originalIndex] + imageData.data[originalIndex + 1] + imageData.data[originalIndex + 2]) / 3;
binaryPixels[gridRow * gridWidth + gridCol] = grayscale < threshold ? 1 : 0; // Apply threshold
}
}
let trimmedBinaryPixels = binaryPixels.slice(startIndex);
return { binaryPixels: trimmedBinaryPixels, width: targetWidth, height: targetHeight };
}
async function navigateAndWait(client: BitmapClient, page: number, retries: number = 15000) {
// Wait for the websocket to open
let currentRetry = 0;
if (!client.websocketOpen) {
while (true) {
if (currentRetry >= retries) break;
if (client.websocketOpen) break;
currentRetry += 100;
await new Promise(r => setTimeout(r, 100));
}
}
// Wait for the chunk index to change
client.setChunkIndex(page - 1);
currentRetry = 0;
if (!client.chunkLoaded) {
while (true) {
if (currentRetry >= retries) break;
if (client.chunkLoaded) break;
currentRetry += 100;
await new Promise(r => setTimeout(r, 100));
}
}
}
let client = new BitmapClient();
await navigateAndWait(client, page);
let combatMode = false;
async function drawImage() {
let chosenBinaryPixels;
try {
let { binaryPixels, width, height } = await convertToBlackAndWhite( "./images/pong.png", 60, startIndex, 128 );
chosenBinaryPixels = binaryPixels;
}
catch (err) {
return
}
return new Promise( (resolve, reject) => {
for (let index = 0; index < chosenBinaryPixels.length; index++) {
let pixel = chosenBinaryPixels[index];
let pixelIndex = startIndex + index;
let isChecked = client.isChecked(pixelIndex);
if (
pixel === 1 && !isChecked ||
pixel === 0 && isChecked
) {
try { client.toggle(pixelIndex); }
catch (err) { console.log(err); }
}
}
resolve("finished")
})
}
while (true) {
try {
if (!combatMode) await new Promise(r => setTimeout(r, 300));
await navigateAndWait(client, page);
await drawImage()
await new Promise(r => setImmediate(r));;
}
catch (error) {
console.log(error);
process.exit(1)
}
}
}
main();