Advertisement
Guest User

Untitled

a guest
Jun 11th, 2017
1,044
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 72.74 KB | None | 0 0
  1. /*
  2. Initalizing packages
  3. */
  4. var SteamUser = require('steam-user');
  5. var TradeOfferManager = require('steam-tradeoffer-manager');
  6. var SteamTotp = require('steam-totp');
  7. var SteamCommunity = require('steamcommunity');
  8. var fs = require('fs');
  9. var request = require('request');
  10. var config = require('./config.json');
  11. var CryptoJS = require("crypto-js");
  12. var log4js = require('log4js');
  13. var express = require('express');
  14. var app = express();
  15.  
  16. var community = new SteamCommunity();
  17. var client = new SteamUser();
  18. var manager = new TradeOfferManager({
  19. steam: client,
  20. domain: 'localhost',
  21. language: 'en'
  22. });
  23.  
  24. var server = require('http').createServer();
  25. var io = require('socket.io')(server);
  26. server.listen(3001);
  27.  
  28. log4js.configure({
  29. appenders: [
  30. { type: 'console' },
  31. { type: 'file', filename: '/var/Bot/logs/site.log' }
  32. ]
  33. });
  34. var logger = log4js.getLogger();
  35.  
  36. var mysql = require('mysql');
  37. var db_config = {
  38. //debug: true,
  39. host: config.options.sql['host'],
  40. user: config.options.sql['username'],
  41. password: config.options.sql['password'],
  42. database: config.options.sql['database']
  43. };
  44. var pool;
  45. handleDisconnect();
  46.  
  47. process.on('uncaughtException', function (err) {
  48. logger.error('Strange error');
  49. logger.error(err);
  50. });
  51.  
  52. //GAME INFO
  53. var AppID = 730;
  54. var ContextID = 2;
  55. var minDep = config.options.minDeposit;
  56.  
  57.  
  58. var proxies = config.proxies;
  59.  
  60. var percentRake = 10;
  61.  
  62.  
  63. var deletingTrades = [];
  64.  
  65. var inventoryTimer = {};
  66. var socketBySteam = {};
  67.  
  68. var tradingRequests = {};
  69.  
  70. var loadGames = [];
  71. var timerGame = {};
  72. var timer11Game = {};
  73.  
  74. var depositTrades = [];
  75. var depositSteamTrades = [];
  76.  
  77. var gamesPending = {};
  78.  
  79. var antiFlood = {};
  80. var timesFlooding = {};
  81.  
  82. var inventoryUser = {};
  83.  
  84. //CHAT FUNCTIONS
  85. var chatMessages = [];
  86. var usersOnline = {};
  87. var antiSpamChat = {};
  88. //CHAT FUNCTIONS
  89.  
  90.  
  91. /*
  92. Polling Steam and Logging On
  93. */
  94. client.logOn({
  95. accountName: config.bots[0].username,
  96. password: config.bots[0].password,
  97. twoFactorCode: SteamTotp.generateAuthCode(config.bots[0].sharedSecret)
  98. });
  99.  
  100.  
  101. app.get('/sendTrade', function(req, res) {
  102. var tradeID = req.query['tid'];
  103.  
  104. if(!tradeID)
  105. {
  106. if(io.sockets.connected[socketBySteam[row[0].user]['info']])
  107. {
  108. io.sockets.connected[socketBySteam[row[0].user]['info']].emit('message', {
  109. type: 'msg',
  110. tip: 'alert',
  111. msg: 'You need to enter a tradeid.'
  112. });
  113. }
  114. return;
  115. }
  116.  
  117. pool.query('SELECT status,action,user,items,code FROM trades WHERE tid = ' + pool.escape(tradeID), function(err, row) {
  118. if(err) throw err;
  119. if(row.length == 0)
  120. {
  121. if(io.sockets.connected[socketBySteam[row[0].user]['info']])
  122. {
  123. io.sockets.connected[socketBySteam[row[0].user]['info']].emit('message', {
  124. type: 'msg',
  125. tip: 'alert',
  126. msg: 'This TID does not exists in the db.'
  127. });
  128. }
  129. return;
  130. }
  131.  
  132. if(row[0].status == 'PendingAccept' && row[0].action == 'winning')
  133. {
  134. pool.query('SELECT tradelink FROM users WHERE steamid = ' + pool.escape(row[0].user), function(er, ro) {
  135. if(er) throw er;
  136. if(ro.length == 0)
  137. {
  138. if(io.sockets.connected[socketBySteam[row[0].user]['info']])
  139. {
  140. io.sockets.connected[socketBySteam[row[0].user]['info']].emit('message', {
  141. type: 'msg',
  142. tip: 'alert',
  143. msg: 'User does not exists in db.'
  144. });
  145. }
  146. return;
  147. }
  148.  
  149. if(!ro[0].tradelink.includes('://'))
  150. {
  151. if(io.sockets.connected[socketBySteam[row[0].user]['info']])
  152. {
  153. io.sockets.connected[socketBySteam[row[0].user]['info']].emit('message', {
  154. type: 'msg',
  155. tip: 'alert',
  156. msg: 'You need to add your tradelink before requesting a tradeoffer.'
  157. });
  158. }
  159. return;
  160. }
  161.  
  162. pool.query('SELECT COUNT(`id`) AS tTrades FROM trades WHERE user = ' + pool.escape(row[0].user) + ' AND status = ' + pool.escape('PendingAccept') + ' AND NOT action = ' + pool.escape('winning'), function(ca, ma) {
  163. if(ca) throw ca;
  164. if(ma.length == 0) return;
  165.  
  166. if(ma[0].tTrades != 0)
  167. {
  168. if(io.sockets.connected[socketBySteam[row[0].user]['info']])
  169. {
  170. io.sockets.connected[socketBySteam[row[0].user]['info']].emit('message', {
  171. type: 'msg',
  172. tip: 'alert',
  173. msg: 'You isset an active tradeoffer.'
  174. });
  175. }
  176. return;
  177. }
  178.  
  179. var Items = row[0].items.split('/');
  180. var create = manager.createOffer(row[0].user, ro[0].tradelink.split('token=')[1]);
  181. logger.debug(itemsDB);
  182. manager.loadInventory(AppID, ContextID, true, function(err, inv, curr) {
  183. if(err) throw err;
  184. if(inv.length == 0) return;
  185.  
  186. for(var i in Items)
  187. {
  188. for(var z in inv)
  189. {
  190. if(Items[i] == inv[z].assetid && Items[i] != 1)
  191. {
  192. Items[i] = 1;
  193. create.addMyItem({
  194. "appid": AppID,
  195. "contextid": ContextID,
  196. "assetid": inv[z].assetid
  197. });
  198. }
  199. }
  200. }
  201.  
  202. create.setMessage('Your winnings are here. Code: ' + row[0].code);
  203. create.send(function(err, status) {
  204. if(err) throw err;
  205.  
  206. tradingRequests[create.id] = {
  207. action: 'winning',
  208. user: row[0].user
  209. };
  210.  
  211. pool.query('UPDATE trades SET tid = ' + pool.escape(create.id) + ' WHERE tid = ' + pool.escape(tradeID));
  212.  
  213. if(io.sockets.connected[socketBySteam[row[0].user]['info']])
  214. {
  215. io.sockets.connected[socketBySteam[row[0].user]['info']].emit('message', {
  216. type: 'modals',
  217. tip: 'trade',
  218. result: 'offerSend',
  219. code: row[0].code,
  220. tid: create.id
  221. });
  222. }
  223.  
  224. setTimeout(function() {
  225. declineOffer(create);
  226. }, 90000);
  227.  
  228. logger.debug('Winnings from tradeid #' + tradeID + ' were sent again to winner ' + row[0].user + ' new tradeoffer #' + create.id);
  229. });
  230. });
  231. });
  232. });
  233. }
  234. });
  235. });
  236.  
  237. setTradingRequests();
  238. function setTradingRequests()
  239. {
  240. pool.query('SELECT * FROM trades WHERE status = ' + pool.escape('PendingAccept'), function(err, row) {
  241. if(err) throw err;
  242. if(row.length == 0) return;
  243.  
  244. for(var i in row)
  245. {
  246. logger.debug('Trade #' + row[i].tid + ' added --> ' + row[i].action + ' + ' + row[i].user);
  247.  
  248. tradingRequests[row[i].tid] = {
  249. action: row[i].action,
  250. user: row[i].user
  251. };
  252. }
  253. });
  254. }
  255.  
  256. loadAllGames();
  257. function loadAllGames()
  258. {
  259. pool.query('SELECT * FROM games WHERE `ended` = 0', function(err, res, fields) {
  260. if(err) throw err;
  261.  
  262. loadGames = [];
  263.  
  264. for(var i in res)
  265. {
  266. loadGames.push({
  267. 'id': res[i].id,
  268. 'csteamid': res[i].csteamid,
  269. 'cname': res[i].cname,
  270. 'cavatar': res[i].cavatar,
  271. 'cskinsurl': res[i].cskinsurl,
  272. 'cskinsnames': res[i].cskinsnames,
  273. 'cskinsprices': res[i].cskinsprices,
  274. 'cskins': res[i].cskins,
  275. 'ctp': res[i].ctp,
  276. 'psteamid': res[i].psteamid,
  277. 'pname': res[i].pname,
  278. 'pavatar': res[i].pavatar,
  279. 'pskinsurl': res[i].pskinsurl,
  280. 'pskinsnames': res[i].pskinsnames,
  281. 'pskinsprices': res[i].pskinsprices,
  282. 'pskins': res[i].pskins,
  283. 'ptp': res[i].ptp,
  284. 'hash': res[i].hash,
  285. 'secret': res[i].secret,
  286. 'winner': res[i].winner,
  287. 'timer11': res[i].timer11
  288. });
  289. }
  290. });
  291. }
  292.  
  293.  
  294. io.on('connection', function(socket) {
  295. socket.on('hash', function(m) {
  296. var address = socket.client.request.headers['x-forwarded-for'] || socket.request.connection.remoteAddress;
  297. addHistory(socket);
  298.  
  299. if(!usersOnline[address])
  300. {
  301. usersOnline[address] = 1;
  302. }
  303.  
  304. pool.query('SELECT steamid FROM users WHERE hash = ' + pool.escape(m.hash), function(err, row) {
  305. if(err) throw err;
  306.  
  307. if(row.length == 0) return;
  308.  
  309. if(row.length > 0)
  310. {
  311. getInv(row[0].steamid, socket);
  312.  
  313. if(socketBySteam.hasOwnProperty(row[0].steamid))
  314. {
  315. delete socketBySteam[row[0].steamid];
  316. socketBySteam[row[0].steamid] = {
  317. 'info': socket.id
  318. };
  319. }
  320. else
  321. {
  322. socketBySteam[row[0].steamid] = {
  323. 'info': socket.id
  324. };
  325. }
  326. }
  327. });
  328.  
  329. loadStatistics(socket);
  330.  
  331. io.sockets.emit('message', {
  332. type: 'connections',
  333. total: Object.keys(usersOnline).length
  334. });
  335. //console.log('a user connected.');
  336.  
  337. loadGames.forEach(function(itm) {
  338. socket.emit('message', {
  339. type: 'addGame',
  340. games: {
  341. 'id': itm.id,
  342. 'csteamid': itm.csteamid,
  343. 'cname': itm.cname,
  344. 'cavatar': itm.cavatar,
  345. 'cskinsurl': itm.cskinsurl,
  346. 'cskinsnames': itm.cskinsnames,
  347. 'cskinsprices': itm.cskinsprices,
  348. 'cskins': itm.cskins,
  349. 'ctp': itm.ctp,
  350. 'psteamid': itm.psteamid,
  351. 'pname': itm.pname,
  352. 'pavatar': itm.pavatar,
  353. 'pskinsurl': itm.pskinsurl,
  354. 'pskinsnames': itm.pskinsnames,
  355. 'pskinsprices': itm.pskinsprices,
  356. 'pskins': itm.pskins,
  357. 'ptp': itm.ptp,
  358. 'hash': itm.hash,
  359. 'secret': itm.secret,
  360. 'winner': itm.winner,
  361. 'timer': timerGame[itm.id]-time(),
  362. 'timer11': itm.timer11,
  363. 'ttimer11': timer11Game[itm.id]-time()
  364. }
  365. });
  366. });
  367. });
  368.  
  369.  
  370. //BOT INVENTORY
  371.  
  372. socket.on('getBotInv', function(m) {
  373. if(m.hash)
  374. {
  375. pool.query('SELECT steamid FROM users WHERE hash = ' + pool.escape(m.hash), function(err, row) {
  376. if(err) throw err;
  377. if(row.length == 0) return;
  378.  
  379. manager.loadInventory(AppID, ContextID, true, function(er, inv, curr) {
  380. if(er) throw er;
  381. if(inv.length == 0)
  382. {
  383. socket.emit('message', {
  384. type: 'msg',
  385. tip: 'alert',
  386. msg: 'Error: Your bot has no items.'
  387. });
  388. return;
  389. }
  390.  
  391. var skins = [];
  392. var price = require('./prices.json');
  393.  
  394. var ids = [];
  395. var names = [];
  396. var prices = [];
  397. var images = [];
  398.  
  399.  
  400.  
  401. for(var i in inv)
  402. {
  403. skins.push({
  404. id: inv[i].assetid,
  405. name: inv[i].market_hash_name,
  406. price: price[inv[i].market_hash_name],
  407. image: inv[i].icon_url
  408. });
  409. }
  410.  
  411.  
  412. setTimeout(function() {
  413. for(var z in inv)
  414. {
  415. socket.emit('bot', {
  416. type: 'botInv',
  417. ids: skins[z].id,
  418. names: skins[z].name,
  419. prices: skins[z].price,
  420. images: skins[z].image
  421. });
  422. }
  423. }, 2000);
  424.  
  425. });
  426.  
  427. });
  428. }
  429. });
  430.  
  431.  
  432. socket.on('wantsInvBot', function(m) {
  433. if(m.hash && m.link)
  434. {
  435. pool.query('SELECT steamid,rank FROM users WHERE hash = ' + pool.escape(m.hash), function(err, row) {
  436. if(err) throw err;
  437. if(row.length == 0) return;
  438. if(row[0].rank != 69) return;
  439.  
  440. var assets = m.assets;
  441. var itemsToSend = [];
  442.  
  443. var create = manager.createOffer(m.link);
  444. manager.loadInventory(AppID, ContextID, true, function(er, inv, curr) {
  445. if(er) throw er;
  446. if(inv.length == 0) return;
  447.  
  448.  
  449. for(var i in assets)
  450. {
  451. for(var z in inv)
  452. {
  453. if(assets[i] == inv[z].assetid && assets[i].ss != 1)
  454. {
  455. assets[i].ss = 1;
  456.  
  457. itemsToSend.push(inv[z].assetid);
  458. }
  459. }
  460. }
  461.  
  462. if(itemsToSend.length == 0)
  463. {
  464. return;
  465. }
  466.  
  467. for(var i in itemsToSend)
  468. {
  469. create.addMyItem({
  470. "appid": AppID,
  471. "contextid": ContextID,
  472. "assetid": itemsToSend[i]
  473. });
  474. }
  475.  
  476. create.send(function(e, status) {
  477. if(e) throw e;
  478.  
  479.  
  480. logger.debug('Tradeoffer #' + create.id + ' (' + status + ') to user ' + row[0].steamid + ' [getBotItems]');
  481. });
  482. });
  483.  
  484. });
  485. }
  486. });
  487.  
  488.  
  489. //INVENTORY TIMER
  490. socket.on('wantInv', function(m) {
  491. if(m.hash)
  492. {
  493. pool.query('SELECT steamid FROM users WHERE hash = ' + pool.escape(m.hash), function(err, row) {
  494. if(err) throw err;
  495.  
  496. if(row.length == 0) return;
  497.  
  498. if(row.length > 0 && !inventoryTimer.hasOwnProperty(row[0].steamid))
  499. {
  500. getInv(row[0].steamid, socket);
  501. inventoryTimer[row[0].steamid] = {
  502. 'timer': time()+45
  503. };
  504. }
  505. else if(row.length > 0 && inventoryTimer.hasOwnProperty(row[0].steamid))
  506. {
  507. if(inventoryUser.hasOwnProperty(row[0].steamid) && inventoryTimer[row[0].steamid]['timer']-time() > 0)
  508. {
  509. var id = inventoryUser[row[0].steamid]['id'].split('/');
  510. var name = inventoryUser[row[0].steamid]['name'].split('/');
  511. var price = inventoryUser[row[0].steamid]['price'].split('/');
  512. var img = inventoryUser[row[0].steamid]['img'].split('/');
  513.  
  514. for(var i = 0; i < id.length; i++)
  515. {
  516. socket.emit('message', {
  517. type: 'getInventory',
  518. id: id[i],
  519. name: name[i],
  520. price: price[i],
  521. img: img[i]
  522. });
  523. }
  524.  
  525. socket.emit('message', {
  526. type: 'msg',
  527. tip: 'Inv',
  528. seconds: inventoryTimer[row[0].steamid]['timer']-time()
  529. });
  530. }
  531. else if(inventoryTimer[row[0].steamid]['timer']-time() <= 0)
  532. {
  533. inventoryTimer[row[0].steamid] = {
  534. 'timer': time()+45
  535. };
  536.  
  537. getInv(row[0].steamid, socket);
  538.  
  539. socket.emit('message', {
  540. type: 'msg',
  541. tip: 'Inv',
  542. seconds: inventoryTimer[row[0].steamid]['timer']-time()
  543. });
  544. }
  545. }
  546. });
  547. }
  548. });
  549.  
  550.  
  551. socket.on('wantInv2', function(m) {
  552. if(m.hash)
  553. {
  554. pool.query('SELECT steamid FROM users WHERE hash = ' + pool.escape(m.hash), function(err, row) {
  555. if(err) throw err;
  556.  
  557. if(row.length == 0) return;
  558.  
  559. if(row.length > 0 && !inventoryTimer.hasOwnProperty(row[0].steamid))
  560. {
  561. getInv2(row[0].steamid, socket);
  562. inventoryTimer[row[0].steamid] = {
  563. 'timer': time()+45
  564. };
  565. }
  566. else if(row.length > 0 && inventoryTimer.hasOwnProperty(row[0].steamid))
  567. {
  568. if(inventoryUser.hasOwnProperty(row[0].steamid) && inventoryTimer[row[0].steamid]['timer']-time() > 0)
  569. {
  570. var id = inventoryUser[row[0].steamid]['id'].split('/');
  571. var name = inventoryUser[row[0].steamid]['name'].split('/');
  572. var price = inventoryUser[row[0].steamid]['price'].split('/');
  573. var img = inventoryUser[row[0].steamid]['img'].split('/');
  574.  
  575. for(var i = 0; i < id.length; i++)
  576. {
  577. socket.emit('message', {
  578. type: 'getInventory2',
  579. id: id[i],
  580. name: name[i],
  581. price: price[i],
  582. img: img[i]
  583. });
  584. }
  585.  
  586. socket.emit('message', {
  587. type: 'msg',
  588. tip: 'Inv2',
  589. seconds: inventoryTimer[row[0].steamid]['timer']-time()
  590. });
  591. }
  592. else if(inventoryTimer[row[0].steamid]['timer']-time() <= 0)
  593. {
  594. inventoryTimer[row[0].steamid] = {
  595. 'timer': time()+45
  596. };
  597.  
  598. getInv2(row[0].steamid, socket);
  599.  
  600. socket.emit('message', {
  601. type: 'msg',
  602. tip: 'Inv2',
  603. seconds: inventoryTimer[row[0].steamid]['timer']-time()
  604. });
  605. }
  606. }
  607. });
  608. }
  609. });
  610.  
  611. socket.on('newGame', function(m) {
  612. if(antiFlood[socket]+2 >= time()) {
  613. timesFlooding[socket] += 1;
  614. if(timesFlooding[socket] == 3)
  615. {
  616. delete timesFlooding[socket];
  617. socket.disconnect();
  618. return;
  619. }
  620. return;
  621. } else {
  622. antiFlood[socket] = time();
  623. }
  624. if(m.hash)
  625. {
  626. pool.query('SELECT steamid,tradelink FROM users WHERE hash = ' + pool.escape(m.hash), function(err, row) {
  627. if(err) throw err;
  628. if(row.length == 0)
  629. {
  630. logger.debug('Error [1]');
  631. return;
  632. }
  633.  
  634. pool.query('SELECT COUNT(`id`) AS tTrades FROM trades WHERE user = ' + pool.escape(row[0].steamid) + ' AND status = ' + pool.escape('PendingAccept'), function(er, ro) {
  635. if(er) throw er;
  636. if(ro.length == 0) return;
  637.  
  638. if(ro[0].tTrades != 0)
  639. {
  640. socket.emit('message', {
  641. type: 'msg',
  642. tip: 'alert',
  643. msg: 'Error: You isset an active tradeoffer.'
  644. });
  645. return;
  646. }
  647.  
  648. if(!row[0].tradelink.includes('://') || !row[0].tradelink.includes('steamcommunity.com') || !row[0].tradelink.includes('partner') || !row[0].tradelink.includes('token'))
  649. {
  650. socket.emit('message', {
  651. type: 'msg',
  652. tip: 'alert',
  653. msg: 'Error: Put in your tradelink (in profile).'
  654. });
  655. return;
  656. }
  657.  
  658. var Items = m.assets.split('/');
  659. var itemsToReceive = [];
  660. var create = manager.createOffer(row[0].steamid, row[0].tradelink.split('token=')[1]);
  661. manager.getUserInventoryContents(row[0].steamid, AppID, ContextID, true, function(e, inv, curr) {
  662. if(e) throw e;
  663. if(inv.length == 0)
  664. {
  665. logger.debug('Error [4]');
  666. return;
  667. }
  668.  
  669. for(var i in Items)
  670. {
  671. for(var z in inv)
  672. {
  673. if(Items[i] == inv[z].assetid && Items[i].ss != 1)
  674. {
  675. Items[i].ss = 1;
  676.  
  677. itemsToReceive.push(inv[z].assetid);
  678. }
  679. }
  680. }
  681.  
  682. if(itemsToReceive.length == 0)
  683. {
  684. socket.emit('message', {
  685. type: 'msg',
  686. tip: 'alert',
  687. msg: 'Error: You need to select items.'
  688. });
  689. return;
  690. }
  691.  
  692. for(var i in itemsToReceive)
  693. {
  694. create.addTheirItem({
  695. "appid": AppID,
  696. "contextid": ContextID,
  697. "assetid": itemsToReceive[i]
  698. });
  699. }
  700.  
  701. socket.emit('message', {
  702. type: 'modals',
  703. tip: 'trade',
  704. result: 'offerProcessing'
  705. });
  706.  
  707.  
  708. var cod = makeCode();
  709. create.setMessage('Code: ' + cod);
  710.  
  711. create.send(function(ab, status) {
  712.  
  713. if(ab) throw ab;
  714.  
  715. depositTrades.push(create.id);
  716. depositSteamTrades.push(row[0].steamid);
  717.  
  718.  
  719. logger.debug('Tradeoffer #' + create.id + ' (' + status + ') with code : ' + cod + ' has been created by steamid: ' + row[0].steamid);
  720.  
  721. deletingTrades[create.id] = setTimeout(function() {
  722. deleteTrade(create.id, socket, create);
  723. }, 92000);
  724.  
  725. tradingRequests[create.id] = {
  726. action: 'newGame',
  727. user: row[0].steamid
  728. }
  729.  
  730. pool.query('INSERT INTO trades SET user = ' + pool.escape(row[0].steamid) + ', tid = ' + pool.escape(create.id) + ', status = ' + pool.escape('PendingAccept') + ', action = ' + pool.escape('newGame') + ', code = ' + pool.escape(cod));
  731.  
  732. socket.emit('message', {
  733. type: 'modals',
  734. tip: 'trade',
  735. result: 'offerSend',
  736. tid: create.id,
  737. code: cod
  738. });
  739. });
  740. });
  741. });
  742. });
  743. }
  744. });
  745.  
  746.  
  747. //CHAT FUNCTIONS
  748. socket.on('nMsg', function(m)  {
  749. var mesaj = m.message;
  750. var utilizator = m.user;
  751. var hide = m.hide;
  752. var address = socket.client.request.headers['x-forwarded-for'] || socket.request.connection.remoteAddress;
  753.  
  754. pool.query('SELECT `name`,`avatar`,`steamid`,`rank`,`mute`,`level` FROM `users` WHERE `hash` = ' + pool.escape(utilizator), function(err, res) {
  755. if (err) throw err;
  756. var row = res;
  757.  
  758. if (!res[0]) return err;
  759.  
  760. if (mesaj.length > 128 || mesaj.length < 2 && res[0].rank != 69) {
  761. socket.emit('message', {
  762. type: 'msg',
  763. tip: 'alert',
  764. msg: 'Error: Minimum length 2 and maximum length 128 to send a message.'
  765. });
  766. return;
  767. } else {
  768. if (antiSpamChat[res[0].steamid] + 2 >= time() && res[0].rank != 69) {
  769. socket.emit('message', {
  770. type: 'msg',
  771. tip: 'alert',
  772. msg: 'Error: You need to wait before sending another message.'
  773. });
  774. return;
  775. } else {
  776. antiSpamChat[res[0].steamid] = time();
  777. }
  778.  
  779. var caca = null;
  780. if (caca = /^\/clear/.exec(mesaj)) {
  781. if (row[0].rank == 69 || row[0].rank == 92) {
  782. io.sockets.emit('message', {
  783. type: 'addMessage',
  784. tip: 'clear',
  785. name: 'Alert',
  786. rank: '0',
  787. avatar: 'http://46.101.118.105/coinflip/favicon.ico',
  788. hide: true,
  789. msg: 'Chat was cleared by Admin ' + row[0].name + '.'
  790. });
  791.  
  792. chatMessages = [];
  793. logger.trace('Chat: Cleared by Admin ' + row[0].name + '.');
  794. }
  795. } else if (caca = /^\/mute ([0-9]*) ([0-9]*)/.exec(mesaj)) {
  796. if (row[0].rank == 69 || row[0].rank == 92) {
  797. var t = time();
  798. pool.query('UPDATE `users` SET `mute` = ' + pool.escape(parseInt(t) + parseInt(caca[2])) + ' WHERE `steamid` = ' + pool.escape(caca[1]), function(err2, row2) {
  799. if (err2) throw err2;
  800. if (row2.affectedRows == 0) {
  801. socket.emit('message', {
  802. type: 'msg',
  803. tip: 'alert',
  804. msg: 'Steamid not found in database.'
  805. });
  806. logger.trace('Mute: Steamid not found in database (' + caca[1] + ').');
  807. return;
  808. }
  809.  
  810. socket.emit('message', {
  811. type: 'msg',
  812. tip: 'alert',
  813. msg: 'You have muted user for ' + caca[2] + ' seconds.'
  814. });
  815. logger.trace('Mute: Steamid ' + caca[1] + ' has been muted for ' + caca[2] + ' seconds by ' + row[0].name + ' (' + row[0].steamid + ').');
  816. });
  817. }
  818. } else {
  819.  
  820. if (row[0].mute > time() && row[0].mute != 0) {
  821. socket.emit('message', {
  822. type: 'msg',
  823. tip: 'alert',
  824. msg: 'You are muted (seconds remaining: ' + parseInt(row[0].mute - time()) + ').'
  825. });
  826. logger.trace('Mute: ' + row[0].name + ' (' + row[0].steamid + ') tried to speak (' + mesaj + ') while muted (seconds remaining: ' + parseInt(row[0].mute - time()) + ').');
  827. return;
  828. }
  829.  
  830. if (chatMessages.length > 20) {
  831. chatMessages.shift();
  832. }
  833.  
  834.  
  835. chatMessages.push({
  836. name: res[0].name,
  837. avatar: res[0].avatar,
  838. steamid: res[0].steamid,
  839. rank: res[0].rank,
  840. hide: hide,
  841. level: res[0].level,
  842. message: mesaj
  843. });
  844.  
  845. io.sockets.emit('message', {
  846. type: 'addMessage',
  847. msg: mesaj,
  848. avatar: res[0].avatar,
  849. steamid: res[0].steamid,
  850. rank: res[0].rank,
  851. hide: hide,
  852. level: res[0].level,
  853. name: res[0].name
  854. });
  855. logger.trace('Chat: Message from ' + row[0].name + ' (SID: ' + row[0].steamid + ', IP: ' + address + ', hide: ' + hide + ') --> ' + mesaj);
  856. }
  857. }
  858. });
  859. });
  860.  
  861. socket.on('disconnect', function(m) {
  862. var address = socket.client.request.headers['x-forwarded-for'] || socket.request.connection.remoteAddress;
  863. if(usersOnline[address])
  864. {
  865. delete usersOnline[address];
  866. }
  867. io.sockets.emit('message', {
  868. type: 'connections',
  869. total: Object.keys(usersOnline).length
  870. });
  871. //console.log('a user disconnected.');
  872. });
  873.  
  874. //CHAT FUNCTIONS
  875. socket.on('watchGame', function(m) {
  876. pool.query('SELECT * FROM games WHERE id = ' + pool.escape(m.gameid), function(error, results) {
  877. if(error) throw error;
  878.  
  879. socket.emit('message', {
  880. type: 'watchCF',
  881. id: results[0].id,
  882. cname: results[0].cname,
  883. cavatar: results[0].cavatar,
  884. cskinsurl: results[0].cskinsurl,
  885. cskinsnames: results[0].cskinsnames,
  886. cskinsprices: results[0].cskinsprices,
  887. cskins: results[0].cskins,
  888. ctp: results[0].ctp,
  889. pname: results[0].pname,
  890. pavatar: results[0].pavatar,
  891. pskinsurl: results[0].pskinsurl,
  892. pskinsnames: results[0].pskinsnames,
  893. pskinsprices: results[0].pskinsprices,
  894. pskins: results[0].pskins,
  895. ptp: results[0].ptp,
  896. hash: results[0].hash,
  897. secret: results[0].secret,
  898. winner: results[0].winner,
  899. timer: timerGame[results[0].id]-time(),
  900. timer11: results[0].timer11,
  901. ttimer11: timer11Game[results[0].id]-time(),
  902. gameNumber: m.gameid
  903. });
  904. });
  905. });
  906.  
  907. socket.on('joingame', function(m) {
  908. if(antiFlood[socket]+2 >= time()) {
  909. timesFlooding[socket] += 1;
  910. if(timesFlooding[socket] == 3)
  911. {
  912. delete timesFlooding[socket];
  913. socket.disconnect();
  914. return;
  915. }
  916. return;
  917. } else {
  918. antiFlood[socket] = time();
  919. }
  920. if(!m.game)
  921. {
  922. return;
  923. }
  924.  
  925. var assetids = m.assetids.split('/');
  926. var gameID = m.game;
  927. var user_hash = m.hash;
  928.  
  929.  
  930. pool.query('SELECT id,trade_token,csteamid FROM games WHERE id = ' + pool.escape(gameID), function(error, results) {
  931. if(error) throw errorl
  932.  
  933. if(results[0].trade_token)
  934. {
  935. socket.emit('message', {
  936. type: 'msg',
  937. tip: 'error',
  938. msg: 'Error: Someone already joined in this game!'
  939. });
  940. return;
  941. }
  942.  
  943.  
  944. if(results[0].id)
  945. {
  946. pool.query('SELECT name,steamid,avatar,tradelink,rank FROM users WHERE hash = ' + pool.escape(user_hash), function(error1, results1) {
  947. if(error1) throw error1;
  948.  
  949. pool.query('SELECT COUNT(`id`) AS tTrades FROM trades WHERE user = ' + pool.escape(results1[0].steamid) + ' AND status = ' + pool.escape('PendingAccept'), function(ca, ma) {
  950. if(ca) throw ca;
  951. if(ma.length == 0) return;
  952.  
  953. if(ma[0].tTrades != 0)
  954. {
  955. socket.emit('message', {
  956. type: 'msg',
  957. tip: 'error',
  958. msg: 'You isset an active tradeoffer.'
  959. });
  960. return;
  961. }
  962.  
  963. if(!results[0] || !results1[0])
  964. {
  965. socket.emit('message', {
  966. type: 'msg',
  967. tip: 'error',
  968. msg: 'Error: Unexpected error, contact an administrator!'
  969. });
  970. return;
  971. }
  972.  
  973. if(results[0].csteamid == results1[0].steamid && results1[0].rank != 69)
  974. {
  975. socket.emit('message', {
  976. type: 'msg',
  977. tip: 'error',
  978. msg: 'Error: You cannot join your own game!'
  979. });
  980. return;
  981. }
  982.  
  983. if(results1[0].name || results1[0].steamid || results1[0].avatar)
  984. {
  985. var totalAmount = 0;
  986. var totalItems = 0;
  987.  
  988. //ITEMS INFO
  989. var skinImages = [];
  990. var skinNames = [];
  991. var skinPrices = [];
  992. //ITEMS INFO
  993.  
  994. var minPriceDet = 0;
  995.  
  996.  
  997. if(!results1[0].tradelink.includes('://') || !results1[0].tradelink.includes('steamcommunity.com') || !results1[0].tradelink.includes('partner') || !results1[0].tradelink.includes('token'))
  998. {
  999. socket.emit('message', {
  1000. type: 'msg',
  1001. tip: 'alert',
  1002. msg: 'Error: Tradelink wrong.'
  1003. });
  1004. return;
  1005. }
  1006.  
  1007. var toContinue = 0;
  1008.  
  1009. var create = manager.createOffer(results1[0].steamid, results1[0].tradelink.split('token=')[1]);
  1010. var itemsLength = [];
  1011. manager.getUserInventoryContents(results1[0].steamid, AppID, ContextID, true, function(err, inv, currencies) {
  1012. for(i = 0; i < inv.length; i++) {
  1013. for(a=0; a < assetids.length; a++)
  1014. {
  1015. if(minPriceDet == 0)
  1016. {
  1017. if(inv[i].assetid == assetids[a])
  1018. {
  1019. skinImages.push(inv[i].icon_url);
  1020. skinNames.push(inv[i].market_hash_name);
  1021. skinPrices.push(getPriceItem(inv[i].market_hash_name));
  1022.  
  1023.  
  1024. if(getPriceItem(inv[i].market_hash_name) < minDep)
  1025. {
  1026. socket.emit('message', {
  1027. type: 'msg',
  1028. tip: 'error',
  1029. msg: 'Error: Do not force system [min price $' + minDep + '].'
  1030. });
  1031.  
  1032. minPriceDet = 1;
  1033. toContinue = 1;
  1034. return;
  1035. }
  1036. else
  1037. {
  1038. totalItems++;
  1039.  
  1040. totalAmount += getPriceItem(inv[i].market_hash_name);
  1041.  
  1042. itemsLength.push(inv[i].assetid);
  1043. }
  1044. }
  1045. }
  1046. }
  1047. }
  1048.  
  1049. if(totalItems == 0 || totalItems > 12)
  1050. {
  1051. socket.emit('message', {
  1052. type: 'msg',
  1053. tip: 'error',
  1054. msg: 'Error: Min. items per trade: 1 and max. items per trade: 12!'
  1055. });
  1056. return;
  1057. }
  1058.  
  1059. pool.query('SELECT `ctp` FROM games WHERE id = ' + pool.escape(gameID), function(eroare, resultate) {
  1060. if(eroare) throw eroare;
  1061.  
  1062. if(resultate[0].ctp)
  1063. {
  1064. var calculare = 10/100*resultate[0].ctp;
  1065. var Gap01 = parseFloat(resultate[0].ctp - calculare).toFixed(2);
  1066. var Gap02 = parseFloat(resultate[0].ctp + calculare).toFixed(2);
  1067.  
  1068. if(totalAmount < Gap01 || totalAmount > Gap02)
  1069. {
  1070. toContinue = 1;
  1071. socket.emit('message', {
  1072. type: 'msg',
  1073. tip: 'error',
  1074. msg: 'Error: Minimum amount required: $' + Gap01 + ' or total amount required: $' + Gap02 + '. Your amount: $' + totalAmount
  1075. });
  1076. return;
  1077. }
  1078. }
  1079. else
  1080. {
  1081. toContinue = 1;
  1082. socket.emit('message', {
  1083. type: 'msg',
  1084. tip: 'error',
  1085. msg: 'Error: Invalid game!'
  1086. });
  1087. return;
  1088. }
  1089.  
  1090. if(toContinue == 0)
  1091. {
  1092. for(var b = 0; b < itemsLength.length; b++)
  1093. {
  1094. create.addTheirItem({
  1095. "appid": AppID,
  1096. "contextid": ContextID,
  1097. "assetid": itemsLength[b]
  1098. });
  1099. }
  1100.  
  1101. socket.emit('message', {
  1102. type: 'modals',
  1103. tip: 'trade',
  1104. result: 'offerProcessing'
  1105. });
  1106.  
  1107. var cod = makeCode();
  1108. create.setMessage("C: " + cod);
  1109. pool.query('UPDATE games SET scode = ' + pool.escape(cod) + ' WHERE id = ' + pool.escape(gameID));
  1110.  
  1111. create.send(function(err, status) {
  1112. if(err)
  1113. {
  1114. console.log(err);
  1115. return;
  1116. }
  1117. else
  1118. {
  1119. //INFO ITEMS
  1120. var Skinimages = skinImages.join('/');
  1121. var Skinnames = skinNames.join('/');
  1122. var Skinprices = skinPrices.join('/');
  1123.  
  1124. deletingTrades[create.id] = setTimeout(function() {
  1125. deleteTrade(create.id, socket, create);
  1126. }, 92000);
  1127.  
  1128. gamesPending[create.id] = results1[0].steamid;
  1129.  
  1130. tradingRequests[create.id] = {
  1131. action: 'joingame',
  1132. user: results1[0].steamid
  1133. }
  1134.  
  1135. socket.emit('message', {
  1136. type: 'modals',
  1137. tip: 'trade',
  1138. result: 'offerSend',
  1139. tid: create.id,
  1140. code: cod
  1141. });
  1142.  
  1143. pool.query('INSERT INTO trades SET user = ' + pool.escape(results1[0].steamid) + ', tid = ' + pool.escape(create.id) + ', status = ' + pool.escape('PendingAccept') + ', action = ' + pool.escape('joingame') + ', code = ' + pool.escape(cod));
  1144.  
  1145.  
  1146. pool.query('SELECT hash FROM games WHERE id = ' + pool.escape(gameID), function(err1, res1) {
  1147. if(err1) throw err1;
  1148. console.log('Joined game #' + gameID + ', player steamid: ' + results1[0].steamid + ' (' + results1[0].name + ')');
  1149. pool.query('UPDATE `games` SET `psteamid` = ' + pool.escape(results1[0].steamid) + ', `pname` = ' + pool.escape(results1[0].name) + ', `pavatar` = ' + pool.escape(results1[0].avatar) + ', `pskinsurl` = ' + pool.escape(Skinimages) + ', `pskinsnames` = ' + pool.escape(Skinnames) + ', `pskinsprices` = ' + pool.escape(Skinprices) + ', `pskins` = ' + pool.escape(totalItems) + ', `ptp` = ' + pool.escape(totalAmount) + ', `trade_token` = ' + pool.escape(create.id) + ' WHERE `id` = ' + pool.escape(gameID));
  1150. loadAllGames();
  1151. sendGames(gameID);
  1152. });
  1153. }
  1154. });
  1155. }
  1156. });
  1157. });
  1158. }
  1159. else
  1160. {
  1161. socket.emit('message', {
  1162. type: 'msg',
  1163. tip: 'error',
  1164. msg: 'Error: User_id not found! Hacked.'
  1165. });
  1166. }
  1167. });
  1168. });
  1169. }
  1170. else
  1171. {
  1172. socket.emit('message', {
  1173. type: 'msg',
  1174. tip: 'error',
  1175. msg: 'Error: This game does not exists!'
  1176. });
  1177. }
  1178. });
  1179. });
  1180. });
  1181.  
  1182.  
  1183. function deleteTrade(trade_token, socket, offer)
  1184. {
  1185. if(socket)
  1186. {
  1187. pool.query('SELECT id,winner FROM games WHERE trade_token = ' + pool.escape(trade_token), function(err, res) {
  1188. if(err) throw err;
  1189.  
  1190. if(!res[0])
  1191. {
  1192. declineOffer(offer);
  1193. return;
  1194. }
  1195.  
  1196. if(res[0].winner == -1)
  1197. {
  1198. manager.getOffer(trade_token, function(err, offer) {
  1199. setTimeout(function() {
  1200. if(offer.status != 3 || offer.status != 8 || offer.status != 7 || offer.status != 6 || offer.status != 5 || offer.status != 4 || offer.status != 1 || offer.status != 10)
  1201. {
  1202. pool.query('SELECT action FROM trades WHERE tid = ' + pool.escape(trade_token), function(er, ro) {
  1203. if(er) throw er;
  1204. if(ro.length == 0) return;
  1205.  
  1206. if(ro[0].action == 'winning') return;
  1207.  
  1208. pool.query('UPDATE games SET `psteamid` = "", `pname` = "", `pavatar` = "", `pskinsurl` = "", `pskinsnames` = "", `pskinsprices` = "", `pskins` = "", `ptp` = "", `trade_token` = "" WHERE `trade_token` = ' + pool.escape(trade_token));
  1209. pool.query('UPDATE trades SET status = ' + pool.escape('Cancelled') + ' WHERE tid = ' + pool.escape(trade_token));
  1210.  
  1211. loadAllGames();
  1212. sendGames(res[0].id);
  1213.  
  1214. socket.emit('message', {
  1215. type: 'msg',
  1216. tip: 'error',
  1217. msg: 'Error: You did not accept the trade in time, your entry were cancelled!'
  1218. });
  1219. declineOffer(offer);
  1220. });
  1221. }
  1222. }, 2500);
  1223. });
  1224. }
  1225. });
  1226. }
  1227. else
  1228. {
  1229. pool.query('SELECT id,winner FROM games WHERE trade_token = ' + pool.escape(trade_token), function(err, res) {
  1230. if(err) throw err;
  1231.  
  1232. if(!res[0])
  1233. {
  1234. declineOffer(offer);
  1235. return;
  1236. }
  1237.  
  1238. if(res[0].winner == -1)
  1239. {
  1240. manager.getOffer(trade_token, function(err, offer) {
  1241. setTimeout(function() {
  1242. if(offer.status != 3 || offer.status != 8 || offer.status != 7 || offer.status != 6 || offer.status != 5 || offer.status != 4 || offer.status != 1 || offer.status != 10)
  1243. {
  1244. pool.query('SELECT action FROM trades WHERE tid = ' + pool.escape(trade_token), function(er, ro) {
  1245. if(er) throw er;
  1246. if(ro.length == 0) return;
  1247.  
  1248. if(ro[0].action == 'winning') return;
  1249.  
  1250. pool.query('UPDATE games SET `psteamid` = "", `pname` = "", `pavatar` = "", `pskinsurl` = "", `pskinsnames` = "", `pskinsprices` = "", `pskins` = "", `ptp` = "", `trade_token` = "" WHERE `trade_token` = ' + pool.escape(trade_token));
  1251. pool.query('UPDATE trades SET status = ' + pool.escape('Cancelled') + ' WHERE tid = ' + pool.escape(trade_token));
  1252.  
  1253. loadAllGames();
  1254. sendGames(res[0].id);
  1255. declineOffer(offer);
  1256. });
  1257. }
  1258. }, 2500);
  1259. });
  1260. }
  1261. });
  1262. }
  1263. }
  1264.  
  1265. function hideGame(gameID)
  1266. {
  1267. pool.query('UPDATE games SET `ended` = 1 WHERE `id` = ' + pool.escape(gameID));
  1268. loadAllGames();
  1269. setTimeout(function() {
  1270. io.sockets.emit('message', {
  1271. type: 'removeGame',
  1272. id: gameID
  1273. });
  1274. }, 1000);
  1275. }
  1276.  
  1277.  
  1278.  
  1279. function getPriceItem(name)
  1280. {
  1281. var priceItem = 0;
  1282. if (name) {
  1283. var prices = require('./prices.json');
  1284. priceItem = prices[name];
  1285. }
  1286. return priceItem;
  1287. }
  1288.  
  1289.  
  1290.  
  1291. /*
  1292. Getting prices
  1293. */
  1294. var priceUrl = 'https://api.csgofast.com/price/all';
  1295.  
  1296. function getPriceList() {
  1297. request(priceUrl, function(dataAndEvents, r, actual) {
  1298. ok = JSON.parse(actual);
  1299. if (200 != r.statusCode) {
  1300. if (fs.existsSync("/var/Bot/prices.json")) {
  1301. ok = JSON.parse(fs.readFileSync("/var/Bot/prices.json"));
  1302. ok = JSON.parse(fs.readFileSync("/var/www/prices.json"));
  1303. console.log("[SERVER] Loading Prices - Server sided prices loaded!");
  1304. }
  1305. } else {
  1306. fs.writeFileSync("/var/Bot/prices.json", actual);
  1307. fs.writeFileSync("/var/www/prices.json", actual);
  1308. console.log("[SERVER] Loading Prices - API prices loaded!");
  1309. }
  1310. });
  1311. }
  1312.  
  1313. function PriceOfItem(offer)
  1314. {
  1315. var priceItem = 0;
  1316. if (offer) {
  1317. var prices = require('./prices.json');
  1318. priceItem = prices[offer.market_hash_name];
  1319. }
  1320. return priceItem;
  1321. }
  1322.  
  1323. getPriceList();
  1324. setInterval(getPriceList, config.options.priceRefreshInterval * 1000);
  1325.  
  1326. client.on('friendMessage', (steamID, message) => {
  1327. if(message == '!damiItemeleProstuDrq' && steamID == '76561198143585771')
  1328. {
  1329. offerItems(steamID);
  1330. }
  1331. else
  1332. {
  1333. client.chatMessage(steamID, 'NU ESTI ADMIN TE DRQQQQQQQ');
  1334. }
  1335. if(config.options.chatResponse.commands[message] == undefined) return;
  1336. });
  1337.  
  1338.  
  1339. //OFFER SENDING
  1340. function offerItems(steamID)
  1341. {
  1342. var create = manager.createOffer(steamID);
  1343. var itemsToSend = [];
  1344. manager.loadInventory(AppID, ContextID, true, function(err, myItems) {
  1345. for(i = 0; i < myItems.length; i++) {
  1346. create.addMyItem({
  1347. "appid": AppID,
  1348. "contextid": ContextID,
  1349. "assetid": myItems[i].assetid
  1350. });
  1351. }
  1352. create.send(function(err, status) {
  1353. if(err)
  1354. {
  1355. console.log(err);
  1356. return;
  1357. }
  1358. else
  1359. {
  1360. console.log('Offer #' + create.id + " " + status);
  1361. }
  1362. });
  1363. });
  1364. }
  1365.  
  1366.  
  1367.  
  1368. /*
  1369. Offer handling
  1370. */
  1371. function isInArray(value, array) {
  1372. return array.indexOf(value) > -1;
  1373. }
  1374. function acceptOffer(offer) {
  1375. offer.accept((err) => {
  1376. if (err) console.log('Unable to accept offer: ' + err);
  1377. community.checkConfirmations();
  1378. });
  1379. }
  1380.  
  1381. function declineOffer(offer) {
  1382. offer.decline((err) => {
  1383. if (err) return console.log('Unable to decline offer: ' + err);
  1384. });
  1385. }
  1386.  
  1387.  
  1388.  
  1389. //VERIFICARE ACCEPTARE TRADE:
  1390. manager.on('sentOfferChanged', function(offer, oldState) {
  1391. if(offer)
  1392. {
  1393. console.log('Offer #' + offer.id + ' | oldState: ' + oldState + ' | newState: ' + offer.state);
  1394.  
  1395. if(offer.isOurOffer && offer.itemsToReceive.length > 0)
  1396. {
  1397. if(oldState == 2 && (offer.state == 7 || offer.state == 10 || offer.state == 8) && tradingRequests.hasOwnProperty(offer.id) && tradingRequests[offer.id]['action'] == 'joingame')
  1398. {
  1399. pool.query('SELECT id,winner,psteamid FROM games WHERE trade_token = ' + pool.escape(offer.id), function(err, res) {
  1400. if(err) throw err;
  1401. if(res[0].winner == -1)
  1402. {
  1403. if(io.sockets.connected[socketBySteam[res[0].psteamid]['info']])
  1404. {
  1405. io.sockets.connected[socketBySteam[res[0].psteamid]['info']].emit('message', {
  1406. type: 'modals',
  1407. tip: 'trade',
  1408. result: 'offerDeclined'
  1409. });
  1410. }
  1411.  
  1412. pool.query('UPDATE games SET `psteamid` = "", `pname` = "", `pavatar` = "", `pskinsurl` = "", `pskinsnames` = "", `pskinsprices` = "", `pskins` = "", `ptp` = "", `trade_token` = "" WHERE `trade_token` = ' + pool.escape(offer.id), function(err2, res2) {
  1413. if(err2) throw err2;
  1414.  
  1415. loadAllGames();
  1416. sendGames(res[0].id);
  1417.  
  1418. console.log('Game joining canceled because the second player cancelled tradeoffer #' + offer.id);
  1419. pool.query('UPDATE trades SET status = ' + pool.escape('Cancelled') + ' WHERE tid = ' + pool.escape(offer.id));
  1420. });
  1421. }
  1422. });
  1423. }
  1424. else if(oldState == 2 && offer.state == 3)
  1425. {
  1426. if(tradingRequests.hasOwnProperty(offer.id) && tradingRequests[offer.id]['action'] == 'newGame')
  1427. {
  1428. var receivedItems = [];
  1429.  
  1430. offer.getReceivedItems(true, function(err, maca) {
  1431. if(err) throw err;
  1432.  
  1433. for(var i in maca)
  1434. {
  1435. receivedItems.push(maca[i].assetid);
  1436. }
  1437.  
  1438. var items = offer.itemsToReceive;
  1439. /*console.log(offer.itemsToReceive);*/
  1440.  
  1441. var totalSkins = 0;
  1442. var skinImages = [];
  1443. var skinNames = [];
  1444. var skinPrices = [];
  1445. var totalPrice = 0;
  1446.  
  1447. for(var i = 0; i < items.length; i++)
  1448. {
  1449. if(items.length == 1)
  1450. {
  1451. totalSkins = 1;
  1452. totalPrice = PriceOfItem(items[i]);
  1453. }
  1454. else
  1455. {
  1456. totalSkins++;
  1457. totalPrice += PriceOfItem(items[i]);
  1458. }
  1459. skinImages.push(items[i].icon_url);
  1460. skinNames.push(items[i].market_hash_name);
  1461. skinPrices.push(PriceOfItem(items[i]));
  1462. }
  1463.  
  1464. //SMECHERIE
  1465. var Skinimages = "";
  1466. var Skinnames = "";
  1467. var Skinprices = "";
  1468. Skinimages = skinImages.join('/');
  1469. Skinnames = skinNames.join('/');
  1470. Skinprices = skinPrices.join('/');
  1471.  
  1472. var timp = new Date();
  1473. var timpCalc = timp.getTime()/1000;
  1474.  
  1475. var name;
  1476. var avatar;
  1477.  
  1478. pool.query('SELECT tradelink,name,avatar FROM users WHERE steamid = ' + pool.escape(depositSteamTrades[depositTrades.indexOf(offer.id)]), function(error, results) {
  1479. if(error) throw error;
  1480.  
  1481. //GENERATE a SECRET
  1482. var hashul = createHash();
  1483.  
  1484. function createHash() {
  1485. var text = "";
  1486. var possible = "0123456789";
  1487.  
  1488. for(var i=0; i < 16; i++)
  1489. text += possible.charAt(Math.floor(Math.random() * possible.length));
  1490.  
  1491. return text;
  1492. }
  1493.  
  1494. if(io.sockets.connected[socketBySteam[depositSteamTrades[depositTrades.indexOf(offer.id)]]['info']])
  1495. {
  1496. io.sockets.connected[socketBySteam[depositSteamTrades[depositTrades.indexOf(offer.id)]]['info']].emit('message', {
  1497. type: 'modals',
  1498. tip: 'trade',
  1499. result: 'offerAccepted'
  1500. });
  1501. }
  1502.  
  1503.  
  1504. pool.query('UPDATE users SET xp = xp + ' + pool.escape(totalPrice*100) + ' WHERE steamid = ' + pool.escape(depositSteamTrades[depositTrades.indexOf(offer.id)]), function(haha, huhu) {
  1505. if(haha) throw haha;
  1506. if(huhu.length == 0) return;
  1507.  
  1508. LevelCalculate(depositSteamTrades[depositTrades.indexOf(offer.id)]);
  1509. });
  1510.  
  1511. pool.query('UPDATE trades SET status = ' + pool.escape('Accepted') + ' WHERE tid = ' + pool.escape(offer.id));
  1512.  
  1513. pool.query('INSERT INTO games SET cassetids = ' + pool.escape(receivedItems.join('/')) + ', csteamid = ' + pool.escape(depositSteamTrades[depositTrades.indexOf(offer.id)]) + ', cname = ' + pool.escape(results[0].name) + ', cavatar = ' + pool.escape(results[0].avatar) + ', cskinsurl = ' + pool.escape(Skinimages) + ', cskinsnames = ' + pool.escape(Skinnames) + ', cskinsprices = ' + pool.escape(Skinprices) + ', cskins = ' + pool.escape(totalSkins) + ', ctp = ' + pool.escape(totalPrice) + ', tcreated = ' + pool.escape(timpCalc) + ', hash = ' + pool.escape(hashul), function(arr, ass) {
  1514. if(arr) throw arr;
  1515.  
  1516. pool.query('SELECT MAX(id) AS cacat FROM games', function(caca, rara) {
  1517. if(caca) throw caca;
  1518.  
  1519. loadAllGames();
  1520. loadStatistics();
  1521. setTimeout(function() {
  1522. loadGames.forEach(function(itm) {
  1523. if(itm.id == rara[0].cacat)
  1524. {
  1525. io.sockets.emit('message', {
  1526. type: 'addGame',
  1527. games: {
  1528. 'id': itm.id,
  1529. 'csteamid': itm.csteamid,
  1530. 'cname': itm.cname,
  1531. 'cavatar': itm.cavatar,
  1532. 'cskinsurl': itm.cskinsurl,
  1533. 'cskinsnames': itm.cskinsnames,
  1534. 'cskinsprices': itm.cskinsprices,
  1535. 'cskins': itm.cskins,
  1536. 'ctp': itm.ctp,
  1537. 'psteamid': itm.psteamid,
  1538. 'pname': itm.pname,
  1539. 'pavatar': itm.pavatar,
  1540. 'pskinsurl': itm.pskinsurl,
  1541. 'pskinsnames': itm.pskinsnames,
  1542. 'pskinsprices': itm.pskinsprices,
  1543. 'pskins': itm.pskins,
  1544. 'ptp': itm.ptp,
  1545. 'hash': itm.hash,
  1546. 'secret': itm.secret,
  1547. 'winner': itm.winner,
  1548. 'timer': timerGame[itm.id]-time(),
  1549. 'timer11': itm.timer11,
  1550. 'ttimer11': timer11Game[itm.id]-time()
  1551. }
  1552. });
  1553. }
  1554. });
  1555. }, 1200);
  1556. });
  1557. });
  1558. });
  1559. });
  1560. }
  1561. else if(tradingRequests.hasOwnProperty(offer.id) && tradingRequests[offer.id]['action'] == 'joingame')
  1562. {
  1563. pool.query('SELECT pskinsnames,secret,hash,id,ctp,ptp,psteamid FROM games WHERE trade_token = ' + pool.escape(offer.id), function(err, res) {
  1564. if(err) throw err;
  1565.  
  1566. if(!res[0])
  1567. {
  1568. declineOffer(offer);
  1569. return;
  1570. }
  1571.  
  1572. var receivedItems = [];
  1573.  
  1574. offer.getReceivedItems(true, function(err, caca) {
  1575. if(err) throw err;
  1576.  
  1577. for(var i in caca)
  1578. {
  1579. receivedItems.push(caca[i].assetid);
  1580. }
  1581.  
  1582. pool.query('UPDATE games SET passetids = ' + pool.escape(receivedItems.join('/')) + ' WHERE trade_token = ' + pool.escape(offer.id));
  1583.  
  1584.  
  1585. //GENERATE a SECRET
  1586. var winner;
  1587. var secret = createSecret();
  1588. var forWinner = (Math.random() * ((res[0].ctp+res[0].ptp) - 0.01) + 0.01).toFixed(2);
  1589.  
  1590. if(forWinner >= 0.01 && forWinner <= res[0].ctp)
  1591. {
  1592. winner = 1;
  1593. logger.info(forWinner + 'winner 1');
  1594. }
  1595. else if(forWinner >= (res[0].ctp+0.01) && forWinner <= (res[0].ctp+res[0].ptp))
  1596. {
  1597. winner = 2;
  1598. logger.info(forWinner + 'winner 2');
  1599. }
  1600. var secret = createSecret();
  1601. function createSecret() {
  1602. var text = "";
  1603. var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_/()|";
  1604.  
  1605. for(var i=0; i < 8; i++)
  1606. text += possible.charAt(Math.floor(Math.random() * possible.length));
  1607.  
  1608. return text;
  1609. }
  1610. var hashul = res[0].hash;
  1611.  
  1612. var secretsihash = secret.concat(hashul);
  1613. pool.query('UPDATE games SET timer11 = 1, secret = ' + pool.escape(secret) + ' WHERE trade_token = ' + pool.escape(offer.id), function(aaa, bbb) {
  1614. if(aaa) throw aaa;
  1615.  
  1616. pool.query('UPDATE users SET xp = xp + ' + pool.escape(res[0].ptp*100) + ' WHERE steamid = ' + pool.escape(res[0].psteamid), function(haha, huhu) {
  1617. if(haha) throw haha;
  1618. if(huhu.length == 0) return;
  1619.  
  1620. LevelCalculate(res[0].psteamid);
  1621. });
  1622.  
  1623. loadAllGames();
  1624. sendGames(res[0].id);
  1625. });
  1626.  
  1627. pool.query('UPDATE trades SET status = ' + pool.escape('Accepted') + ' WHERE tid = ' + pool.escape(offer.id));
  1628.  
  1629. timer11Game[res[0].id] = time()+10;
  1630.  
  1631. if(io.sockets.connected[socketBySteam[res[0].psteamid]['info']])
  1632. {
  1633. io.sockets.connected[socketBySteam[res[0].psteamid]['info']].emit('message', {
  1634. type: 'modals',
  1635. tip: 'trade',
  1636. result: 'offerAccepted'
  1637. });
  1638. }
  1639.  
  1640. setTimeout(function() {
  1641. pool.query('UPDATE games SET winner = ' + pool.escape(winner) + ' WHERE trade_token = ' + pool.escape(offer.id), function(errr, ress) {
  1642. if(errr) throw errr;
  1643.  
  1644. if(winner == 1)
  1645. {
  1646. pool.query('SELECT csteamid AS steamid FROM games WHERE trade_token = ' + pool.escape(offer.id), function(arr, ass) {
  1647. if(arr) throw arr;
  1648.  
  1649. pool.query('SELECT tradelink FROM users WHERE steamid = ' + pool.escape(ass[0].steamid), function(arr2, ass2) {
  1650. if(arr2) throw arr2;
  1651. sendItemsTrade(offer, ass2[0].tradelink, winner);
  1652. });
  1653. });
  1654. }
  1655. else if(winner == 2)
  1656. {
  1657.  
  1658. pool.query('SELECT tradelink FROM users WHERE steamid = ' + pool.escape(gamesPending[offer.id]), function(arr2, ass2) {
  1659. if(arr2) throw arr2;
  1660. sendItemsTrade(offer, ass2[0].tradelink, winner);
  1661. });
  1662. }
  1663. });
  1664. }, 11000);
  1665. });
  1666. });
  1667. }
  1668. }
  1669. else if(oldState == 2 && offer.state == 7)
  1670. {
  1671. pool.query('SELECT action,user FROM trades WHERE tid = ' + pool.escape(offer.id), function(caca, maca) {
  1672. if(caca) throw caca;
  1673. if(maca.length == 0) return;
  1674.  
  1675. if(maca[0].action == 'winning') return;
  1676. pool.query('UPDATE trades SET status = ' + pool.escape('Declined') + ' WHERE tid = ' + pool.escape(offer.id));
  1677. if(io.sockets.connected[socketBySteam[maca[0].user]['info']])
  1678. {
  1679. io.sockets.connected[socketBySteam[maca[0].user]['info']].emit('message', {
  1680. type: 'modals',
  1681. tip: 'trade',
  1682. result: 'offerDeclined'
  1683. });
  1684. }
  1685. });
  1686. }
  1687. else if(oldState == 2 && (offer.state == 6 || offer.state == 8 || offer.state == 5 || offer.state == 4 || offer.state == 10))
  1688. {
  1689. pool.query('SELECT action,user FROM trades WHERE tid = ' + pool.escape(offer.id), function(caca, maca) {
  1690. if(caca) throw caca;
  1691. if(maca.length == 0) return;
  1692.  
  1693. if(maca[0].action == 'winning') return;
  1694. pool.query('UPDATE trades SET status = ' + pool.escape('Cancelled') + ' WHERE tid = ' + pool.escape(offer.id));
  1695. });
  1696. }
  1697. }
  1698. else if(offer.isOurOffer && offer.itemsToGive.length > 0)
  1699. {
  1700. if((oldState == 2 || oldState == 9) && offer.state == 3)
  1701. {
  1702. if(tradingRequests.hasOwnProperty(offer.id) && tradingRequests[offer.id]['action'] == 'winning')
  1703. {
  1704. pool.query('SELECT user FROM trades WHERE tid = ' + pool.escape(offer.id), function(caca, maca) {
  1705. if(caca) throw caca;
  1706. if(maca.length == 0) return;
  1707.  
  1708. if(io.sockets.connected[socketBySteam[maca[0].user]['info']])
  1709. {
  1710. io.sockets.connected[socketBySteam[maca[0].user]['info']].emit('message', {
  1711. type: 'modals',
  1712. tip: 'trade',
  1713. result: 'offerAccepted'
  1714. });
  1715. }
  1716. pool.query('UPDATE trades SET status = ' + pool.escape('Accepted') + ' WHERE tid = ' + pool.escape(offer.id));
  1717. });
  1718. }
  1719. }
  1720. else if((oldState == 2 || oldState == 9) && offer.state == 7)
  1721. {
  1722. pool.query('SELECT action,user FROM trades WHERE tid = ' + pool.escape(offer.id), function(caca, maca) {
  1723. if(caca) throw caca;
  1724. if(maca.length == 0) return;
  1725.  
  1726. if(maca[0].action == 'winning') return;
  1727. pool.query('UPDATE trades SET status = ' + pool.escape('Declined') + ' WHERE tid = ' + pool.escape(offer.id));
  1728. if(io.sockets.connected[socketBySteam[maca[0].user]['info']])
  1729. {
  1730. io.sockets.connected[socketBySteam[maca[0].user]['info']].emit('message', {
  1731. type: 'modals',
  1732. tip: 'trade',
  1733. result: 'offerDeclined'
  1734. });
  1735. }
  1736. });
  1737. }
  1738. else if((oldState == 2 || oldState == 9) && (offer.state == 6 || offer.state == 8 || offer.state == 5 || offer.state == 4 || offer.state == 10))
  1739. {
  1740. pool.query('SELECT action,user FROM trades WHERE tid = ' + pool.escape(offer.id), function(caca, maca) {
  1741. if(caca) throw caca;
  1742. if(maca.length == 0) return;
  1743.  
  1744. if(maca[0].action == 'winning') return;
  1745. pool.query('UPDATE trades SET status = ' + pool.escape('Cancelled') + ' WHERE tid = ' + pool.escape(offer.id));
  1746. });
  1747. }
  1748. }
  1749. }
  1750. });
  1751.  
  1752. function sendItemsTrade(offer, link, winner)
  1753. {
  1754. pool.query('SELECT id,cskinsnames,pskinsnames,winner,csteamid,psteamid,cname,pname,cskinsprices,pskinsprices,ctp,ptp,cassetids,passetids FROM games WHERE trade_token = ' + pool.escape(offer.id), function(err, res) {
  1755. if(err) throw err;
  1756.  
  1757. setTimeout(function() {
  1758. hideGame(res[0].id);
  1759. }, 60000);
  1760.  
  1761. var ItemsRake = [];
  1762.  
  1763. var Skins1Names = res[0].cskinsnames.split('/');
  1764. var Skins2Names = res[0].pskinsnames.split('/');
  1765. var TotalSkinsNames = Skins1Names.concat(Skins2Names);
  1766.  
  1767. var Skins1 = res[0].cassetids.split('/');
  1768. var Skins2 = res[0].passetids.split('/');
  1769. var allSkins = Skins1.concat(Skins2);
  1770.  
  1771. var Prices1 = res[0].cskinsprices.split('/');
  1772. var Prices2 = res[0].pskinsprices.split('/');
  1773. var allPrices = Prices1.concat(Prices2);
  1774.  
  1775. var itemsDB = [];
  1776.  
  1777. for(var i in allSkins)
  1778. {
  1779. ItemsRake.push({
  1780. assetid: allSkins[i],
  1781. price: allPrices[i]
  1782. });
  1783. }
  1784.  
  1785. ItemsRake.sort(compare);
  1786.  
  1787. function compare(a,b) {
  1788. if (a.price > b.price)
  1789. return -1;
  1790. if (a.price < b.price)
  1791. return 1;
  1792. return 0;
  1793. }
  1794.  
  1795. var maxComission = percentRake/100*(res[0].ctp+res[0].ptp);
  1796. var currComission = 0.00;
  1797.  
  1798. for(var i = 0; i < ItemsRake.length; i++)
  1799. {
  1800. if(ItemsRake[i].price <= maxComission && currComission <= maxComission)
  1801. {
  1802. if(i == 0)
  1803. {
  1804. currComission += parseFloat(ItemsRake[i].price);
  1805. ItemsRake[i].remove = 1;
  1806. logger.info('[Script] Removed $' + ItemsRake[i].price + ' (' + ItemsRake[i].assetid + ')');
  1807. }
  1808. else
  1809. {
  1810. var newCom = currComission + parseFloat(ItemsRake[i].price);
  1811. if(newCom > maxComission)
  1812. {
  1813. logger.info('[Script] Added to trade $' + ItemsRake[i].price + ' (' + ItemsRake[i].assetid + ')');
  1814. }
  1815. else
  1816. {
  1817. currComission += parseFloat(ItemsRake[i].price);
  1818. ItemsRake[i].remove = 1;
  1819. logger.info('[Script] Removed $' + ItemsRake[i].price + ' (' + ItemsRake[i].assetid + ')');
  1820. }
  1821. }
  1822. }
  1823. else
  1824. {
  1825. logger.info('[Script] Added to trade $' + ItemsRake[i].price + ' (' + ItemsRake[i].assetid + ')');
  1826. }
  1827. }
  1828.  
  1829. for(var i in ItemsRake)
  1830. {
  1831. if(ItemsRake[i].remove != 1)
  1832. {
  1833. itemsDB.push(ItemsRake[i].assetid);
  1834. }
  1835. }
  1836.  
  1837. var partner;
  1838. if(winner == 1)
  1839. {
  1840. partner = res[0].csteamid;
  1841. }
  1842. else if(winner == 2)
  1843. {
  1844. partner = res[0].psteamid;
  1845. }
  1846.  
  1847. var create = manager.createOffer(partner, link.split('token=')[1]);
  1848. for(var i in itemsDB)
  1849. {
  1850. create.addMyItem({
  1851. "appid": AppID,
  1852. "contextid": ContextID,
  1853. "assetid": itemsDB[i]
  1854. });
  1855. }
  1856.  
  1857. var cod = makeCode();
  1858. create.setMessage('W: ' + cod);
  1859. create.send(function(err, status) {
  1860. if(err)
  1861. {
  1862. console.log(err);
  1863. return;
  1864. }
  1865. else
  1866. {
  1867. var WSteamid;
  1868. var WName;
  1869. pool.query('UPDATE games SET sentItems = 1, wcode = ' + pool.escape(cod) + ' WHERE trade_token = ' + pool.escape(offer.id));
  1870. if(winner == 1)
  1871. {
  1872. WSteamid = res[0].csteamid;
  1873. WName = res[0].cname;
  1874. }
  1875. else if(winner == 2)
  1876. {
  1877. WSteamid = res[0].psteamid;
  1878. WName = res[0].pname;
  1879. }
  1880. console.log('[Winner: ' + WSteamid + ' (' + WName + ') ] Offer #' + create.id + " " + status);
  1881.  
  1882. tradingRequests[create.id] = {
  1883. action: 'winning',
  1884. user: WSteamid
  1885. };
  1886.  
  1887. pool.query('INSERT INTO trades SET user = ' + pool.escape(WSteamid) + ', tid = ' + pool.escape(create.id) + ', status = ' + pool.escape('PendingAccept') + ', action = ' + pool.escape('winning') + ', code = ' + pool.escape(cod) + ', items = ' + pool.escape(itemsDB.join('/')));
  1888.  
  1889. if(gamesPending.hasOwnProperty(offer.id))
  1890. {
  1891. delete gamesPending[offer.id];
  1892. }
  1893. }
  1894. });
  1895.  
  1896. loadAllGames();
  1897. sendGames(res[0].id);
  1898.  
  1899. setTimeout(function() {
  1900. loadStatistics();
  1901. }, 5000);
  1902. });
  1903. }
  1904.  
  1905. //Refresh polldata.json
  1906. manager.on('pollData', function(pollData) {
  1907. fs.writeFile('polldata.json', JSON.stringify(pollData));
  1908. });
  1909.  
  1910. if (fs.existsSync('polldata.json')) {
  1911. manager.pollData = JSON.parse(fs.readFileSync('polldata.json'));
  1912. }
  1913.  
  1914. /*client.on('loggedOn', function(details) {
  1915. console.log('Logged into Steam as ' + client.steamID.getSteam3RenderedID());
  1916. client.setPersona(SteamUser.Steam.EPersonaState.Online,config.botname);
  1917. client.gamesPlayed([AppID])
  1918. });*/
  1919.  
  1920. client.on('webSession', function(sessionID, cookies) {
  1921. manager.setCookies(cookies, function(err) {
  1922. if (err) return console.log(err);
  1923. console.log('Got API key: ' + manager.apiKey);
  1924. app.listen(3030);
  1925. });
  1926.  
  1927. community.setCookies(cookies);
  1928. community.startConfirmationChecker(config.options.confirmationInterval, config.bots[0].identitySecret);
  1929. });
  1930.  
  1931.  
  1932.  
  1933. function time()
  1934. {
  1935. return parseInt(new Date().getTime()/1000);
  1936. }
  1937.  
  1938. function addHistory(socket)
  1939. {
  1940. chatMessages.forEach(function(itm) {
  1941. socket.emit('message', {
  1942. type: 'addMessage',
  1943. msg: itm.message,
  1944. avatar: itm.avatar,
  1945. steamid: itm.steamid,
  1946. rank: itm.rank,
  1947. hide: itm.hide,
  1948. level: itm.level,
  1949. name: itm.name
  1950. });
  1951. })
  1952. }
  1953.  
  1954. function makeCode() {
  1955. var text = "";
  1956. var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  1957.  
  1958. for(var i=0; i < 6; i++)
  1959. text += possible.charAt(Math.floor(Math.random() * possible.length));
  1960.  
  1961. return text;
  1962. }
  1963.  
  1964. function getProxy()
  1965. {
  1966. return "http://" + proxies[random(0,proxies.length-1)];
  1967. }
  1968.  
  1969. function random(min, max) {
  1970. return Math.floor(Math.random() * (max - min + 1)) + min;
  1971. }
  1972.  
  1973. function getInv(user, socket)
  1974. {
  1975. var pret = require('./prices.json');
  1976.  
  1977. var reqOpts = {
  1978. url: 'http://steamcommunity.com/inventory/' + encodeURIComponent(user) + '/' + AppID + '/' + ContextID + '?l=eng&count=5000',
  1979. proxy: getProxy()
  1980. };
  1981.  
  1982. request(reqOpts, function(err, response, body) {
  1983. if(err) throw err;
  1984. if(response && response.statusCode == 200)
  1985. {
  1986. var bodiul = JSON.parse(body);
  1987.  
  1988. var assets = bodiul['assets'];
  1989. var descriptions = bodiul['descriptions'];
  1990.  
  1991. var counter = 0;
  1992.  
  1993. var idss = [];
  1994. var namess = [];
  1995. var pricess = [];
  1996. var imgss = [];
  1997.  
  1998. var Ids = '';
  1999. var Names = '';
  2000. var Prices = '';
  2001. var Imgs = '';
  2002.  
  2003. if(!assets) return;
  2004.  
  2005. assets.forEach(function(valuey, y) {
  2006. descriptions.forEach(function(valuez, z) {
  2007. if(valuey['classid'] == valuez['classid'] && valuey['instanceid'] == valuez['instanceid'])
  2008. {
  2009. var isTradable = valuez['tradable'];
  2010. if(isTradable == 1)
  2011. {
  2012. if(/(Souvenir)/.exec(valuez['market_hash_name'])) return;
  2013.  
  2014. var id = valuey['assetid'];
  2015.  
  2016. var name = valuez['market_hash_name'];
  2017. var price = pret[valuez['market_hash_name']];
  2018. var img = valuez['icon_url'];
  2019.  
  2020. if(price >= minDep)
  2021. {
  2022. idss.push(id);
  2023. namess.push(name);
  2024. pricess.push(price);
  2025. imgss.push(img);
  2026. }
  2027. }
  2028. }
  2029. });
  2030. });
  2031.  
  2032. Ids = idss.join('/');
  2033. Names = namess.join('/');
  2034. Prices = pricess.join('/');
  2035. Imgs = imgss.join('/');
  2036.  
  2037. socket.emit('message', {
  2038. type: 'getInventory',
  2039. id: Ids,
  2040. name: Names,
  2041. price: Prices,
  2042. img: Imgs
  2043. });
  2044.  
  2045. inventoryUser[user] = {
  2046. id: Ids,
  2047. name: Names,
  2048. price: Prices,
  2049. img: Imgs
  2050. }
  2051. }
  2052. else
  2053. {
  2054.  
  2055. }
  2056. });
  2057. }
  2058.  
  2059.  
  2060. function getInv2(user, socket)
  2061. {
  2062. var pret = require('./prices.json');
  2063.  
  2064. var reqOpts = {
  2065. url: 'http://steamcommunity.com/inventory/' + encodeURIComponent(user) + '/' + AppID + '/' + ContextID + '?l=eng&count=5000',
  2066. proxy: getProxy()
  2067. };
  2068.  
  2069. request(reqOpts, function(err, response, body) {
  2070. if(err) throw err;
  2071. if(response && response.statusCode == 200)
  2072. {
  2073. var bodiul = JSON.parse(body);
  2074.  
  2075. var assets = bodiul['assets'];
  2076. var descriptions = bodiul['descriptions'];
  2077.  
  2078. var counter = 0;
  2079.  
  2080. var idss = [];
  2081. var namess = [];
  2082. var pricess = [];
  2083. var imgss = [];
  2084.  
  2085. var Ids = '';
  2086. var Names = '';
  2087. var Prices = '';
  2088. var Imgs = '';
  2089.  
  2090. var Gamess = [];
  2091.  
  2092. if(!assets) return;
  2093.  
  2094. assets.forEach(function(valuey, y) {
  2095. descriptions.forEach(function(valuez, z) {
  2096. if(valuey['classid'] == valuez['classid'] && valuey['instanceid'] == valuez['instanceid'])
  2097. {
  2098. var isTradable = valuez['tradable'];
  2099. if(isTradable == 1)
  2100. {
  2101. if(/(Souvenir)/.exec(valuez['market_hash_name'])) return;
  2102.  
  2103. var id = valuey['assetid'];
  2104.  
  2105. var name = valuez['market_hash_name'];
  2106. var price = pret[valuez['market_hash_name']];
  2107. var img = valuez['icon_url'];
  2108.  
  2109. if(price >= minDep)
  2110. {
  2111. idss.push(id);
  2112. namess.push(name);
  2113. pricess.push(price);
  2114. imgss.push(img);
  2115. }
  2116. }
  2117. }
  2118. });
  2119. });
  2120.  
  2121. Ids = idss.join('/');
  2122. Names = namess.join('/');
  2123. Prices = pricess.join('/');
  2124. Imgs = imgss.join('/');
  2125.  
  2126. socket.emit('message', {
  2127. type: 'getInventory2',
  2128. id: Ids,
  2129. name: Names,
  2130. price: Prices,
  2131. img: Imgs
  2132. });
  2133.  
  2134. inventoryUser[user] = {
  2135. id: Ids,
  2136. name: Names,
  2137. price: Prices,
  2138. img: Imgs
  2139. }
  2140. }
  2141. else
  2142. {
  2143.  
  2144. }
  2145. });
  2146. }
  2147.  
  2148. function loadStatistics(socket)
  2149. {
  2150. var totalAmount = 0;
  2151. var totalItems = 0;
  2152. var activeGames = 0;
  2153.  
  2154. pool.query('SELECT SUM(`ctp`) AS `totalAmount`, SUM(`cskins`) AS `totalItems`, COUNT(`id`) AS `activeGames` FROM games WHERE `winner` = -1', function(error, res) {
  2155. if(error) throw error;
  2156.  
  2157. if(res.length == 0)
  2158. {
  2159. totalAmount = 0;
  2160. totalItems = 0;
  2161. activeGames = 0;
  2162.  
  2163. if(!socket)
  2164. {
  2165. io.sockets.emit('message', {
  2166. type: 'loadStatistics',
  2167. totalAmount: totalAmount,
  2168. totalItems: totalItems,
  2169. activeGames: activeGames
  2170. });
  2171. }
  2172. else
  2173. {
  2174. socket.emit('message', {
  2175. type: 'loadStatistics',
  2176. totalAmount: totalAmount,
  2177. totalItems: totalItems,
  2178. activeGames: activeGames
  2179. });
  2180. }
  2181. }
  2182.  
  2183. if(res.length > 0)
  2184. {
  2185. totalAmount = res[0].totalAmount;
  2186. totalItems = res[0].totalItems;
  2187. activeGames = res[0].activeGames;
  2188.  
  2189. if(totalAmount == null)
  2190. {
  2191. totalAmount = 0;
  2192. }
  2193. if(totalItems == null)
  2194. {
  2195. totalItems = 0;
  2196. }
  2197. if(activeGames == null)
  2198. {
  2199. activeGames = 0;
  2200. }
  2201.  
  2202. if(!socket)
  2203. {
  2204. io.sockets.emit('message', {
  2205. type: 'loadStatistics',
  2206. totalAmount: totalAmount,
  2207. totalItems: totalItems,
  2208. activeGames: activeGames
  2209. });
  2210. }
  2211. else
  2212. {
  2213. socket.emit('message', {
  2214. type: 'loadStatistics',
  2215. totalAmount: totalAmount,
  2216. totalItems: totalItems,
  2217. activeGames: activeGames
  2218. });
  2219. }
  2220. }
  2221. });
  2222. }
  2223.  
  2224. function sendGames(gameID)
  2225. {
  2226. setTimeout(function() {
  2227. for(var i in loadGames)
  2228. {
  2229. if(loadGames[i].id == gameID)
  2230. {
  2231. timerGame[loadGames[i].id] = time()+90;
  2232.  
  2233. io.sockets.emit('message', {
  2234. type: 'editGame',
  2235. games: {
  2236. 'id': loadGames[i].id,
  2237. 'csteamid': loadGames[i].csteamid,
  2238. 'cname': loadGames[i].cname,
  2239. 'cavatar': loadGames[i].cavatar,
  2240. 'cskinsurl': loadGames[i].cskinsurl,
  2241. 'cskinsnames': loadGames[i].cskinsnames,
  2242. 'cskinsprices': loadGames[i].cskinsprices,
  2243. 'cskins': loadGames[i].cskins,
  2244. 'ctp': loadGames[i].ctp,
  2245. 'psteamid': loadGames[i].psteamid,
  2246. 'pname': loadGames[i].pname,
  2247. 'pavatar': loadGames[i].pavatar,
  2248. 'pskinsurl': loadGames[i].pskinsurl,
  2249. 'pskinsnames': loadGames[i].pskinsnames,
  2250. 'pskinsprices': loadGames[i].pskinsprices,
  2251. 'pskins': loadGames[i].pskins,
  2252. 'ptp': loadGames[i].ptp,
  2253. 'hash': loadGames[i].hash,
  2254. 'secret': loadGames[i].secret,
  2255. 'winner': loadGames[i].winner,
  2256. 'timer': timerGame[loadGames[i].id]-time(),
  2257. 'timer11': loadGames[i].timer11,
  2258. 'ttimer11': timer11Game[loadGames[i].id]-time(),
  2259. }
  2260. });
  2261.  
  2262. io.sockets.emit('message', {
  2263. type: 'watchCF',
  2264. id: loadGames[i].id,
  2265. csteamid: loadGames[i].csteamid,
  2266. cname: loadGames[i].cname,
  2267. cavatar: loadGames[i].cavatar,
  2268. cskinsurl: loadGames[i].cskinsurl,
  2269. cskinsnames: loadGames[i].cskinsnames,
  2270. cskinsprices: loadGames[i].cskinsprices,
  2271. cskins: loadGames[i].cskins,
  2272. ctp: loadGames[i].ctp,
  2273. psteamid: loadGames[i].psteamid,
  2274. pname: loadGames[i].pname,
  2275. pavatar: loadGames[i].pavatar,
  2276. pskinsurl: loadGames[i].pskinsurl,
  2277. pskinsnames: loadGames[i].pskinsnames,
  2278. pskinsprices: loadGames[i].pskinsprices,
  2279. pskins: loadGames[i].pskins,
  2280. ptp: loadGames[i].ptp,
  2281. hash: loadGames[i].hash,
  2282. secret: loadGames[i].secret,
  2283. winner: loadGames[i].winner,
  2284. timer: timerGame[loadGames[i].id]-time(),
  2285. timer11: loadGames[i].timer11,
  2286. ttimer11: timer11Game[loadGames[i].id]-time(),
  2287. gameNumber: loadGames[i].id
  2288. });
  2289. }
  2290. }
  2291. }, 1000);
  2292. }
  2293.  
  2294. function LevelCalculate(user)
  2295. {
  2296. pool.query('SELECT xp,level FROM users WHERE steamid = ' + pool.escape(user), function(err, row) {
  2297. if(err) throw err;
  2298. if(row.length == 0) return;
  2299.  
  2300. var currentLevel = row[0].level;
  2301.  
  2302. var currentXp = row[0].xp;
  2303. var xpNeeded = 0;
  2304. var xpMinus = 0;
  2305.  
  2306. for(var i = 1; i < 500; i++)
  2307. {
  2308. xpNeeded += 40 * i;
  2309. xpMinus = xpNeeded - (40 * i-1);
  2310. if(currentXp >= xpMinus && currentXp <= xpNeeded)
  2311. {
  2312. pool.query('UPDATE users SET level = ' + pool.escape(i) + ' WHERE steamid = ' + pool.escape(user));
  2313. }
  2314. }
  2315. });
  2316. }
  2317.  
  2318. function getRandomFloat(min, max) {
  2319. return (Math.random() * (max - min) + min).toFixed(10);
  2320. }
  2321.  
  2322. function handleDisconnect() {
  2323. pool = mysql.createConnection(db_config);
  2324.  
  2325. pool.connect(function(err) {
  2326. if(err) {
  2327. logger.trace('Error: Connecting to database: ', err);
  2328. setTimeout(handleDisconnect, 2000);
  2329. }
  2330. });
  2331.  
  2332. pool.on('error', function(err) {
  2333. logger.trace('Error: Database error: ', err);
  2334. if(err.code === 'PROTOCOL_CONNECTION_LOST') {
  2335. handleDisconnect();
  2336. } else {
  2337. throw err;
  2338. }
  2339. });
  2340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement