Advertisement
David_your_friend

NodeJS

Dec 17th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Node JS
  2. var discord = require('discord-bot-webhook');
  3. var mysql = require('mysql');
  4. var io = require('socket.io')(8000);
  5. var connection = mysql.createConnection({
  6.   host     : 'localhost',
  7.   user     : 'root',
  8.   password : '',
  9.   database : 'rollgg'
  10. });
  11. connection.connect();
  12.  
  13. discord.hookId = '';
  14. discord.hookToken = '';
  15.  
  16. var users = [];
  17. var timer = 20;
  18. var currentBets = [];
  19. var currentRollInfo = [];
  20. var rolling = false;
  21. var usersOnline = 0;
  22.  
  23. io.on('connection', function(socket) {
  24.     usersOnline += 1
  25.     io.sockets.emit("users", usersOnline);
  26.     socket.on('authenticate', function(identifier) {
  27.         getAccountInfo(identifier,function(account){
  28.             //discord.sendMessage('new connection with the steamid: ' + account.steamid);
  29.             console.log('new connection with the steamid: ' + account.steamid);
  30.             users[socket.id] = {steamid: account.steamid, identifier: identifier, avatar: account.avatar, name: account.nickname};
  31.         });
  32.     });
  33.     socket.on('disconnect', function () {
  34.       //discord.sendMessage(users[socket.id].steamid + ' disconnected');
  35.       console.log(users[socket.id].steamid + ' disconnected');
  36.       usersOnline -= 1;
  37.       io.sockets.emit("users", usersOnline);
  38.       //delete users[socket.id];
  39.   });
  40. });
  41.  
  42. setInterval(function() {
  43.     if(timer.toFixed(2) >= -0.00)
  44.     {
  45.        io.sockets.emit("updateTime", timer);
  46.     }
  47.     timer-= 0.010;
  48.     if(timer.toFixed(2) == -1.00)
  49.     {
  50.         var rolledNumber = (Math.floor(Math.random() * 14));
  51.         var colour;
  52.         if (rolledNumber == 0)
  53.         {
  54.             colour = "green";
  55.         }
  56.         else if (rolledNumber <= 7)
  57.         {
  58.             colour = "red";
  59.         }
  60.         else
  61.         {
  62.             colour = "black";
  63.         }
  64.         currentRollInfo.push({roll: rolledNumber, colour: colour, time: Math.round((new Date()).getTime() / 1000)});
  65.         console.log("Rolled " + colour);
  66.         io.sockets.emit("roll", rolledNumber);
  67.         connection.query('INSERT INTO rolls SET ?', currentRollInfo);
  68.         rolling = true;
  69.         setInterval(function() {
  70.             rolling = false;
  71.         }, 3250);
  72.     }
  73.     if(timer.toFixed(2) == -4.20)
  74.     {
  75.         currentBets.forEach(function(bet) {
  76.             if(bet.colour == currentRollInfo[0].colour)
  77.             {
  78.                 console.log(bet.steamid + " Has won " + bet.amount*2);
  79.                 connection.query('UPDATE `users` SET `balance` = `balance` + '+bet.amount*2+' WHERE `steamid` = '+bet.steamid);
  80.             }
  81.         });
  82.         currentBets = [];
  83.         currentRollInfo = [];
  84.         timer = 20;
  85.     }
  86. },  10);
  87.  
  88. function getAccountInfo(identifier, callback)
  89. {
  90.     connection.query("SELECT * from users WHERE identifier = " + connection.escape(identifier), function(err, row) {
  91.         if (err)
  92.         {
  93.             console.log('Error while performing Query.');
  94.             //io.sockets.emit('notification', { message: "Error obtaining user details. Please try again later.", identifier: identifier, type: "error" });
  95.             return;
  96.         }
  97.         callback(row[0]);
  98.     });
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement