Advertisement
Guest User

Untitled

a guest
Jun 12th, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 73.50 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 skinAssets = [];
  1443. var skinImages = [];
  1444. var skinNames = [];
  1445. var skinPrices = [];
  1446. var totalPrice = 0;
  1447.  
  1448. for(var i = 0; i < items.length; i++)
  1449. {
  1450. if(items.length == 1)
  1451. {
  1452. totalSkins = 1;
  1453. totalPrice = PriceOfItem(items[i]);
  1454. }
  1455. else
  1456. {
  1457. totalSkins++;
  1458. totalPrice += PriceOfItem(items[i]);
  1459. }
  1460. skinAssets.push(items[i].assetid);
  1461. skinImages.push(items[i].icon_url);
  1462. skinNames.push(items[i].market_hash_name);
  1463. skinPrices.push(PriceOfItem(items[i]));
  1464. }
  1465.  
  1466. //SMECHERIE
  1467. var Skinassets = "";
  1468. var Skinimages = "";
  1469. var Skinnames = "";
  1470. var Skinprices = "";
  1471. Skinassets = skinAssets.join('/');
  1472. Skinimages = skinImages.join('/');
  1473. Skinnames = skinNames.join('/');
  1474. Skinprices = skinPrices.join('/');
  1475.  
  1476. var timp = new Date();
  1477. var timpCalc = timp.getTime()/1000;
  1478.  
  1479. var name;
  1480. var avatar;
  1481.  
  1482. pool.query('SELECT tradelink,name,avatar FROM users WHERE steamid = ' + pool.escape(depositSteamTrades[depositTrades.indexOf(offer.id)]), function(error, results) {
  1483. if(error) throw error;
  1484.  
  1485. //GENERATE a SECRET
  1486. var hashul = createHash();
  1487.  
  1488. function createHash() {
  1489. var text = "";
  1490. var possible = "0123456789";
  1491.  
  1492. for(var i=0; i < 16; i++)
  1493. text += possible.charAt(Math.floor(Math.random() * possible.length));
  1494.  
  1495. return text;
  1496. }
  1497.  
  1498. if(io.sockets.connected[socketBySteam[depositSteamTrades[depositTrades.indexOf(offer.id)]]['info']])
  1499. {
  1500. io.sockets.connected[socketBySteam[depositSteamTrades[depositTrades.indexOf(offer.id)]]['info']].emit('message', {
  1501. type: 'modals',
  1502. tip: 'trade',
  1503. result: 'offerAccepted'
  1504. });
  1505. }
  1506.  
  1507.  
  1508. pool.query('UPDATE users SET xp = xp + ' + pool.escape(totalPrice*100) + ' WHERE steamid = ' + pool.escape(depositSteamTrades[depositTrades.indexOf(offer.id)]), function(haha, huhu) {
  1509. if(haha) throw haha;
  1510. if(huhu.length == 0) return;
  1511.  
  1512. LevelCalculate(depositSteamTrades[depositTrades.indexOf(offer.id)]);
  1513. });
  1514.  
  1515. pool.query('UPDATE trades SET status = ' + pool.escape('Accepted') + ' WHERE tid = ' + pool.escape(offer.id));
  1516.  
  1517. pool.query('INSERT INTO games SET cassetids = ' + pool.escape(Skinassets) + ', 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) {
  1518. if(arr) throw arr;
  1519.  
  1520. pool.query('SELECT MAX(id) AS cacat FROM games', function(caca, rara) {
  1521. if(caca) throw caca;
  1522.  
  1523. loadAllGames();
  1524. loadStatistics();
  1525. setTimeout(function() {
  1526. loadGames.forEach(function(itm) {
  1527. if(itm.id == rara[0].cacat)
  1528. {
  1529. io.sockets.emit('message', {
  1530. type: 'addGame',
  1531. games: {
  1532. 'id': itm.id,
  1533. 'csteamid': itm.csteamid,
  1534. 'cname': itm.cname,
  1535. 'cavatar': itm.cavatar,
  1536. 'cskinsurl': itm.cskinsurl,
  1537. 'cskinsnames': itm.cskinsnames,
  1538. 'cskinsprices': itm.cskinsprices,
  1539. 'cskins': itm.cskins,
  1540. 'ctp': itm.ctp,
  1541. 'psteamid': itm.psteamid,
  1542. 'pname': itm.pname,
  1543. 'pavatar': itm.pavatar,
  1544. 'pskinsurl': itm.pskinsurl,
  1545. 'pskinsnames': itm.pskinsnames,
  1546. 'pskinsprices': itm.pskinsprices,
  1547. 'pskins': itm.pskins,
  1548. 'ptp': itm.ptp,
  1549. 'hash': itm.hash,
  1550. 'secret': itm.secret,
  1551. 'winner': itm.winner,
  1552. 'timer': timerGame[itm.id]-time(),
  1553. 'timer11': itm.timer11,
  1554. 'ttimer11': timer11Game[itm.id]-time()
  1555. }
  1556. });
  1557. }
  1558. });
  1559. }, 1200);
  1560. });
  1561. });
  1562. });
  1563. });
  1564. }
  1565. else if(tradingRequests.hasOwnProperty(offer.id) && tradingRequests[offer.id]['action'] == 'joingame')
  1566. {
  1567. pool.query('SELECT pskinsnames,secret,hash,id,ctp,ptp,psteamid FROM games WHERE trade_token = ' + pool.escape(offer.id), function(err, res) {
  1568. if(err) throw err;
  1569.  
  1570. if(!res[0])
  1571. {
  1572. declineOffer(offer);
  1573. return;
  1574. }
  1575.  
  1576. var receivedItems = [];
  1577. var receivedNames = [];
  1578. var receivedPrices = [];
  1579. var receivedImages = [];
  1580.  
  1581. offer.getReceivedItems(true, function(err, caca) {
  1582. if(err) throw err;
  1583.  
  1584. for(var i in caca)
  1585. {
  1586. receivedItems.push(caca[i].assetid);
  1587. receivedNames.push(caca[i].market_hash_name);
  1588. receivedPrices.push(PriceOfItem(caca[i]));
  1589. receivedImages.push(caca[i].icon_url);
  1590. }
  1591.  
  1592. pool.query('UPDATE games SET passetids = ' + pool.escape(receivedItems.join('/')) + ', pskinsnames = ' + pool.escape(receivedNames.join('/')) + ', pskinsprices = ' + pool.escape(receivedPrices.join('/')) + ', pskinsurl = ' + pool.escape(receivedImages.join('/')) + ' WHERE trade_token = ' + pool.escape(offer.id));
  1593.  
  1594.  
  1595. //GENERATE a SECRET
  1596. var winner;
  1597. var secret = createSecret();
  1598. var forWinner = (Math.random() * ((res[0].ctp+res[0].ptp) - 0.01) + 0.01).toFixed(2);
  1599.  
  1600. if(forWinner >= 0.01 && forWinner <= res[0].ctp)
  1601. {
  1602. winner = 1;
  1603. logger.info(forWinner + 'winner 1');
  1604. }
  1605. else if(forWinner >= (res[0].ctp+0.01) && forWinner <= (res[0].ctp+res[0].ptp))
  1606. {
  1607. winner = 2;
  1608. logger.info(forWinner + 'winner 2');
  1609. }
  1610. var secret = createSecret();
  1611. function createSecret() {
  1612. var text = "";
  1613. var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_/()|";
  1614.  
  1615. for(var i=0; i < 8; i++)
  1616. text += possible.charAt(Math.floor(Math.random() * possible.length));
  1617.  
  1618. return text;
  1619. }
  1620. var hashul = res[0].hash;
  1621.  
  1622. var secretsihash = secret.concat(hashul);
  1623. pool.query('UPDATE games SET timer11 = 1, secret = ' + pool.escape(secret) + ' WHERE trade_token = ' + pool.escape(offer.id), function(aaa, bbb) {
  1624. if(aaa) throw aaa;
  1625.  
  1626. pool.query('UPDATE users SET xp = xp + ' + pool.escape(res[0].ptp*100) + ' WHERE steamid = ' + pool.escape(res[0].psteamid), function(haha, huhu) {
  1627. if(haha) throw haha;
  1628. if(huhu.length == 0) return;
  1629.  
  1630. LevelCalculate(res[0].psteamid);
  1631. });
  1632.  
  1633. loadAllGames();
  1634. sendGames(res[0].id);
  1635. });
  1636.  
  1637. pool.query('UPDATE trades SET status = ' + pool.escape('Accepted') + ' WHERE tid = ' + pool.escape(offer.id));
  1638.  
  1639. timer11Game[res[0].id] = time()+10;
  1640.  
  1641. if(io.sockets.connected[socketBySteam[res[0].psteamid]['info']])
  1642. {
  1643. io.sockets.connected[socketBySteam[res[0].psteamid]['info']].emit('message', {
  1644. type: 'modals',
  1645. tip: 'trade',
  1646. result: 'offerAccepted'
  1647. });
  1648. }
  1649.  
  1650. setTimeout(function() {
  1651. pool.query('UPDATE games SET winner = ' + pool.escape(winner) + ' WHERE trade_token = ' + pool.escape(offer.id), function(errr, ress) {
  1652. if(errr) throw errr;
  1653.  
  1654. if(winner == 1)
  1655. {
  1656. pool.query('SELECT csteamid AS steamid FROM games WHERE trade_token = ' + pool.escape(offer.id), function(arr, ass) {
  1657. if(arr) throw arr;
  1658.  
  1659. pool.query('SELECT tradelink FROM users WHERE steamid = ' + pool.escape(ass[0].steamid), function(arr2, ass2) {
  1660. if(arr2) throw arr2;
  1661. sendItemsTrade(offer, ass2[0].tradelink, winner);
  1662. });
  1663. });
  1664. }
  1665. else if(winner == 2)
  1666. {
  1667.  
  1668. pool.query('SELECT tradelink FROM users WHERE steamid = ' + pool.escape(gamesPending[offer.id]), function(arr2, ass2) {
  1669. if(arr2) throw arr2;
  1670. sendItemsTrade(offer, ass2[0].tradelink, winner);
  1671. });
  1672. }
  1673. });
  1674. }, 11000);
  1675. });
  1676. });
  1677. }
  1678. }
  1679. else if(oldState == 2 && offer.state == 7)
  1680. {
  1681. pool.query('SELECT action,user FROM trades WHERE tid = ' + pool.escape(offer.id), function(caca, maca) {
  1682. if(caca) throw caca;
  1683. if(maca.length == 0) return;
  1684.  
  1685. if(maca[0].action == 'winning') return;
  1686. pool.query('UPDATE trades SET status = ' + pool.escape('Declined') + ' WHERE tid = ' + pool.escape(offer.id));
  1687. if(io.sockets.connected[socketBySteam[maca[0].user]['info']])
  1688. {
  1689. io.sockets.connected[socketBySteam[maca[0].user]['info']].emit('message', {
  1690. type: 'modals',
  1691. tip: 'trade',
  1692. result: 'offerDeclined'
  1693. });
  1694. }
  1695. });
  1696. }
  1697. else if(oldState == 2 && (offer.state == 6 || offer.state == 8 || offer.state == 5 || offer.state == 4 || offer.state == 10))
  1698. {
  1699. pool.query('SELECT action,user FROM trades WHERE tid = ' + pool.escape(offer.id), function(caca, maca) {
  1700. if(caca) throw caca;
  1701. if(maca.length == 0) return;
  1702.  
  1703. if(maca[0].action == 'winning') return;
  1704. pool.query('UPDATE trades SET status = ' + pool.escape('Cancelled') + ' WHERE tid = ' + pool.escape(offer.id));
  1705. });
  1706. }
  1707. }
  1708. else if(offer.isOurOffer && offer.itemsToGive.length > 0)
  1709. {
  1710. if((oldState == 2 || oldState == 9) && offer.state == 3)
  1711. {
  1712. if(tradingRequests.hasOwnProperty(offer.id) && tradingRequests[offer.id]['action'] == 'winning')
  1713. {
  1714. pool.query('SELECT user FROM trades WHERE tid = ' + pool.escape(offer.id), function(caca, maca) {
  1715. if(caca) throw caca;
  1716. if(maca.length == 0) return;
  1717.  
  1718. if(io.sockets.connected[socketBySteam[maca[0].user]['info']])
  1719. {
  1720. io.sockets.connected[socketBySteam[maca[0].user]['info']].emit('message', {
  1721. type: 'modals',
  1722. tip: 'trade',
  1723. result: 'offerAccepted'
  1724. });
  1725. }
  1726. pool.query('UPDATE trades SET status = ' + pool.escape('Accepted') + ' WHERE tid = ' + pool.escape(offer.id));
  1727. });
  1728. }
  1729. }
  1730. else if((oldState == 2 || oldState == 9) && offer.state == 7)
  1731. {
  1732. pool.query('SELECT action,user FROM trades WHERE tid = ' + pool.escape(offer.id), function(caca, maca) {
  1733. if(caca) throw caca;
  1734. if(maca.length == 0) return;
  1735.  
  1736. if(maca[0].action == 'winning') return;
  1737. pool.query('UPDATE trades SET status = ' + pool.escape('Declined') + ' WHERE tid = ' + pool.escape(offer.id));
  1738. if(io.sockets.connected[socketBySteam[maca[0].user]['info']])
  1739. {
  1740. io.sockets.connected[socketBySteam[maca[0].user]['info']].emit('message', {
  1741. type: 'modals',
  1742. tip: 'trade',
  1743. result: 'offerDeclined'
  1744. });
  1745. }
  1746. });
  1747. }
  1748. else if((oldState == 2 || oldState == 9) && (offer.state == 6 || offer.state == 8 || offer.state == 5 || offer.state == 4 || offer.state == 10))
  1749. {
  1750. pool.query('SELECT action,user FROM trades WHERE tid = ' + pool.escape(offer.id), function(caca, maca) {
  1751. if(caca) throw caca;
  1752. if(maca.length == 0) return;
  1753.  
  1754. if(maca[0].action == 'winning') return;
  1755. pool.query('UPDATE trades SET status = ' + pool.escape('Cancelled') + ' WHERE tid = ' + pool.escape(offer.id));
  1756. });
  1757. }
  1758. }
  1759. }
  1760. });
  1761.  
  1762. function sendItemsTrade(offer, link, winner)
  1763. {
  1764. 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) {
  1765. if(err) throw err;
  1766.  
  1767. setTimeout(function() {
  1768. hideGame(res[0].id);
  1769. }, 60000);
  1770.  
  1771. var ItemsRake = [];
  1772.  
  1773. var Skins1Names = res[0].cskinsnames.split('/');
  1774. var Skins2Names = res[0].pskinsnames.split('/');
  1775. var TotalSkinsNames = Skins1Names.concat(Skins2Names);
  1776.  
  1777. var Skins1 = res[0].cassetids.split('/');
  1778. var Skins2 = res[0].passetids.split('/');
  1779. var allSkins = Skins1.concat(Skins2);
  1780.  
  1781. var Prices1 = res[0].cskinsprices.split('/');
  1782. var Prices2 = res[0].pskinsprices.split('/');
  1783. var allPrices = Prices1.concat(Prices2);
  1784.  
  1785. var itemsDB = [];
  1786.  
  1787. for(var i in allSkins)
  1788. {
  1789. ItemsRake.push({
  1790. assetid: allSkins[i],
  1791. price: allPrices[i]
  1792. });
  1793. }
  1794.  
  1795. ItemsRake.sort(compare);
  1796.  
  1797. function compare(a,b) {
  1798. if (a.price > b.price)
  1799. return -1;
  1800. if (a.price < b.price)
  1801. return 1;
  1802. return 0;
  1803. }
  1804.  
  1805. var maxComission = percentRake/100*(res[0].ctp+res[0].ptp);
  1806. var currComission = 0.00;
  1807.  
  1808. for(var i = 0; i < ItemsRake.length; i++)
  1809. {
  1810. if(ItemsRake[i].price <= maxComission && currComission <= maxComission)
  1811. {
  1812. if(i == 0)
  1813. {
  1814. currComission += parseFloat(ItemsRake[i].price);
  1815. ItemsRake[i].remove = 1;
  1816. logger.info('[Script] Removed $' + ItemsRake[i].price + ' (' + ItemsRake[i].assetid + ' - ' + ItemsRake[i].name + ')');
  1817. }
  1818. else
  1819. {
  1820. var newCom = currComission + parseFloat(ItemsRake[i].price);
  1821. if(newCom > maxComission)
  1822. {
  1823. logger.info('[Script] Added to trade $' + ItemsRake[i].price + ' (' + ItemsRake[i].assetid + ' - ' + ItemsRake[i].name + ')');
  1824. }
  1825. else
  1826. {
  1827. currComission += parseFloat(ItemsRake[i].price);
  1828. ItemsRake[i].remove = 1;
  1829. logger.info('[Script] Removed $' + ItemsRake[i].price + ' (' + ItemsRake[i].assetid + ' - ' + ItemsRake[i].name + ')');
  1830. }
  1831. }
  1832. }
  1833. else
  1834. {
  1835. logger.info('[Script] Added to trade $' + ItemsRake[i].price + ' (' + ItemsRake[i].assetid + ' - ' + ItemsRake[i].name + ')');
  1836. }
  1837. }
  1838.  
  1839. for(var i in ItemsRake)
  1840. {
  1841. if(ItemsRake[i].remove != 1)
  1842. {
  1843. itemsDB.push(ItemsRake[i].assetid);
  1844. }
  1845. }
  1846.  
  1847. var partner;
  1848. if(winner == 1)
  1849. {
  1850. partner = res[0].csteamid;
  1851. }
  1852. else if(winner == 2)
  1853. {
  1854. partner = res[0].psteamid;
  1855. }
  1856.  
  1857. var create = manager.createOffer(partner, link.split('token=')[1]);
  1858. for(var i in itemsDB)
  1859. {
  1860. create.addMyItem({
  1861. "appid": AppID,
  1862. "contextid": ContextID,
  1863. "assetid": itemsDB[i]
  1864. });
  1865. }
  1866.  
  1867. var cod = makeCode();
  1868. create.setMessage('W: ' + cod);
  1869. create.send(function(err, status) {
  1870. if(err)
  1871. {
  1872. console.log(err);
  1873. return;
  1874. }
  1875. else
  1876. {
  1877. var WSteamid;
  1878. var WName;
  1879. pool.query('UPDATE games SET sentItems = 1, wcode = ' + pool.escape(cod) + ' WHERE trade_token = ' + pool.escape(offer.id));
  1880. if(winner == 1)
  1881. {
  1882. WSteamid = res[0].csteamid;
  1883. WName = res[0].cname;
  1884. }
  1885. else if(winner == 2)
  1886. {
  1887. WSteamid = res[0].psteamid;
  1888. WName = res[0].pname;
  1889. }
  1890. console.log('[Winner: ' + WSteamid + ' (' + WName + ') ] Offer #' + create.id + " " + status);
  1891.  
  1892. tradingRequests[create.id] = {
  1893. action: 'winning',
  1894. user: WSteamid
  1895. };
  1896.  
  1897. 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('/')));
  1898.  
  1899. if(gamesPending.hasOwnProperty(offer.id))
  1900. {
  1901. delete gamesPending[offer.id];
  1902. }
  1903. }
  1904. });
  1905.  
  1906. loadAllGames();
  1907. sendGames(res[0].id);
  1908.  
  1909. setTimeout(function() {
  1910. loadStatistics();
  1911. }, 5000);
  1912. });
  1913. }
  1914.  
  1915. //Refresh polldata.json
  1916. manager.on('pollData', function(pollData) {
  1917. fs.writeFile('polldata.json', JSON.stringify(pollData));
  1918. });
  1919.  
  1920. if (fs.existsSync('polldata.json')) {
  1921. manager.pollData = JSON.parse(fs.readFileSync('polldata.json'));
  1922. }
  1923.  
  1924. /*client.on('loggedOn', function(details) {
  1925. console.log('Logged into Steam as ' + client.steamID.getSteam3RenderedID());
  1926. client.setPersona(SteamUser.Steam.EPersonaState.Online,config.botname);
  1927. client.gamesPlayed([AppID])
  1928. });*/
  1929.  
  1930. client.on('webSession', function(sessionID, cookies) {
  1931. manager.setCookies(cookies, function(err) {
  1932. if (err) return console.log(err);
  1933. console.log('Got API key: ' + manager.apiKey);
  1934. app.listen(3030);
  1935. });
  1936.  
  1937. community.setCookies(cookies);
  1938. community.startConfirmationChecker(config.options.confirmationInterval, config.bots[0].identitySecret);
  1939. });
  1940.  
  1941.  
  1942.  
  1943. function time()
  1944. {
  1945. return parseInt(new Date().getTime()/1000);
  1946. }
  1947.  
  1948. function addHistory(socket)
  1949. {
  1950. chatMessages.forEach(function(itm) {
  1951. socket.emit('message', {
  1952. type: 'addMessage',
  1953. msg: itm.message,
  1954. avatar: itm.avatar,
  1955. steamid: itm.steamid,
  1956. rank: itm.rank,
  1957. hide: itm.hide,
  1958. level: itm.level,
  1959. name: itm.name
  1960. });
  1961. })
  1962. }
  1963.  
  1964. function makeCode() {
  1965. var text = "";
  1966. var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  1967.  
  1968. for(var i=0; i < 6; i++)
  1969. text += possible.charAt(Math.floor(Math.random() * possible.length));
  1970.  
  1971. return text;
  1972. }
  1973.  
  1974. function getProxy()
  1975. {
  1976. return "http://" + proxies[random(0,proxies.length-1)];
  1977. }
  1978.  
  1979. function random(min, max) {
  1980. return Math.floor(Math.random() * (max - min + 1)) + min;
  1981. }
  1982.  
  1983. function getInv(user, socket)
  1984. {
  1985. var pret = require('./prices.json');
  1986.  
  1987. var reqOpts = {
  1988. url: 'http://steamcommunity.com/inventory/' + encodeURIComponent(user) + '/' + AppID + '/' + ContextID + '?l=eng&count=5000',
  1989. proxy: getProxy()
  1990. };
  1991.  
  1992. request(reqOpts, function(err, response, body) {
  1993. if(err) throw err;
  1994. if(response && response.statusCode == 200)
  1995. {
  1996. var bodiul = JSON.parse(body);
  1997.  
  1998. var assets = bodiul['assets'];
  1999. var descriptions = bodiul['descriptions'];
  2000.  
  2001. var counter = 0;
  2002.  
  2003. var idss = [];
  2004. var namess = [];
  2005. var pricess = [];
  2006. var imgss = [];
  2007.  
  2008. var Ids = '';
  2009. var Names = '';
  2010. var Prices = '';
  2011. var Imgs = '';
  2012.  
  2013. if(!assets) return;
  2014.  
  2015. assets.forEach(function(valuey, y) {
  2016. descriptions.forEach(function(valuez, z) {
  2017. if(valuey['classid'] == valuez['classid'] && valuey['instanceid'] == valuez['instanceid'])
  2018. {
  2019. var isTradable = valuez['tradable'];
  2020. if(isTradable == 1)
  2021. {
  2022. if(/(Souvenir)/.exec(valuez['market_hash_name'])) return;
  2023.  
  2024. var id = valuey['assetid'];
  2025.  
  2026. var name = valuez['market_hash_name'];
  2027. var price = pret[valuez['market_hash_name']];
  2028. var img = valuez['icon_url'];
  2029.  
  2030. if(price >= minDep)
  2031. {
  2032. idss.push(id);
  2033. namess.push(name);
  2034. pricess.push(price);
  2035. imgss.push(img);
  2036. }
  2037. }
  2038. }
  2039. });
  2040. });
  2041.  
  2042. Ids = idss.join('/');
  2043. Names = namess.join('/');
  2044. Prices = pricess.join('/');
  2045. Imgs = imgss.join('/');
  2046.  
  2047. socket.emit('message', {
  2048. type: 'getInventory',
  2049. id: Ids,
  2050. name: Names,
  2051. price: Prices,
  2052. img: Imgs
  2053. });
  2054.  
  2055. inventoryUser[user] = {
  2056. id: Ids,
  2057. name: Names,
  2058. price: Prices,
  2059. img: Imgs
  2060. }
  2061. }
  2062. else
  2063. {
  2064.  
  2065. }
  2066. });
  2067. }
  2068.  
  2069.  
  2070. function getInv2(user, socket)
  2071. {
  2072. var pret = require('./prices.json');
  2073.  
  2074. var reqOpts = {
  2075. url: 'http://steamcommunity.com/inventory/' + encodeURIComponent(user) + '/' + AppID + '/' + ContextID + '?l=eng&count=5000',
  2076. proxy: getProxy()
  2077. };
  2078.  
  2079. request(reqOpts, function(err, response, body) {
  2080. if(err) throw err;
  2081. if(response && response.statusCode == 200)
  2082. {
  2083. var bodiul = JSON.parse(body);
  2084.  
  2085. var assets = bodiul['assets'];
  2086. var descriptions = bodiul['descriptions'];
  2087.  
  2088. var counter = 0;
  2089.  
  2090. var idss = [];
  2091. var namess = [];
  2092. var pricess = [];
  2093. var imgss = [];
  2094.  
  2095. var Ids = '';
  2096. var Names = '';
  2097. var Prices = '';
  2098. var Imgs = '';
  2099.  
  2100. var Gamess = [];
  2101.  
  2102. if(!assets) return;
  2103.  
  2104. assets.forEach(function(valuey, y) {
  2105. descriptions.forEach(function(valuez, z) {
  2106. if(valuey['classid'] == valuez['classid'] && valuey['instanceid'] == valuez['instanceid'])
  2107. {
  2108. var isTradable = valuez['tradable'];
  2109. if(isTradable == 1)
  2110. {
  2111. if(/(Souvenir)/.exec(valuez['market_hash_name'])) return;
  2112.  
  2113. var id = valuey['assetid'];
  2114.  
  2115. var name = valuez['market_hash_name'];
  2116. var price = pret[valuez['market_hash_name']];
  2117. var img = valuez['icon_url'];
  2118.  
  2119. if(price >= minDep)
  2120. {
  2121. idss.push(id);
  2122. namess.push(name);
  2123. pricess.push(price);
  2124. imgss.push(img);
  2125. }
  2126. }
  2127. }
  2128. });
  2129. });
  2130.  
  2131. Ids = idss.join('/');
  2132. Names = namess.join('/');
  2133. Prices = pricess.join('/');
  2134. Imgs = imgss.join('/');
  2135.  
  2136. socket.emit('message', {
  2137. type: 'getInventory2',
  2138. id: Ids,
  2139. name: Names,
  2140. price: Prices,
  2141. img: Imgs
  2142. });
  2143.  
  2144. inventoryUser[user] = {
  2145. id: Ids,
  2146. name: Names,
  2147. price: Prices,
  2148. img: Imgs
  2149. }
  2150. }
  2151. else
  2152. {
  2153.  
  2154. }
  2155. });
  2156. }
  2157.  
  2158. function loadStatistics(socket)
  2159. {
  2160. var totalAmount = 0;
  2161. var totalItems = 0;
  2162. var activeGames = 0;
  2163.  
  2164. pool.query('SELECT SUM(`ctp`) AS `totalAmount`, SUM(`cskins`) AS `totalItems`, COUNT(`id`) AS `activeGames` FROM games WHERE `winner` = -1', function(error, res) {
  2165. if(error) throw error;
  2166.  
  2167. if(res.length == 0)
  2168. {
  2169. totalAmount = 0;
  2170. totalItems = 0;
  2171. activeGames = 0;
  2172.  
  2173. if(!socket)
  2174. {
  2175. io.sockets.emit('message', {
  2176. type: 'loadStatistics',
  2177. totalAmount: totalAmount,
  2178. totalItems: totalItems,
  2179. activeGames: activeGames
  2180. });
  2181. }
  2182. else
  2183. {
  2184. socket.emit('message', {
  2185. type: 'loadStatistics',
  2186. totalAmount: totalAmount,
  2187. totalItems: totalItems,
  2188. activeGames: activeGames
  2189. });
  2190. }
  2191. }
  2192.  
  2193. if(res.length > 0)
  2194. {
  2195. totalAmount = res[0].totalAmount;
  2196. totalItems = res[0].totalItems;
  2197. activeGames = res[0].activeGames;
  2198.  
  2199. if(totalAmount == null)
  2200. {
  2201. totalAmount = 0;
  2202. }
  2203. if(totalItems == null)
  2204. {
  2205. totalItems = 0;
  2206. }
  2207. if(activeGames == null)
  2208. {
  2209. activeGames = 0;
  2210. }
  2211.  
  2212. if(!socket)
  2213. {
  2214. io.sockets.emit('message', {
  2215. type: 'loadStatistics',
  2216. totalAmount: totalAmount,
  2217. totalItems: totalItems,
  2218. activeGames: activeGames
  2219. });
  2220. }
  2221. else
  2222. {
  2223. socket.emit('message', {
  2224. type: 'loadStatistics',
  2225. totalAmount: totalAmount,
  2226. totalItems: totalItems,
  2227. activeGames: activeGames
  2228. });
  2229. }
  2230. }
  2231. });
  2232. }
  2233.  
  2234. function sendGames(gameID)
  2235. {
  2236. setTimeout(function() {
  2237. for(var i in loadGames)
  2238. {
  2239. if(loadGames[i].id == gameID)
  2240. {
  2241. timerGame[loadGames[i].id] = time()+90;
  2242.  
  2243. io.sockets.emit('message', {
  2244. type: 'editGame',
  2245. games: {
  2246. 'id': loadGames[i].id,
  2247. 'csteamid': loadGames[i].csteamid,
  2248. 'cname': loadGames[i].cname,
  2249. 'cavatar': loadGames[i].cavatar,
  2250. 'cskinsurl': loadGames[i].cskinsurl,
  2251. 'cskinsnames': loadGames[i].cskinsnames,
  2252. 'cskinsprices': loadGames[i].cskinsprices,
  2253. 'cskins': loadGames[i].cskins,
  2254. 'ctp': loadGames[i].ctp,
  2255. 'psteamid': loadGames[i].psteamid,
  2256. 'pname': loadGames[i].pname,
  2257. 'pavatar': loadGames[i].pavatar,
  2258. 'pskinsurl': loadGames[i].pskinsurl,
  2259. 'pskinsnames': loadGames[i].pskinsnames,
  2260. 'pskinsprices': loadGames[i].pskinsprices,
  2261. 'pskins': loadGames[i].pskins,
  2262. 'ptp': loadGames[i].ptp,
  2263. 'hash': loadGames[i].hash,
  2264. 'secret': loadGames[i].secret,
  2265. 'winner': loadGames[i].winner,
  2266. 'timer': timerGame[loadGames[i].id]-time(),
  2267. 'timer11': loadGames[i].timer11,
  2268. 'ttimer11': timer11Game[loadGames[i].id]-time(),
  2269. }
  2270. });
  2271.  
  2272. io.sockets.emit('message', {
  2273. type: 'watchCF',
  2274. id: loadGames[i].id,
  2275. csteamid: loadGames[i].csteamid,
  2276. cname: loadGames[i].cname,
  2277. cavatar: loadGames[i].cavatar,
  2278. cskinsurl: loadGames[i].cskinsurl,
  2279. cskinsnames: loadGames[i].cskinsnames,
  2280. cskinsprices: loadGames[i].cskinsprices,
  2281. cskins: loadGames[i].cskins,
  2282. ctp: loadGames[i].ctp,
  2283. psteamid: loadGames[i].psteamid,
  2284. pname: loadGames[i].pname,
  2285. pavatar: loadGames[i].pavatar,
  2286. pskinsurl: loadGames[i].pskinsurl,
  2287. pskinsnames: loadGames[i].pskinsnames,
  2288. pskinsprices: loadGames[i].pskinsprices,
  2289. pskins: loadGames[i].pskins,
  2290. ptp: loadGames[i].ptp,
  2291. hash: loadGames[i].hash,
  2292. secret: loadGames[i].secret,
  2293. winner: loadGames[i].winner,
  2294. timer: timerGame[loadGames[i].id]-time(),
  2295. timer11: loadGames[i].timer11,
  2296. ttimer11: timer11Game[loadGames[i].id]-time(),
  2297. gameNumber: loadGames[i].id
  2298. });
  2299. }
  2300. }
  2301. }, 1000);
  2302. }
  2303.  
  2304. function LevelCalculate(user)
  2305. {
  2306. pool.query('SELECT xp,level FROM users WHERE steamid = ' + pool.escape(user), function(err, row) {
  2307. if(err) throw err;
  2308. if(row.length == 0) return;
  2309.  
  2310. var currentLevel = row[0].level;
  2311.  
  2312. var currentXp = row[0].xp;
  2313. var xpNeeded = 0;
  2314. var xpMinus = 0;
  2315.  
  2316. for(var i = 1; i < 500; i++)
  2317. {
  2318. xpNeeded += 40 * i;
  2319. xpMinus = xpNeeded - (40 * i-1);
  2320. if(currentXp >= xpMinus && currentXp <= xpNeeded)
  2321. {
  2322. pool.query('UPDATE users SET level = ' + pool.escape(i) + ' WHERE steamid = ' + pool.escape(user));
  2323. }
  2324. }
  2325. });
  2326. }
  2327.  
  2328. function getRandomFloat(min, max) {
  2329. return (Math.random() * (max - min) + min).toFixed(10);
  2330. }
  2331.  
  2332. function handleDisconnect() {
  2333. pool = mysql.createConnection(db_config);
  2334.  
  2335. pool.connect(function(err) {
  2336. if(err) {
  2337. logger.trace('Error: Connecting to database: ', err);
  2338. setTimeout(handleDisconnect, 2000);
  2339. }
  2340. });
  2341.  
  2342. pool.on('error', function(err) {
  2343. logger.trace('Error: Database error: ', err);
  2344. if(err.code === 'PROTOCOL_CONNECTION_LOST') {
  2345. handleDisconnect();
  2346. } else {
  2347. throw err;
  2348. }
  2349. });
  2350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement