daily pastebin goal
32%
SHARE
TWEET

Untitled

a guest Apr 27th, 2016 26 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var _ = require('lodash');
  2. var server = require('http').Server();
  3. var io = require('socket.io')(server);
  4. var mysql = require('mysql');
  5. var request = require("request");
  6.  
  7.  
  8. var connection = mysql.createConnection({
  9.     host: 'localhost',
  10.     user: 'XXXX',
  11.     password: 'XXXX',
  12.     database: 'XXXX'
  13. });
  14.  
  15. server.listen(4555);
  16.  
  17. var timeleft;
  18. var winnerNumber = 99;
  19. var gameTime = 45;
  20. //var end_at = 0;
  21.  
  22.  
  23. var end_at = (new Date).getTime() + 45000;
  24.  
  25.  
  26. io.on('connection', function (socket) {
  27.  
  28.     getLatestColors(socket);
  29.  
  30.     //emitujemy timer do nowo polaczonych graczy
  31.     socket.emit('startTimer', (new Date).getTime(), end_at);
  32.  
  33.  
  34.     setInterval(function () {
  35.         if (winnerNumber != 99) {
  36.             socket.emit('clearGame', winnerNumber);
  37.             setTimeout(function () {
  38.                 winnerNumber = 99;
  39.                 socket.emit('clearWinner', true);
  40.             }, 5000);
  41.             return;
  42.         }
  43.         if (timeleft < 1) {
  44.             socket.emit('counter', 45);
  45.         } else {
  46.             socket.emit('counter', timeleft);
  47.         }
  48.     }, 1000);
  49. });
  50.  
  51.  
  52. connection.connect(function (err) {
  53.     if (err) {
  54.         console.error('error connecting: ' + err.stack);
  55.         return;
  56.     }
  57. });
  58. ////////////////// CSBOX.pl Bot v0.0.1
  59.  
  60. /*
  61.  setInterval(function () {
  62.  CheckTimer();
  63.  }, 1000);
  64.  */
  65. function CheckTimer() {
  66.     // aktualnie brak timera
  67.     if (end_at <= (new Date).getTime()) {
  68.  
  69.         // pobieramy uzytkownikow z aktualnej gry
  70.         connection.query('SELECT count(*) as cnt,(SELECT id FROM roulette_history ORDER BY id DESC LIMIT 1) as gameid, (SELECT endTime FROM roulette_history ORDER BY id DESC LIMIT 1) as endTime FROM placed_bets WHERE gameID = (SELECT id FROM roulette_history ORDER BY id DESC LIMIT 1)', function (err, rows) {
  71.             var users = rows[0]['cnt'];
  72.             var endTime = rows[0]['cnt'];
  73.             var roundId = rows[0]['gameid'];
  74.             if (users > 0 && endTime == 0) {
  75.                 var unixTime = parseInt(new Date().getTime() / 1000, 10);
  76.                 connection.query('UPDATE roulette_history SET endTime="' + unixTime + '" WHERE id= (SELECT id FROM roulette_history ORDER BY id DESC LIMIT 1)', function (err, rows) {
  77.                     if (err) {
  78.                         console.log('Wystąpił błąd');
  79.                     } else {
  80.                         //zwyciezcy starej gry
  81.                         //proceedWinners(roundId);
  82.                         console.log('zwyciezcy starej gry');
  83.                         //puszczamy nowy timer/gre
  84.                         end_at = endTime;
  85.                         io.on('connection', function (socket) {
  86.                             socket.emit('startTimer', (new Date).getTime(), end_at);
  87.                         });
  88.                     }
  89.                 });
  90.                 return;
  91.             }
  92.         });
  93.  
  94.     } else {
  95.         //aktualnie mamy timerek
  96.  
  97.     }
  98.  
  99.     /*
  100.      // handleOffers();
  101.      var unixTime = parseInt(new Date().getTime() / 1000, 10);
  102.      connection.query('SELECT id,endTime FROM roulette_history ORDER BY id DESC limit 1', function (err, rows) {
  103.      if (err) return;
  104.      var roundID = rows[0]['id'];
  105.      var endTime = rows[0]['endTime'];
  106.  
  107.      connection.query('SELECT count(*) as cnt FROM placed_bets WHERE gameID="' + roundID + '"', function (err, rows) {
  108.      var Users = rows[0]['cnt'];
  109.      if (endTime == 0 && Users != 0) {
  110.      connection.query('UPDATE roulette_history SET endTime="' + unixTime + '" WHERE id="' + roundID + '"', function (err, rows) {
  111.      if (err) {
  112.      console.log('Wystąpił błąd');
  113.      } else {
  114.      //console.log('Zaczynam rundę');
  115.      }
  116.      });
  117.      return;
  118.      }
  119.  
  120.      if (Users != 0) {
  121.      timeleft = endTime + gameTime - unixTime;
  122.      if (timeleft < 6 && timeleft > 0) {
  123.      //console.log('Pozostały czas rundy:' + timeleft);
  124.      }
  125.      if (timeleft == 0) {
  126.      proceedWinners(roundID);
  127.      return;
  128.      }
  129.      }
  130.  
  131.      }
  132.      )
  133.      ;
  134.      });
  135.  
  136.      */
  137.  
  138. }
  139.  
  140. function getLatestColors(socket) {
  141.     connection.query("SELECT * FROM roulette_history WHERE numberWon != '99' ORDER BY id DESC LIMIT 10", function (err, rows) {
  142.         socket.emit('lastColors', rows);
  143.     });
  144. };
  145.  
  146.  
  147. /*
  148.  function proceedWinners(roundID) {
  149.  request({
  150.  url: 'http://csgourban.com/getWinningColor98475983457xkjhas82',
  151.  json: true
  152.  }, function (error, response, body) {
  153.  console.log(body['number']);
  154.  winnerNumber = body['number'];
  155.  });
  156.  proceeded = false;
  157.  } */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand