Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //site.js\\
- var mysql = require('mysql');
- var log4js = require('log4js');
- var io = require('socket.io')(8000);
- var request = require('request');
- var fs = require('fs');
- var md5 = require('md5');
- var sha256 = require('sha256');
- var math = require('mathjs');
- var crashNumber = 100;
- var crashState = 'NULL';
- var crashAt;
- var timp = 700;
- var intervalul;
- var isStatedSet = 0;
- var rCAt;
- var instaCrash = 0;
- var HaShGame;
- var timerC1;
- var timerC2;
- var timerC3;
- var timerC4;
- var timerC5;
- var timerC6;
- var timerC7;
- var timerC8;
- var timerC9;
- var timerC10;
- var timerC11;
- var timerC12;
- var CRbr = 1;
- var usersCr = {};
- var CRusersAmount = {};
- var CRcurrentBets = [];
- log4js.configure({
- appenders: [
- { type: 'console' },
- { type: 'file', filename: 'logs/site.log' }
- ]
- });
- var logger = log4js.getLogger();
- var pool = mysql.createPool({
- connectionLimit : 10,
- database: 'ro',
- host: 'localhost',
- user: 'root',
- password: 'PASSWORD'
- });
- process.on('uncaughtException', function (err) {
- logger.trace('Strange error');
- logger.debug(err);
- });
- /* */
- var accept = 30;
- var wait = 10;
- var br = 3;
- var chat = 2;
- var chatb = 10000;
- var maxbet = 10000000;
- var minbet = 50;
- var q1 = 2;
- var q2 = 14;
- var timer = -1;
- var users = {};
- var roll = 0;
- var currentBets = [];
- var historyRolls = [];
- var usersBr = {};
- var usersAmount = {};
- var currentSums = {
- '0-0': 0,
- '1-7': 0,
- '8-14': 0
- };
- var canPlayersBet = 1;
- var currentRollid = 0;
- var pause = false;
- var hash = '';
- var last_message = {};
- /* */
- load();
- var prices;
- request('http://backpack.tf/api/IGetMarketPrices/v1/?key=58503fc4e338774aec34d9b0&appid=730', function(error, response, body) {
- prices = JSON.parse(body);
- if(prices.response.success == 0) {
- logger.warn('It was not possible to load prices. Prices taken from the cache');
- if(fs.existsSync(__dirname + '/prices.txt')){
- prices = JSON.parse(fs.readFileSync(__dirname + '/var/www/prices.txt'));
- logger.warn('Prices have been retrieved from the cache');
- } else {
- logger.error('No prices in cache');
- process.exit(0);
- }
- if(fs.existsSync(__dirname + '/var/www/prices.txt')){
- prices = JSON.parse(fs.readFileSync(__dirname + '/var/www/prices.txt'));
- logger.warn('/var/www/prices.txt Loaded cached');
- } else {
- logger.error('No /var/www/prices.txt in cache');
- process.exit(0);
- }
- } else {
- fs.writeFileSync('/var/www/prices.txt', body);
- logger.trace('Prices loaded successfully');
- }
- });
- updateHash();
- function updateHash() {
- query('SELECT * FROM `hash` ORDER BY `id` DESC LIMIT 1', function(err, row) {
- if(err) {
- logger.error('Cant get the hash, stopping');
- logger.debug(err);
- process.exit(0);
- return;
- }
- if(row.length == 0) {
- logger.error('Wrong hash found, stopping');
- process.exit(0);
- } else {
- if(hash != row[0].hash) logger.warn('Loaded hash'+row[0].hash);
- hash = row[0].hash;
- }
- });
- }
- io.on('connection', function(socket) {
- var user = false;
- socket.on('hash', function(hash) {
- query('SELECT * FROM `users` WHERE `hash` = '+pool.escape(hash), function(err, row) {
- if((err) || (!row.length)) return socket.disconnect();
- user = row[0];
- socket.join(user.steamid);
- socket.join(user.steamid);
- users[user.steamid] = {
- socket: socket.id,
- balance: parseInt(row[0].balance)
- }
- socket.emit('message', {
- accept: accept,
- balance: row[0].balance,
- br: br,
- chat: chat,
- chatb: chatb,
- count: timer-wait,
- icon: row[0].avatar,
- maxbet: maxbet,
- minbet: minbet,
- name: row[0].name,
- rank: row[0].rank,
- rolls: historyRolls,
- type: 'hello',
- user: row[0].steamid
- });
- socket.emit('message', {
- type: 'logins',
- count: Object.size(io.sockets.connected)
- });
- currentBets.forEach(function(itm) {
- socket.emit('message', {
- type: 'bet',
- bet: {
- amount: itm.amount,
- betid: itm.betid,
- icon: itm.icon,
- lower: itm.lower,
- name: itm.name,
- rollid: itm.rollid,
- upper: itm.upper,
- user: itm.user,
- won: null
- },
- sums: {
- 0: currentSums['0-0'],
- 1: currentSums['1-7'],
- 2: currentSums['8-14'],
- }
- });
- });
- });
- });
- CRcurrentBets.forEach(function(itm) {
- socket.emit('message', {
- type: 'crbet',
- bet: {
- autoCashout: itm.autoCash,
- amount: itm.amount,
- betid: itm.betid,
- icon: itm.icon,
- name: itm.name,
- user: itm.user,
- won: '0'
- }
- });
- });
- socket.on('mes', function(m) {
- if(!user) return;
- logger.debug(m);
- if(m.type == "bet") return setBet(m, user, socket);
- if(m.type == "crbet") {
- if(m.mtype == 'joinCrash') {
- return setTimeout(CRsetBet(m, user, socket), 250);
- }else if(m.mtype == 'withdraw') {
- return CrashCashout(m, user, socket);
- }
- }
- if(m.type == "balance") return getBalance(user, socket);
- if(m.type == "chat") return ch(m, user, socket);
- });
- socket.on('disconnect', function() {
- io.sockets.emit('message', {
- type: 'logins',
- count: Object.size(io.sockets.connected)
- });
- delete users[user.steamid];
- socket.leave(user.steamid);
- })
- });
- function plus(user, socket) {
- query('SELECT * FROM `users` WHERE `steamid` = '+pool.escape(user.steamid), function(err, row) {
- if(err) return;
- if(time() > row[0].plus) {
- query('UPDATE `users` SET `plus` = '+pool.escape(time()+86400)+', `balance` = `balance` + 100 WHERE `steamid` = '+user.steamid);
- socket.emit('message', {
- type: 'alert',
- alert: 'Confirmed'
- });
- getBalance(user, socket);
- } else {
- socket.emit('message', {
- type: 'alert',
- alert: 'You have '+(row[0].plus-time())+' to accept'
- });
- }
- });
- }
- function ch(m, user, socket) {
- if(m.msg) {
- var res = null;
- if (res = /^\/send ([0-9]*) ([0-9]*)/.exec(m.msg)) {
- logger.trace('problem with translating from russian'+res[2]+' user '+res[1]);
- query('SELECT `balance` FROM `users` WHERE `steamid` = '+pool.escape(user.steamid), function(err, row) {
- if((err) || (!row.length)) {
- logger.error('Could not find user to send the coins');
- logger.debug(err);
- socket.emit('message', {
- type: 'error',
- enable: false,
- error: 'Error: You are not DB.'
- });
- return;
- }
- if(row[0].balance < res[2]) {
- socket.emit('message', {
- type: 'error',
- enable: false,
- error: 'Error: Insufficient funds.'
- });
- } else if(res[2] <= 0) {
- socket.emit('message', {
- type: 'error',
- enable: false,
- error: 'Error: Amount must be greater than 0.'
- });
- } else {
- query('SELECT `name` FROM `users` WHERE `steamid` = '+pool.escape(res[1]), function(err2, row2) {
- if((err) || (!row.length)) {
- logger.error('Could not get people to move');
- logger.debug(err);
- socket.emit('message', {
- type: 'error',
- enable: false,
- error: 'Error: Unknown receiver.'
- });
- return;
- }
- query('UPDATE `users` SET `balance` = `balance` - '+res[2]+' WHERE `steamid` = '+pool.escape(user.steamid));
- query('UPDATE `users` SET `balance` = `balance` + '+res[2]+' WHERE `steamid` = '+pool.escape(res[1]));
- query('INSERT INTO `transfers` SET `from1` = '+pool.escape(user.steamid)+', `to1` = '+pool.escape(res[1])+', `amount` = '+pool.escape(res[2])+', `time` = '+pool.escape(time()));
- socket.emit('message', {
- type: 'alert',
- alert: 'You sent '+res[2]+' coins to '+row2[0].name+'.'
- });
- getBalance(user, socket);
- });
- }
- });
- }else {
- query('SELECT SUM(`amount`) AS castor FROM `bets` WHERE `user` = '+pool.escape(user.steamid), function(err, row) {
- if((err) || (!row.length)) {
- logger.error('Failed to get the person to transfer');
- logger.debug(err);
- socket.emit('message', {
- type: 'error',
- enable: false,
- error: 'Error: Unknown receiver.'
- });
- return;
- }
- if(row[0].castor <= chatb) {
- socket.emit('message', {
- type: 'error',
- enable: false,
- error: 'Chat unlocks after 10,000 coins have been played. You need: '+pool.escape(row[0].castor-chatb + ' coins')
- });
- } else if (res = /^\/mute ([0-9]*) ([0-9]*)/.exec(m.msg)) {
- if(user.rank > 0) {
- var t = time();
- query('UPDATE `users` SET `mute` = '+pool.escape(parseInt(t)+parseInt(res[2]))+' WHERE `steamid` = '+pool.escape(res[1]));
- socket.emit('message', {
- type: 'alert',
- alert: 'You muted '+res[1]+' to '+res[2]
- });
- }
- } else {
- query('SELECT `mute` FROM `users` WHERE `steamid` = '+pool.escape(user.steamid), function(err, row) {
- if(err) return;
- if(row[0].mute > time()) {
- socket.emit('message', {
- type: 'alert',
- alert: 'You are muted '+(row[0].mute-time() + ' more seconds')
- });
- return;
- } else if (res = /^\/ban ([0-9]*) ([0-9]*)/.exec(m.msg)) {
- if(user.rank == 1) {
- var t = time();
- query('UPDATE `users` SET `ban` = 1 WHERE `steamid` = '+pool.escape(res[1]));
- socket.emit('message', {
- type: 'alert',
- alert: 'You ban '+res[1]+' '+res[2]
- });
- return;
- }
- }
- else if (res = /^\/unban ([0-9]*) ([0-9]*)/.exec(m.msg)) {
- if(user.rank == 1) {
- var t = time();
- query('UPDATE `users` SET `ban` = 0 WHERE `steamid` = '+pool.escape(res[1]));
- socket.emit('message', {
- type: 'alert',
- alert: 'You unban '+res[1]+' '+res[2]
- });
- return;
- }
- }
- else if (res = /^\/banwithdraw ([0-9]*) ([0-9]*)/.exec(m.msg)) {
- if(user.rank == 1) {
- var t = time();
- query('UPDATE `users` SET `banwithdraw` = 1 WHERE `steamid` = '+pool.escape(res[1]));
- socket.emit('message', {
- type: 'alert',
- alert: 'You ban '+(res[1]+' '+res[2] + ' to withdraw')
- });
- return;
- }
- }
- else if (res = /^\/unbanwithdraw ([0-9]*) ([0-9]*)/.exec(m.msg)) {
- if(user.rank == 1) {
- var t = time();
- query('UPDATE `users` SET `banwithdraw` = 0 WHERE `steamid` = '+pool.escape(res[1]));
- socket.emit('message', {
- type: 'alert',
- alert: 'You unban '+(res[1]+' '+res[2] + ' to withdraw')
- });
- return;
- }
- }
- else if (res = /^\/setbalance ([0-9]*) ([0-9]*)/.exec(m.msg)) {
- if(user.rank == 1) {
- var t = time();
- query('UPDATE `users` SET `balance` = '+res[2]+' WHERE `steamid` = '+pool.escape(res[1]));
- socket.emit('message', {
- type: 'alert',
- alert: 'You set '+res[1]+' balance to '+res[2]
- });
- return;
- }
- }
- else if (res = /^\/setrank ([0-9]*) ([0-9]*)/.exec(m.msg)) {
- if(user.rank == 1) {
- var t = time();
- query('UPDATE `users` SET `rank` = '+res[2]+' WHERE `steamid` = '+pool.escape(res[1]));
- socket.emit('message', {
- type: 'alert',
- alert: 'You set '+res[1]+' rank to '+res[2]
- });
- return;
- }
- }
- else if (res = /^\/ranks ([0-9]*)/.exec(m.msg)) {
- if(user.rank == 1) {
- var t = time();
- socket.emit('message', {
- type: 'alert',
- alert: '\n1- Admin \n2- Mod \n3- Twitch \n4- V.I.P \n5- Developer \n6- YouTube \n7- Support'
- });
- return;
- }
- }
- io.sockets.emit('message', {
- type: 'chat',
- msg: safe_tags_replace(m.msg),
- name: user.name,
- icon: user.avatar,
- user: user.steamid,
- rank: user.rank,
- lang: m.lang,
- hide: m.hide
- });
- });
- }
- });
- }
- }
- }
- function getBalance(user, socket) {
- query('SELECT `balance` FROM `users` WHERE `steamid` = '+pool.escape(user.steamid), function(err, row) {
- if((err) || (!row.length)) {
- logger.error('Could not get a person into balance');
- logger.debug(err);
- socket.emit('message', {
- type: 'error',
- enable: true,
- error: 'Error: You are not DB.'
- });
- return;
- }
- socket.emit('message', {
- type: 'balance',
- balance: row[0].balance
- });
- if(user.steamid) users[user.steamid].balance = parseInt(row[0].balance);
- })
- }
- function setBet(m, user, socket) {
- if((usersBr[user.steamid] !== undefined) && (usersBr[user.steamid] == br)) {
- socket.emit('message', {
- type: 'error',
- enable: true,
- error: 'You\'ve already placed '+usersBr[user.steamid]+'/'+br+' bets this roll.'
- });
- return;
- }
- if((m.amount < minbet) || (m.amount > maxbet)) {
- socket.emit('message', {
- type: 'error',
- enable: true,
- error: 'Min. bet: 100 coin!'
- });
- return;
- }
- if(pause) {
- socket.emit('message', {
- type: 'error',
- enable: false,
- error: 'Betting for this round is closed.'
- });
- return;
- }
- if(m.upper - m.lower > 6){
- logger.warn("User tried to place an invalid bid!! (Might be hacking)");
- socket.emit('message', {
- type: 'error',
- enable: true,
- error: 'Don\'t cheat!'
- });
- return;
- } else {
- if(m.lower != 0 && m.lower != 1 && m.lower != 8){
- logger.warn("User is trying some weird offset!! (Might be hacking)");
- logger.warn("User tried to place an invalid bid!! (Might be hacking)");
- socket.emit('message', {
- type: 'error',
- enable: true,
- error: 'Invalid bet boundaries. Don\'t cheat!'
- });
- return;
- }
- if(m.lower == 0){
- m.upper = 0;
- } else {
- m.upper = m.lower + 6;
- }
- }
- var isnum = /^\d+$/.test(m.amount);
- if(!isnum){
- socket.emit('message', {
- type: 'error',
- enable: true,
- error: 'Invalid bet'
- });
- return;
- }
- var start_time = new Date();
- query('SELECT `balance` FROM `users` WHERE `steamid` = '+pool.escape(user.steamid), function(err, row) {
- if((err) || (!row.length)) {
- logger.error('Could not get people to rate');
- logger.debug(err);
- socket.emit('message', {
- type: 'error',
- enable: true,
- error: 'You are not DB!'
- });
- return;
- }
- if(row[0].balance >= m.amount) {
- query('UPDATE `users` SET `balance` = `balance` - '+parseInt(m.amount)+' WHERE `steamid` = '+pool.escape(user.steamid), function(err2, row2) {
- if(err2) {
- logger.error('There is not enough points.');
- logger.debug(err);
- socket.emit('message', {
- type: 'error',
- enable: true,
- error: 'You dont have enough points!'
- });
- return;
- }
- query('INSERT INTO `bets` SET `user` = '+pool.escape(user.steamid)+', `amount` = '+pool.escape(m.amount)+', `lower` = '+pool.escape(m.lower)+', `upper` = '+pool.escape(m.upper), function(err3, row3) {
- if(err3) {
- logger.error('Add error rate in the database');
- logger.debug(err);
- return;
- }
- var end = new Date();
- if(usersBr[user.steamid] === undefined) {
- usersBr[user.steamid] = 1;
- } else {
- usersBr[user.steamid]++;
- }
- if(usersAmount[user.steamid] === undefined) {
- usersAmount[user.steamid] = {
- '0-0': 0,
- '1-7': 0,
- '8-14': 0
- };
- }
- usersAmount[user.steamid][m.lower+'-'+m.upper] += parseInt(m.amount);
- currentSums[m.lower+'-'+m.upper] += m.amount;
- socket.emit('message', {
- type: 'betconfirm',
- bet: {
- betid: row3.insertId,
- lower: m.lower,
- upper: m.upper,
- amount: usersAmount[user.steamid][m.lower+'-'+m.upper]
- },
- balance: row[0].balance-m.amount,
- mybr: usersBr[user.steamid],
- br: br,
- exec: (end.getTime()-start_time.getTime()).toFixed(3)
- });
- users[user.steamid].balance = row[0].balance-m.amount;
- io.sockets.emit('message', {
- type: 'bet',
- bet: {
- amount: usersAmount[user.steamid][m.lower+'-'+m.upper],
- betid: row3.insertId,
- icon: user.avatar,
- lower: m.lower,
- name: user.name,
- rollid: currentRollid,
- upper: m.upper,
- user: user.steamid,
- won: null
- },
- sums: {
- 0: currentSums['0-0'],
- 1: currentSums['1-7'],
- 2: currentSums['8-14'],
- }
- });
- currentBets.push({
- amount: m.amount,
- betid: row3.insertId,
- icon: user.avatar,
- lower: m.lower,
- name: user.name,
- rollid: currentRollid,
- upper: m.upper,
- user: user.steamid,
- });
- logger.debug('I bet #'+row3.insertId+' amount '+m.amount);
- checkTimer();
- })
- });
- } else {
- socket.emit('message', {
- type: 'error',
- enable: true,
- error: 'You dont any money!'
- });
- }
- });
- }
- function checkTimer() {
- if((currentBets.length > 0) && (timer == -1) && (!pause)) {
- logger.trace('Starts timer');
- timer = accept+wait;
- timerID = setInterval(function() {
- logger.trace('Timer: '+timer+' Site timer: '+(timer-wait));
- if (timer == wait) {
- pause = true;
- logger.trace('Pause is on');
- var inprog = getRandomInt(0, (currentBets.length/4).toFixed(0));
- io.sockets.emit('message', {
- type: 'preroll',
- totalbets: currentBets.length-inprog,
- inprog: inprog,
- sums: {
- 0: currentSums['0-0'],
- 1: currentSums['1-7'],
- 2: currentSums['8-14'],
- }
- });
- }
- if (timer == wait-2) {
- logger.trace('load timer');
- toWin(); // Choosing the winner
- }
- if(timer == 0) {
- logger.trace('Reset Tipo');
- timer = accept+wait;
- currentBets = [];
- historyRolls.push({id: currentRollid, roll: roll});
- if(historyRolls.length > 10) historyRolls.slice(1);
- usersBr = {}; // how many users have
- usersAmount = {}; // how many users have coins
- currentSums = {
- '0-0': 0,
- '1-7': 0,
- '8-14': 0
- };
- currentRollid = currentRollid+1;
- pause = false;
- }
- timer--;
- }, 1000);
- }
- }
- function toWin() {
- var sh = sha256(hash+'-'+currentRollid);
- roll = sh.substr(0, 8);
- roll = parseInt(roll, 16);
- roll = math.abs(roll) % 15;
- logger.trace('Tipo dropped '+roll);
- var r = '';
- var s = q1;
- var wins = {
- '0-0': 0,
- '1-7': 0,
- '8-14': 0
- }
- if(roll == 0) { r = '0-0'; s = q2; wins['0-0'] = currentSums['0-0']*s; }
- if((roll > 0) && (roll < 8)) { r = '1-7'; wins['1-7'] = currentSums['1-7']*s; }
- if((roll > 7) && (roll < 15)) { r = '8-14'; wins['8-14'] = currentSums['8-14']*s; }
- logger.debug(currentBets);
- logger.debug(usersBr);
- logger.debug(usersAmount);
- logger.debug(currentSums);
- for(key in users) {
- if(usersAmount[key] === undefined) {
- var balance = null;
- var won = 0;
- } else {
- var balance = parseInt(users[key].balance)+usersAmount[key][r]*s;
- var won = usersAmount[key][r]*s;
- }
- if (io.sockets.connected[users[key].socket]) io.sockets.connected[users[key].socket].emit('message', {
- balance: balance,
- count: accept,
- nets: [{
- lower: 0,
- samount: currentSums['0-0'],
- swon: wins['0-0'],
- upper: 0
- }, {
- lower: 1,
- samount: currentSums['1-7'],
- swon: wins['1-7'],
- upper: 7
- }, {
- lower: 8,
- samount: currentSums['8-14'],
- swon: wins['8-14'],
- upper: 14
- }
- ],
- roll: roll,
- rollid: currentRollid+1,
- type: "roll",
- wait: wait-2,
- wobble: getRandomArbitary(0, 1),
- won: won
- });
- }
- currentBets.forEach(function(itm) {
- if((roll >= itm.lower) && (roll <= itm.upper)) {
- logger.debug('Rate #'+itm.betid+' sum '+itm.amount+' win '+(itm.amount*s));
- query('UPDATE `users` SET `balance` = `balance` + '+itm.amount*s+' WHERE `steamid` = '+pool.escape(itm.user));
- }
- });
- query('UPDATE `rolls` SET `roll` = '+pool.escape(roll)+', `hash` = '+pool.escape(hash)+', `time` = '+pool.escape(time())+' WHERE `id` = '+pool.escape(currentRollid));
- query('INSERT INTO `rolls` SET `roll` = -1');
- updateHash();
- }
- /* */
- var tagsToReplace = {
- '&': '&',
- '<': '<',
- '>': '>',
- 'script': '&nsbp;'
- };
- function replaceTag(tag) {
- return tagsToReplace[tag] || tag;
- }
- function safe_tags_replace(str) {
- return str.replace(/[&<>]/g, replaceTag);
- }
- Object.size = function(obj) {
- var size = 0,
- key;
- for (key in obj) {
- if (obj.hasOwnProperty(key)) size++;
- }
- return size;
- };
- function getRandomInt(min, max) {
- return Math.floor(Math.random() * (max - min + 1)) + min;
- }
- function getRandomArbitary(min, max) {
- return Math.random() * (max - min) + min;
- }
- function query(sql, callback) {
- if (typeof callback === 'undefined') {
- callback = function() {};
- }
- pool.getConnection(function(err, connection) {
- if(err) return callback(err);
- logger.info('Id connection with database: '+connection.threadId);
- connection.query(sql, function(err, rows) {
- if(err) return callback(err);
- connection.release();
- return callback(null, rows);
- });
- });
- }
- function load() {
- query('SET NAMES utf8');
- query('SELECT `id` FROM `rolls` ORDER BY `id` DESC LIMIT 1', function(err, row) {
- if((err) || (!row.length)) {
- logger.error('you could not get the number last game');
- logger.debug(err);
- process.exit(0);
- return;
- }
- currentRollid = row[0].id;
- logger.trace('the current number of rolling'+currentRollid);
- });
- loadHistory();
- setTimeout(function() { io.listen(8080); }, 3000);
- }
- function loadHistory() {
- query('SELECT * FROM `rolls` ORDER BY `id` LIMIT 10', function(err, row) {
- if(err) {
- logger.error('Failed to load the list of establishments');
- logger.debug(err);
- process.exit(0);
- }
- logger.trace('loaded the history of plants');
- row.forEach(function(itm) {
- if(itm.roll != -1) historyRolls.push(itm);
- });
- });
- }
- function time() {
- return parseInt(new Date().getTime()/1000)
- }
- function send100Crash() {
- io.sockets.emit('message', {
- type: 'urcarecrash',
- grafic: crashNumber
- });
- }
- function CrashCashout(m, user, socket) {
- var crashPoint = crashNumber;
- if(crashState != 'STARTED') {
- socket.emit('message', {
- type: 'error',
- error: 'The game is ended'
- })
- return;
- }
- if(CRusersAmount[user.steamid]['cashedOut'] == '0') {
- var profit = parseInt(CRusersAmount[user.steamid]['amount']/2*(crashPoint)/100) - CRusersAmount[user.steamid]['amount']/2;
- io.sockets.emit('message', {
- type: 'crashCashout',
- playerSTEAMID: user.steamid,
- playerNAME: user.name,
- playerAMOUNT: CRusersAmount[user.steamid]['amount'],
- playerCASHOUT: crashPoint,
- playerPROFIT: profit
- });
- CRusersAmount[user.steamid]['cashedOut'] = '1';
- CRusersAmount[user.steamid]['autoCashout'] = crashPoint/100;
- CRusersAmount[user.steamid]['profit'] = profit;
- socket.emit('message', {
- type: 'changeBTNCrash',
- amount: CRusersAmount[user.steamid]['amount']/2+profit
- });
- logger.debug('[CASHOUT] UPDATED BALANCE OF '+user.steamid+' WITH + '+profit+' [CASHOUT: '+crashPoint/100+'x]');
- query('UPDATE `users` SET `balance`=`balance`+'+parseInt(profit+CRusersAmount[user.steamid]['amount']/2)+' WHERE `steamid`='+pool.escape(user.steamid));
- query('UPDATE `crbets` SET `autoCashout`='+pool.escape(crashPoint)+', `cashedOut`=1 ORDER BY id DESC LIMIT 1');
- }else {
- socket.emit('message', {
- type: 'error',
- error: 'You have already cashed out!'
- });
- }
- }
- function checkCashouts() {
- CRcurrentBets.forEach(function(itm) {
- if(itm.amount >= 1) {
- if(CRusersAmount[itm.user]['cashedOut'] == '0') {
- var monii = itm.amount*(crashNumber/100);
- io.sockets.in(itm.user).emit('message', {
- type: 'withdrawBTN',
- money: monii
- });
- }
- }
- if(itm.betid !== undefined && itm.autoCashout == crashNumber && itm.amount >= 1){
- if(CRusersAmount[itm.user]['cashedOut'] == '0') {
- var profit = parseInt(CRusersAmount[itm.user]['amount']/2*(itm.autoCashout)/100) - CRusersAmount[itm.user]['amount']/2;
- io.sockets.emit('message', {
- type: 'crashCashout',
- playerBETID: itm.betid,
- playerSTEAMID: itm.user,
- playerNAME: itm.name,
- playerAMOUNT: itm.amount,
- playerCASHOUT: itm.autoCashout,
- playerPROFIT: profit
- });
- CRusersAmount[itm.user]['cashedOut'] = '1';
- CRusersAmount[itm.user]['autoCashout'] = itm.autoCashout/100;
- CRusersAmount[itm.user]['profit'] = profit;
- io.sockets.in(itm.user).emit('message', {
- type: 'changeBTNCrash',
- amount: CRusersAmount[itm.user]['amount']/2+profit
- });
- logger.debug('[AUTO CASHOUT] UPDATED BALANCE OF '+itm.user+' WITH + '+profit+' [CASHOUT: '+itm.autoCashout/100+'x]');
- query('UPDATE `users` SET `balance`=`balance`+'+pool.escape(profit+CRusersAmount[itm.user]['amount']/2)+' WHERE `steamid`='+pool.escape(itm.user));
- query('UPDATE `crbets` SET `cashedOut`=1 WHERE `id`='+pool.escape(itm.betid));
- }
- }
- });
- }
- function reluareCrash() {
- if(crashNumber <= 100) {
- send100Crash();
- }
- setTimeout(function() {
- CRcurrentBets.forEach(function(itm) {
- logger.debug('[CRASH] BetID: #' + itm.betid + ' || User: ' + itm.user + ' || AutoCashout: ' + itm.autoCashout + ' || Suma: ' + itm.amount + ' || Won: ' + itm.won);
- });
- io.sockets.emit('message', {
- type: 'crashed',
- crashmanule: rCAt
- });
- CRcurrentBets = [];
- usersCr = {};
- CRusersAmount = {};
- crashState = 'ENDED';
- query('INSERT INTO `crash` SET `crashAt`='+pool.escape(rCAt)+',`hash`='+pool.escape(HaShGame));
- setTimeout(function() {
- startCrashGame();
- io.sockets.emit('message', {
- type: 'removeQCR'
- });
- }, 5000);
- }, 25);
- }
- function getCrashAmount() {
- if(crashNumber == 120) {
- clearInterval(timerC1);
- startTimer(2);
- }else if(crashNumber == 165) {
- clearInterval(timerC2);
- startTimer(3);
- }else if(crashNumber == 203) {
- clearInterval(timerC3);
- startTimer(4);
- }else if(crashNumber == 310) {
- clearInterval(timerC4);
- startTimer(5);
- }else if(crashNumber == 560) {
- clearInterval(timerC5);
- startTimer(6);
- }else if(crashNumber == 800) {
- clearInterval(timerC6);
- startTimer(7);
- }else if(crashNumber == 1200) {
- clearInterval(timerC7);
- startTimer(8);
- }else if(crashNumber == 3566) {
- clearInterval(timerC8);
- startTimer(9);
- }else if(crashNumber == 7248) {
- clearInterval(timerC9);
- startTimer(10);
- }else if(crashNumber == 10258) {
- clearInterval(timerC10);
- startTimer(11);
- }else if(crashNumber == 49210) {
- clearInterval(timerC11);
- startTimer(12);
- }
- }
- function CRsetBet(m, user, socket) {
- if(canPlayersBet == '1') {
- if((usersCr[user.steamid] !== undefined) && (usersCr[user.steamid] == CRbr)) {
- socket.emit('message', {
- type: 'error',
- enable: true,
- error: 'You have already joined the crash.'
- });
- return;
- }
- if((m.amount < minbet) || (m.amount > maxbet)) {
- socket.emit('message', {
- type: 'error',
- enable: true,
- error: 'Invalid bet amount ['+minbet+'-'+maxbet+'].'
- });
- return;
- }
- if(crashState != 'STARTING' && usersCr[user.steamid] != CRbr) {
- socket.emit('message', {
- type: 'error',
- enable: true,
- error: 'The game have been already started!'
- });
- return;
- }
- var start_time = new Date();
- query('SELECT `balance` FROM `users` WHERE `steamid` = '+pool.escape(user.steamid), function(err, row) {
- if((err) || (!row.length)) {
- logger.error('Failed to find DB');
- logger.debug(err);
- socket.emit('message', {
- type: 'error',
- enable: true,
- error: 'You are not DB'
- });
- return;
- }
- if(row[0].balance >= m.amount) {
- query('UPDATE `users` SET `balance` = `balance` - '+parseInt(m.amount)+' WHERE `steamid` = '+pool.escape(user.steamid), function(err2, row2) {
- if(err2) {
- logger.error('Error in withdraw');
- logger.debug(err);
- socket.emit('message', {
- type: 'error',
- enable: true,
- error: 'You dont have enough points'
- });
- return;
- }
- query('INSERT INTO `crbets` SET `user` = '+pool.escape(user.steamid)+', `amount` = '+pool.escape(m.amount)+', `autoCashout` = '+pool.escape(m.autoCash), function(err3, row3) {
- if(err3) {
- logger.error('Error in DB');
- logger.debug(err);
- return;
- }
- var end = new Date();
- if(usersCr[user.steamid] === undefined) {
- usersCr[user.steamid] = 1;
- }
- if(CRusersAmount[user.steamid] === undefined) {
- CRusersAmount[user.steamid] = {
- 'amount': m.amount,
- 'autoCashout': m.autoCash,
- 'cashedOut': '0',
- 'profit': 0
- };
- }
- CRusersAmount[user.steamid]['amount'] += parseInt(m.amount);
- socket.emit('message', {
- type: 'crbetconfirm',
- bet: {
- betid: row3.insertId,
- amount: m.amount,
- autoCashout: m.autoCash
- },
- balance: row[0].balance-m.amount,
- mybr: usersCr[user.steamid],
- br: CRbr,
- exec: (end.getTime()-start_time.getTime()).toFixed(3)
- });
- users[user.steamid].balance = row[0].balance-m.amount;
- io.sockets.emit('message', {
- type: 'crbet',
- bet: {
- cashedOut: '0',
- amount: m.amount,
- autoCashout: m.autoCash,
- betid: row3.insertId,
- icon: user.avatar,
- name: user.name,
- user: user.steamid
- }
- });
- CRcurrentBets.push({
- cashedOut: '0',
- autoCashout: m.autoCash,
- amount: m.amount,
- betid: row3.insertId,
- icon: user.avatar,
- name: user.name,
- user: user.steamid,
- won: '0'
- });
- socket.emit('message', {
- type: 'setIsPlaying'
- });
- logger.debug('CRBet confirmed #'+row3.insertId+' | Amount: '+m.amount+' | AutoCashout: '+m.autoCash+' | User: '+pool.escape(user.steamid));
- checkTimer();
- })
- });
- } else {
- socket.emit('message', {
- type: 'error',
- enable: true,
- error: 'Error: You dont have any money.'
- });
- }
- });
- }else {
- socket.emit('message', {
- type: 'error',
- enable: true,
- error: 'Error: You cannot bet, because the bet is offline.'
- });
- }
- }
- function stopTimers() {
- clearInterval(timerC1);
- clearInterval(timerC2);
- clearInterval(timerC3);
- clearInterval(timerC4);
- clearInterval(timerC5);
- clearInterval(timerC6);
- clearInterval(timerC7);
- clearInterval(timerC8);
- clearInterval(timerC9);
- clearInterval(timerC10);
- clearInterval(timerC11);
- clearInterval(timerC12);
- }
- function startTimer(number) {
- if(number == 1) {
- timerC1 = setInterval(function() {
- crashNumber += 1;
- getCrashAmount();
- checkCashouts();
- if(crashNumber == crashAt) {
- reluareCrash();
- stopTimers();
- }
- io.sockets.emit('message', {
- type: 'urcarecrash',
- grafic: crashNumber
- });
- }, 200);
- }else if(number == 2) {
- timerC2 = setInterval(function() {
- crashNumber += 1;
- getCrashAmount();
- checkCashouts();
- if(crashNumber == crashAt) {
- reluareCrash();
- stopTimers();
- }
- io.sockets.emit('message', {
- type: 'urcarecrash',
- grafic: crashNumber
- });
- }, 150);
- }else if(number == 3) {
- timerC3 = setInterval(function() {
- crashNumber += 1;
- getCrashAmount();
- checkCashouts();
- if(crashNumber == crashAt) {
- reluareCrash();
- stopTimers();
- }
- io.sockets.emit('message', {
- type: 'urcarecrash',
- grafic: crashNumber
- });
- }, 130);
- }else if(number == 4) {
- timerC4 = setInterval(function() {
- crashNumber += 1;
- getCrashAmount();
- checkCashouts();
- if(crashNumber == crashAt) {
- reluareCrash();
- stopTimers();
- }
- io.sockets.emit('message', {
- type: 'urcarecrash',
- grafic: crashNumber
- });
- }, 100);
- }else if(number == 5) {
- timerC5 = setInterval(function() {
- crashNumber += 1;
- getCrashAmount();
- checkCashouts();
- if(crashNumber == crashAt) {
- reluareCrash();
- stopTimers();
- }
- io.sockets.emit('message', {
- type: 'urcarecrash',
- grafic: crashNumber
- });
- }, 80);
- }else if(number == 6) {
- timerC6 = setInterval(function() {
- crashNumber += 1;
- getCrashAmount();
- checkCashouts();
- if(crashNumber == crashAt) {
- reluareCrash();
- stopTimers();
- }
- io.sockets.emit('message', {
- type: 'urcarecrash',
- grafic: crashNumber
- });
- }, 60);
- }else if(number == 7) {
- timerC7 = setInterval(function() {
- crashNumber += 1;
- getCrashAmount();
- checkCashouts();
- if(crashNumber == crashAt) {
- reluareCrash();
- stopTimers();
- }
- io.sockets.emit('message', {
- type: 'urcarecrash',
- grafic: crashNumber
- });
- }, 30);
- }else if(number == 8) {
- timerC8 = setInterval(function() {
- crashNumber += 1;
- getCrashAmount();
- checkCashouts();
- if(crashNumber == crashAt) {
- reluareCrash();
- stopTimers();
- }
- io.sockets.emit('message', {
- type: 'urcarecrash',
- grafic: crashNumber
- });
- }, 15);
- }else if(number == 9) {
- timerC9 = setInterval(function() {
- crashNumber += 1;
- getCrashAmount();
- checkCashouts();
- if(crashNumber == crashAt) {
- reluareCrash();
- stopTimers();
- }
- io.sockets.emit('message', {
- type: 'urcarecrash',
- grafic: crashNumber
- });
- }, 0.1);
- }else if(number == 10) {
- timerC10 = setInterval(function() {
- crashNumber += 1;
- getCrashAmount();
- checkCashouts();
- if(crashNumber == crashAt) {
- reluareCrash();
- stopTimers();
- }
- io.sockets.emit('message', {
- type: 'urcarecrash',
- grafic: crashNumber
- });
- }, 0.01);
- }else if(number == 11) {
- timerC11 = setInterval(function() {
- crashNumber += 1;
- getCrashAmount();
- checkCashouts();
- if(crashNumber == crashAt) {
- reluareCrash();
- stopTimers();
- }
- io.sockets.emit('message', {
- type: 'urcarecrash',
- grafic: crashNumber
- });
- }, 0.001);
- }else if(number == 12) {
- timerC12 = setInterval(function() {
- crashNumber += 1;
- getCrashAmount();
- checkCashouts();
- if(crashNumber == crashAt) {
- reluareCrash();
- stopTimers();
- }
- io.sockets.emit('message', {
- type: 'urcarecrash',
- grafic: crashNumber
- });
- }, 0.0001);
- }
- }
- function sendCrashAmount() {
- if(isStatedSet == 0) {
- crashState = 'STARTED';
- isStatedSet = 1;
- }
- if(instaCrash == 1) {
- reluareCrash();
- instaCrash = 0;
- CRcurrentBets = [];
- usersCr = {};
- CRusersAmount = {};
- io.sockets.emit('message', {
- type: 'crashed',
- crashmanule: rCAt
- });
- crashState = 'ENDED';
- }else {
- startTimer(1);
- }
- }
- function startCrashGame() {
- var timpEmis;
- crashState = 'STARTING';
- timp = 700;
- crashNumber = 100;
- isStatedSet = 0;
- var crypto = require('crypto');
- var serverSeed = 'Test';
- var genGameHash = function(serverSeed) {
- return crypto.createHash('sha256').update(serverSeed).digest('hex');
- };
- var crashPointFromHash = function(serverSeed) {
- var hash = crypto.createHmac('sha256', serverSeed).digest('hex');
- var divisible = function divisible(hash, mod) {
- var val = 0;
- var o = hash.length % 4;
- for (var i = o > 0 ? o - 4 : 0; i < hash.length; i += 4) {
- val = ((val << 16) + parseInt(hash.substring(i, i+4), 16)) % mod;
- }
- return val === 0;
- };
- var INSTANT_CRASH_PERCENTAGE = 5;
- if (divisible(hash, 100 / parseInt(INSTANT_CRASH_PERCENTAGE, 10)))
- return 0;
- var h = parseInt(hash.slice(0,52/4),16);
- var e = Math.pow(2,52);
- return Math.floor((100 * e - h) / (e - h));
- };
- serverSeed = genGameHash(serverSeed+time());
- var terminatingHash = genGameHash(serverSeed);
- var crashhh = crashPointFromHash(serverSeed);
- rCAt = crashhh;
- crashAt = rCAt;
- logger.debug('New hash game: ' + terminatingHash + ' with crashPoint: ' + (crashhh / 100).toFixed(2) +'x');
- HaShGame = terminatingHash;
- if(rCAt <= 100) {
- instaCrash = 1;
- }
- intervalul = setInterval(function() {
- timp = timp - 10;
- timpEmis = timp;
- if(timp == 0){
- sendCrashAmount();
- setTimeout(function() {
- clearInterval(intervalul);
- }, 10);
- }
- io.sockets.emit('message', {
- type: 'startcrash',
- time: timpEmis
- });
- }, 100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement