Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var auth = require('http-auth'),
- scribe = require('scribe-js')(),
- console = process.console,
- config = require('./config.js'),
- app = require('express')(),
- server = require('http').Server(app),
- io = require('socket.io')(server),
- redis = require('redis'),
- requestify = require('requestify'),
- bot = require('./bot.js');
- shop = require('./shop.js');
- var redisClient = redis.createClient(),
- client = redis.createClient();
- bot.init(redis, io, requestify);
- shop.init(redis, requestify);
- server.listen(config.serverPort);
- console.tag('SOCKET', logTime()).log('Сервер запущен на ' + config.domain + ':' + config.serverPort);
- var basicAuth = auth.basic({
- realm: "WebPanel",
- file: __dirname + "/users.htpasswd"
- });
- app.use('/logs', auth.connect(basicAuth), scribe.webPanel());
- redisClient.subscribe('ioMessage');
- redisClient.subscribe('show.winners');
- redisClient.subscribe('queue');
- redisClient.subscribe('newDeposit');
- redisClient.subscribe('msgChannel');
- redisClient.subscribe('MessageShop');
- redisClient.subscribe('depositDecline');
- redisClient.subscribe('withdraw_message');
- redisClient.subscribe('withdraw_message_close');
- redisClient.setMaxListeners(0);
- redisClient.on("message", function(channel, message) {
- if(channel == 'ioMessage') {
- message = JSON.parse(message);
- io.sockets.emit(message.io, message.message);
- }
- if(channel == 'withdraw_message') io.sockets.emit(channel, JSON.parse(message));
- if(channel == 'withdraw_message_close') io.sockets.emit(channel, JSON.parse(message));
- if(channel == 'depositDecline' || channel == 'queue' || channel == 'msgChannel'){
- io.sockets.emit(channel, message);
- }
- if(channel == 'show.winners'){
- clearInterval(timer);
- timerStatus = false;
- console.log('Принудительная остановка!');
- game.status = 3;
- showSliderWinners();
- }
- if(channel == 'newDeposit'){
- io.sockets.emit(channel, message);
- message = JSON.parse(message);
- if(!timerStatus && message.gameStatus == 1){
- game.status = 1;
- startTimer(io.sockets);
- }
- }
- });
- /* CHAT MESSGAGE */
- redisClient.subscribe('chat.message');
- redisClient.subscribe('new.msg');
- redisClient.subscribe('del.msg');
- redisClient.on("message", function (channel, message) {
- if (channel == 'new.msg')
- {
- updateChat();
- }
- if (channel == 'del.msg')
- {
- io.sockets.emit('DMessage', message);
- console.tag('CHAT', logTime()).log('Сообщение удалено!');
- }
- });
- /* CHAT MESSGAGE END */
- io.sockets.on('connection', function (socket) {
- updateOnline();
- socket.on('disconnect', function () {
- setTimeout(function () {
- updateOnline();
- }, 1500)
- })
- });
- function updateOnline() {
- io.sockets.emit('online', Object.keys(io.sockets.adapter.rooms).length);
- online = Object.keys(io.sockets.adapter.rooms).length;
- setTimeout(function () {
- if (online != Object.keys(io.sockets.adapter.rooms).length) {
- console.tag('SOCKET','ONLINE', logTime()).info('Подключено ' + Object.keys(io.sockets.adapter.rooms).length + ' пользователей');
- }
- }, 5000)
- }
- var steamStatus = [],
- game,
- timer,
- ngtimer,
- timerStatus = false,
- timerTime = config.gameTime,
- preFinishingTime = 2;
- online = 0;
- getCurrentGame();
- checkSteamInventoryStatus();
- // Chat & Stats & Winners Functions Start
- function updateMaxPrice() {
- requestify.post('http://' + config.domain + '/api/maxprice', {
- secretKey: config.secretKey
- })
- .then(function (response) {
- console.tag('Статистика', logTime()).log('Макс. Выигрыш - Обновлено');
- data = JSON.parse(response.body);
- io.sockets.emit('updateMaxPrice', data);
- }
- );
- }
- function updateUsersToday() {
- requestify.post('http://' + config.domain + '/api/userstoday', {
- secretKey: config.secretKey
- })
- .then(function (response) {
- console.tag('Статистика', logTime()).log('Игроков Сегодня - Обновлено');
- data = JSON.parse(response.body);
- io.sockets.emit('updateUsersToday', data);
- }
- );
- }
- function updateGamesToday() {
- requestify.post('http://' + config.domain + '/api/gamestoday', {
- secretKey: config.secretKey
- })
- .then(function (response) {
- console.tag('Статистика', logTime()).log('Игр Сегодня - Обновлено');
- data = JSON.parse(response.body);
- io.sockets.emit('updateGamesToday', data);
- }
- );
- }
- function updateChat() {
- requestify.post('http://' + config.domain + '/api/chat', {
- secretKey: config.secretKey
- })
- .then(function(response) {
- chat_messages = JSON.parse(response.body);
- io.sockets.emit('CMessages', chat_messages);
- console.tag('CHAT', logTime()).log('Новое сообщение!');
- }, function(response) {
- console.tag('CHAT', logTime()).log('Что-то не так [getChatMessages]');
- });
- }
- function lastwinner() {
- requestify.get('http://' + config.domain + '/api/lastwinner', {
- secretKey: config.secretKey
- })
- .then(function(response) {
- data = JSON.parse(response.body);
- console.tag('Last Winner', logTime()).log('Updating...');
- console.tag('Last Winner', logTime()).log('Success!');
- io.sockets.emit('LastWinner', data);
- },function(err){
- console.tag('Last Winner', logTime()).error(err);
- setTimeout(lastwinner, 1000);
- });
- }
- function happywinner() {
- requestify.get('http://' + config.domain + '/api/happywinner', {
- secretKey: config.secretKey
- })
- .then(function(response) {
- data = JSON.parse(response.body);
- console.tag('Happy Winner', logTime()).log('Updating...');
- console.tag('Happy Winner', logTime()).log('Success!');
- io.sockets.emit('HappyWinner', data);
- },function(err){
- console.tag('Happy Winner', logTime()).error(err);
- setTimeout(happywinner, 1000);
- });
- }
- function ChatBot() {
- requestify.post('http://' + config.domain + '/api/ChatBot', {
- secretKey: config.secretKey
- })
- .then(function (response) {
- updateChat();
- }, function (err) {
- console.tag('Игра').error(err);
- setTimeout(ChatBot, 1000);
- });
- }
- // Chat & Stats & Winners Functions End
- // Game Functions Start
- function logTime() {
- var date = new Date();
- var hour = date.getHours();
- var min = date.getMinutes();
- var sec = date.getSeconds();
- var year = date.getFullYear();
- var month = date.getMonth() + 1;
- var day = date.getDate();
- hour = (hour < 10 ? "0" : "") + hour;
- min = (min < 10 ? "0" : "") + min;
- sec = (sec < 10 ? "0" : "") + sec;
- month = (month < 10 ? "0" : "") + month;
- day = (day < 10 ? "0" : "") + day;
- return hour + ":" + min + ":" + sec;
- }
- function updatePrices(){
- requestify.post('http://' + config.domain + '/api/updatePrices', {
- secretKey: config.secretKey
- })
- .then(function (response) {
- console.tag('updatePrices', logTime()).log('Updating...');
- console.tag('updatePrices', logTime()).log('Success!');
- }, function (response) {
- console.tag('updatePrices', logTime()).log('Что-то не так [updatePrices]');
- });
- }
- function getRandomArbitary(min, max)
- {
- return Math.random() * (max - min) + min;
- }
- function offers() {
- io.sockets.emit('lastOffers');
- console.tag('LastBets', logTime()).log('Принимаем последние ставки...');
- preFinish = true;
- timerStatus = true;
- setTimeout(function(){
- setGameStatus(2);
- showSliderWinners();
- }, Math.round(getRandomArbitary(10000,15000)));
- }
- var preFinish = false;
- function startTimer(){
- var time = timerTime;
- timerStatus = true;
- clearInterval(timer);
- console.tag('Игра').log('Игра началась.');
- timer = setInterval(function(){
- console.tag('Игра', logTime()).log('Таймер: ' + time);
- io.sockets.emit('Time', time--);
- if(time <= 0){
- clearInterval(timer);
- console.tag('Игра', logTime()).log('Игра завершена.');
- offers();
- }
- }, 1000);
- }
- function startNGTimer(winners){
- var time = 22;
- data = JSON.parse(winners);
- data.showSlider = true;
- clearInterval(ngtimer);
- ngtimer = setInterval(function(){
- if(time <= 14) data.showSlider = false;
- console.tag('Игра', logTime()).log('Новая игра через: ' + time);
- if(time == 5) {
- lastwinner();
- happywinner();
- ChatBot();
- data.time = time--;
- }
- data.time = time--;
- io.sockets.emit('Roulette', data);
- if(time <= 0){
- clearInterval(ngtimer);
- newGame();
- }
- }, 1000);
- }
- function getCurrentGame(){
- requestify.post('http://'+config.domain+'/api/getCurrentGame', {
- secretKey: config.secretKey
- })
- .then(function(response) {
- game = JSON.parse(response.body);
- console.tag('Игра', logTime()).log('Текущая Игра #' + game.id);
- if(game.status == 1) startTimer();
- if(game.status == 2) startTimer();
- if(game.status == 3) newGame();
- },function(err){
- console.tag('Игра', logTime()).log(err);
- setTimeout(getCurrentGame, 1000);
- });
- }
- function newGame(){
- requestify.post('http://'+config.domain+'/api/newGame', {
- secretKey: config.secretKey
- })
- .then(function(response) {
- preFinish = false;
- game = JSON.parse(response.body);
- console.tag('Игра', logTime()).log('Новая игра, успешно создалась! #' + game.id);
- io.sockets.emit('newRound', game);
- bot.handleOffers();
- updateMaxPrice();
- updateUsersToday();
- updateGamesToday();
- },function(err){
- console.tag('Игра', logTime()).error(err);
- setTimeout(newGame, 1000);
- });
- }
- function showSliderWinners(){
- requestify.post('http://'+config.domain+'/api/getWinners', {
- secretKey: config.secretKey
- })
- .then(function(response) {
- var winners = response.body;
- timerStatus = false;
- console.tag('Игра', logTime()).log('Показываем слайдер!');
- startNGTimer(winners);
- setGameStatus(3);
- },function(err){
- console.tag('Игра', logTime()).error(err);
- setTimeout(showSliderWinners, 5000);
- });
- }
- function setGameStatus(status){
- requestify.post('http://'+config.domain+'/api/setGameStatus', {
- status: status,
- secretKey: config.secretKey
- })
- .then(function(response) {
- game = JSON.parse(response.body);
- console.tag('Игра', logTime()).log('Закрываем игру. Ставки будут перенаправлены на новую игру.');
- },function(response){
- console.tag('Игра', logTime()).error('Что-то не так [setGameStatus]');
- setTimeout(setGameStatus, 1000);
- });
- }
- function checkSteamInventoryStatus(){
- requestify.get('https://api.steampowered.com/ICSGOServers_730/GetGameServersStatus/v1/?key=' + config.apiKey)
- .then(function(response) {
- var answer = JSON.parse(response.body);
- steamStatus = answer.result.services;
- console.tag('SteamStatus', logTime()).info(steamStatus);
- client.set('steam.community.status', steamStatus.SteamCommunity);
- client.set('steam.inventory.status', steamStatus.IEconItems);
- if(steamStatus.SteamCommunity == 'normal') io.sockets.emit('SteamStatus', 'normal');
- if(steamStatus.SteamCommunity == 'delayed') io.sockets.emit('SteamStatus', 'delayed');
- if(steamStatus.SteamCommunity == 'critical') io.sockets.emit('SteamStatus', 'critical');
- },function(response){
- console.log('Something wrong [5]');
- console.log(response.body);
- });
- }
- // Game Functions End
- setInterval(checkSteamInventoryStatus, 120000);
- setInterval(updatePrices, 10800000); // 86400000 24 hours
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement