-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathindex.js
More file actions
623 lines (547 loc) · 22.1 KB
/
index.js
File metadata and controls
623 lines (547 loc) · 22.1 KB
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
const axios = require('axios');
const { v4: uuidv4 } = require('uuid');
const fs = require('fs').promises;
const colors = {
reset: "\x1b[0m",
cyan: "\x1b[36m",
green: "\x1b[32m",
yellow: "\x1b[33m",
red: "\x1b[31m",
white: "\x1b[37m",
bold: "\x1b[1m"
};
const logger = {
info: (msg) => console.log(`${colors.green}[✓] ${msg}${colors.reset}`),
warn: (msg) => console.log(`${colors.yellow}[⚠] ${msg}${colors.reset}`),
error: (msg) => console.log(`${colors.red}[✗] ${msg}${colors.reset}`),
success: (msg) => console.log(`${colors.green}[✅] ${msg}${colors.reset}`),
loading: (msg) => console.log(`${colors.cyan}[⟳] ${msg}${colors.reset}`),
step: (msg) => console.log(`${colors.white}[➤] ${msg}${colors.reset}`),
banner: () => {
console.log(`${colors.cyan}${colors.bold}`);
console.log(`---------------------------------------------`);
console.log(` Craft World Auto Bot - Airdrop Insiders `);
console.log(`---------------------------------------------${colors.reset}`);
console.log();
}
};
const getRandomUserAgent = () => {
const browsers = [
{ ua: '"Brave";v="137", "Chromium";v="137", "Not/A)Brand";v="24"', platform: 'Windows' },
{ ua: '"Google Chrome";v="128", "Chromium";v="128", "Not;A=Brand";v="99"', platform: 'Macintosh' },
{ ua: '"Firefox";v="127", "Gecko";v="20100101", "Mozilla";v="5.0"', platform: 'Linux' },
{ ua: '"Safari";v="17.0", "AppleWebKit";v="605.1.15", "Version";v="17.0"', platform: 'Macintosh' }
];
const selected = browsers[Math.floor(Math.random() * browsers.length)];
return { 'sec-ch-ua': selected.ua, 'sec-ch-ua-platform': selected.platform };
};
async function readConfig() {
try {
const data = await fs.readFile('config.json', 'utf8');
return JSON.parse(data);
} catch (error) {
logger.error('Error reading config.json: ' + error.message);
return {};
}
}
async function readTokens() {
try {
const data = await fs.readFile('token.txt', 'utf8');
return data.split('\n').map(token => token.trim()).filter(token => token.length > 0);
} catch (error) {
logger.error('Error reading token.txt: ' + error.message);
return [];
}
}
class CraftWorldBot {
constructor(accountIndex) {
this.baseURL = 'https://preview.craft-world.gg/api/1/user-actions/ingest';
this.authToken = null;
this.mineId = null;
this.factoryId = null;
this.areaId = null;
this.accountIndex = accountIndex;
this.isRunning = false;
this.previousResources = {};
const userAgent = getRandomUserAgent();
this.headers = {
"accept": "*/*",
"accept-language": "en-US,en;q=0.7",
"content-type": "application/json",
"priority": "u=1, i",
"sec-ch-ua": userAgent['sec-ch-ua'],
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": userAgent['sec-ch-ua-platform'],
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"sec-gpc": "1",
"x-app-version": "0.33.7",
"Referer": "https://preview.craft-world.gg/",
"Referrer-Policy": "strict-origin-when-cross-origin"
};
}
async setIdsFromConfig(config) {
const index = this.accountIndex + 1;
this.mineId = config[`mineId_${index}`];
this.factoryId = config[`factoryId_${index}`];
this.areaId = config[`areaId_${index}`];
if (!this.mineId || !this.factoryId || !this.areaId) {
logger.error(`Missing IDs for account ${index} in config.json`);
return false;
}
logger.info(`IDs set for account ${index}: mineId=${this.mineId}, factoryId=${this.factoryId}, areaId=${this.areaId}`);
return true;
}
setAuthToken(token) {
this.authToken = token.startsWith('Bearer jwt_') ? token : `Bearer jwt_${token}`;
this.headers.authorization = this.authToken;
logger.success(`Auth token set successfully for account ${this.accountIndex + 1}`);
}
generateActionId() {
return uuidv4();
}
formatNumber(num) {
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
getResourceEmoji(symbol) {
const emojiMap = {
'EARTH': '🌍', 'WATER': '💧', 'FIRE': '🔥', 'MUD': '🟤',
'CLAY': '🧱', 'SAND': '🏖️', 'COPPER': '🟠', 'SEAWATER': '🌊',
'HEAT': '🌡️', 'ALGAE': '🌿', 'LAVA': '🌋', 'CERAMICS': '🏺',
'STEEL': '⚙️', 'OXYGEN': '💨', 'GLASS': '🪟', 'GAS': '💨',
'STONE': '🪨', 'STEAM': '♨️', 'SCREWS': '🔩', 'FUEL': '⛽',
'CEMENT': '🏗️', 'OIL': '🛢️', 'ACID': '🧪', 'SULFUR': '💛',
'PLASTICS': '♻️', 'FIBERGLASS': '🪟', 'ENERGY': '⚡', 'HYDROGEN': '💨',
'DYNAMITE': '💥', 'COIN': '🪙'
};
return emojiMap[symbol] || '📦';
}
displayResourceInfo(account) {
logger.step(`=== RESOURCE INFORMATION (Account ${this.accountIndex + 1}) ===`);
logger.info(`Power: ${this.formatNumber(account.power || 0)}`);
logger.info(`Experience Points: ${this.formatNumber(account.experiencePoints || 0)}`);
logger.info(`Wallet: ${account.walletAddress || 'N/A'}`);
if (account.skillPoints !== undefined) {
logger.info(`Skill Points: ${this.formatNumber(account.skillPoints)}`);
}
logger.step('Resources:');
if (account.resources && Array.isArray(account.resources)) {
const availableResources = account.resources.filter(resource => resource.amount > 0);
if (availableResources.length > 0) {
availableResources.forEach(resource => {
const emoji = this.getResourceEmoji(resource.symbol);
logger.info(`${emoji} ${resource.symbol}: ${this.formatNumber(resource.amount)}`);
});
} else {
logger.info('No resources available');
}
}
}
storeCurrentResources(account) {
this.previousResources = {};
if (account.resources && Array.isArray(account.resources)) {
account.resources.forEach(resource => {
this.previousResources[resource.symbol] = resource.amount;
});
}
this.previousResources.power = account.power || 0;
this.previousResources.experiencePoints = account.experiencePoints || 0;
this.previousResources.skillPoints = account.skillPoints || 0;
}
displayClaimSummary(account) {
const updates = [];
if (account.resources && Array.isArray(account.resources)) {
account.resources
.filter(resource => resource.amount > 0)
.forEach(resource => {
const emoji = this.getResourceEmoji(resource.symbol);
updates.push(`[✓] ${emoji} ${resource.symbol}: ${this.formatNumber(resource.amount)}`);
});
}
if (account.experiencePoints !== undefined) {
updates.push(`[✓] Experience Points: ${this.formatNumber(account.experiencePoints)}`);
}
return updates.join(' , ');
}
async startFactory() {
let retries = 3;
while (retries > 0) {
try {
const actionId = this.generateActionId();
const payload = {
data: [{
id: actionId,
actionType: "START_FACTORY",
payload: { factoryId: this.factoryId },
time: Date.now()
}]
};
const response = await axios.post(this.baseURL, payload, { headers: this.headers });
if (response.data.data.processed.includes(actionId)) {
logger.success(`Factory started successfully for account ${this.accountIndex + 1}`);
return true;
}
logger.error(`Failed to start factory for account ${this.accountIndex + 1}: Action ID not processed`);
return false;
} catch (error) {
retries--;
const errorMessage = error.response
? `Status: ${error.response.status}, Data: ${JSON.stringify(error.response.data)}`
: error.message;
logger.error(`Error starting factory for account ${this.accountIndex + 1} (retries left: ${retries}): ${errorMessage}`);
if (retries === 0) return false;
await this.sleep(2000 * (4 - retries));
}
}
return false;
}
async claimArea(amountToClaim = 1) {
let retries = 3;
while (retries > 0) {
try {
const actionId = this.generateActionId();
const payload = {
data: [{
id: actionId,
actionType: "CLAIM_AREA",
payload: { areaId: this.areaId, amountToClaim },
time: Date.now()
}]
};
const response = await axios.post(this.baseURL, payload, { headers: this.headers });
if (response.data.data.processed.includes(actionId)) {
logger.success(`Area claimed successfully for account ${this.accountIndex + 1}`);
return true;
}
logger.error(`Failed to claim area for account ${this.accountIndex + 1}: Action ID not processed`);
return false;
} catch (error) {
retries--;
const errorMessage = error.response
? `Status: ${error.response.status}, Data: ${JSON.stringify(error.response.data)}`
: error.message;
logger.error(`Error claiming area for account ${this.accountIndex + 1} (retries left: ${retries}): ${errorMessage}`);
if (retries === 0) return false;
await this.sleep(2000 * (4 - retries));
}
}
return false;
}
async startMine() {
let retries = 3;
while (retries > 0) {
try {
const actionId = this.generateActionId();
const payload = {
data: [{
id: actionId,
actionType: "START_MINE",
payload: { mineId: this.mineId },
time: Date.now()
}]
};
const response = await axios.post(this.baseURL, payload, { headers: this.headers });
if (response.data.data.processed.includes(actionId)) {
this.storeCurrentResources(response.data.data.account);
logger.success(`Mine started successfully for account ${this.accountIndex + 1}`);
return response.data.data.account;
}
logger.error(`Failed to start mine for account ${this.accountIndex + 1}: Action ID not processed`);
return null;
} catch (error) {
retries--;
const errorMessage = error.response
? `Status: ${error.response.status}, Data: ${JSON.stringify(error.response.data)}`
: error.message;
logger.error(`Error starting mine for account ${this.accountIndex + 1} (retries left: ${retries}): ${errorMessage}`);
if (retries === 0) return null;
await this.sleep(2000 * (4 - retries));
}
}
return null;
}
async claimMine() {
let retries = 3;
while (retries > 0) {
try {
const actionId = this.generateActionId();
const payload = {
data: [{
id: actionId,
actionType: "CLAIM_MINE",
payload: { mineId: this.mineId },
time: Date.now()
}]
};
const response = await axios.post(this.baseURL, payload, { headers: this.headers });
if (response.data.data.processed.includes(actionId)) {
logger.success(`Mine claimed successfully for account ${this.accountIndex + 1} - ${this.displayClaimSummary(response.data.data.account)}`);
return response.data.data.account;
}
logger.error(`Failed to claim mine for account ${this.accountIndex + 1}: Action ID not processed`);
return null;
} catch (error) {
retries--;
const errorMessage = error.response
? `Status: ${error.response.status}, Data: ${JSON.stringify(error.response.data)}`
: error.message;
logger.error(`Error claiming mine for account ${this.accountIndex + 1} (retries left: ${retries}): ${errorMessage}`);
if (retries === 0) return null;
await this.sleep(2000 * (4 - retries));
}
}
return null;
}
async getAccountInfo() {
let retries = 3;
while (retries > 0) {
try {
const actionId = this.generateActionId();
const payload = {
data: [{
id: actionId,
actionType: "START_MINE",
payload: { mineId: this.mineId },
time: Date.now()
}]
};
const response = await axios.post(this.baseURL, payload, { headers: this.headers });
logger.success(`Account info fetched successfully for account ${this.accountIndex + 1}`);
return response.data.data.account;
} catch (error) {
retries--;
const errorMessage = error.response
? `Status: ${error.response.status}, Data: ${JSON.stringify(error.response.data)}`
: error.message;
logger.error(`Error getting account info for account ${this.accountIndex + 1} (retries left: ${retries}): ${errorMessage}`);
if (retries === 0) return null;
await this.sleep(2000 * (4 - retries));
}
}
return null;
}
async displayAccountStatus() {
logger.loading(`Fetching current account status for account ${this.accountIndex + 1}...`);
const account = await this.getAccountInfo();
if (account) {
this.displayResourceInfo(account);
} else {
logger.error(`Failed to fetch account status for account ${this.accountIndex + 1}`);
}
}
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async startCombinedLoop(mineInterval = 60000, claimInterval = 60000, enableFactory = true, enableArea = true) {
if (this.isRunning) {
logger.warn(`Bot is already running for account ${this.accountIndex + 1}!`);
return;
}
if (!this.authToken) {
logger.error(`Please set auth token first for account ${this.accountIndex + 1} using setAuthToken()`);
return;
}
if (!this.mineId || !this.factoryId || !this.areaId) {
logger.error(`Missing IDs for account ${this.accountIndex + 1}. Please check config.json`);
return;
}
this.isRunning = true;
let mineCount = 0;
while (this.isRunning) {
try {
logger.step(`=== CYCLE ${mineCount + 1} (Account ${this.accountIndex + 1}) ===`);
logger.loading('Starting mine and factory...');
const mineAccount = await this.startMine();
if (mineAccount) {
logger.success(`Mine started successfully for account ${this.accountIndex + 1}`);
this.displayResourceInfo(mineAccount);
}
if (enableFactory) {
await this.sleep(5000);
const factorySuccess = await this.startFactory();
if (factorySuccess) {
logger.success(`Factory started successfully for account ${this.accountIndex + 1}`);
}
}
logger.loading(`Waiting ${mineInterval/1000}s before claiming for account ${this.accountIndex + 1}...`);
await this.sleep(mineInterval);
const claimAccount = await this.claimMine();
if (claimAccount) {
logger.success(`Mine claimed successfully for account ${this.accountIndex + 1} - ${this.displayClaimSummary(claimAccount)}`);
}
if (enableArea) {
await this.sleep(5000);
const areaSuccess = await this.claimArea(1);
if (areaSuccess) {
logger.success(`Area claimed successfully for account ${this.accountIndex + 1}`);
}
}
mineCount++;
logger.loading(`Waiting ${claimInterval/1000}s before next cycle for account ${this.accountIndex + 1}...`);
await this.sleep(claimInterval);
} catch (error) {
const errorMessage = error.response
? `Status: ${error.response.status}, Data: ${JSON.stringify(error.response.data)}`
: error.message;
logger.error(`Error in combined loop for account ${this.accountIndex + 1}: ${errorMessage}`);
await this.sleep(5000);
}
}
}
async startFactoryLoop(interval = 60000) {
if (this.isRunning) {
logger.warn(`Bot is already running for account ${this.accountIndex + 1}!`);
return;
}
if (!this.authToken) {
logger.error(`Please set auth token first for account ${this.accountIndex + 1} using setAuthToken()`);
return;
}
if (!this.factoryId) {
logger.error(`Missing factoryId for account ${this.accountIndex + 1}. Please check config.json`);
return;
}
this.isRunning = true;
let factoryCount = 0;
while (this.isRunning) {
try {
logger.step(`=== FACTORY CYCLE ${factoryCount + 1} (Account ${this.accountIndex + 1}) ===`);
const factorySuccess = await this.startFactory();
if (factorySuccess) {
logger.success(`Factory started successfully for account ${this.accountIndex + 1}`);
}
logger.loading(`Waiting ${interval/1000}s before next factory start for account ${this.accountIndex + 1}...`);
await this.sleep(interval);
factoryCount++;
} catch (error) {
const errorMessage = error.response
? `Status: ${error.response.status}, Data: ${JSON.stringify(error.response.data)}`
: error.message;
logger.error(`Error in factory loop for account ${this.accountIndex + 1}: ${errorMessage}`);
await this.sleep(5000);
}
}
}
async startAreaLoop(interval = 60000, amountToClaim = 1) {
if (this.isRunning) {
logger.warn(`Bot is already running for account ${this.accountIndex + 1}!`);
return;
}
if (!this.authToken) {
logger.error(`Please set auth token first for account ${this.accountIndex + 1} using setAuthToken()`);
return;
}
if (!this.areaId) {
logger.error(`Missing areaId for account ${this.accountIndex + 1}. Please check config.json`);
return;
}
this.isRunning = true;
let areaCount = 0;
while (this.isRunning) {
try {
logger.step(`=== AREA CYCLE ${areaCount + 1} (Account ${this.accountIndex + 1}) ===`);
const areaSuccess = await this.claimArea(amountToClaim);
if (areaSuccess) {
logger.success(`Area claimed successfully for account ${this.accountIndex + 1}`);
}
logger.loading(`Waiting ${interval/1000}s before next area claim for account ${this.accountIndex + 1}...`);
await this.sleep(interval);
areaCount++;
} catch (error) {
const errorMessage = error.response
? `Status: ${error.response.status}, Data: ${JSON.stringify(error.response.data)}`
: error.message;
logger.error(`Error in area loop for account ${this.accountIndex + 1}: ${errorMessage}`);
await this.sleep(5000);
}
}
}
async startMiningLoop(mineInterval = 60000, claimInterval = 60000) {
if (this.isRunning) {
logger.warn(`Bot is already running for account ${this.accountIndex + 1}!`);
return;
}
if (!this.authToken) {
logger.error(`Please set auth token first for account ${this.accountIndex + 1} using setAuthToken()`);
return;
}
if (!this.mineId) {
logger.error(`Missing mineId for account ${this.accountIndex + 1}. Please check config.json`);
return;
}
this.isRunning = true;
let mineCount = 0;
while (this.isRunning) {
try {
logger.step(`=== CYCLE ${mineCount + 1} (Account ${this.accountIndex + 1}) ===`);
logger.loading(`Starting mine for account ${this.accountIndex + 1}...`);
const mineAccount = await this.startMine();
if (mineAccount) {
logger.success(`Mine started successfully for account ${this.accountIndex + 1}`);
this.displayResourceInfo(mineAccount);
}
logger.loading(`Waiting ${mineInterval/1000}s before claiming for account ${this.accountIndex + 1}...`);
await this.sleep(mineInterval);
const claimAccount = await this.claimMine();
if (claimAccount) {
logger.success(`Mine claimed successfully for account ${this.accountIndex + 1} - ${this.displayClaimSummary(claimAccount)}`);
}
mineCount++;
logger.loading(`Waiting ${claimInterval/1000}s before next cycle for account ${this.accountIndex + 1}...`);
await this.sleep(claimInterval);
} catch (error) {
const errorMessage = error.response
? `Status: ${error.response.status}, Data: ${JSON.stringify(error.response.data)}`
: error.message;
logger.error(`Error in mining loop for account ${this.accountIndex + 1}: ${errorMessage}`);
await this.sleep(5000);
}
}
}
stopBot() {
this.isRunning = false;
logger.warn(`Bot stopped for account ${this.accountIndex + 1}`);
}
}
async function main() {
logger.banner();
const tokens = await readTokens();
const config = await readConfig();
if (tokens.length === 0) {
logger.error('No tokens found in token.txt');
return;
}
logger.info(`Found ${tokens.length} tokens in token.txt`);
for (let i = 0; i < tokens.length; i++) {
const bot = new CraftWorldBot(i);
logger.step(`=== Processing Account ${i + 1}/${tokens.length} ===`);
const idsSet = await bot.setIdsFromConfig(config);
if (!idsSet) {
logger.error(`Skipping account ${i + 1} due to missing IDs in config.json`);
continue;
}
bot.setAuthToken(tokens[i]);
await bot.displayAccountStatus();
await bot.startCombinedLoop(60000, 60000, true, true);
if (i < tokens.length - 1) {
logger.loading(`Waiting 5 seconds before processing next account...`);
await bot.sleep(5000);
}
bot.stopBot();
}
logger.success('All accounts processed!');
}
process.on('SIGINT', () => {
logger.warn('Shutting down bot...');
process.exit(0);
});
module.exports = CraftWorldBot;
if (require.main === module) {
main().catch(error => {
const errorMessage = error.response
? `Status: ${error.response.status}, Data: ${JSON.stringify(error.response.data)}`
: error.message;
logger.error(`Main error: ${errorMessage}`);
});
}