-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
204 lines (178 loc) ยท 6.46 KB
/
main.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
import 'dotenv/config';
import fs from 'node:fs/promises';
import logger from './logger/logger.js';
import puppeteer from 'puppeteer';
import schedule from 'node-schedule';
import playGame from './games/main.js';
import TgClient from './bot/telegram.js';
import ReportGenerator from './reports/report.js';
import { getGeneralProfile, updateProfileProxy, adsOpenBrowser } from './ads/api.js';
import { shuffleArray } from './utils/shuffle.js';
import { getRandomNumberBetween, randomDelay } from './utils/delay.js';
import { formatTime } from './utils/datetime.js';
class ExecuteContainer {
#initRun = process.env.INIT_RUN === 'true';
#processedAccounts = new Set();
#reports = [];
#telegram;
constructor() {
this.#validateEnv();
this.#telegram = {
client: new TgClient(process.env.TG_TOKEN),
receiverId: process.env.TG_RECEIVER_ID,
};
}
play() {
this.#initRun ? this.executeTask() : this.scheduleTask();
}
scheduleTask() {
const taskTime = new Date(Date.now() + getRandomNumberBetween(181, 228) * 60 * 1000);
this.#telegram.client.sendAndPinMessage(`๐ NEXT FIRE ON <b>${formatTime(taskTime)}</b> ๐`, this.#telegram.receiverId);
const job = schedule.scheduleJob(taskTime, async () => {
await this.executeTask();
job.cancel();
});
}
async executeTask() {
const result = await getGeneralProfile();
if (!result.success) {
logger.error(result.message);
return;
}
try {
const [_, tgApps] = await Promise.all([this.#telegram.client.startPolling(), fs.readFile('./data/apps.json', 'utf8')]);
const tgApplications = JSON.parse(tgApps);
const totalResultGames = await this.startPlayingGames(result.message, tgApplications);
this.#reports.push(...totalResultGames);
const summaryText = this.prepareBriefSummaryText();
await this.#telegram.client.sendMessage(summaryText, this.#telegram.receiverId);
if (this.#processedAccounts.size === tgApplications.length) {
logger.debug(`Success processed all accounts (${tgApplications.length}), scheduling process...`);
const groupedGames = this.groupValuesByGame(this.#reports);
await this.sendReports(groupedGames);
this.clearAndScheduleTask();
} else {
logger.debug(`Only ${this.#processedAccounts.size} accounts processed, run others again.`);
await this.executeTask();
}
} catch (e) {
logger.error(e);
await this.#telegram.client.sendMessage(`๐ซ ${e}`, this.#telegram.receiverId);
} finally {
await this.#telegram.client.stopPolling();
}
}
async startPlayingGames(profileUserId, tgApps) {
const totalResultGames = [];
for (const tgApp of shuffleArray(tgApps)) {
if (this.#processedAccounts.has(tgApp.id)) {
continue;
}
const rawResultGames = await this.playGamesByAccount(profileUserId, tgApp);
if (rawResultGames.length) {
const resultGames = this.prepareResultGames(rawResultGames, tgApp);
totalResultGames.push(...resultGames);
this.#processedAccounts.add(tgApp.id);
}
}
return totalResultGames;
}
async playGamesByAccount(profileUserId, tgApp) {
if (!tgApp.active) {
logger.debug(`๐ #${tgApp.id}`);
return [];
}
logger.debug(`๐ #${tgApp.id}`);
// const updateResult = await updateProfileProxy(profileUserId, tgApp.proxy);
// if (!updateResult.success) {
// logger.error(updateResult.message);
// return [];
// }
// logger.info(`Successfully updated proxy: ${JSON.stringify(updateResult.message)}`);
const wsEndpoint = await this.establishWsEndpoint(profileUserId, 3);
if (!wsEndpoint) {
logger.error(`Failed to open browser: WebSocket endpoint is not defined after 3 attempts`);
return [];
}
let browser = null;
try {
browser = await puppeteer.connect({ browserWSEndpoint: wsEndpoint, defaultViewport: null });
} catch (e) {
logger.error(`Cannot connect to ws: ${wsEndpoint}`);
logger.error(e);
return [];
}
const resultGames = [];
for (const [appName, appUrl] of Object.entries(tgApp.games)) {
if (appUrl) {
const resultGame = await playGame(appName, browser, appUrl);
resultGames.push({ game: appName, data: resultGame });
} else {
logger.warning(`There is no link to the [${appName}] app`);
}
}
await randomDelay(4, 8, 's');
await browser.close();
return resultGames;
}
async establishWsEndpoint(profileUserId, maxRetries) {
let wsEndpoint;
for (let attempt = 1; attempt <= maxRetries && !wsEndpoint; attempt++) {
try {
const openResult = await adsOpenBrowser(profileUserId);
wsEndpoint = openResult?.data?.ws?.puppeteer;
if (!wsEndpoint) {
throw new Error('WebSocket endpoint not found');
}
} catch (error) {
logger.error(`Attempt ${attempt} failed: ${error.message}`);
await randomDelay(4, 8, 's');
}
}
return wsEndpoint;
}
async sendReports(groupedGames) {
const reportInstance = new ReportGenerator();
for (const [game, data] of Object.entries(groupedGames)) {
const report = reportInstance.generateReport(game, data);
await this.#telegram.client.sendCSVDocument(report, this.#telegram.receiverId, game);
}
}
prepareResultGames(resultGames, tgApp) {
return resultGames.map((item) => ({
game: item.game,
data: {
...item.data,
Account: tgApp.id,
User: tgApp.username,
},
}));
}
prepareBriefSummaryText() {
return `๐ฎ Game Results Summary:\r\n\r\n- Processed accounts (${this.#processedAccounts.size}): ${Array.from(
this.#processedAccounts,
).join(' | ')}\r\n\r\n- ๐ Detailed reports are being sent as CSV files.`.trim();
}
groupValuesByGame(inputArray) {
const grouped = inputArray.reduce((acc, { game, data }) => {
if (!acc[game]) acc[game] = [];
acc[game].push(data);
return acc;
}, {});
Object.keys(grouped).forEach((game) => {
grouped[game] = grouped[game].map((element, index) => ({ ...element, Number: index + 1 }));
});
return grouped;
}
clearAndScheduleTask() {
this.#reports = [];
this.#processedAccounts.clear();
this.scheduleTask();
}
#validateEnv() {
if (!process.env.TG_TOKEN || !process.env.TG_RECEIVER_ID) {
throw new Error('Environment variables TG_TOKEN and TG_RECEIVER_ID must be set');
}
}
}
new ExecuteContainer().play();