Guest User

Untitled

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