· 7 years ago · Feb 09, 2018, 02:02 PM
1var auth = require('http-auth'),
2 scribe = require('scribe-js')(),
3 console = process.console,
4 config = require('./config.js'),
5 app = require('express')(),
6 server = require('http').Server(app),
7 io = require('socket.io')(server),
8 redis = require('redis'),
9 requestify = require('requestify'),
10 bot = require('./bot.js');
11 shop = require('./shop.js');
12
13var redisClient = redis.createClient(),
14 client = redis.createClient();
15
16bot.init(redis, io, requestify);
17shop.init(redis, requestify);
18
19server.listen(config.serverPort);
20
21console.tag('SOCKET', logTime()).log('Сервер запущен на ' + config.domain + ':' + config.serverPort);
22
23redisClient.subscribe('ioMessage');
24redisClient.subscribe('show.winners');
25redisClient.subscribe('queue');
26redisClient.subscribe('newDeposit');
27redisClient.subscribe('msgChannel');
28redisClient.subscribe('MessageShop');
29redisClient.subscribe('depositDecline');
30redisClient.subscribe('withdraw_message');
31redisClient.subscribe('withdraw_message_close');
32redisClient.setMaxListeners(0);
33redisClient.on("message", function(channel, message) {
34 if(channel == 'ioMessage') {
35 message = JSON.parse(message);
36 io.sockets.emit(message.io, message.message);
37 }
38 if(channel == 'withdraw_message') io.sockets.emit(channel, JSON.parse(message));
39 if(channel == 'withdraw_message_close') io.sockets.emit(channel, JSON.parse(message));
40 if(channel == 'depositDecline' || channel == 'queue' || channel == 'msgChannel'){
41 io.sockets.emit(channel, message);
42 }
43 if(channel == 'show.winners'){
44 clearInterval(timer);
45 timerStatus = false;
46 console.log('ÐŸÑ€Ð¸Ð½ÑƒÐ´Ð¸Ñ‚ÐµÐ»ÑŒÐ½Ð°Ñ Ð¾Ñтановка!');
47 game.status = 3;
48 showSliderWinners();
49 }
50 if(channel == 'newDeposit'){
51 io.sockets.emit(channel, message);
52 message = JSON.parse(message);
53 if(!timerStatus && message.gameStatus == 1){
54 game.status = 1;
55 startTimer(io.sockets);
56 }
57
58 }
59});
60
61/* CHAT MESSGAGE */
62
63redisClient.subscribe('chat.message');
64redisClient.subscribe('new.msg');
65redisClient.subscribe('del.msg');
66redisClient.on("message", function (channel, message) {
67 if (channel == 'new.msg')
68 {
69 updateChat();
70 }
71 if (channel == 'del.msg')
72 {
73 io.sockets.emit('DMessage', message);
74 console.tag('CHAT', logTime()).log('Сообщение удалено!');
75 }
76});
77
78/* CHAT MESSGAGE END */
79
80io.sockets.on('connection', function (socket) {
81 updateOnline();
82 socket.on('disconnect', function () {
83 setTimeout(function () {
84 updateOnline();
85 }, 1500)
86 })
87});
88
89function updateOnline() {
90 io.sockets.emit('online', Object.keys(io.sockets.adapter.rooms).length);
91 online = Object.keys(io.sockets.adapter.rooms).length;
92 setTimeout(function () {
93 if (online != Object.keys(io.sockets.adapter.rooms).length) {
94 console.tag('SOCKET','ONLINE', logTime()).info('Подключено ' + Object.keys(io.sockets.adapter.rooms).length + ' пользователей');
95 }
96 }, 5000)
97}
98
99var steamStatus = [],
100 game,
101 timer,
102 ngtimer,
103 timerStatus = false,
104 timerTime = config.gameTime,
105 preFinishingTime = 2;
106 online = 0;
107
108getCurrentGame();
109checkSteamInventoryStatus();
110
111
112// Chat & Stats & Winners Functions Start
113
114function updateMaxPrice() {
115 requestify.post('http://' + config.domain + '/api/maxprice', {
116 secretKey: config.secretKey
117 })
118 .then(function (response) {
119 console.tag('СтатиÑтика', logTime()).log('МакÑ. Выигрыш - Обновлено');
120 data = JSON.parse(response.body);
121 io.sockets.emit('updateMaxPrice', data);
122 }
123 );
124}
125function updateUsersToday() {
126 requestify.post('http://' + config.domain + '/api/userstoday', {
127 secretKey: config.secretKey
128 })
129 .then(function (response) {
130 console.tag('СтатиÑтика', logTime()).log('Игроков Ð¡ÐµÐ³Ð¾Ð´Ð½Ñ - Обновлено');
131 data = JSON.parse(response.body);
132 io.sockets.emit('updateUsersToday', data);
133 }
134 );
135}
136function updateGamesToday() {
137 requestify.post('http://' + config.domain + '/api/gamestoday', {
138 secretKey: config.secretKey
139 })
140 .then(function (response) {
141 console.tag('СтатиÑтика', logTime()).log('Игр Ð¡ÐµÐ³Ð¾Ð´Ð½Ñ - Обновлено');
142 data = JSON.parse(response.body);
143 io.sockets.emit('updateGamesToday', data);
144 }
145 );
146}
147
148function updateChat() {
149 requestify.post('http://' + config.domain + '/api/chat', {
150 secretKey: config.secretKey
151 })
152 .then(function(response) {
153 chat_messages = JSON.parse(response.body);
154 io.sockets.emit('CMessages', chat_messages);
155 console.tag('CHAT', logTime()).log('Ðовое Ñообщение!');
156 }, function(response) {
157 console.tag('CHAT', logTime()).log('Что-то не так [getChatMessages]');
158 });
159}
160
161function lastwinner() {
162 requestify.get('http://' + config.domain + '/api/lastwinner', {
163 secretKey: config.secretKey
164 })
165 .then(function(response) {
166 data = JSON.parse(response.body);
167 console.tag('Last Winner', logTime()).log('Updating...');
168 console.tag('Last Winner', logTime()).log('Success!');
169 io.sockets.emit('LastWinner', data);
170 },function(err){
171 console.tag('Last Winner', logTime()).error(err);
172 setTimeout(lastwinner, 1000);
173 });
174}
175
176function happywinner() {
177 requestify.get('http://' + config.domain + '/api/happywinner', {
178 secretKey: config.secretKey
179 })
180 .then(function(response) {
181 data = JSON.parse(response.body);
182 console.tag('Happy Winner', logTime()).log('Updating...');
183 console.tag('Happy Winner', logTime()).log('Success!');
184 io.sockets.emit('HappyWinner', data);
185 },function(err){
186 console.tag('Happy Winner', logTime()).error(err);
187 setTimeout(happywinner, 1000);
188 });
189}
190
191
192function ChatBot() {
193 requestify.post('http://' + config.domain + '/api/ChatBot', {
194 secretKey: config.secretKey
195 })
196 .then(function (response) {
197 updateChat();
198 }, function (err) {
199 console.tag('Игра').error(err);
200 setTimeout(ChatBot, 1000);
201 });
202}
203
204// Chat & Stats & Winners Functions End
205
206
207// Game Functions Start
208
209function logTime() {
210
211 var date = new Date();
212 var hour = date.getHours();
213 var min = date.getMinutes();
214 var sec = date.getSeconds();
215
216 var year = date.getFullYear();
217 var month = date.getMonth() + 1;
218 var day = date.getDate();
219
220 hour = (hour < 10 ? "0" : "") + hour;
221 min = (min < 10 ? "0" : "") + min;
222 sec = (sec < 10 ? "0" : "") + sec;
223 month = (month < 10 ? "0" : "") + month;
224 day = (day < 10 ? "0" : "") + day;
225
226 return hour + ":" + min + ":" + sec;
227}
228
229function updatePrices(){
230 requestify.post('http://' + config.domain + '/api/updatePrices', {
231 secretKey: config.secretKey
232 })
233 .then(function (response) {
234 console.tag('updatePrices', logTime()).log('Updating...');
235 console.tag('updatePrices', logTime()).log('Success!');
236 }, function (response) {
237 console.tag('updatePrices', logTime()).log('Что-то не так [updatePrices]');
238 });
239}
240
241function getRandomArbitary(min, max)
242{
243 return Math.random() * (max - min) + min;
244}
245
246function offers() {
247 io.sockets.emit('lastOffers');
248 console.tag('LastBets', logTime()).log('Принимаем поÑледние Ñтавки...');
249 preFinish = true;
250 timerStatus = true;
251 setTimeout(function(){
252 setGameStatus(2);
253 showSliderWinners();
254 }, Math.round(getRandomArbitary(10000,15000)));
255}
256
257
258var preFinish = false;
259function startTimer(){
260 var time = timerTime;
261 timerStatus = true;
262 clearInterval(timer);
263 console.tag('Игра').log('Игра началаÑÑŒ.');
264 timer = setInterval(function(){
265 console.tag('Игра', logTime()).log('Таймер: ' + time);
266 io.sockets.emit('Time', time--);
267 if(time <= 0){
268 clearInterval(timer);
269 console.tag('Игра', logTime()).log('Игра завершена.');
270 offers();
271 }
272 }, 1000);
273}
274
275function startNGTimer(winners){
276 var time = 22;
277 data = JSON.parse(winners);
278 data.showSlider = true;
279 clearInterval(ngtimer);
280 ngtimer = setInterval(function(){
281 if(time <= 14) data.showSlider = false;
282 console.tag('Игра', logTime()).log('ÐÐ¾Ð²Ð°Ñ Ð¸Ð³Ñ€Ð° через: ' + time);
283 if(time == 5) {
284 lastwinner();
285 happywinner();
286 ChatBot();
287 data.time = time--;
288 }
289 data.time = time--;
290 io.sockets.emit('Roulette', data);
291 if(time <= 0){
292 clearInterval(ngtimer);
293 newGame();
294 }
295 }, 1000);
296}
297
298function getCurrentGame(){
299 requestify.post('http://'+config.domain+'/api/getCurrentGame', {
300 secretKey: config.secretKey
301 })
302 .then(function(response) {
303 game = JSON.parse(response.body);
304 console.tag('Игра', logTime()).log('Ð¢ÐµÐºÑƒÑ‰Ð°Ñ Ð˜Ð³Ñ€Ð° #' + game.id);
305 if(game.status == 1) startTimer();
306 if(game.status == 2) startTimer();
307 if(game.status == 3) newGame();
308 },function(err){
309 console.tag('Игра', logTime()).log(err);
310 setTimeout(getCurrentGame, 1000);
311 });
312}
313
314function newGame(){
315 requestify.post('http://'+config.domain+'/api/newGame', {
316 secretKey: config.secretKey
317 })
318 .then(function(response) {
319 preFinish = false;
320 game = JSON.parse(response.body);
321 console.tag('Игра', logTime()).log('ÐÐ¾Ð²Ð°Ñ Ð¸Ð³Ñ€Ð°, уÑпешно ÑоздалаÑÑŒ! #' + game.id);
322 io.sockets.emit('newRound', game);
323 bot.handleOffers();
324 updateMaxPrice();
325 updateUsersToday();
326 updateGamesToday();
327 },function(err){
328 console.tag('Игра', logTime()).error(err);
329 setTimeout(newGame, 1000);
330 });
331}
332
333
334function showSliderWinners(){
335 requestify.post('http://'+config.domain+'/api/getWinners', {
336 secretKey: config.secretKey
337 })
338 .then(function(response) {
339 var winners = response.body;
340 timerStatus = false;
341 console.tag('Игра', logTime()).log('Показываем Ñлайдер!');
342 startNGTimer(winners);
343 setGameStatus(3);
344 },function(err){
345 console.tag('Игра', logTime()).error(err);
346 setTimeout(showSliderWinners, 5000);
347 });
348}
349
350function setGameStatus(status){
351 requestify.post('http://'+config.domain+'/api/setGameStatus', {
352 status: status,
353 secretKey: config.secretKey
354 })
355 .then(function(response) {
356 game = JSON.parse(response.body);
357 console.tag('Игра', logTime()).log('Закрываем игру. Ставки будут перенаправлены на новую игру.');
358 },function(response){
359 console.tag('Игра', logTime()).error('Что-то не так [setGameStatus]');
360 setTimeout(setGameStatus, 1000);
361 });
362}
363
364function checkSteamInventoryStatus(){
365 requestify.get('https://api.steampowered.com/ICSGOServers_730/GetGameServersStatus/v1/?key=' + config.apiKey)
366 .then(function(response) {
367 var answer = JSON.parse(response.body);
368 steamStatus = answer.result.services;
369 console.tag('SteamStatus', logTime()).info(steamStatus);
370 client.set('steam.community.status', steamStatus.SteamCommunity);
371 client.set('steam.inventory.status', steamStatus.IEconItems);
372 if(steamStatus.SteamCommunity == 'normal') io.sockets.emit('SteamStatus', 'normal');
373 if(steamStatus.SteamCommunity == 'delayed') io.sockets.emit('SteamStatus', 'delayed');
374 if(steamStatus.SteamCommunity == 'critical') io.sockets.emit('SteamStatus', 'critical');
375 },function(response){
376 console.log('Something wrong [5]');
377 console.log(response.body);
378 });
379}
380
381
382// Game Functions End
383
384setInterval(checkSteamInventoryStatus, 120000);
385setInterval(updatePrices, 10800000); // 86400000 24 hours