· 8 years ago · Sep 27, 2017, 06:46 PM
1var fs = require('fs');
2
3var Winston = require('winston');
4var Logger = new(Winston['Logger'])({ transports: [new(Winston['transports']['Console'])({ colorize: true, timestamp: true, level: 'debug', label: 'BOT' }), new(Winston['transports']['File'])({ level: 'info', label: 'BOT', createDirectory: true, timestamp: true, filename: 'bot.log', json: false })] });
5var scribe = require('scribe-js')(),
6 console = process.console,
7 config = require('./config.js'),
8
9 app = require('express')(),
10 server = require('http').Server(app),
11 io = require('socket.io')(server),
12
13 redis = require('redis'),
14 requestify = require('requestify'),
15 mysql = require('mysql'),
16 bot = require('./bot.js');
17
18server.listen(config.serverPort);
19
20app.get('/', function (req, res) {
21 res.json({
22 message: 'Please, fuck yourself, asshole!',
23 });
24});
25
26var redisClient = redis.createClient({host: config.redis.host, port: config.redis.port}),
27 client = redis.createClient({host: config.redis.host, port: config.redis.port});
28
29Logger.info('Domain ' + config.domain );
30Logger.info('Port ' + config.serverPort);
31
32bot.init(redis, io, requestify);
33
34var pool = mysql.createPool({
35 connectionLimit : 1000,
36 database: config.db.database,
37 host: 'localhost',
38 user: 'root',
39 password: config.db.password
40});
41
42redisClient.subscribe('show.winners');
43redisClient.subscribe('message');
44redisClient.subscribe('updateBalance');
45redisClient.subscribe('queue');
46redisClient.subscribe('newDeposit');
47
48redisClient.subscribe('depositDecline');
49redisClient.subscribe('new.msg');
50redisClient.subscribe('del.msg');
51redisClient.subscribe('dbl_p');
52redisClient.subscribe('msgChannel');
53redisClient.setMaxListeners(0);
54redisClient.on("message", function (channel, message) {
55 if (channel == 'depositDecline' || channel == 'queue' || channel == 'dbl_p') {
56 io.sockets.emit(channel, message);
57 }
58 if (channel == 'message') {
59 io.sockets.emit('message', message);
60 }
61 if (channel == 'updateBalance') {
62 io.sockets.emit('updateBalance', message);
63 }
64 if (channel == 'show.winners') {
65 clearInterval(timer);
66 timerStatus = false;
67 console.log('Force Stop');
68 game.status = 3;
69 showSliderWinners();
70 }
71 if (channel == 'newDeposit') {
72 io.sockets.emit(channel, message);
73
74 message = JSON.parse(message);
75 if (!timerStatus && message.gameStatus == 1) {
76 game.status = 1;
77 startTimer(io.sockets);
78 }
79 }
80 if (channel == 'new.msg') {
81 io.sockets.emit('chat', message);
82 }
83 if (channel == 'del.msg') {
84 io.sockets.emit('chatdel', message);
85 }
86});
87
88/* USERS ONLINE SITE */
89
90io.sockets.on('connection', function(socket) {
91
92 updateOnline();
93
94 socket.on('disconnect', function(){
95 updateOnline();
96 })
97});
98
99function updateOnline(){
100 io.sockets.emit('online', Object.keys(io.sockets.adapter.rooms).length );
101}
102
103/* USERS ONLINE SITE END */
104
105var steamStatus = [],
106 game,
107 timer,
108 ngtimer,
109 timerStatus = false,
110 timerTime = 90,
111 preFinishingTime = 2;
112
113getPriceItems();
114getCurrentGame();
115checkSteamInventoryStatus();
116
117var preFinish = false;
118
119function checkInventor() {
120 setInterval(function () {
121 requestify.post(config.domain + '/viplatitj', {
122 secretKey: config.secretKey
123 })
124 .then(function(response) {
125 console.tag('INVESTOR').log('Checked investors!');
126 }, function(response) {
127 console.tag('INVESTOR').log('Something wrong [checkInvestor]');
128 });
129 }, 420000);
130}
131
132function startTimer() {
133 var time = timerTime;
134 timerStatus = true;
135 clearInterval(timer);
136 console.tag('Игра').log('Игра началаÑÑŒ.');
137 timer = setInterval(function () {
138 //console.tag('Game').log('Timer:' + time);
139 io.sockets.emit('timer', time--);
140 if ((game.status == 1) && (time <= preFinishingTime)) {
141 if (!preFinish) {
142 preFinish = true;
143 setGameStatus(2);
144 }
145 }
146 if (time <= 0) {
147 clearInterval(timer);
148 timerStatus = false;
149 console.tag('Игра').log('Игра окончена!');
150 showSliderWinners();
151 }
152 }, 1000);
153}
154
155function startNGTimer(winners) {
156 var time = 15;
157 data = JSON.parse(winners);
158 data.showSlider = true;
159 clearInterval(ngtimer);
160 ngtimer = setInterval(function () {
161 if (time <= 12) data.showSlider = false;
162 //console.tag('Game').log('NewGame Timer:' + time);
163 data.time = time--;
164 io.sockets.emit('slider', data);
165 if (time <= 0) {
166 clearInterval(ngtimer);
167 newGame();
168 }
169 }, 1000);
170}
171
172function getPriceItems() {
173 requestify.post(config.domain + '/api/getPriceItems', {
174 secretKey: config.secretKey
175 })
176 .then(function (response) {
177 Logger.info('Prices for items added');
178 }, function (response) {
179 Logger.info('Something wrong [getPriceItems]');
180 });
181}
182
183function getCurrentGame() {
184 requestify.post(config.domain + '/api/getCurrentGame', {
185 secretKey: config.secretKey
186 })
187 .then(function (response) {
188 game = JSON.parse(response.body);
189 Logger.info('Current Game #' + game.id);
190 if (game.status == 1) startTimer();
191 if (game.status == 2) startTimer();
192 if (game.status == 3) newGame();
193 }, function (response) {
194 console.tag('Game').log('Something wrong [getCurrentGame]');
195 setTimeout(getCurrentGame, 1000);
196 });
197}
198
199function bonus_bot(){
200 requestify.post(config.domain+'/api/bonus_bot', {
201 secretKey: config.secretKey
202 })
203}
204
205function newGame() {
206 requestify.post(config.domain + '/api/newGame', {
207 secretKey: config.secretKey
208 })
209 .then(function (response) {
210 preFinish = false;
211 game = JSON.parse(response.body);
212 console.tag('Game').log('New game! #' + game.id);
213 io.sockets.emit('newGame', game);
214 updateMaxPrice();
215 updateUsersToday();
216 updateGamesToday();
217 bonus_bot();
218 }, function (response) {
219 console.log(response);
220 console.tag('Game').error('Something wrong [newGame]');
221 setTimeout(newGame, 1000);
222 });
223}
224
225function showSliderWinners() {
226 requestify.post(config.domain + '/api/getWinners', {
227 secretKey: config.secretKey
228 })
229 .then(function (response) {
230 var winners = response.body;
231 console.tag('Game').log('Show slider!');
232 startNGTimer(winners);
233 setGameStatus(3);
234 //io.sockets.emit('slider', winners)
235 }, function (response) {
236 console.tag('Game').error('Something wrong [showSlider]');
237 setTimeout(showSliderWinners, 1000);
238 });
239}
240
241function setGameStatus(status) {
242 requestify.post(config.domain + '/api/setGameStatus', {
243 status: status,
244 secretKey: config.secretKey
245 })
246 .then(function (response) {
247 game = JSON.parse(response.body);
248 console.tag('Game').log('Set game to a prefinishing status. Bets are redirected to a new game.');
249 }, function (response) {
250 console.tag('Game').error('Something wrong [setGameStatus]');
251 setTimeout(setGameStatus, 1000);
252 });
253}
254
255function updateMaxPrice() {
256 requestify.post(config.domain + '/api/maxprice', {
257 secretKey: config.secretKey
258 })
259 .then(function (response) {
260 console.tag('СтатиÑтика', logTime()).log('МакÑ. Выигрыш - Обновлено');
261 data = JSON.parse(response.body);
262 io.sockets.emit('updateMaxPrice', data);
263 }
264 );
265}
266function updateUsersToday() {
267 requestify.post(config.domain + '/api/userstoday', {
268 secretKey: config.secretKey
269 })
270 .then(function (response) {
271 console.tag('СтатиÑтика', logTime()).log('Игроков Ð¡ÐµÐ³Ð¾Ð´Ð½Ñ - Обновлено');
272 data = JSON.parse(response.body);
273 io.sockets.emit('updateUsersToday', data);
274 }
275 );
276}
277function updateGamesToday() {
278 requestify.post(config.domain + '/api/gamestoday', {
279 secretKey: config.secretKey
280 })
281 .then(function (response) {
282 console.tag('СтатиÑтика', logTime()).log('Игр Ð¡ÐµÐ³Ð¾Ð´Ð½Ñ - Обновлено');
283 data = JSON.parse(response.body);
284 io.sockets.emit('updateGamesToday', data);
285 }
286 );
287}
288
289function checkSteamInventoryStatus() {
290 requestify.get('https://api.steampowered.com/ICSGOServers_730/GetGameServersStatus/v1/?key=' + config.apiKey)
291 .then(function (response) {
292 var answer = JSON.parse(response.body);
293 steamStatus = answer.result.services;
294 client.set('steam.community.status', steamStatus.SteamCommunity);
295 client.set('steam.inventory.status', steamStatus.IEconItems);
296 }, function (response) {
297 console.log('Something wrong [5]');
298 });
299}
300
301setInterval(checkSteamInventoryStatus, 12000);