Guest User

PASTEBIN

a guest
May 21st, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 36.40 KB | None | 0 0
  1. var mysql = require('mysql');
  2. var log4js = require('log4js');
  3. var io = require('socket.io')(8000);
  4. var request = require('request');
  5. var fs = require('fs');
  6. var md5 = require('md5');
  7. var sha256 = require('sha256');
  8. var math = require('mathjs');
  9. var crashNumber = 100;
  10. var crashState = 'NULL';
  11. var crashAt;
  12. var timp = 700;
  13. var intervalul;
  14. var isStatedSet = 0;
  15. var rCAt;
  16. var instaCrash = 0;
  17. var HaShGame;
  18. var timerC1;
  19. var timerC2;
  20. var timerC3;
  21. var timerC4;
  22. var timerC5;
  23. var timerC6;
  24. var timerC7;
  25. var timerC8;
  26. var timerC9;
  27. var timerC10;
  28. var timerC11;
  29. var timerC12;
  30. var CRbr = 1;
  31. var usersCr = {};
  32. var CRusersAmount = {};
  33. var CRcurrentBets = [];
  34.  
  35.  
  36. log4js.configure({
  37. appenders: [
  38. { type: 'console' },
  39. { type: 'file', filename: 'logs/site.log' }
  40. ]
  41. });
  42. var logger = log4js.getLogger();
  43.  
  44. var pool = mysql.createPool({
  45. connectionLimit : 10,
  46. database: 'roulette',
  47. host: 'localhost',
  48. user: 'root',
  49. password: 'yourpassword'
  50. });
  51.  
  52. process.on('uncaughtException', function (err) {
  53. logger.trace('Strange error');
  54. logger.debug(err);
  55. });
  56.  
  57. /* */
  58. var accept = 30;
  59. var wait = 10;
  60. var br = 3;
  61. var chat = 2;
  62. var chatb = 200000;
  63. var maxbet = 10000000;
  64. var minbet = 100;
  65. var q1 = 2;
  66. var q2 = 14;
  67. var timer = -1;
  68. var users = {};
  69. var roll = 0;
  70. var currentBets = [];
  71. var historyRolls = [];
  72. var usersBr = {};
  73. var usersAmount = {};
  74. var currentSums = {
  75. '0-0': 0,
  76. '1-7': 0,
  77. '8-14': 0
  78. };
  79.  
  80. var canPlayersBet = 1;
  81.  
  82. var currentRollid = 0;
  83. var pause = false;
  84. var hash = '';
  85. var last_message = {};
  86. /* */
  87.  
  88. load();
  89. startCrashGame();
  90.  
  91. var prices;
  92. request('http://backpack.tf/api/IGetMarketPrices/v1/?key=58503fc4e338774aec34d9b0&appid=730', function(error, response, body) {
  93. prices = JSON.parse(body);
  94. if(prices.response.success == 0) {
  95. logger.warn('It was not possible to load prices. Prices taken from the cache');
  96. if(fs.existsSync(__dirname + '/prices.txt')){
  97. prices = JSON.parse(fs.readFileSync(__dirname + '/var/www/prices.txt'));
  98. logger.warn('Prices have been retrieved from the cache');
  99. } else {
  100. logger.error('No prices in cache');
  101. process.exit(0);
  102. }
  103. if(fs.existsSync(__dirname + '/var/www/prices.txt')){
  104. prices = JSON.parse(fs.readFileSync(__dirname + '/var/www/prices.txt'));
  105. logger.warn('/var/www/prices.txt Loaded cached');
  106. } else {
  107. logger.error('No /var/www/prices.txt in cache');
  108. process.exit(0);
  109. }
  110. } else {
  111. fs.writeFileSync('/var/www/prices.txt', body);
  112. logger.trace('Prices loaded successfully');
  113. }
  114. });
  115.  
  116. updateHash();
  117. function updateHash() {
  118. query('SELECT * FROM `hash` ORDER BY `id` DESC LIMIT 1', function(err, row) {
  119. if(err) {
  120. logger.error('Cant get the hash, stopping');
  121. logger.debug(err);
  122. process.exit(0);
  123. return;
  124. }
  125. if(row.length == 0) {
  126. logger.error('Wrong hash found, stopping');
  127. process.exit(0);
  128. } else {
  129. if(hash != row[0].hash) logger.warn('Loaded hash'+row[0].hash);
  130. hash = row[0].hash;
  131. }
  132. });
  133. }
  134.  
  135. io.on('connection', function(socket) {
  136. var user = false;
  137. socket.on('hash', function(hash) {
  138. query('SELECT * FROM `users` WHERE `hash` = '+pool.escape(hash), function(err, row) {
  139. if((err) || (!row.length)) return socket.disconnect();
  140. user = row[0];
  141. socket.join(user.steamid);
  142. socket.join(user.steamid);
  143. users[user.steamid] = {
  144. socket: socket.id,
  145. balance: parseInt(row[0].balance)
  146. }
  147. socket.emit('message', {
  148. accept: accept,
  149. balance: row[0].balance,
  150. br: br,
  151. chat: chat,
  152. chatb: chatb,
  153. count: timer-wait,
  154. icon: row[0].avatar,
  155. maxbet: maxbet,
  156. minbet: minbet,
  157. name: row[0].name,
  158. rank: row[0].rank,
  159. rolls: historyRolls,
  160. type: 'hello',
  161. user: row[0].steamid
  162. });
  163. socket.emit('message', {
  164. type: 'logins',
  165. count: Object.size(io.sockets.connected)
  166. });
  167. currentBets.forEach(function(itm) {
  168. socket.emit('message', {
  169. type: 'bet',
  170. bet: {
  171. amount: itm.amount,
  172. betid: itm.betid,
  173. icon: itm.icon,
  174. lower: itm.lower,
  175. name: itm.name,
  176. rollid: itm.rollid,
  177. upper: itm.upper,
  178. user: itm.user,
  179. won: null
  180. },
  181. sums: {
  182. 0: currentSums['0-0'],
  183. 1: currentSums['1-7'],
  184. 2: currentSums['8-14'],
  185. }
  186. });
  187. });
  188. });
  189. });
  190. CRcurrentBets.forEach(function(itm) {
  191. socket.emit('message', {
  192. type: 'crbet',
  193. bet: {
  194. autoCashout: itm.autoCash,
  195. amount: itm.amount,
  196. betid: itm.betid,
  197. icon: itm.icon,
  198. name: itm.name,
  199. user: itm.user,
  200. won: '0'
  201. }
  202. });
  203. });
  204. socket.on('mes', function(m) {
  205. if(!user) return;
  206. logger.debug(m);
  207. if(m.type == "bet") return setBet(m, user, socket);
  208. if(m.type == "crbet") {
  209. if(m.mtype == 'joinCrash') {
  210. return setTimeout(CRsetBet(m, user, socket), 250);
  211. }else if(m.mtype == 'withdraw') {
  212. return CrashCashout(m, user, socket);
  213. }
  214. }
  215. if(m.type == "balance") return getBalance(user, socket);
  216. if(m.type == "chat") return ch(m, user, socket);
  217. });
  218. socket.on('disconnect', function() {
  219. io.sockets.emit('message', {
  220. type: 'logins',
  221. count: Object.size(io.sockets.connected)
  222. });
  223. delete users[user.steamid];
  224. socket.leave(user.steamid);
  225. })
  226. });
  227.  
  228. function ch(m, user, socket) {
  229. if(m.msg) {
  230. var res = null;
  231. if (res = /^\/send ([0-9]*) ([0-9]*)/.exec(m.msg)) {
  232. logger.trace('problem with translating from russian'+res[2]+' user '+res[1]);
  233. query('SELECT `balance` FROM `users` WHERE `steamid` = '+pool.escape(user.steamid), function(err, row) {
  234. if((err) || (!row.length)) {
  235. logger.error('Could not find user to send the coins');
  236. logger.debug(err);
  237. socket.emit('message', {
  238. type: 'error',
  239. enable: false,
  240. error: 'Error: You are not DB.'
  241. });
  242. return;
  243. }
  244. if(row[0].balance < res[2]) {
  245. socket.emit('message', {
  246. type: 'error',
  247. enable: false,
  248. error: 'Error: Insufficient funds.'
  249. });
  250. } else if(res[2] <= 0) {
  251. socket.emit('message', {
  252. type: 'error',
  253. enable: false,
  254. error: 'Error: Amount must be greater than 0.'
  255. });
  256. } else {
  257. query('SELECT `name` FROM `users` WHERE `steamid` = '+pool.escape(res[1]), function(err2, row2) {
  258. if((err) || (!row.length)) {
  259. logger.error('Could not get people to move');
  260. logger.debug(err);
  261. socket.emit('message', {
  262. type: 'error',
  263. enable: false,
  264. error: 'Error: Unknown receiver.'
  265. });
  266. return;
  267. }
  268. query('UPDATE `users` SET `balance` = `balance` - '+res[2]+' WHERE `steamid` = '+pool.escape(user.steamid));
  269. query('UPDATE `users` SET `balance` = `balance` + '+res[2]+' WHERE `steamid` = '+pool.escape(res[1]));
  270. query('INSERT INTO `transfers` SET `from1` = '+pool.escape(user.steamid)+', `to1` = '+pool.escape(res[1])+', `amount` = '+pool.escape(res[2])+', `time` = '+pool.escape(time()));
  271. socket.emit('message', {
  272. type: 'alert',
  273. alert: 'You sent '+res[2]+' coins to '+row2[0].name+'.'
  274. });
  275. getBalance(user, socket);
  276. });
  277. }
  278. });
  279. }else {
  280. query('SELECT SUM(`amount`) AS castor FROM `bets` WHERE `user` = '+pool.escape(user.steamid), function(err, row) {
  281. if((err) || (!row.length)) {
  282. logger.error('Failed to get the person to transfer');
  283. logger.debug(err);
  284. socket.emit('message', {
  285. type: 'error',
  286. enable: false,
  287. error: 'Error: Unknown receiver.'
  288. });
  289. return;
  290. }
  291. if(row[0].castor <= chatb) {
  292. socket.emit('message', {
  293. type: 'error',
  294. enable: false,
  295. error: 'Chat unlocks after 200,000 coins have been played. You need: '+pool.escape(row[0].castor-chatb + ' coins')
  296. });
  297. } else if (res = /^\/mute ([0-9]*) ([0-9]*)/.exec(m.msg)) {
  298. if(user.rank > 0) {
  299. var t = time();
  300. query('UPDATE `users` SET `mute` = '+pool.escape(parseInt(t)+parseInt(res[2]))+' WHERE `steamid` = '+pool.escape(res[1]));
  301. socket.emit('message', {
  302. type: 'alert',
  303. alert: 'You muted '+res[1]+' to '+res[2]
  304. });
  305. }
  306. } else {
  307. query('SELECT `mute` FROM `users` WHERE `steamid` = '+pool.escape(user.steamid), function(err, row) {
  308. if(err) return;
  309. if(row[0].mute > time()) {
  310. socket.emit('message', {
  311. type: 'alert',
  312. alert: 'You are muted '+(row[0].mute-time() + ' more seconds')
  313. });
  314. return;
  315. } else if (res = /^\/ban ([0-9]*) ([0-9]*)/.exec(m.msg)) {
  316. if(user.rank == 1) {
  317. var t = time();
  318. query('UPDATE `users` SET `ban` = 1 WHERE `steamid` = '+pool.escape(res[1]));
  319. socket.emit('message', {
  320. type: 'alert',
  321. alert: 'You ban '+res[1]+' '+res[2]
  322. });
  323. return;
  324. }
  325. }
  326. else if (res = /^\/unban ([0-9]*) ([0-9]*)/.exec(m.msg)) {
  327. if(user.rank == 1) {
  328. var t = time();
  329. query('UPDATE `users` SET `ban` = 0 WHERE `steamid` = '+pool.escape(res[1]));
  330. socket.emit('message', {
  331. type: 'alert',
  332. alert: 'You unban '+res[1]+' '+res[2]
  333. });
  334. return;
  335. }
  336. }
  337. else if (res = /^\/banwithdraw ([0-9]*) ([0-9]*)/.exec(m.msg)) {
  338. if(user.rank == 1) {
  339. var t = time();
  340. query('UPDATE `users` SET `banwithdraw` = 1 WHERE `steamid` = '+pool.escape(res[1]));
  341. socket.emit('message', {
  342. type: 'alert',
  343. alert: 'You ban '+(res[1]+' '+res[2] + ' to withdraw')
  344. });
  345. return;
  346. }
  347. }
  348. else if (res = /^\/unbanwithdraw ([0-9]*) ([0-9]*)/.exec(m.msg)) {
  349. if(user.rank == 1) {
  350. var t = time();
  351. query('UPDATE `users` SET `banwithdraw` = 0 WHERE `steamid` = '+pool.escape(res[1]));
  352. socket.emit('message', {
  353. type: 'alert',
  354. alert: 'You unban '+(res[1]+' '+res[2] + ' to withdraw')
  355. });
  356. return;
  357. }
  358. }
  359. else if (res = /^\/setbalance ([0-9]*) ([0-9]*)/.exec(m.msg)) {
  360. if(user.rank == 1) {
  361. var t = time();
  362. query('UPDATE `users` SET `balance` = '+res[2]+' WHERE `steamid` = '+pool.escape(res[1]));
  363. socket.emit('message', {
  364. type: 'alert',
  365. alert: 'You set '+res[1]+' balance to '+res[2]
  366. });
  367. return;
  368. }
  369. }
  370. else if (res = /^\/setrank ([0-9]*) ([0-9]*)/.exec(m.msg)) {
  371. if(user.rank == 1) {
  372. var t = time();
  373. query('UPDATE `users` SET `rank` = '+res[2]+' WHERE `steamid` = '+pool.escape(res[1]));
  374. socket.emit('message', {
  375. type: 'alert',
  376. alert: 'You set '+res[1]+' rank to '+res[2]
  377. });
  378. return;
  379. }
  380. }
  381. else if (res = /^\/ranks ([0-9]*)/.exec(m.msg)) {
  382. if(user.rank == 1) {
  383. var t = time();
  384. socket.emit('message', {
  385. type: 'alert',
  386. alert: '\n1- Admin \n2- Mod \n3- Twitch \n4- V.I.P \n5- Developer \n6- YouTube \n7- Support'
  387. });
  388. return;
  389. }
  390. }
  391. io.sockets.emit('message', {
  392. type: 'chat',
  393. msg: safe_tags_replace(m.msg),
  394. name: user.name,
  395. icon: user.avatar,
  396. user: user.steamid,
  397. rank: user.rank,
  398. lang: m.lang,
  399. hide: m.hide
  400. });
  401. });
  402. }
  403. });
  404. }
  405. }
  406. }
  407.  
  408. function getBalance(user, socket) {
  409. query('SELECT `balance` FROM `users` WHERE `steamid` = '+pool.escape(user.steamid), function(err, row) {
  410. if((err) || (!row.length)) {
  411. logger.error('Could not get a person into balance');
  412. logger.debug(err);
  413. socket.emit('message', {
  414. type: 'error',
  415. enable: true,
  416. error: 'Error: You are not DB.'
  417. });
  418. return;
  419. }
  420. socket.emit('message', {
  421. type: 'balance',
  422. balance: row[0].balance
  423. });
  424. if(user.steamid) users[user.steamid].balance = parseInt(row[0].balance);
  425. })
  426. }
  427.  
  428. function setBet(m, user, socket) {
  429. if((usersBr[user.steamid] !== undefined) && (usersBr[user.steamid] == br)) {
  430. socket.emit('message', {
  431. type: 'error',
  432. enable: true,
  433. error: 'You\'ve already placed '+usersBr[user.steamid]+'/'+br+' bets this roll.'
  434. });
  435. return;
  436. }
  437. if((m.amount < minbet) || (m.amount > maxbet)) {
  438. socket.emit('message', {
  439. type: 'error',
  440. enable: true,
  441. error: 'Min. bet: 100 coin!'
  442. });
  443. return;
  444. }
  445. if(pause) {
  446. socket.emit('message', {
  447. type: 'error',
  448. enable: false,
  449. error: 'Betting for this round is closed.'
  450. });
  451. return;
  452. }
  453. if(m.upper - m.lower > 6){
  454. logger.warn("User tried to place an invalid bid!! (Might be hacking)");
  455. socket.emit('message', {
  456. type: 'error',
  457. enable: true,
  458. error: 'Don\'t cheat!'
  459. });
  460. return;
  461. } else {
  462. if(m.lower != 0 && m.lower != 1 && m.lower != 8){
  463. logger.warn("User is trying some weird offset!! (Might be hacking)");
  464. logger.warn("User tried to place an invalid bid!! (Might be hacking)");
  465. socket.emit('message', {
  466. type: 'error',
  467. enable: true,
  468. error: 'Invalid bet boundaries. Don\'t cheat!'
  469. });
  470. return;
  471. }
  472. if(m.lower == 0){
  473. m.upper = 0;
  474. } else {
  475. m.upper = m.lower + 6;
  476. }
  477. }
  478. var isnum = /^\d+$/.test(m.amount);
  479. if(!isnum){
  480. socket.emit('message', {
  481. type: 'error',
  482. enable: true,
  483. error: 'Invalid bet'
  484. });
  485. return;
  486. }
  487. var start_time = new Date();
  488. query('SELECT `balance` FROM `users` WHERE `steamid` = '+pool.escape(user.steamid), function(err, row) {
  489. if((err) || (!row.length)) {
  490. logger.error('Could not get people to rate');
  491. logger.debug(err);
  492. socket.emit('message', {
  493. type: 'error',
  494. enable: true,
  495. error: 'You are not DB!'
  496. });
  497. return;
  498. }
  499. if(row[0].balance >= m.amount) {
  500. query('UPDATE `users` SET `balance` = `balance` - '+parseInt(m.amount)+' WHERE `steamid` = '+pool.escape(user.steamid), function(err2, row2) {
  501. if(err2) {
  502. logger.error('There is not enough points.');
  503. logger.debug(err);
  504. socket.emit('message', {
  505. type: 'error',
  506. enable: true,
  507. error: 'You dont have enough points!'
  508. });
  509. return;
  510. }
  511. 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) {
  512. if(err3) {
  513. logger.error('Add error rate in the database');
  514. logger.debug(err);
  515. return;
  516. }
  517. var end = new Date();
  518. if(usersBr[user.steamid] === undefined) {
  519. usersBr[user.steamid] = 1;
  520. } else {
  521. usersBr[user.steamid]++;
  522. }
  523. if(usersAmount[user.steamid] === undefined) {
  524. usersAmount[user.steamid] = {
  525. '0-0': 0,
  526. '1-7': 0,
  527. '8-14': 0
  528. };
  529. }
  530. usersAmount[user.steamid][m.lower+'-'+m.upper] += parseInt(m.amount);
  531. currentSums[m.lower+'-'+m.upper] += m.amount;
  532. socket.emit('message', {
  533. type: 'betconfirm',
  534. bet: {
  535. betid: row3.insertId,
  536. lower: m.lower,
  537. upper: m.upper,
  538. amount: usersAmount[user.steamid][m.lower+'-'+m.upper]
  539. },
  540. balance: row[0].balance-m.amount,
  541. mybr: usersBr[user.steamid],
  542. br: br,
  543. exec: (end.getTime()-start_time.getTime()).toFixed(3)
  544. });
  545. users[user.steamid].balance = row[0].balance-m.amount;
  546. io.sockets.emit('message', {
  547. type: 'bet',
  548. bet: {
  549. amount: usersAmount[user.steamid][m.lower+'-'+m.upper],
  550. betid: row3.insertId,
  551. icon: user.avatar,
  552. lower: m.lower,
  553. name: user.name,
  554. rollid: currentRollid,
  555. upper: m.upper,
  556. user: user.steamid,
  557. won: null
  558. },
  559. sums: {
  560. 0: currentSums['0-0'],
  561. 1: currentSums['1-7'],
  562. 2: currentSums['8-14'],
  563. }
  564. });
  565. currentBets.push({
  566. amount: m.amount,
  567. betid: row3.insertId,
  568. icon: user.avatar,
  569. lower: m.lower,
  570. name: user.name,
  571. rollid: currentRollid,
  572. upper: m.upper,
  573. user: user.steamid,
  574. });
  575. logger.debug('I bet #'+row3.insertId+' amount '+m.amount);
  576. checkTimer();
  577. })
  578. });
  579. } else {
  580. socket.emit('message', {
  581. type: 'error',
  582. enable: true,
  583. error: 'You dont any money!'
  584. });
  585. }
  586. });
  587. }
  588.  
  589. function checkTimer() {
  590. if((currentBets.length > 0) && (timer == -1) && (!pause)) {
  591. logger.trace('Starts timer');
  592. timer = accept+wait;
  593. timerID = setInterval(function() {
  594. logger.trace('Timer: '+timer+' Site timer: '+(timer-wait));
  595. if (timer == wait) {
  596. pause = true;
  597. logger.trace('Pause is on');
  598. var inprog = getRandomInt(0, (currentBets.length/4).toFixed(0));
  599. io.sockets.emit('message', {
  600. type: 'preroll',
  601. totalbets: currentBets.length-inprog,
  602. inprog: inprog,
  603. sums: {
  604. 0: currentSums['0-0'],
  605. 1: currentSums['1-7'],
  606. 2: currentSums['8-14'],
  607. }
  608. });
  609. }
  610. if (timer == wait-2) {
  611. logger.trace('load timer');
  612. toWin(); // Choosing the winner
  613. }
  614. if(timer == 0) {
  615. logger.trace('Reset Tipo');
  616. timer = accept+wait;
  617. currentBets = [];
  618. historyRolls.push({id: currentRollid, roll: roll});
  619. if(historyRolls.length > 10) historyRolls.slice(1);
  620. usersBr = {}; // how many users have
  621. usersAmount = {}; // how many users have coins
  622. currentSums = {
  623. '0-0': 0,
  624. '1-7': 0,
  625. '8-14': 0
  626. };
  627. currentRollid = currentRollid+1;
  628. pause = false;
  629. }
  630. timer--;
  631. }, 1000);
  632. }
  633. }
  634.  
  635. function toWin() {
  636. var sh = sha256(hash+'-'+currentRollid);
  637. roll = sh.substr(0, 8);
  638. roll = parseInt(roll, 16);
  639. roll = math.abs(roll) % 15;
  640. logger.trace('Tipo dropped '+roll);
  641. var r = '';
  642. var s = q1;
  643. var wins = {
  644. '0-0': 0,
  645. '1-7': 0,
  646. '8-14': 0
  647. }
  648. if(roll == 0) { r = '0-0'; s = q2; wins['0-0'] = currentSums['0-0']*s; }
  649. if((roll > 0) && (roll < 8)) { r = '1-7'; wins['1-7'] = currentSums['1-7']*s; }
  650. if((roll > 7) && (roll < 15)) { r = '8-14'; wins['8-14'] = currentSums['8-14']*s; }
  651. logger.debug(currentBets);
  652. logger.debug(usersBr);
  653. logger.debug(usersAmount);
  654. logger.debug(currentSums);
  655. for(key in users) {
  656. if(usersAmount[key] === undefined) {
  657. var balance = null;
  658. var won = 0;
  659. } else {
  660. var balance = parseInt(users[key].balance)+usersAmount[key][r]*s;
  661. var won = usersAmount[key][r]*s;
  662. }
  663. if (io.sockets.connected[users[key].socket]) io.sockets.connected[users[key].socket].emit('message', {
  664. balance: balance,
  665. count: accept,
  666. nets: [{
  667. lower: 0,
  668. samount: currentSums['0-0'],
  669. swon: wins['0-0'],
  670. upper: 0
  671. }, {
  672. lower: 1,
  673. samount: currentSums['1-7'],
  674. swon: wins['1-7'],
  675. upper: 7
  676. }, {
  677. lower: 8,
  678. samount: currentSums['8-14'],
  679. swon: wins['8-14'],
  680. upper: 14
  681. }
  682. ],
  683. roll: roll,
  684. rollid: currentRollid+1,
  685. type: "roll",
  686. wait: wait-2,
  687. wobble: getRandomArbitary(0, 1),
  688. won: won
  689. });
  690. }
  691. currentBets.forEach(function(itm) {
  692. if((roll >= itm.lower) && (roll <= itm.upper)) {
  693. logger.debug('Rate #'+itm.betid+' sum '+itm.amount+' win '+(itm.amount*s));
  694. query('UPDATE `users` SET `balance` = `balance` + '+itm.amount*s+' WHERE `steamid` = '+pool.escape(itm.user));
  695. }
  696. });
  697. query('UPDATE `rolls` SET `roll` = '+pool.escape(roll)+', `hash` = '+pool.escape(hash)+', `time` = '+pool.escape(time())+' WHERE `id` = '+pool.escape(currentRollid));
  698. query('INSERT INTO `rolls` SET `roll` = -1');
  699. updateHash();
  700. }
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710. /* */
  711. var tagsToReplace = {
  712. '&': '&amp;',
  713. '<': '&lt;',
  714. '>': '&gt;',
  715. 'script': '&nsbp;'
  716. };
  717.  
  718. function replaceTag(tag) {
  719. return tagsToReplace[tag] || tag;
  720. }
  721.  
  722. function safe_tags_replace(str) {
  723. return str.replace(/[&<>]/g, replaceTag);
  724. }
  725. Object.size = function(obj) {
  726. var size = 0,
  727. key;
  728. for (key in obj) {
  729. if (obj.hasOwnProperty(key)) size++;
  730. }
  731. return size;
  732. };
  733. function getRandomInt(min, max) {
  734. return Math.floor(Math.random() * (max - min + 1)) + min;
  735. }
  736. function getRandomArbitary(min, max) {
  737. return Math.random() * (max - min) + min;
  738. }
  739.  
  740. function query(sql, callback) {
  741. if (typeof callback === 'undefined') {
  742. callback = function() {};
  743. }
  744. pool.getConnection(function(err, connection) {
  745. if(err) return callback(err);
  746. logger.info('Id connection with database: '+connection.threadId);
  747. connection.query(sql, function(err, rows) {
  748. if(err) return callback(err);
  749. connection.release();
  750. return callback(null, rows);
  751. });
  752. });
  753. }
  754. function load() {
  755. query('SET NAMES utf8');
  756. query('SELECT `id` FROM `rolls` ORDER BY `id` DESC LIMIT 1', function(err, row) {
  757. if((err) || (!row.length)) {
  758. logger.error('you could not get the number last game');
  759. logger.debug(err);
  760. process.exit(0);
  761. return;
  762. }
  763. currentRollid = row[0].id;
  764. logger.trace('the current number of rolling'+currentRollid);
  765. });
  766. loadHistory();
  767. setTimeout(function() { io.listen(8080); }, 3000);
  768. }
  769. function loadHistory() {
  770. query('SELECT * FROM `rolls` ORDER BY `id` LIMIT 10', function(err, row) {
  771. if(err) {
  772. logger.error('Failed to load the list of establishments');
  773. logger.debug(err);
  774. process.exit(0);
  775. }
  776. logger.trace('loaded the history of plants');
  777. row.forEach(function(itm) {
  778. if(itm.roll != -1) historyRolls.push(itm);
  779. });
  780. });
  781. }
  782.  
  783. function time() {
  784. return parseInt(new Date().getTime()/1000)
  785. }
  786. function send100Crash() {
  787. io.sockets.emit('message', {
  788. type: 'urcarecrash',
  789. grafic: crashNumber
  790. });
  791. }
  792.  
  793. function CrashCashout(m, user, socket) {
  794. var crashPoint = crashNumber;
  795.  
  796. if(crashState != 'STARTED') {
  797. socket.emit('message', {
  798. type: 'error',
  799. error: 'The game is ended'
  800. })
  801. return;
  802. }
  803.  
  804. if(CRusersAmount[user.steamid]['cashedOut'] == '0') {
  805. var profit = parseInt(CRusersAmount[user.steamid]['amount']/2*(crashPoint)/100) - CRusersAmount[user.steamid]['amount']/2;
  806.  
  807. io.sockets.emit('message', {
  808. type: 'crashCashout',
  809. playerSTEAMID: user.steamid,
  810. playerNAME: user.name,
  811. playerAMOUNT: CRusersAmount[user.steamid]['amount'],
  812. playerCASHOUT: crashPoint,
  813. playerPROFIT: profit
  814. });
  815. CRusersAmount[user.steamid]['cashedOut'] = '1';
  816. CRusersAmount[user.steamid]['autoCashout'] = crashPoint/100;
  817. CRusersAmount[user.steamid]['profit'] = profit;
  818.  
  819. socket.emit('message', {
  820. type: 'changeBTNCrash',
  821. amount: CRusersAmount[user.steamid]['amount']/2+profit
  822. });
  823.  
  824. logger.debug('[CASHOUT] UPDATED BALANCE OF '+user.steamid+' WITH + '+profit+' [CASHOUT: '+crashPoint/100+'x]');
  825. query('UPDATE `users` SET `balance`=`balance`+'+parseInt(profit+CRusersAmount[user.steamid]['amount']/2)+' WHERE `steamid`='+pool.escape(user.steamid));
  826. query('UPDATE `crbets` SET `autoCashout`='+pool.escape(crashPoint)+', `cashedOut`=1 ORDER BY id DESC LIMIT 1');
  827. }else {
  828. socket.emit('message', {
  829. type: 'error',
  830. error: 'You have already cashed out!'
  831. });
  832. }
  833. }
  834.  
  835. function checkCashouts() {
  836. CRcurrentBets.forEach(function(itm) {
  837. if(itm.amount >= 1) {
  838.  
  839. if(CRusersAmount[itm.user]['cashedOut'] == '0') {
  840. var monii = itm.amount*(crashNumber/100);
  841.  
  842. io.sockets.in(itm.user).emit('message', {
  843. type: 'withdrawBTN',
  844. money: monii
  845. });
  846. }
  847.  
  848. }
  849.  
  850. if(itm.betid !== undefined && itm.autoCashout == crashNumber && itm.amount >= 1){
  851. if(CRusersAmount[itm.user]['cashedOut'] == '0') {
  852. var profit = parseInt(CRusersAmount[itm.user]['amount']/2*(itm.autoCashout)/100) - CRusersAmount[itm.user]['amount']/2;
  853.  
  854. io.sockets.emit('message', {
  855. type: 'crashCashout',
  856. playerBETID: itm.betid,
  857. playerSTEAMID: itm.user,
  858. playerNAME: itm.name,
  859. playerAMOUNT: itm.amount,
  860. playerCASHOUT: itm.autoCashout,
  861. playerPROFIT: profit
  862. });
  863.  
  864. CRusersAmount[itm.user]['cashedOut'] = '1';
  865. CRusersAmount[itm.user]['autoCashout'] = itm.autoCashout/100;
  866. CRusersAmount[itm.user]['profit'] = profit;
  867.  
  868. io.sockets.in(itm.user).emit('message', {
  869. type: 'changeBTNCrash',
  870. amount: CRusersAmount[itm.user]['amount']/2+profit
  871. });
  872.  
  873. logger.debug('[AUTO CASHOUT] UPDATED BALANCE OF '+itm.user+' WITH + '+profit+' [CASHOUT: '+itm.autoCashout/100+'x]');
  874. query('UPDATE `users` SET `balance`=`balance`+'+pool.escape(profit+CRusersAmount[itm.user]['amount']/2)+' WHERE `steamid`='+pool.escape(itm.user));
  875. query('UPDATE `crbets` SET `cashedOut`=1 WHERE `id`='+pool.escape(itm.betid));
  876. }
  877. }
  878. });
  879. }
  880.  
  881.  
  882. function reluareCrash() {
  883. if(crashNumber <= 100) {
  884. send100Crash();
  885. }
  886. setTimeout(function() {
  887. CRcurrentBets.forEach(function(itm) {
  888. logger.debug('[CRASH] BetID: #' + itm.betid + ' || User: ' + itm.user + ' || AutoCashout: ' + itm.autoCashout + ' || Suma: ' + itm.amount + ' || Won: ' + itm.won);
  889. });
  890.  
  891. io.sockets.emit('message', {
  892. type: 'crashed',
  893. crashmanule: rCAt
  894. });
  895.  
  896. CRcurrentBets = [];
  897. usersCr = {};
  898. CRusersAmount = {};
  899. crashState = 'ENDED';
  900. query('INSERT INTO `crash` SET `crashAt`='+pool.escape(rCAt)+',`hash`='+pool.escape(HaShGame));
  901.  
  902. setTimeout(function() {
  903. startCrashGame();
  904.  
  905. io.sockets.emit('message', {
  906. type: 'removeQCR'
  907. });
  908. }, 5000);
  909. }, 25);
  910. }
  911.  
  912. function getCrashAmount() {
  913. if(crashNumber == 120) {
  914. clearInterval(timerC1);
  915. startTimer(2);
  916. }else if(crashNumber == 165) {
  917. clearInterval(timerC2);
  918. startTimer(3);
  919. }else if(crashNumber == 203) {
  920. clearInterval(timerC3);
  921. startTimer(4);
  922. }else if(crashNumber == 310) {
  923. clearInterval(timerC4);
  924. startTimer(5);
  925. }else if(crashNumber == 560) {
  926. clearInterval(timerC5);
  927. startTimer(6);
  928. }else if(crashNumber == 800) {
  929. clearInterval(timerC6);
  930. startTimer(7);
  931. }else if(crashNumber == 1200) {
  932. clearInterval(timerC7);
  933. startTimer(8);
  934. }else if(crashNumber == 3566) {
  935. clearInterval(timerC8);
  936. startTimer(9);
  937. }else if(crashNumber == 7248) {
  938. clearInterval(timerC9);
  939. startTimer(10);
  940. }else if(crashNumber == 10258) {
  941. clearInterval(timerC10);
  942. startTimer(11);
  943. }else if(crashNumber == 49210) {
  944. clearInterval(timerC11);
  945. startTimer(12);
  946. }
  947. }
  948.  
  949. function CRsetBet(m, user, socket) {
  950. if(canPlayersBet == '1') {
  951. if((usersCr[user.steamid] !== undefined) && (usersCr[user.steamid] == CRbr)) {
  952. socket.emit('message', {
  953. type: 'error',
  954. enable: true,
  955. error: 'You have already joined the crash.'
  956. });
  957. return;
  958. }
  959. if((m.amount < minbet) || (m.amount > maxbet)) {
  960. socket.emit('message', {
  961. type: 'error',
  962. enable: true,
  963. error: 'Invalid bet amount ['+minbet+'-'+maxbet+'].'
  964. });
  965. return;
  966. }
  967. if(crashState != 'STARTING' && usersCr[user.steamid] != CRbr) {
  968. socket.emit('message', {
  969. type: 'error',
  970. enable: true,
  971. error: 'The game have been already started!'
  972. });
  973. return;
  974. }
  975.  
  976. var start_time = new Date();
  977. query('SELECT `balance` FROM `users` WHERE `steamid` = '+pool.escape(user.steamid), function(err, row) {
  978. if((err) || (!row.length)) {
  979. logger.error('Failed to find DB');
  980. logger.debug(err);
  981. socket.emit('message', {
  982. type: 'error',
  983. enable: true,
  984. error: 'You are not DB'
  985. });
  986. return;
  987. }
  988. if(row[0].balance >= m.amount) {
  989. query('UPDATE `users` SET `balance` = `balance` - '+parseInt(m.amount)+' WHERE `steamid` = '+pool.escape(user.steamid), function(err2, row2) {
  990. if(err2) {
  991. logger.error('Error in withdraw');
  992. logger.debug(err);
  993. socket.emit('message', {
  994. type: 'error',
  995. enable: true,
  996. error: 'You dont have enough points'
  997. });
  998. return;
  999. }
  1000. query('INSERT INTO `crbets` SET `user` = '+pool.escape(user.steamid)+', `amount` = '+pool.escape(m.amount)+', `autoCashout` = '+pool.escape(m.autoCash), function(err3, row3) {
  1001. if(err3) {
  1002. logger.error('Error in DB');
  1003. logger.debug(err);
  1004. return;
  1005. }
  1006. var end = new Date();
  1007. if(usersCr[user.steamid] === undefined) {
  1008. usersCr[user.steamid] = 1;
  1009. }
  1010. if(CRusersAmount[user.steamid] === undefined) {
  1011. CRusersAmount[user.steamid] = {
  1012. 'amount': m.amount,
  1013. 'autoCashout': m.autoCash,
  1014. 'cashedOut': '0',
  1015. 'profit': 0
  1016. };
  1017. }
  1018. CRusersAmount[user.steamid]['amount'] += parseInt(m.amount);
  1019. socket.emit('message', {
  1020. type: 'crbetconfirm',
  1021. bet: {
  1022. betid: row3.insertId,
  1023. amount: m.amount,
  1024. autoCashout: m.autoCash
  1025. },
  1026. balance: row[0].balance-m.amount,
  1027. mybr: usersCr[user.steamid],
  1028. br: CRbr,
  1029. exec: (end.getTime()-start_time.getTime()).toFixed(3)
  1030. });
  1031. users[user.steamid].balance = row[0].balance-m.amount;
  1032. io.sockets.emit('message', {
  1033. type: 'crbet',
  1034. bet: {
  1035. cashedOut: '0',
  1036.  
  1037. amount: m.amount,
  1038. autoCashout: m.autoCash,
  1039. betid: row3.insertId,
  1040. icon: user.avatar,
  1041. name: user.name,
  1042. user: user.steamid
  1043. }
  1044. });
  1045. CRcurrentBets.push({
  1046. cashedOut: '0',
  1047. autoCashout: m.autoCash,
  1048. amount: m.amount,
  1049. betid: row3.insertId,
  1050. icon: user.avatar,
  1051. name: user.name,
  1052. user: user.steamid,
  1053. won: '0'
  1054. });
  1055. socket.emit('message', {
  1056. type: 'setIsPlaying'
  1057. });
  1058. logger.debug('CRBet confirmed #'+row3.insertId+' | Amount: '+m.amount+' | AutoCashout: '+m.autoCash+' | User: '+pool.escape(user.steamid));
  1059. checkTimer();
  1060. })
  1061. });
  1062. } else {
  1063. socket.emit('message', {
  1064. type: 'error',
  1065. enable: true,
  1066. error: 'Error: You dont have any money.'
  1067. });
  1068. }
  1069. });
  1070. }else {
  1071. socket.emit('message', {
  1072. type: 'error',
  1073. enable: true,
  1074. error: 'Error: You cannot bet, because the bet is offline.'
  1075. });
  1076. }
  1077. }
  1078.  
  1079. function stopTimers() {
  1080. clearInterval(timerC1);
  1081. clearInterval(timerC2);
  1082. clearInterval(timerC3);
  1083. clearInterval(timerC4);
  1084. clearInterval(timerC5);
  1085. clearInterval(timerC6);
  1086. clearInterval(timerC7);
  1087. clearInterval(timerC8);
  1088. clearInterval(timerC9);
  1089. clearInterval(timerC10);
  1090. clearInterval(timerC11);
  1091. clearInterval(timerC12);
  1092. }
  1093.  
  1094. function startTimer(number) {
  1095. if(number == 1) {
  1096. timerC1 = setInterval(function() {
  1097. crashNumber += 1;
  1098. getCrashAmount();
  1099. checkCashouts();
  1100.  
  1101. if(crashNumber == crashAt) {
  1102. reluareCrash();
  1103. stopTimers();
  1104. }
  1105.  
  1106. io.sockets.emit('message', {
  1107. type: 'urcarecrash',
  1108. grafic: crashNumber
  1109. });
  1110. }, 200);
  1111. }else if(number == 2) {
  1112. timerC2 = setInterval(function() {
  1113. crashNumber += 1;
  1114. getCrashAmount();
  1115. checkCashouts();
  1116.  
  1117. if(crashNumber == crashAt) {
  1118. reluareCrash();
  1119. stopTimers();
  1120. }
  1121.  
  1122. io.sockets.emit('message', {
  1123. type: 'urcarecrash',
  1124. grafic: crashNumber
  1125. });
  1126. }, 150);
  1127. }else if(number == 3) {
  1128. timerC3 = setInterval(function() {
  1129. crashNumber += 1;
  1130. getCrashAmount();
  1131. checkCashouts();
  1132.  
  1133. if(crashNumber == crashAt) {
  1134. reluareCrash();
  1135. stopTimers();
  1136. }
  1137.  
  1138. io.sockets.emit('message', {
  1139. type: 'urcarecrash',
  1140. grafic: crashNumber
  1141. });
  1142. }, 130);
  1143. }else if(number == 4) {
  1144. timerC4 = setInterval(function() {
  1145. crashNumber += 1;
  1146. getCrashAmount();
  1147. checkCashouts();
  1148.  
  1149. if(crashNumber == crashAt) {
  1150. reluareCrash();
  1151. stopTimers();
  1152. }
  1153.  
  1154. io.sockets.emit('message', {
  1155. type: 'urcarecrash',
  1156. grafic: crashNumber
  1157. });
  1158. }, 100);
  1159. }else if(number == 5) {
  1160. timerC5 = setInterval(function() {
  1161. crashNumber += 1;
  1162. getCrashAmount();
  1163. checkCashouts();
  1164.  
  1165. if(crashNumber == crashAt) {
  1166. reluareCrash();
  1167. stopTimers();
  1168. }
  1169.  
  1170. io.sockets.emit('message', {
  1171. type: 'urcarecrash',
  1172. grafic: crashNumber
  1173. });
  1174. }, 80);
  1175. }else if(number == 6) {
  1176. timerC6 = setInterval(function() {
  1177. crashNumber += 1;
  1178. getCrashAmount();
  1179. checkCashouts();
  1180.  
  1181. if(crashNumber == crashAt) {
  1182. reluareCrash();
  1183. stopTimers();
  1184. }
  1185.  
  1186. io.sockets.emit('message', {
  1187. type: 'urcarecrash',
  1188. grafic: crashNumber
  1189. });
  1190. }, 60);
  1191. }else if(number == 7) {
  1192. timerC7 = setInterval(function() {
  1193. crashNumber += 1;
  1194. getCrashAmount();
  1195. checkCashouts();
  1196.  
  1197. if(crashNumber == crashAt) {
  1198. reluareCrash();
  1199. stopTimers();
  1200. }
  1201.  
  1202. io.sockets.emit('message', {
  1203. type: 'urcarecrash',
  1204. grafic: crashNumber
  1205. });
  1206. }, 30);
  1207. }else if(number == 8) {
  1208. timerC8 = setInterval(function() {
  1209. crashNumber += 1;
  1210. getCrashAmount();
  1211. checkCashouts();
  1212.  
  1213. if(crashNumber == crashAt) {
  1214. reluareCrash();
  1215. stopTimers();
  1216. }
  1217.  
  1218. io.sockets.emit('message', {
  1219. type: 'urcarecrash',
  1220. grafic: crashNumber
  1221. });
  1222. }, 15);
  1223. }else if(number == 9) {
  1224. timerC9 = setInterval(function() {
  1225. crashNumber += 1;
  1226. getCrashAmount();
  1227. checkCashouts();
  1228.  
  1229. if(crashNumber == crashAt) {
  1230. reluareCrash();
  1231. stopTimers();
  1232. }
  1233.  
  1234. io.sockets.emit('message', {
  1235. type: 'urcarecrash',
  1236. grafic: crashNumber
  1237. });
  1238. }, 0.1);
  1239. }else if(number == 10) {
  1240. timerC10 = setInterval(function() {
  1241. crashNumber += 1;
  1242. getCrashAmount();
  1243. checkCashouts();
  1244.  
  1245. if(crashNumber == crashAt) {
  1246. reluareCrash();
  1247. stopTimers();
  1248. }
  1249.  
  1250. io.sockets.emit('message', {
  1251. type: 'urcarecrash',
  1252. grafic: crashNumber
  1253. });
  1254. }, 0.01);
  1255. }else if(number == 11) {
  1256. timerC11 = setInterval(function() {
  1257. crashNumber += 1;
  1258. getCrashAmount();
  1259. checkCashouts();
  1260.  
  1261. if(crashNumber == crashAt) {
  1262. reluareCrash();
  1263. stopTimers();
  1264. }
  1265.  
  1266. io.sockets.emit('message', {
  1267. type: 'urcarecrash',
  1268. grafic: crashNumber
  1269. });
  1270. }, 0.001);
  1271. }else if(number == 12) {
  1272. timerC12 = setInterval(function() {
  1273. crashNumber += 1;
  1274. getCrashAmount();
  1275. checkCashouts();
  1276.  
  1277. if(crashNumber == crashAt) {
  1278. reluareCrash();
  1279. stopTimers();
  1280. }
  1281.  
  1282. io.sockets.emit('message', {
  1283. type: 'urcarecrash',
  1284. grafic: crashNumber
  1285. });
  1286. }, 0.0001);
  1287. }
  1288. }
  1289.  
  1290. function sendCrashAmount() {
  1291. if(isStatedSet == 0) {
  1292. crashState = 'STARTED';
  1293. isStatedSet = 1;
  1294. }
  1295.  
  1296. if(instaCrash == 1) {
  1297.  
  1298. reluareCrash();
  1299. instaCrash = 0;
  1300.  
  1301. CRcurrentBets = [];
  1302. usersCr = {};
  1303. CRusersAmount = {};
  1304.  
  1305. io.sockets.emit('message', {
  1306. type: 'crashed',
  1307. crashmanule: rCAt
  1308. });
  1309.  
  1310. crashState = 'ENDED';
  1311. }else {
  1312. startTimer(1);
  1313. }
  1314. }
  1315.  
  1316.  
  1317. function startCrashGame() {
  1318. var timpEmis;
  1319.  
  1320.  
  1321. crashState = 'STARTING';
  1322. timp = 700;
  1323. crashNumber = 100;
  1324.  
  1325. isStatedSet = 0;
  1326.  
  1327. var crypto = require('crypto');
  1328.  
  1329. var serverSeed = 'Test';
  1330.  
  1331. var genGameHash = function(serverSeed) {
  1332. return crypto.createHash('sha256').update(serverSeed).digest('hex');
  1333. };
  1334.  
  1335. var crashPointFromHash = function(serverSeed) {
  1336. var hash = crypto.createHmac('sha256', serverSeed).digest('hex');
  1337.  
  1338. var divisible = function divisible(hash, mod) {
  1339. var val = 0;
  1340.  
  1341. var o = hash.length % 4;
  1342. for (var i = o > 0 ? o - 4 : 0; i < hash.length; i += 4) {
  1343. val = ((val << 16) + parseInt(hash.substring(i, i+4), 16)) % mod;
  1344. }
  1345.  
  1346. return val === 0;
  1347. };
  1348.  
  1349. var INSTANT_CRASH_PERCENTAGE = 5;
  1350. if (divisible(hash, 100 / parseInt(INSTANT_CRASH_PERCENTAGE, 10)))
  1351. return 0;
  1352.  
  1353. var h = parseInt(hash.slice(0,52/4),16);
  1354. var e = Math.pow(2,52);
  1355.  
  1356. return Math.floor((100 * e - h) / (e - h));
  1357. };
  1358.  
  1359. serverSeed = genGameHash(serverSeed+time());
  1360.  
  1361. var terminatingHash = genGameHash(serverSeed);
  1362. var crashhh = crashPointFromHash(serverSeed);
  1363. rCAt = crashhh;
  1364. crashAt = rCAt;
  1365. logger.debug('New hash game: ' + terminatingHash + ' with crashPoint: ' + (crashhh / 100).toFixed(2) +'x');
  1366. HaShGame = terminatingHash;
  1367.  
  1368.  
  1369.  
  1370.  
  1371. if(rCAt <= 100) {
  1372. instaCrash = 1;
  1373. }
  1374.  
  1375. intervalul = setInterval(function() {
  1376. timp = timp - 10;
  1377. timpEmis = timp;
  1378.  
  1379. if(timp == 0){
  1380. sendCrashAmount();
  1381.  
  1382. setTimeout(function() {
  1383. clearInterval(intervalul);
  1384. }, 10);
  1385. }
  1386.  
  1387. io.sockets.emit('message', {
  1388. type: 'startcrash',
  1389. time: timpEmis
  1390. });
  1391.  
  1392. }, 100);
  1393. }
Add Comment
Please, Sign In to add comment