forked from placeDE/pixel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
475 lines (404 loc) · 13.9 KB
/
bot.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
import WebSocket from 'ws';
import getPixels from "get-pixels";
import fetch from 'node-fetch';
//Logging
import sig from 'signale';
const { Signale } = sig;
const options = {
types: {
debug: {
color: 'cyan'
}
}
};
const signale = new Signale(options);
signale.config({
displayFilename: true,
displayTimestamp: true,
displayDate: false
});
var placeOrders = [];
const VERSION_NUMBER = 1;
let args=null
let redditSessionCookies = null
try {
redditSessionCookies = JSON.parse(process.env.PLACE_TOKENS);
} catch (e) {
signale.error("Invalid array in PLACE_TOKENS env var: " + e);
process.exit(1);
}
if (redditSessionCookies.length == 0) {
signale.error('Missing access token. Expected json array in PLACE_TOKENS env var');
process.exit(1);
}
let defaultAccessToken;
if (redditSessionCookies.length > 4) {
console.warn("Mehr als 4 Reddit Accounts gleichzeitig werden nicht empfohlen!")
}
var socket;
const COLOR_MAPPINGS = {
'#BE0039': 1,
'#6D001A': 0,
'#BE0039': 1,
'#FF4500': 2,
'#FFA800': 3,
'#FFD635': 4,
'#FFF8B8': 5,
'#00A368': 6,
'#00CC78': 7,
'#7EED56': 8,
'#00756F': 9,
'#009EAA': 10,
'#00CCC0': 11,
'#2450A4': 12,
'#3690EA': 13,
'#51E9F4': 14,
'#493AC1': 15,
'#6A5CFF': 16,
'#94B3FF': 17,
'#811E9F': 18,
'#B44AC0': 19,
'#E4ABFF': 20,
'#DE107F': 21,
'#FF3881': 22,
'#FF99AA': 23,
'#6D482F': 24,
'#9C6926': 25,
'#FFB470': 26,
'#000000': 27,
'#515252': 28,
'#898D90': 29,
'#D4D7D9': 30,
'#FFFFFF': 31
};
(async function() {
await refreshTokens()
connectSocket();
const interval = 300 / args.length;
var delay = 0;
for (let accessToken of args) {
t(accessToken, delay * 10);
delay += interval;
}
setInterval(refreshTokens, 30 * 60 * 1000);
signale.info("Setup complete")
})();
let rgbaJoinH = (a1, a2, rowSize = 1000, cellSize = 4) => {
const rawRowSize = rowSize * cellSize;
const rows = a1.length / rawRowSize;
let result = new Uint8Array(a1.length + a2.length);
for (var row = 0; row < rows; row++) {
result.set(a1.slice(rawRowSize * row, rawRowSize * (row+1)), rawRowSize * 2 * row);
result.set(a2.slice(rawRowSize * row, rawRowSize * (row+1)), rawRowSize * (2 * row + 1));
}
return result;
};
let rgbaJoinV = (a1, a2, rowSize = 2000, cellSize = 4) => {
let result = new Uint8Array(a1.length + a2.length);
const rawRowSize = rowSize * cellSize;
const rows1 = a1.length / rawRowSize;
for (var row = 0; row < rows1; row++) {
result.set(a1.slice(rawRowSize * row, rawRowSize * (row+1)), rawRowSize * row);
}
const rows2 = a2.length / rawRowSize;
for (var row = 0; row < rows2; row++) {
result.set(a2.slice(rawRowSize * row, rawRowSize * (row+1)), (rawRowSize * row) + a1.length);
}
return result;
};
let rgbaJoin = (a1, a2, rowSize = 1000, cellSize = 4) => {
const rawRowSize = rowSize * cellSize;
const rows = a1.length / rawRowSize;
let result = new Uint8Array(a1.length + a2.length);
for (var row = 0; row < rows; row++) {
result.set(a1.slice(rawRowSize * row, rawRowSize * (row + 1)), rawRowSize * 2 * row);
result.set(a2.slice(rawRowSize * row, rawRowSize * (row + 1)), rawRowSize * (2 * row + 1));
}
return result;
};
function shuffleWeighted(array) {
for (const item of array) {
item.rndPriority = Math.round(placeOrders.priorities[item.priority] * Math.random());
}
array.sort((a, b) => b.rndPriority - a.rndPriority);
}
function getPixelList() {
const structures = [];
for (const structureName in placeOrders.structures) {
shuffleWeighted(placeOrders.structures[structureName].pixels);
structures.push(placeOrders.structures[structureName]);
}
shuffleWeighted(structures);
return structures.map(structure => structure.pixels).flat();
}
async function refreshTokens() {
let tokens = [];
for (const cookie of redditSessionCookies) {
const response = await fetch("https://www.reddit.com/r/place/", {
headers: {
cookie: `reddit_session=${cookie}`
}
});
const responseText = await response.text()
let token = responseText.split('\"accessToken\":\"')[1].split('"')[0];
tokens.push(token);
}
signale.info("Refreshed tokens: ", sanetizeTokens(tokens))
args = tokens;
defaultAccessToken = tokens[0];
}
function connectSocket() {
signale.info('Verbinden mit Commando server...')
socket = new WebSocket('wss://place.computerscholler.com/api/ws');
socket.onerror = function(e) {
signale.error("Socket error: " + e.message)
}
socket.onopen = function () {
signale.info('Verbunden mit Commando server!')
socket.send(JSON.stringify({ type: 'getmap' }));
socket.send(JSON.stringify({ type: 'brand', brand: `nodeheadlessV${VERSION_NUMBER}` }));
};
socket.onmessage = async function (message) {
var data;
try {
data = JSON.parse(message.data);
} catch (e) {
return;
}
switch (data.type.toLowerCase()) {
case 'map':
if(data.data === undefined) {
socket.send(JSON.stringify({ type: 'getmap' }));
break;
}
signale.log(`Neue map geladen (reason: ${data.reason ? data.reason : 'verbunden mit server'})`)
const structureCount = Object.keys(data.data.structures).length;
let pixelCount = 0;
for (const structureName in data.data.structures) {
pixelCount += data.data.structures[structureName].pixels.length;
}
signale.info('Neue Strukturen geladen. Bilder: ' + structureCount + ' - Pixels: ' + pixelCount + '.');
placeOrders = data.data;
break;
default:
break;
}
};
socket.onclose = function (e) {
signale.warn(`Commando server hat die Verbindung unterbrochen: ${e.reason}`)
signale.error('Socketfehler: ', e.reason);
socket.close();
setTimeout(connectSocket, 1000);
};
}
function t(token, time) {
setTimeout((t) => attemptPlace(t), time, token)
}
async function attemptPlace(token) {
var map0;
var map1;
var map2;
var map3;
signale.info(`Trying to place with ${sanetizeToken(token)}`)
let retry = () => attemptPlace(token);
try {
map0 = await getMapFromUrl(await getCurrentImageUrl('0'))
map1 = await getMapFromUrl(await getCurrentImageUrl('1'));
map2 = await getMapFromUrl(await getCurrentImageUrl('2'))
map3 = await getMapFromUrl(await getCurrentImageUrl('3'))
} catch (e) {
signale.warn('Fehler beim Abrufen der Zeichenfläche. Neuer Versuch in 15 Sekunden: ', e);
t(token, 15000); // probeer opnieuw in 15sec.
return;
}
const rgbaCanvas =rgbaJoinV(rgbaJoinH(map0.data, map1.data), rgbaJoinH(map2.data, map3.data))
const pixelList = getPixelList();
let foundPixel = false;
let wrongCount = 0;
for (const order of pixelList) {
const x = order.x;
const y = order.y;
const colorId = COLOR_MAPPINGS[order.color] ?? order.color;
//const rgbaAtLocation = ctx.getImageData(x, y, 1, 1).data;
const rgbaAtLocation = getRgbaAt(rgbaCanvas, x, y);
const hex = rgbToHex(rgbaAtLocation[0], rgbaAtLocation[1], rgbaAtLocation[2]);
const currentColorId = COLOR_MAPPINGS[hex];
// Pixel already set
if (currentColorId == colorId) continue;
wrongCount++;
if (foundPixel) continue;
foundPixel = true;
signale.info('Pixel wird gesetzt auf ' + x + ', ' + y + '...');
const time = new Date().getTime();
let nextAvailablePixelTimestamp = await place(x, y, colorId, token) ?? new Date(time + 1000 * 60 * 5 + 1000 * 15)
// Sanity check timestamp
if (nextAvailablePixelTimestamp < time || nextAvailablePixelTimestamp > time + 1000 * 60 * 5 + 1000 * 15) {
nextAvailablePixelTimestamp = time + 1000 * 60 * 5 + 1000 * 15;
}
// Add a few random seconds to the next available pixel timestamp
const waitFor = nextAvailablePixelTimestamp - time + (Math.random() * 1000 * 15);
const minutes = Math.floor(waitFor / (1000 * 60))
const seconds = Math.floor((waitFor / 1000) % 60)
signale.info('Warten auf Abkühlzeit ' + minutes + ':' + seconds + ' bis ' + new Date(nextAvailablePixelTimestamp).toLocaleTimeString());
t(token, waitFor);
}
if (foundPixel) {
signale.info(`${wrongCount} sind noch falsch`)
return
}
signale.success('Alle bestellten Pixel haben bereits die richtige Farbe!');
t(token, 2000); // probeer opnieuw in 30sec.
}
/**
* Places a pixel on the canvas, returns the "nextAvailablePixelTimestamp", if succesfull
* @param x
* @param y
* @param color
* @returns {Promise<number>}
*/
async function place(x, y, color, token = defaultAccessToken) {
const response = await fetch('https://gql-realtime-2.reddit.com/query', {
method: 'POST',
body: JSON.stringify({
'operationName': 'setPixel',
'variables': {
'input': {
'actionName': 'r/replace:set_pixel',
'PixelMessageData': {
'coordinate': {
'x': x % 1000,
'y': y % 1000
},
'colorIndex': color,
'canvasIndex': getCanvas(x, y)
}
}
},
'query': `mutation setPixel($input: ActInput!) {
act(input: $input) {
data {
... on BasicMessage {
id
data {
... on GetUserCooldownResponseMessageData {
nextAvailablePixelTimestamp
__typename
}
... on SetPixelResponseMessageData {
timestamp
__typename
}
__typename
}
__typename
}
__typename
}
__typename
}
}
`
}),
headers: {
'origin': 'https://hot-potato.reddit.com',
'referer': 'https://hot-potato.reddit.com/',
'apollographql-client-name': 'mona-lisa',
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}
});
const data = await response.json()
if (data.errors != undefined) {
signale.warn("Fehler beim Platzieren des Pixels, warte auf Abkühlzeit...");
return data.errors[0].extensions?.nextAvailablePixelTs
} else {
signale.success("Pixel wurde platziert!")
}
return data?.data?.act?.data?.[0]?.data?.nextAvailablePixelTimestamp
}
async function getCurrentImageUrl(id = '0') {
return new Promise((resolve, reject) => {
const ws = new WebSocket('wss://gql-realtime-2.reddit.com/query', 'graphql-ws', {
headers: {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:98.0) Gecko/20100101 Firefox/98.0",
"Origin": "https://hot-potato.reddit.com"
}
});
ws.onopen = () => {
ws.send(JSON.stringify({
'type': 'connection_init',
'payload': {
'Authorization': `Bearer ${defaultAccessToken}`
}
}));
ws.send(JSON.stringify({
'id': '1',
'type': 'start',
'payload': {
'variables': {
'input': {
'channel': {
'teamOwner': 'AFD2022',
'category': 'CANVAS',
'tag': id
}
}
},
'extensions': {},
'operationName': 'replace',
'query': 'subscription replace($input: SubscribeInput!) {\n subscribe(input: $input) {\n id\n ... on BasicMessage {\n data {\n __typename\n ... on FullFrameMessageData {\n __typename\n name\n timestamp\n }\n }\n __typename\n }\n __typename\n }\n}'
}
}));
};
ws.onmessage = (message) => {
const { data } = message;
const parsed = JSON.parse(data);
if (!parsed.payload) {
return;
}
if (parsed.payload.hasOwnProperty('message')) {
signale.fatal('Error while getting current image url: ' + parsed.payload.message + ' - Möglicherweise ein ungültiger access-token.');
}
if (!parsed.payload.data || !parsed.payload.data.subscribe || !parsed.payload.data.subscribe.data) {
return;
}
ws.close();
resolve(parsed.payload.data.subscribe.data.name + `?noCache=${Date.now() * Math.random()}`);
}
ws.onerror = reject;
});
}
function getMapFromUrl(url) {
return new Promise((resolve, reject) => {
getPixels(url, function(err, pixels) {
if (err) {
signale.error("Could not get Map");
reject()
return
}
signale.debug("Got pixels from Map ", pixels.shape.slice())
resolve(pixels)
})
});
}
function getRgbaAt(rgbaCanvas, x, y) {
var start = (y * 2000 + x) * 4;
var rgba = rgbaCanvas.slice(start, start + 4);
return rgba;
}
function rgbToHex(r, g, b) {
return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();
}
function sanetizeToken(token) {
return "*".repeat(token.length - 5) + token.slice(-5)
}
function sanetizeTokens(list) {
return list.map(sanetizeToken)
}
function getCanvas(x, y) {
if (x <= 999) {
return y <= 999 ? 0 : 2;
} else {
return y <= 999 ? 1 : 3;
}
}