Advertisement
Guest User

Untitled

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