-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
645 lines (589 loc) · 24 KB
/
index.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
(async () => {
// Check for correct page
if (!window.location.href.startsWith('https://www.reddit.com/r/place/') && !window.location.href.startsWith('https://new.reddit.com/r/place/')) {
return;
}
// Check for marker; only load the script once!
if (document.head.querySelector('meta[name="zinnsoldat"]')) {
console.warn('Script already loaded!');
return;
}
const marker = document.createElement('meta');
marker.setAttribute('name', 'zinnsoldat');
document.head.appendChild(marker);
const zs_style = document.createElement('style');
zs_style.innerHTML = `
.zs-hidden {
display: none;
}
.zs-pixeled {
border: 3px solid #000000;
box-shadow: 8px 8px 0px rgba(0, 0, 0, 0.75);
font-family: -apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,'Helvetica Neue',Arial,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol',sans-serif;
font-size: 14px;
}
.zs-button {
position: fixed;
width: 148px;
height: 52px;
bottom: 28px;
left: 15px;
z-index: 100;
color: #fff;
}
.zs-title {
font-weight: 600;
line-height: 20px;
}
.zs-subtitle {
font-size: 12px;
font-weight: 400;
line-height: 16px;
}
.zs-startbutton {
background: linear-gradient(-90deg, #C03400 var(--zs_timeout), #FF4500 var(--zs_timeout));
}
.zs-startbutton:hover {
background: linear-gradient(-90deg, #802300 var(--zs_timeout), #E03D00 var(--zs_timeout));
}
.zs-stopbutton {
background: linear-gradient(-90deg, #007B4E var(--zs_timeout), #00A368 var(--zs_timeout));
}
.zs-stopbutton:hover {
background: linear-gradient(-90deg, #005234 var(--zs_timeout), #008F5B var(--zs_timeout));
}
`;
document.head.appendChild(zs_style);
// ----------------------------------------
// Toaster
// ----------------------------------------
await new Promise((resolve, reject) => {
var toastifyStyle = document.createElement('link');
toastifyStyle.type = "text/css";
toastifyStyle.rel = "stylesheet";
toastifyStyle.media = "screen";
toastifyStyle.href = 'https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.css';
document.head.appendChild(toastifyStyle);
var toastifyScript = document.createElement('script');
toastifyScript.setAttribute('src', 'https://cdn.jsdelivr.net/npm/toastify-js');
toastifyScript.setAttribute('async', false);
document.body.appendChild(toastifyScript);
toastifyScript.addEventListener('load', (ev) => {
resolve({ status: true });
});
toastifyScript.addEventListener('error', (ev) => {
reject({ status: false, message: `Failed to load the toastify` });
});
});
class Toaster {
static info = (msg) => {
Toastify({
text: msg,
duration: 5000,
gravity: 'bottom',
position: 'right',
stopOnFocus: true,
className: 'zs-pixeled',
style: {
background: '#383838',
color: '#fff',
'box-shadow': '8px 8px 0px rgba(0, 0, 0, 0.75)',
},
}).showToast();
}
static warn = (msg) => {
Toastify({
text: msg,
duration: 5000,
gravity: 'bottom',
position: 'right',
stopOnFocus: true,
className: 'zs-pixeled',
style: {
background: '#FFA800',
color: '#000',
'box-shadow': '8px 8px 0px rgba(0, 0, 0, 0.75)',
},
}).showToast();
}
static error = (msg) => {
Toastify({
text: msg,
duration: 5000,
gravity: 'bottom',
position: 'right',
stopOnFocus: true,
className: 'zs-pixeled',
style: {
background: '#d93a00',
color: '#fff',
'box-shadow': '8px 8px 0px rgba(0, 0, 0, 0.75)',
},
}).showToast();
}
static time_error = (msg) => {
Toastify({
text: msg,
duration: 30000,
gravity: 'bottom',
position: 'right',
stopOnFocus: true,
className: 'zs-pixeled',
style: {
background: '#d93a00',
color: '#fff',
'box-shadow': '8px 8px 0px rgba(0, 0, 0, 0.75)',
},
}).showToast();
}
static success = (msg) => {
Toastify({
text: msg,
duration: 5000,
gravity: 'bottom',
position: 'right',
stopOnFocus: true,
className: 'zs-pixeled',
style: {
background: '#00A368',
color: '#fff',
'box-shadow': '8px 8px 0px rgba(0, 0, 0, 0.75)',
},
}).showToast();
}
static update = () => {
Toastify({
text: 'Neue Version auf https://place.army/ verfügbar!',
destination: 'https://place.army/',
duration: -1,
gravity: 'bottom',
position: 'right',
stopOnFocus: true,
className: 'zs-pixeled',
style: {
background: '#3690EA',
color: '#fff',
'box-shadow': '8px 8px 0px rgba(0, 0, 0, 0.75)',
},
}).showToast();
}
static place = (msg, x, y) => {
Toastify({
text: msg,
duration: 5000,
gravity: 'bottom',
position: 'right',
stopOnFocus: true,
className: 'zs-pixeled',
onClick: () => location.href = `https://www.reddit.com/r/place/?cx=${x}&cy=${y}&px=18&screenmode=fullscreen`,
style: {
background: '#00A368',
color: '#fff',
'box-shadow': '8px 8px 0px rgba(0, 0, 0, 0.75)',
},
}).showToast();
}
}
// ----------------------------------------
// Timer
// ----------------------------------------
// Override setTimeout to allow getting the time left
let placeTimeout;
const _setTimeout = setTimeout;
const _clearTimeout = clearTimeout;
const zs_allTimeouts = {};
let zs_maxTimeout = 240000;
setTimeout = (callback, delay) => {
let id = _setTimeout(callback, delay);
zs_allTimeouts[id] = Date.now() + delay;
return id;
};
clearTimeout = (id) => {
_clearTimeout(id);
zs_allTimeouts[id] = undefined;
}
const getTimeout = (id) => {
if (zs_allTimeouts[id]) {
return Math.max(
zs_allTimeouts[id] - Date.now(),
0 // Make sure we get no negative values for timeouts that are already done
)
}
return NaN;
}
setInterval(() => {
if (!zs_running) return
let theTimeout = getTimeout(placeTimeout)
if (Number.isNaN(theTimeout)) {
theTimeout = 0;
}
// Update the percentage
const percentage = 100 - Math.min(Math.max(Math.round((theTimeout/zs_maxTimeout) * 100), 0), 100);
zs_startButton.style.setProperty("--zs_timeout", `${percentage}%`);
zs_startButtonTitle.innerText = `Zinnsoldat v${zs_version}`;
const nextTryInSeconds = Math.floor(theTimeout/1000);
if (theTimeout > 0) {
zs_startButtonSubTitle.innerText = `Nächster Pixel: ${nextTryInSeconds}s`;
} else {
zs_startButtonSubTitle.innerText = 'Warten...';
}
}, 1000)
// ----------------------------------------
// Basics
// ----------------------------------------
let zs_running = true;
let zs_initialized;
const zs_version = "1.6";
let zs_accessToken;
let c2;
// ----------------------------------------
// Canvas
// ----------------------------------------
class Canvas {
static getCanvasId = (x, y) => {
if (y < 0 && x < -500) {
return 0
} else if (y < 0 && x < 500 && x >= -500) {
return 1;
} else if (y < 0 && x >= 500) {
return 2;
} else if (y >= 0 && x < -500) {
return 3;
} else if (y >= 0 && x < 500 && x >= -500) {
return 4;
} else if (y >= 0 && x >= 500) {
return 5;
}
console.error('Unknown canvas!');
return 0;
}
static getCanvasX = (x, y) => {
return Math.abs((x + 1500) % 1000);
}
static getCanvasY = (x, y) => {
return Canvas.getCanvasId(x, y) < 3 ? y + 1000 : y;
}
static placePixel = async (x, y, color) => {
console.log('Trying to place pixel at %s, %s in %s', x, y, color);
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': Canvas.getCanvasX(x, y),
'y': Canvas.getCanvasY(x, y)
},
'colorIndex': color,
'canvasIndex': Canvas.getCanvasId(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://garlic-bread.reddit.com',
'referer': 'https://garlic-bread.reddit.com/',
'apollographql-client-name': 'garlic-bread',
'Authorization': `Bearer ${zs_accessToken}`,
'Content-Type': 'application/json'
}
});
const data = await response.json()
if (data.errors !== undefined) {
if (data.errors[0].message === 'Ratelimited') {
console.log('Could not place pixel at %s, %s in %s - Ratelimit', x, y, color);
Toaster.warn('Du hast noch Abklingzeit!');
return {
status: 'Failure',
timestamp: data.errors[0].extensions?.nextAvailablePixelTs,
reason: data.errors[0].message
};
} else if (data.errors[0].message === 'user is not logged in') {
console.warn('User not logged in!');
Toaster.error('Du musst eingeloggt sein!');
zs_stopBot();
return;
}
console.log('Could not place pixel at %s, %s in %s - Response error', x, y, color);
console.error(data.errors);
Toaster.error('Fehler beim Platzieren des Pixels');
return { status: 'Failure', timestamp: null, reason: '' };
}
// Pixels placed counter
let pixelsPlacedThisSession = parseInt(localStorage.getItem('pixelsPlacedThisSession') ?? '0') + 1;
localStorage.setItem('pixelsPlacedThisSession', pixelsPlacedThisSession);
console.log('Did place pixel at %s, %s in %s', x, y, color);
Toaster.place(`Pixel (${x}, ${y}) platziert! (#${pixelsPlacedThisSession})`, x, y);
return { status: 'Success', timestamp: data?.data?.act?.data?.[0]?.data?.nextAvailablePixelTimestamp, reason: '' };
}
static requestCooldown = async () => {
const response = await fetch('https://gql-realtime-2.reddit.com/query', {
method: 'POST',
body: JSON.stringify({
"operationName": "getUserCooldown",
"variables": {
"input": {
"actionName": "r/replace:get_user_cooldown"
}
},
"query": `mutation getUserCooldown($input: ActInput!) {
act(input: $input) {
data {
... on BasicMessage {
id
data {
... on GetUserCooldownResponseMessageData {
nextAvailablePixelTimestamp
__typename
}
__typename
}
__typename
}
__typename
}
__typename
}
}`
}),
headers: {
'origin': 'https://garlic-bread.reddit.com',
'referer': 'https://garlic-bread.reddit.com/',
'apollographql-client-name': 'garlic-bread',
'Authorization': `Bearer ${zs_accessToken}`,
'Content-Type': 'application/json'
}
});
const data = await response.json();
if (data.errors !== undefined) {
console.error(data.errors);
return null;
}
const timestamp = data?.data?.act?.data?.[0]?.data?.nextAvailablePixelTimestamp;
if (timestamp) {
Toaster.warn('Du hast noch Abklingzeit!');
return timestamp;
}
return null;
}
}
// ----------------------------------------
// RedditAPI
// ----------------------------------------
class RedditApi {
static getAccessToken = async () => {
const usingOldReddit = window.location.href.includes('new.reddit.com');
const url = usingOldReddit ? 'https://new.reddit.com/r/place/' : 'https://www.reddit.com/r/place/';
const response = await fetch(url);
const responseText = await response.text();
return responseText.match(/"accessToken":"(\\"|[^"]*)"/)[1];
}
}
// ----------------------------------------
// TimeChecker
// ----------------------------------------
class TimeChecker {
static checkTime = async () => {
const ntpResponse = await fetch("https://worldtimeapi.org/api/timezone/est")
const data = await ntpResponse.json()
const ntpDate = data['unixtime'] * 1000
if(ntpDate - Date.now() <= -60000){
let error_message = `Deine Computerzeit weicht um >= 1 Minute von der Serverzeit ab!
Um die Server nicht mit Anfragen zu fluten stoppt der Zinnsoldat nun.
Bitte stelle sicher, dass die Zeit deines Computers richtig synchronisiert ist!
Lade im Anschluss die Seite neu um den Zinnsoldaten erneut zu starten.
`
Toaster.time_error(error_message)
return false
}
return true
}
}
// ----------------------------------------
// CarpetBomber
// ----------------------------------------
class CarpetBomber {
static getTokens = () => {
return ['Wololo'];
}
static requestJob = () => {
if (c2.readyState !== c2.OPEN) {
Toaster.error('Verbindung zum "Carpetbomber" abgebrochen. Verbinde...');
CarpetBomber.initCarpetbomberConnection();
return;
}
if (!zs_running) {
return;
}
c2.send(JSON.stringify({ type: "RequestJobs", tokens: CarpetBomber.getTokens() }));
}
static processJobResponse = (jobs) => {
if (!jobs || jobs === {}) {
Toaster.warn('Kein verfügbarer Auftrag. Versuche in 60s erneut');
zs_maxTimeout = 60000;
clearTimeout(placeTimeout);
placeTimeout = setTimeout(() => {
CarpetBomber.requestJob();
}, 60000);
return;
}
let [token, [job, code]] = Object.entries(jobs)[0];
if (!job) {
// Check if ratelimited and schedule retry
const ratelimit = code?.Ratelimited?.until;
if (ratelimit) {
const nextTry = Math.max(5000, Date.parse(ratelimit) + 2000 - Date.now());
zs_maxTimeout = nextTry;
clearTimeout(placeTimeout);
placeTimeout = setTimeout(() => {
CarpetBomber.requestJob();
}, nextTry);
return;
}
// Other error. No jobs left?
Toaster.warn('Kein verfügbarer Auftrag. Versuche in 20s erneut');
zs_maxTimeout = 20000;
clearTimeout(placeTimeout);
placeTimeout = setTimeout(() => {
CarpetBomber.requestJob();
}, 20000);
return;
}
// Execute job
Canvas.placePixel(job.x, job.y, job.color - 1).then((placeResult) => {
const { status, reason, timestamp } = placeResult;
const nextTry = (timestamp ? timestamp - Date.now() : 5*60*1000) + 3000 + Math.floor(Math.random()*18000);
// Replay acknoledgement
const token = CarpetBomber.getTokens()[0];
c2.send(JSON.stringify({ type: "JobStatusReport", tokens: { [token]: { type: status, reason, cooldown: nextTry } }}));
// Schedule next job
zs_maxTimeout = nextTry;
clearTimeout(placeTimeout);
placeTimeout = setTimeout(() => {
CarpetBomber.requestJob();
}, nextTry);
});
}
static startRequestLoop = () => {
Canvas.requestCooldown().then((nextTry) => {
if (!nextTry) {
CarpetBomber.requestJob();
} else {
const nextTryWithOffset = nextTry + 2000 - Date.now()
zs_maxTimeout = nextTryWithOffset;
clearTimeout(placeTimeout);
placeTimeout = setTimeout(() => {
CarpetBomber.requestJob();
}, Math.max(5000, nextTryWithOffset));
}
});
}
static initCarpetbomberConnection = () => {
c2 = new WebSocket("wss://carpetbomber.place.army");
c2.onopen = () => {
Toaster.info('Verbinde mit "Carpetbomber"...');
c2.send(JSON.stringify({ type: "Handshake", version: zs_version }));
setInterval(() => c2.send(JSON.stringify({ type: "Wakeup"})), 40*1000);
}
c2.onerror = (error) => {
Toaster.error('Verbindung zum "Carpetbomber" fehlgeschlagen!');
console.error(error);
//setTimeout(CarpetBomber.initCarpetbomberConnection, 5000);
}
c2.onmessage = (event) => {
const data = JSON.parse(event.data)
// console.log('received: %s', JSON.stringify(data));
if (data.type === 'UpdateVersion') {
Toaster.success('Verbindung aufgebaut!');
if (!data.version || data.version === 'Unsupported') {
zs_stopBot();
Toaster.error('Version nicht mehr unterstützt!');
Toaster.update();
return;
} else if (data.version > zs_version) {
Toaster.update();
}
zs_initialized = true;
CarpetBomber.startRequestLoop();
} else if (data.type == "Jobs") {
CarpetBomber.processJobResponse(data.jobs);
}
}
}
}
// ----------------------------------------
// StartButton
// ----------------------------------------
const zs_startButton = document.createElement('button');
const zs_startButtonTitle = document.createElement('div')
const zs_startButtonSubTitle = document.createElement('div')
zs_startButton.classList.add('zs-pixeled', 'zs-button', 'zs-stopbutton');
zs_startButton.style.setProperty('--zs_timeout', '100%');
zs_startButtonTitle.innerText = `Zinnsoldat v${zs_version}`;
zs_startButtonTitle.classList.add('zs-title');
zs_startButton.appendChild(zs_startButtonTitle);
zs_startButtonSubTitle.innerText = "Initialisieren...";
zs_startButtonSubTitle.classList.add('zs-subtitle');
zs_startButton.appendChild(zs_startButtonSubTitle);
document.body.appendChild(zs_startButton);
const zs_startBot = () => {
if (zs_initialized) {
zs_running = true;
zs_startButton.classList.remove('zs-startbutton');
zs_startButton.classList.add('zs-stopbutton');
zs_startButtonSubTitle.innerText = 'Starten...'
CarpetBomber.startRequestLoop();
} else {
Toaster.error('Version nicht mehr unterstützt!');
}
}
const zs_stopBot = () => {
zs_running = false;
clearTimeout(placeTimeout);
zs_startButton.classList.remove('zs-stopbutton');
zs_startButton.classList.add('zs-startbutton');
zs_startButtonSubTitle.innerText = 'Deaktiviert'
}
zs_startButton.onclick = () => {
if (zs_running) {
zs_stopBot();
} else {
zs_startBot();
}
}
// ----------------------------------------
// Run
// ----------------------------------------
Toaster.info('Erbitte Reddit um Zugriff...');
zs_accessToken = await RedditApi.getAccessToken();
Toaster.success('Zugriff gewährt!');
let isTimeSynchronized = await TimeChecker.checkTime();
if(isTimeSynchronized) { CarpetBomber.initCarpetbomberConnection(); }
})();