-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
executable file
·204 lines (171 loc) · 7.13 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
const puppeteer = require('puppeteer');
const config = require('./util/config');
const logger = require('./util/logger');
const GameMonitor = require('./game/gameMonitor');
const BettingStrategy = require('./game/strategies');
const database = require('./database/database');
const readline = require('readline');
// Create readline interface for user input
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Function to get user input
const askQuestion = (query) => new Promise((resolve) => rl.question(query, resolve));
async function selectStrategy() {
console.log('\nAvailable Strategies:');
console.log('1. Conservative (Lower risk, smaller profits)');
console.log('2. Moderate (Balanced risk and reward)');
console.log('3. Aggressive (Higher risk, larger potential profits)');
console.log('4. Custom (Define your own parameters)\n');
const choice = await askQuestion('Select strategy (1-4): ');
switch (choice) {
case '1':
return config.BETTING_STRATEGIES.CONSERVATIVE;
case '2':
return config.BETTING_STRATEGIES.MODERATE;
case '3':
return config.BETTING_STRATEGIES.AGGRESSIVE;
case '4':
return await customStrategySetup();
default:
logger.warn('Invalid choice, using Moderate strategy');
return config.BETTING_STRATEGIES.MODERATE;
}
}
async function customStrategySetup() {
const strategy = {
initialBet: parseFloat(await askQuestion('Initial bet amount: ')),
maxBet: parseFloat(await askQuestion('Maximum bet amount: ')),
minBet: parseFloat(await askQuestion('Minimum bet amount: ')),
targetMultiplier: parseFloat(await askQuestion('Target multiplier (e.g., 1.5): ')),
stopLoss: parseFloat(await askQuestion('Stop loss amount: ')),
takeProfit: parseFloat(await askQuestion('Take profit amount: ')),
martingaleMultiplier: parseFloat(await askQuestion('Martingale multiplier (e.g., 2): '))
};
// Validate inputs
if (Object.values(strategy).some(isNaN)) {
logger.error('Invalid input detected, using Moderate strategy');
return config.BETTING_STRATEGIES.MODERATE;
}
return strategy;
}
async function initializeBrowser() {
const browser = await puppeteer.launch({
headless: false,
defaultViewport: null, // Automatically adjust viewport
args: ['--start-maximized'] // Start with maximized window
});
const page = await browser.newPage();
page.setDefaultNavigationTimeout(config.NAVIGATION.TIMEOUT);
return { browser, page };
}
async function navigateInitialPages(page) {
await page.goto(config.NAVIGATION.BASE_URL);
for (const [name, selector] of Object.entries(config.SELECTORS.INITIAL)) {
try {
await page.waitForSelector(selector, { timeout: config.NAVIGATION.TIMEOUT });
await page.click(selector);
logger.info(`Clicked ${name}`);
// Add small delay between clicks
await page.waitForTimeout(1000);
} catch (error) {
logger.error(`Failed to click ${name}: ${error.message}`);
throw error;
}
}
}
async function handleNewTab(target, browser, strategyConfig) {
if (target.type() === 'page') {
const newPage = await target.page();
if (newPage) {
try {
await newPage.waitForNavigation({ timeout: config.NAVIGATION.TIMEOUT });
logger.info(`Navigated to game page: ${await newPage.url()}`);
// Pass both page and config to GameMonitor constructor
const gameMonitor = new GameMonitor(newPage, config);
// If you have a strategyConfig, update the strategy
if (strategyConfig) {
gameMonitor.strategy = new BettingStrategy(strategyConfig);
}
// Setup page error handling
newPage.on('error', error => {
logger.error(`Page error: ${error.message}`);
});
newPage.on('pageerror', error => {
logger.error(`Page error: ${error.message}`);
});
gameMonitor.startMonitoring();
// Log strategy info
logger.info('Strategy Configuration:');
logger.info(`Initial Bet: ${strategyConfig.initialBet}`);
logger.info(`Target Multiplier: ${strategyConfig.targetMultiplier}`);
logger.info(`Stop Loss: ${strategyConfig.stopLoss}`);
logger.info(`Take Profit: ${strategyConfig.takeProfit}`);
} catch (error) {
logger.error(`Error in new tab: ${error.message}`);
await newPage.close();
}
}
}
}
async function setupGracefulShutdown(browser) {
const signals = ['SIGINT', 'SIGTERM', 'SIGQUIT'];
signals.forEach(signal => {
process.on(signal, async () => {
logger.info(`Received ${signal}, shutting down gracefully...`);
try {
await browser.close();
// database.disconnect();
logger.info('Cleanup completed');
rl.close();
process.exit(0);
} catch (error) {
logger.error(`Error during shutdown: ${error.message}`);
process.exit(1);
}
});
});
}
async function main() {
try {
logger.info('Starting Aviator Bot...');
// Get strategy configuration from user
const strategyConfig = await selectStrategy();
logger.info('Strategy selected, initializing bot...');
// Initialize database if needed
// database.connect();
const { browser, page } = await initializeBrowser();
logger.info('Browser initialized');
// Setup graceful shutdown
await setupGracefulShutdown(browser);
await navigateInitialPages(page);
// Setup new tab handling with selected strategy
browser.on('targetcreated', (target) => handleNewTab(target, browser, strategyConfig));
// Set up cleanup
setTimeout(async () => {
await browser.close();
// database.disconnect();
logger.info('Bot shutdown completed');
rl.close();
process.exit(0);
}, config.NAVIGATION.RUN_DURATION);
} catch (error) {
logger.error(`Bot initialization error: ${error.message}`);
rl.close();
process.exit(1);
}
}
process.on('unhandledRejection', (reason, promise) => {
logger.error('Unhandled Rejection at:', promise, 'reason:', reason);
});
process.on('uncaughtException', (error) => {
logger.error('Uncaught Exception:', error);
process.exit(1);
});
main().then(() => {
logger.info('Bot initialization completed');
}).catch(error => {
logger.error(`Failed to start bot: ${error.message}`);
process.exit(1);
});