· 9 years ago · Oct 08, 2016, 10:22 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
12var redisClient = redis.createClient(),
13 client = redis.createClient();
14
15bot.init(redis, io, requestify);
16
17server.listen(config.serverPort);
18
19console.log('Server started on ' + config.domain + ':' + config.serverPort);
20
21var basicAuth = auth.basic({ //basic auth config
22 realm: "csgobigocoins.com panel",
23 file: __dirname + "/users.htpasswd" // test:test
24});
25app.use('/logs', auth.connect(basicAuth), scribe.webPanel());
26
27redisClient.subscribe('show.winners');
28redisClient.subscribe('queue');
29redisClient.subscribe('newDeposit');
30redisClient.subscribe('depositDecline');
31redisClient.setMaxListeners(0);
32redisClient.on("message", function(channel, message) {
33 if(channel == 'depositDecline' || channel == 'queue'){
34 io.sockets.emit(channel, message);
35 }
36 if(channel == 'show.winners'){
37 clearInterval(timer);
38 timerStatus = false;
39 console.log('Force Stop');
40 game.status = 3;
41 showSliderWinners();
42 }
43 if(channel == 'newDeposit'){
44 io.sockets.emit(channel, message);
45
46 message = JSON.parse(message);
47 if(!timerStatus && message.gameStatus == 1){
48 game.status = 1;
49 startTimer(io.sockets);
50 }
51
52 }
53});
54
55io.sockets.on('connection', function(socket) {
56
57 updateOnline();
58
59 socket.on('disconnect', function(){
60 updateOnline();
61 })
62});
63
64function updateOnline(){
65 io.sockets.emit('online', Object.keys(io.sockets.adapter.rooms).length);
66 console.info('Connected ' + Object.keys(io.sockets.adapter.rooms).length + ' clients');
67}
68
69var steamStatus = [],
70 game,
71 timer,
72 ngtimer,
73 timerStatus = false,
74 timerTime = 120,
75 preFinishingTime = 2;
76
77getCurrentGame();
78checkSteamInventoryStatus();
79
80var preFinish = false;
81function startTimer(){
82 var time = timerTime;
83 timerStatus = true;
84 clearInterval(timer);
85 console.tag('Game').log('Game start.');
86 timer = setInterval(function(){
87 console.tag('Game').log('Timer:' + time);
88 io.sockets.emit('timer', time--);
89 if((game.status == 1) && (time <= preFinishingTime)){
90 if(!preFinish){
91 preFinish = true;
92 setGameStatus(2);
93 }
94 }
95 if(time <= 0){
96 clearInterval(timer);
97 timerStatus = false;
98 console.tag('Game').log('Game end.');
99 showSliderWinners();
100 }
101 }, 1000);
102}
103
104function startNGTimer(winners){
105 var time = 30;
106 data = JSON.parse(winners);
107 data.showSlider = true;
108 clearInterval(ngtimer);
109 ngtimer = setInterval(function(){
110 if(time <= 17) data.showSlider = false;
111 console.tag('Game').log('NewGame Timer:' + time);
112 data.time = time--;
113 io.sockets.emit('slider', data);
114 if(time <= 0){
115 clearInterval(ngtimer);
116 newGame();
117 }
118 }, 1000);
119}
120
121function getCurrentGame(){
122 requestify.post('http://'+config.domain+'/api/getCurrentGame', {
123 secretKey: config.secretKey
124 })
125 .then(function(response) {
126 game = JSON.parse(response.body);
127 console.tag('Game').log('Current Game #' + game.id);
128 if(game.status == 1) startTimer();
129 if(game.status == 2) startTimer();
130 if(game.status == 3) newGame();
131 },function(response){
132 console.tag('Game').log('Something wrong [getCurrentGame]');
133 setTimeout(getCurrentGame, 1000);
134 });
135}
136
137function newGame(){
138 requestify.post('http://'+config.domain+'/api/newGame', {
139 secretKey: config.secretKey
140 })
141 .then(function(response) {
142 preFinish = false;
143 game = JSON.parse(response.body);
144 console.tag('Game').log('New game! #' + game.id);
145 io.sockets.emit('newGame', game);
146 bot.handleOffers();
147 },function(response){
148 console.tag('Game').error('Something wrong [newGame]');
149 setTimeout(newGame, 1000);
150 });
151}
152
153function showSliderWinners(){
154 requestify.post('http://'+config.domain+'/api/getWinners', {
155 secretKey: config.secretKey
156 })
157 .then(function(response) {
158 var winners = response.body;
159 console.tag('Game').log('Show slider!');
160 startNGTimer(winners);
161 setGameStatus(3);
162 //io.sockets.emit('slider', winners)
163 },function(response){
164 console.tag('Game').error('Something wrong [showSlider]');
165 setTimeout(showSliderWinners, 1000);
166 });
167}
168
169function setGameStatus(status){
170 requestify.post('http://'+config.domain+'/api/setGameStatus', {
171 status: status,
172 secretKey: config.secretKey
173 })
174 .then(function(response) {
175 game = JSON.parse(response.body);
176 console.tag('Game').log('Set game to a prefinishing status. Bets are redirected to a new game.');
177 },function(response){
178 console.tag('Game').error('Something wrong [setGameStatus]');
179 setTimeout(setGameStatus, 1000);
180 });
181}
182
183function checkSteamInventoryStatus(){
184 requestify.get('https://api.steampowered.com/ICSGOServers_730/GetGameServersStatus/v1/?key=' + config.apiKey)
185 .then(function(response) {
186 var answer = JSON.parse(response.body);
187 steamStatus = answer.result.services;
188 console.tag('SteamStatus').info(steamStatus);
189 client.set('steam.community.status', steamStatus.SteamCommunity);
190 client.set('steam.inventory.status', steamStatus.IEconItems);
191 },function(response){
192 console.log('Something wrong [5]');
193 console.log(response.body);
194 });
195}
196setInterval(checkSteamInventoryStatus, 120000);