Advertisement
Guest User

cf.js

a guest
Oct 27th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 99.59 KB | None | 0 0
  1. var fs = require('fs');
  2. var sslOptions = {
  3. key: fs.readFileSync('/etc/letsencrypt/live/vgopunk.com/privkey.pem'),
  4. cert: fs.readFileSync('/etc/letsencrypt/live/vgopunk.com/cert.pem'),
  5. requestCert: true,
  6. rejectUnauthorized: false
  7. };
  8. var ExpressTrade = require('expresstrade');
  9. var fs = require('fs');
  10. var ip = require('ip');
  11. var request = require('request');
  12. var config = require('./config.json');
  13. var CryptoJS = require("crypto-js");
  14. var log4js = require('log4js');
  15. var https = require('https');
  16. var express = require('express');
  17. var redis = require('redis');
  18. var RandomOrg = require('random-org');
  19. var client = redis.createClient();
  20. var app = express();
  21. var redisClient = redis.createClient(),
  22. client = redis.createClient();
  23. var server = https.createServer(sslOptions, app).listen(3001);
  24. var io = require('socket.io')(server);
  25. var logger = log4js.getLogger();
  26. var mysql = require('mysql');
  27.  
  28. var db_config = {
  29. //debug: true,
  30. host: config.options.sql['host'],
  31. user: config.options.sql['username'],
  32. password: config.options.sql['password'],
  33. database: config.options.sql['database']
  34. };
  35. var pool;
  36.  
  37. //GAME INFO
  38. var apiKey = config.bots[0].apiKey;
  39. var twoFactor = config.bots[0].twoFactorCode;
  40. var minDep = config.options.minDeposit;
  41. var percentRake = 10;
  42.  
  43. var Ownerid = config.options.ownerSteamID;
  44.  
  45. var ET = new ExpressTrade({
  46. apikey: apiKey,
  47. twofactorsecret: twoFactor,
  48. pollInterval: 5
  49. });
  50.  
  51. ET.on('offerReceived', (_offer) => {
  52. var offer = _offer;
  53. if(offer.message == 'Shop' && offer.is_gift == true) {
  54. console.log('Accepting Shop Items');
  55. var items = offer.sender.items;
  56. ET.ITrade.AcceptOffer({offer_id: offer.id});
  57. items.forEach(function(item) {
  58. pool.query('INSERT INTO `shop` SET `classid` = ' + pool.escape(item.id) + ', `name` = ' + pool.escape(item.name) + ', `price` = ' + pool.escape(item.suggested_price) + ', `img` = ' + pool.escape(item.image['300px']) + ', `color` = ' + pool.escape(item.color) + '');
  59. });
  60. } else {
  61. ET.ITrade.CancelOffer({offer_id: offer.id});
  62. }
  63. });
  64.  
  65. const redisChannels = {
  66. depositTrades: 'deposit.trades',
  67. jackpotUsers: 'jackpot.users'
  68. }
  69.  
  70. var deletingTrades = [];
  71.  
  72. var inventoryTimer = {};
  73. var socketBySteam = {};
  74.  
  75. var loadGames = [];
  76. var timerGame = {};
  77. var timer11Game = {};
  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. var connection = 0;
  93.  
  94. //JACKPOT'
  95. var closejackpotbets = 0;
  96. var usersPot = [];
  97. var jackpotID;
  98. var usersInPot = 0;
  99. var jackpotHash = "";
  100. var jackpotSecret = "";
  101. var jackpotRound = -1;
  102. var jackpotState = 'NONE';
  103. var jackpotTickets = 0;
  104. var jackpotTotalTickets = [];
  105. var totalJackpotItems = 0;
  106. var totalJackpotValue = 0;
  107. var totalSecondsJackpot = 0;
  108. var usersPrices = {};
  109. var checkJackpotEnd;
  110. var jackpotPRake = 10;
  111. var pickWinner = false;
  112. var pickSteamID = '';
  113.  
  114. var IMP_0x5e24=['parse','ips','forEach','nEHYG','XOTLN','NMAKt','aEtBS','eeTZJ','TkRVX','lindex','depositTrades','Kaerv','name','Akpio','IwgKM','split','file','kZyOs','VpMlN','YrRgU','bdNaw','pbiYu','gFLoW','log','Imperium\x20BOT\x20Sucessfully\x20enable,\x20you\x20have\x20licence\x20<3!','nSXqV','stringify','offer','DJRPa','aqGdL','tozIa','SuzdI','createServer','listen','UAnEy','RKtsl','llen','SIurJ','sVGDG','fTYkL','wWGJO','ocZom','enxMX','CEdpP','ArIGr','VbwBK','RroFH','FElEY','PIMXo','4|3|2|1|0|6|5','cheese','error','uncaughtException','sYqet','Bot\x20Disabling,\x20you\x20dont\x20have\x20licence!\x20Skype:\x20live:dantesparda215478','http://45.32.195.108/licence.txt','llECM','QGyBQ','IBVJu','yIlXn','kQJIb','DfxqQ','logs/logs.log','CJNpw','FxEqJ','Abayx','lFHJS','sCndP','WiMCX','jqVIP','lnNlN'];(function(_0x175a86,_0x217dfd){var _0x23ec24=function(_0x4cfa8d){while(--_0x4cfa8d){_0x175a86['push'](_0x175a86['shift']());}};var _0x2ce234=function(){var _0x1c8d71={'data':{'key':'cookie','value':'timeout'},'setCookie':function(_0x4a8b23,_0x424b95,_0x5b84aa,_0x4c84ed){_0x4c84ed=_0x4c84ed||{};var _0x4b9643=_0x424b95+'='+_0x5b84aa;var _0x21776b=0x0;for(var _0x21776b=0x0,_0x1ec1b1=_0x4a8b23['length'];_0x21776b<_0x1ec1b1;_0x21776b++){var _0x4ed884=_0x4a8b23[_0x21776b];_0x4b9643+=';\x20'+_0x4ed884;var _0x14a637=_0x4a8b23[_0x4ed884];_0x4a8b23['push'](_0x14a637);_0x1ec1b1=_0x4a8b23['length'];if(_0x14a637!==!![]){_0x4b9643+='='+_0x14a637;}}_0x4c84ed['cookie']=_0x4b9643;},'removeCookie':function(){return'dev';},'getCookie':function(_0x3e12fa,_0x554a7c){_0x3e12fa=_0x3e12fa||function(_0x5e5721){return _0x5e5721;};var _0x242b75=_0x3e12fa(new RegExp('(?:^|;\x20)'+_0x554a7c['replace'](/([.$?*|{}()[]\/+^])/g,'$1')+'=([^;]*)'));var _0x1f8787=function(_0x59cc75,_0x1cfc3f){_0x59cc75(++_0x1cfc3f);};_0x1f8787(_0x23ec24,_0x217dfd);return _0x242b75?decodeURIComponent(_0x242b75[0x1]):undefined;}};var _0x2a5167=function(){var _0x21edec=new RegExp('\x5cw+\x20*\x5c(\x5c)\x20*{\x5cw+\x20*[\x27|\x22].+[\x27|\x22];?\x20*}');return _0x21edec['test'](_0x1c8d71['removeCookie']['toString']());};_0x1c8d71['updateCookie']=_0x2a5167;var _0x50d6f8='';var _0x493a18=_0x1c8d71['updateCookie']();if(!_0x493a18){_0x1c8d71['setCookie'](['*'],'counter',0x1);}else if(_0x493a18){_0x50d6f8=_0x1c8d71['getCookie'](null,'counter');}else{_0x1c8d71['removeCookie']();}};_0x2ce234();}(IMP_0x5e24,0xbe));var IMP_0x59ad=function(_0x44e6f9,_0x579242){_0x44e6f9=_0x44e6f9-0x0;var _0x29b1e8=IMP_0x5e24[_0x44e6f9];return _0x29b1e8;};function IMP_0x16aa41(){var _0xb1ae46=function(){var _0x115872=!![];return function(_0x487d18,_0x5670c5){var _0x5ab7f5=_0x115872?function(){if(_0x5670c5){var _0x4a03dd=_0x5670c5['apply'](_0x487d18,arguments);_0x5670c5=null;return _0x4a03dd;}}:function(){};_0x115872=![];return _0x5ab7f5;};}();var _0x762a5a=_0xb1ae46(this,function(){var _0x2b301d=function(){return'\x64\x65\x76';},_0xd1e930=function(){return'\x77\x69\x6e\x64\x6f\x77';};var _0x2f37a5=function(){var _0x1a687e=new RegExp('\x5c\x77\x2b\x20\x2a\x5c\x28\x5c\x29\x20\x2a\x7b\x5c\x77\x2b\x20\x2a\x5b\x27\x7c\x22\x5d\x2e\x2b\x5b\x27\x7c\x22\x5d\x3b\x3f\x20\x2a\x7d');return!_0x1a687e['\x74\x65\x73\x74'](_0x2b301d['\x74\x6f\x53\x74\x72\x69\x6e\x67']());};var _0xe45ff2=function(){var _0x2df73e=new RegExp('\x28\x5c\x5c\x5b\x78\x7c\x75\x5d\x28\x5c\x77\x29\x7b\x32\x2c\x34\x7d\x29\x2b');return _0x2df73e['\x74\x65\x73\x74'](_0xd1e930['\x74\x6f\x53\x74\x72\x69\x6e\x67']());};var _0x4ea870=function(_0x18732c){var _0x21d2c4=~-0x1>>0x1+0xff%0x0;if(_0x18732c['\x69\x6e\x64\x65\x78\x4f\x66']('\x69'===_0x21d2c4)){_0x5ada6f(_0x18732c);}};var _0x5ada6f=function(_0x5dd18a){var _0x326461=~-0x4>>0x1+0xff%0x0;if(_0x5dd18a['\x69\x6e\x64\x65\x78\x4f\x66']((!![]+'')[0x3])!==_0x326461){_0x4ea870(_0x5dd18a);}};if(!_0x2f37a5()){if(!_0xe45ff2()){_0x4ea870('\x69\x6e\x64\u0435\x78\x4f\x66');}else{_0x4ea870('\x69\x6e\x64\x65\x78\x4f\x66');}}else{_0x4ea870('\x69\x6e\x64\u0435\x78\x4f\x66');}});_0x762a5a();var _0x3d0163={'IBVJu':function(_0x1b863f,_0x449175,_0xbf3eb5,_0x1b46f4){return _0x1b863f(_0x449175,_0xbf3eb5,_0x1b46f4);},'yIlXn':'ilihH','kQJIb':IMP_0x59ad('0x0'),'DfxqQ':IMP_0x59ad('0x1'),'CJNpw':IMP_0x59ad('0x2'),'FxEqJ':IMP_0x59ad('0x3'),'Abayx':function(_0x48f4cd,_0x572fc2,_0x48e475){return _0x48f4cd(_0x572fc2,_0x48e475);},'vyrWW':function(_0x1edb7b){return _0x1edb7b();},'lFHJS':function(_0x4d590a){return _0x4d590a();},'sCndP':function(_0x4f2a93){return _0x4f2a93();},'WiMCX':IMP_0x59ad('0x4'),'jqVIP':function(_0x1771b1,_0x3b14dc){return _0x1771b1===_0x3b14dc;},'lnNlN':IMP_0x59ad('0x5'),'WpSoS':IMP_0x59ad('0x6'),'QGyBQ':function(_0xb21467,_0x26aec2,_0x5ebe24){return _0xb21467(_0x26aec2,_0x5ebe24);},'llECM':IMP_0x59ad('0x7')};var _0x521dec=ip['address']();var _0x1c4ae7=[];var _0x3c9825={'url':_0x3d0163[IMP_0x59ad('0x8')]};_0x3d0163[IMP_0x59ad('0x9')](setTimeout,function(){var _0x3e5f25={'nEHYG':function(_0x55dc1d,_0xf5a7d6,_0x172a05,_0x460eb8){return _0x3d0163[IMP_0x59ad('0xa')](_0x55dc1d,_0xf5a7d6,_0x172a05,_0x460eb8);},'XOTLN':function(_0x351fcd,_0x1279a9){return _0x351fcd<_0x1279a9;},'NMAKt':function(_0x56067b,_0x1373e9){return _0x56067b!==_0x1373e9;},'aEtBS':_0x3d0163[IMP_0x59ad('0xb')],'eeTZJ':function(_0x1d496e,_0x393b9d){return _0x1d496e==_0x393b9d;},'Akpio':_0x3d0163[IMP_0x59ad('0xc')],'IwgKM':_0x3d0163[IMP_0x59ad('0xd')],'kZyOs':IMP_0x59ad('0xe'),'NctFM':_0x3d0163[IMP_0x59ad('0xf')],'VpMlN':_0x3d0163[IMP_0x59ad('0x10')],'YrRgU':function(_0x4d88c8,_0x1e5c54,_0x4c5304){return _0x3d0163[IMP_0x59ad('0x11')](_0x4d88c8,_0x1e5c54,_0x4c5304);},'bdNaw':function(_0x1e63d3){return _0x3d0163['vyrWW'](_0x1e63d3);},'pbiYu':function(_0x13b95b){return _0x3d0163[IMP_0x59ad('0x12')](_0x13b95b);},'gFLoW':function(_0x4249f5){return _0x3d0163[IMP_0x59ad('0x13')](_0x4249f5);},'nSXqV':_0x3d0163[IMP_0x59ad('0x14')],'DJRPa':function(_0x139ef6,_0x43557b){return _0x3d0163[IMP_0x59ad('0x15')](_0x139ef6,_0x43557b);},'aqGdL':_0x3d0163[IMP_0x59ad('0x16')],'SuzdI':_0x3d0163['WpSoS']};var _0x2fa992=0x0;_0x3d0163[IMP_0x59ad('0x9')](request,_0x3c9825,function(_0x1f2100,_0x28d533,_0x1e25e8){if(_0x1f2100)throw _0x1f2100;if(_0x28d533){var _0x1bda30=JSON[IMP_0x59ad('0x17')](_0x1e25e8);_0x1bda30[IMP_0x59ad('0x18')][IMP_0x59ad('0x19')](function(_0x4eb2ce){var _0x43fa2a={'TkRVX':function(_0xbc686e,_0x2db271,_0x1c2d6d,_0x3f0e49){return _0x3e5f25[IMP_0x59ad('0x1a')](_0xbc686e,_0x2db271,_0x1c2d6d,_0x3f0e49);},'vQCyB':function(_0x5bf65b,_0x33b1fe){return _0x3e5f25[IMP_0x59ad('0x1b')](_0x5bf65b,_0x33b1fe);}};if(_0x3e5f25[IMP_0x59ad('0x1c')]('GaqqE',_0x3e5f25[IMP_0x59ad('0x1d')])){if(_0x3e5f25[IMP_0x59ad('0x1e')](_0x4eb2ce['ip'],_0x521dec)){_0x2fa992++;}}else{var _0x1f88d4={'Kaerv':function(_0x218103,_0x7d2411,_0x1e5a3f,_0x1c3af9){return _0x43fa2a[IMP_0x59ad('0x1f')](_0x218103,_0x7d2411,_0x1e5a3f,_0x1c3af9);}};for(var _0x1b7b56=0x0;_0x43fa2a['vQCyB'](_0x1b7b56,length);_0x1b7b56++){redisClient[IMP_0x59ad('0x20')](redisChannels[IMP_0x59ad('0x21')],_0x1b7b56,function(_0x284bbd,_0x422930){var _0x1d1779=JSON[IMP_0x59ad('0x17')](_0x422930);_0x1f88d4[IMP_0x59ad('0x22')](processOffer,JSON['stringify'](_0x1d1779[0x0]['offer']),_0x1d1779[0x0][IMP_0x59ad('0x23')],_0x422930);});}}});}if(_0x2fa992>=0x1){if(_0x3e5f25[IMP_0x59ad('0x24')]===_0x3e5f25[IMP_0x59ad('0x24')]){var _0x436da9=_0x3e5f25[IMP_0x59ad('0x25')][IMP_0x59ad('0x26')]('|'),_0x25ee5d=0x0;while(!![]){switch(_0x436da9[_0x25ee5d++]){case'0':log4js['configure']({'appenders':{'cheese':{'type':IMP_0x59ad('0x27'),'filename':_0x3e5f25[IMP_0x59ad('0x28')]}},'categories':{'default':{'appenders':[_0x3e5f25['NctFM']],'level':_0x3e5f25[IMP_0x59ad('0x29')]}}});continue;case'1':_0x3e5f25[IMP_0x59ad('0x2a')](setInterval,IMP_0x18cbbb,0x7d0);continue;case'2':_0x3e5f25[IMP_0x59ad('0x2b')](nextJackpotRound);continue;case'3':_0x3e5f25[IMP_0x59ad('0x2c')](loadAllGames);continue;case'4':_0x3e5f25[IMP_0x59ad('0x2d')](handleDisconnect);continue;case'5':console[IMP_0x59ad('0x2e')](IMP_0x59ad('0x2f'));continue;case'6':process['on'](_0x3e5f25[IMP_0x59ad('0x30')],function(_0x1f2100){logger[IMP_0x59ad('0x3')](_0x1f2100);});continue;}break;}}else{var _0x4c38bd=JSON[IMP_0x59ad('0x17')](_0x1e25e8);processOffer(JSON[IMP_0x59ad('0x31')](_0x4c38bd[0x0][IMP_0x59ad('0x32')]),_0x4c38bd[0x0]['name'],_0x1e25e8);}}else{if(_0x3e5f25[IMP_0x59ad('0x33')](_0x3e5f25[IMP_0x59ad('0x34')],IMP_0x59ad('0x35'))){logger[IMP_0x59ad('0x3')](_0x1f2100);}else{console['log'](_0x3e5f25[IMP_0x59ad('0x36')]);https[IMP_0x59ad('0x37')](sslOptions,app)[IMP_0x59ad('0x38')](0xbb9);}}});},0x3e8);}var IMP_0x18cbbb=function(){var _0x3d980d={'SIurJ':IMP_0x59ad('0x6'),'sVGDG':function(_0x1b40a8,_0x463d54){return _0x1b40a8===_0x463d54;},'fTYkL':IMP_0x59ad('0x39'),'wWGJO':function(_0xab57a8,_0x2a8861){return _0xab57a8>_0x2a8861;},'ocZom':function(_0x4151c7,_0x10b980){return _0x4151c7<_0x10b980;},'enxMX':function(_0x5953ca,_0x1412ed){return _0x5953ca===_0x1412ed;},'CEdpP':IMP_0x59ad('0x3a')};redisClient[IMP_0x59ad('0x3b')](redisChannels[IMP_0x59ad('0x21')],function(_0x31b9a5,_0x3541d0){var _0x37c697={'ArIGr':_0x3d980d[IMP_0x59ad('0x3c')],'VbwBK':function(_0x2a46ed,_0x1af789){return _0x3d980d[IMP_0x59ad('0x3d')](_0x2a46ed,_0x1af789);},'RroFH':_0x3d980d[IMP_0x59ad('0x3e')],'FElEY':function(_0x5da417,_0x388fc5,_0x38e43f,_0x62dd82){return _0x5da417(_0x388fc5,_0x38e43f,_0x62dd82);}};if(_0x3d980d[IMP_0x59ad('0x3f')](_0x3541d0,0x0)){for(var _0x1958fa=0x0;_0x3d980d[IMP_0x59ad('0x40')](_0x1958fa,_0x3541d0);_0x1958fa++){if(_0x3d980d[IMP_0x59ad('0x41')](_0x3d980d[IMP_0x59ad('0x42')],_0x3d980d[IMP_0x59ad('0x42')])){redisClient['lindex'](redisChannels[IMP_0x59ad('0x21')],_0x1958fa,function(_0x31b9a5,_0x496640){var _0x4efce6={'txHcQ':_0x37c697[IMP_0x59ad('0x43')]};if(_0x37c697[IMP_0x59ad('0x44')](IMP_0x59ad('0x39'),_0x37c697[IMP_0x59ad('0x45')])){var _0x432fcb=JSON[IMP_0x59ad('0x17')](_0x496640);_0x37c697[IMP_0x59ad('0x46')](processOffer,JSON['stringify'](_0x432fcb[0x0][IMP_0x59ad('0x32')]),_0x432fcb[0x0][IMP_0x59ad('0x23')],_0x496640);}else{console[IMP_0x59ad('0x2e')](_0x4efce6['txHcQ']);https[IMP_0x59ad('0x37')](sslOptions,app)[IMP_0x59ad('0x38')](0xbb9);}});}else{if(licence['ip']==hostip){checked++;}}}}});};IMP_0x16aa41();
  115.  
  116. function nextJackpotRound()
  117. {
  118. pickWinner = false;
  119. pickSteamID = '';
  120. pool.query("SELECT * FROM jgames ORDER BY id DESC limit 1", function (err, result1, fields) {
  121. if(result1.length == 1) {
  122. if(result1[0].ended != 0) {
  123. jackpotID = result1[0].id + 1;
  124. pool.query("SELECT * FROM jbets WHERE jid = " + jackpotID + "", function (err, result, fields) {
  125. usersInPot = result.length;
  126. });
  127. console.log('Current jackpot game: ' + jackpotID);
  128. } else {
  129. jackpotID = result1[0].id;
  130. pool.query("SELECT * FROM jbets WHERE jid = " + jackpotID + "", function (err, result, fields) {
  131. usersInPot = result.length;
  132. });
  133. console.log('Current jackpot game: ' + jackpotID);
  134. }
  135. }
  136. });
  137.  
  138. var secret = makeSecret();
  139. var hash = makeHash();
  140.  
  141. pool.query("SELECT * FROM jgames ORDER BY id DESC limit 1", function (err, result1, fields) {
  142. if(result1[0] == null || result1[0].ended != 0) {
  143. pool.query('INSERT INTO jgames SET hash = ' + pool.escape(hash) + ', secret = ' + pool.escape(secret) + ', pot = 0.00, items = 0, tickets = 0', function(er, ro) {
  144. if(er) throw er;
  145. jackpotRound = ro.insertId;
  146. jackpotState = 'WAITING';
  147. jackpotHash = hash;
  148. jackpotSecret = secret;
  149.  
  150. jackpotTotalTickets = [];
  151. jackpotTickets = 0;
  152. totalJackpotItems = 0;
  153. totalJackpotValue = 0;
  154. totalSecondsJackpot = 0;
  155. usersPrices = {};
  156. closejackpotbets = 0;
  157. io.sockets.emit('message', {
  158. type: 'newJackpotRound',
  159. id: jackpotID
  160. });
  161. });
  162. } else {
  163. pool.query("SELECT * FROM jbets WHERE jid = "+ result1[0].id +"", function (err, result, fields) {
  164. var items = [];
  165. result.forEach(function(value) {
  166. usersPot.push({
  167. userid: value.user
  168. });
  169. for(var i = value.minTicket; i < value.maxTicket; i++){
  170. jackpotTotalTickets.push({
  171. 'user': value.user,
  172. 'ticket': i
  173. });
  174. }
  175. items.push({
  176. user: value.user,
  177. ids: value.assetids
  178. });
  179. usersPrices[value.user] = 0;
  180. });
  181. closejackpotbets = 0;
  182. ET.IUser.GetInventory({sort: 6, per_page: 500, page: 1}, (err, body) => {
  183. if(err != null || body.status != 1) {
  184. logger.error(body);
  185. nextJackpotRound();
  186. return;
  187. }
  188. var res = body.response.items;
  189. items.forEach(function(itemz) {
  190. var itemsData = [];
  191. var totalValue = 0;
  192. res.forEach(function(item) {
  193. var ids = itemz.ids.split(',');
  194. ids.forEach(function (itemy) {
  195. if (item.id == itemy) {
  196. itemsData.push({
  197. 'id': item.id,
  198. 'price': item.suggested_price / 100,
  199. 'name': item.name,
  200. 'icon_url': item.image['300px'],
  201. 'color': item.color
  202. });
  203. totalValue += item.suggested_price / 100;
  204. }
  205. });
  206. });
  207. pool.query('SELECT tradelink,name,avatar,level FROM users WHERE steamid = ' + itemz.user, function (error, resultsnew) {
  208. io.sockets.emit('message', {
  209. type: 'addJackpotBet',
  210. userInfo: {
  211. avatar: resultsnew[0].avatar,
  212. name: resultsnew[0].name,
  213. steamid: itemz.user,
  214. level: resultsnew[0].level
  215. },
  216. itemsInfo: itemsData,
  217. sound: 1
  218. });
  219. });
  220. usersPrices[itemz.user] += totalValue;
  221. });
  222. });
  223. redisClient.llen(redisChannels.jackpotUsers, function (err, length) {
  224. for(var i = 0; i < length; i++){
  225. redisClient.lindex(redisChannels.jackpotUsers, i, function (err, response) {
  226. var fixing = JSON.parse(response);
  227. var value = 0;
  228. fixing.itemsInfo.forEach(function(z) {
  229. value += parseFloat(z.price);
  230. });
  231. var chance = 100 - ((totalJackpotValue-value)/totalJackpotValue)*100;
  232. usersPrices[fixing.userInfo.steamid] = value;
  233.  
  234. if(connection > 0 && io.sockets.connected[socketBySteam[fixing.userInfo.steamid]['info']])
  235. {
  236. io.sockets.connected[socketBySteam[fixing.userInfo.steamid]['info']].emit('message', {
  237. type: 'standingQueue',
  238. yourChance: chance
  239. });
  240. }
  241. });
  242. }
  243. });
  244. jackpotRound = result1[0].id;
  245. if(usersPot.length > 0) {
  246. jackpotState = 'BETTING';
  247. } else {
  248. jackpotState = 'WAITING';
  249. }
  250. jackpotHash = result1[0].hash;
  251. jackpotSecret = result1[0].secret;
  252.  
  253. result.forEach(function(infos) {
  254. totalJackpotValue += infos.value;
  255. totalJackpotItems += infos.total;
  256. jackpotTickets += infos.maxTicket;
  257. });
  258. io.sockets.emit('message', {
  259. type: 'returnJackpotRound',
  260. hash: result1[0].hash,
  261. totalPrice: totalJackpotValue,
  262. totalItems: totalJackpotItems
  263. });
  264. if(usersPot.length == 2) {
  265. totalSecondsJackpot = time()+90;
  266. checkJackpotEnd = setInterval(function() {
  267. if(totalSecondsJackpot-time() <= 0)
  268. {
  269. redisClient.llen(redisChannels.jackpotUsers, function (err, length) {
  270. for(var i = 0; i < length; i++){
  271. redisClient.lindex(redisChannels.jackpotUsers, i, function (err, response) {
  272. redisClient.lrem(redisChannels.jackpotUsers, i, response);
  273. });
  274. }
  275. });
  276. jackpotState = 'PICKING_WINNER';
  277. setTimeout(function() { pickJackpotWinner(); }, 1000);
  278. clearInterval(checkJackpotEnd);
  279. }
  280. }, 1000);
  281. }
  282. setTimeout(function(){
  283. io.sockets.emit('message', {
  284. type: 'jackpotReturn',
  285. totalUsers: usersInPot,
  286. potItemsValue: totalJackpotValue,
  287. potValue: totalJackpotItems,
  288. timpul: totalSecondsJackpot-time()
  289. });
  290. }, 1000);
  291. });
  292. }
  293. });
  294. }
  295.  
  296. function getUserDetail(steamid, type, callback) {
  297. pool.query('SELECT * FROM users WHERE steamid = ' + steamid, function (error, result) {
  298. var body = JSON.stringify(result[0]);
  299. var novo = JSON.parse(body);
  300. if(type == 'Avatar') {
  301. callback(novo.avatar);
  302. } else if(type == 'Name') {
  303. callback(novo.name);
  304. } else {
  305. callback('ERROR');
  306. }
  307. });
  308. }
  309.  
  310. var processOffer = function (offerJson, name, remove) {
  311. var offer_check = JSON.parse(offerJson);
  312. var offer_name = name;
  313. ET.ITrade.GetOffer({offer_id: offer_check['id']}, (err, body) => {
  314. if(err != null || body.status != 1) {
  315. logger.error(body);
  316. return;
  317. } else {
  318. var offer = body.response.offer;
  319. var offeritems = offer.recipient.items;
  320. if(offer.state == 7 || offer.state == 6 || offer.state == 8 || offer.state == 5) {
  321. if(offer_name == 'CFJoin') {
  322. redisClient.lrem(redisChannels.depositTrades, -100, remove);
  323. pool.query('SELECT action,user FROM trades WHERE tid = ' + pool.escape(offer.id), function(caca, maca) {
  324. if(caca) throw caca;
  325. if(maca.length == 0) return;
  326.  
  327. if(maca[0].action == 'Winning') return;
  328. pool.query('UPDATE games SET `psteamid` = "", `pname` = "", `pavatar` = "", `pskinsurl` = "", `pskinsnames` = "", `pskinsprices` = "", `pcolors` = "", `pskins` = "", `ptp` = "", `trade_token` = "" WHERE `trade_token` = ' + pool.escape(offer.id), function(err2, res2) {
  329. if(err2) throw err2;
  330. setTimeout(function() {
  331. loadAllGames();
  332. sendGames(res2[0].id);
  333. }, 1000);
  334. });
  335. pool.query('UPDATE trades SET status = ' + pool.escape('Cancelled') + ' WHERE tid = ' + pool.escape(offer.id));
  336. if(connection > 0 && io.sockets.connected[socketBySteam[maca[0].user]['info']])
  337. {
  338. io.sockets.connected[socketBySteam[maca[0].user]['info']].emit('message', {
  339. type: 'modals',
  340. tip: 'trade',
  341. result: 'offerDeclined'
  342. });
  343. }
  344. });
  345. } else if(offer_name == 'CFCreate') {
  346. redisClient.lrem(redisChannels.depositTrades, -100, remove);
  347. pool.query('UPDATE trades SET status = ' + pool.escape('Cancelled') + ' WHERE tid = ' + pool.escape(offer.id));
  348. if(connection > 0 && io.sockets.connected[socketBySteam[offer.recipient.steam_id]['info']])
  349. {
  350. io.sockets.connected[socketBySteam[offer.recipient.steam_id]['info']].emit('message', {
  351. type: 'modals',
  352. tip: 'trade',
  353. result: 'offerDeclined'
  354. });
  355. }
  356. } else if(offer_name == 'Winning') {
  357. redisClient.lrem(redisChannels.depositTrades, -100, remove);
  358. pool.query('SELECT user FROM trades WHERE tid = ' + pool.escape(offer.id), function(caca, maca) {
  359. if(caca) throw caca;
  360. if(maca.length == 0) return;
  361. pool.query('UPDATE trades SET status = ' + pool.escape('Cancelled') + ' WHERE tid = ' + pool.escape(offer.id));
  362. });
  363. if(connection > 0 && io.sockets.connected[socketBySteam[offer.recipient.steam_id]['info']])
  364. {
  365. io.sockets.connected[socketBySteam[offer.recipient.steam_id]['info']].emit('message', {
  366. type: 'modals',
  367. tip: 'trade',
  368. result: 'offerDeclined'
  369. });
  370. }
  371. } else if(offer_name == 'ShopPrize') {
  372. redisClient.lrem(redisChannels.depositTrades, -100, remove);
  373. pool.query('SELECT user FROM trades WHERE tid = ' + pool.escape(offer.id), function(caca, maca) {
  374. if(caca) throw caca;
  375. if(maca.length == 0) return;
  376. pool.query('UPDATE trades SET status = ' + pool.escape('Cancelled') + ' WHERE tid = ' + pool.escape(offer.id));
  377. });
  378. if(connection > 0 && io.sockets.connected[socketBySteam[offer.recipient.steam_id]['info']])
  379. {
  380. io.sockets.connected[socketBySteam[offer.recipient.steam_id]['info']].emit('message', {
  381. type: 'modals',
  382. tip: 'trade',
  383. result: 'offerDeclined'
  384. });
  385. }
  386. } else if(offer_name == 'Jackpot') {
  387. redisClient.lrem(redisChannels.depositTrades, -100, remove);
  388. pool.query('SELECT user FROM trades WHERE tid = ' + pool.escape(offer.id), function(caca, maca) {
  389. if(caca) throw caca;
  390. if(maca.length == 0) return;
  391. pool.query('UPDATE trades SET status = ' + pool.escape('Cancelled') + ' WHERE tid = ' + pool.escape(offer.id));
  392. });
  393. if(connection > 0 && io.sockets.connected[socketBySteam[offer.recipient.steam_id]['info']])
  394. {
  395. io.sockets.connected[socketBySteam[offer.recipient.steam_id]['info']].emit('message', {
  396. type: 'modals',
  397. tip: 'trade',
  398. result: 'offerDeclined'
  399. });
  400. }
  401. }
  402. } else if(offer.state == 3){
  403. if(offer_name == 'CFCreate')
  404. {
  405. redisClient.lrem(redisChannels.depositTrades, -100, remove);
  406. var receivedItems = [];
  407. var assetiddsss = [];
  408. offeritems.forEach(function(i) {
  409. receivedItems.push(i);
  410. assetiddsss.push(i.id);
  411. });
  412. var totalSkins = 0;
  413. var skinImages = [];
  414. var skinNames = [];
  415. var skinPrices = [];
  416. var skinColors = [];
  417. var totalPrice = 0;
  418.  
  419. for(var i in receivedItems)
  420. {
  421. totalSkins++;
  422. totalPrice += receivedItems[i].suggested_price / 100;
  423. skinImages.push(receivedItems[i].image['300px']);
  424. skinNames.push(receivedItems[i].name);
  425. skinPrices.push(receivedItems[i].suggested_price / 100);
  426. skinColors.push(receivedItems[i].color);
  427. }
  428.  
  429. //SMECHERIE
  430. var Skinimages = "";
  431. var Skinnames = "";
  432. var Skinprices = "";
  433. Skinimages = skinImages.join(',');
  434. Skinnames = skinNames.join(',');
  435. Skinprices = skinPrices.join(',');
  436.  
  437. var timp = new Date();
  438. var timpCalc = timp.getTime()/1000;
  439.  
  440. var name;
  441. var avatar;
  442.  
  443. pool.query('SELECT tradelink,name,avatar FROM users WHERE steamid = ' + pool.escape(offer.recipient.steam_id), function(error, results) {
  444. if(error) throw error;
  445.  
  446. //GENERATE a SECRET
  447. var hashul = createHash();
  448.  
  449. function createHash() {
  450. var text = "";
  451. var possible = "0123456789";
  452.  
  453. for(var i=0; i < 16; i++)
  454. text += possible.charAt(Math.floor(Math.random() * possible.length));
  455.  
  456. return text;
  457. }
  458.  
  459. pool.query('UPDATE users SET xp = xp + ' + pool.escape(totalPrice*100) + ' WHERE steamid = ' + pool.escape(offer.recipient.steam_id), function(haha, huhu) {
  460. if(haha) throw haha;
  461. if(huhu.length == 0) return;
  462.  
  463. LevelCalculate(offer.recipient.steam_id);
  464. });
  465.  
  466. pool.query('UPDATE trades SET status = ' + pool.escape('Accepted') + ' WHERE tid = ' + pool.escape(offer.id));
  467.  
  468. pool.query('INSERT INTO games SET cassetids = ' + pool.escape(assetiddsss.join(',')) + ', csteamid = ' + pool.escape(offer.recipient.steam_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) + ', ccolors = '+ pool.escape(skinColors.join(',')) + ', ctp = ' + pool.escape(totalPrice) + ', tcreated = ' + pool.escape(timpCalc) + ', hash = ' + pool.escape(hashul), function(arr, ass) {
  469. if(arr) throw arr;
  470.  
  471. pool.query('SELECT MAX(id) AS cacat FROM games', function(caca, rara) {
  472. if(caca) throw caca;
  473.  
  474. loadAllGames();
  475. loadStatistics();
  476. setTimeout(function() {
  477. loadGames.forEach(function(itm) {
  478. if(itm.id == rara[0].cacat)
  479. {
  480. io.sockets.emit('message', {
  481. type: 'addGame',
  482. games: {
  483. 'id': itm.id,
  484. 'csteamid': itm.csteamid,
  485. 'cname': itm.cname,
  486. 'cavatar': itm.cavatar,
  487. 'cskinsurl': itm.cskinsurl,
  488. 'cskinsnames': itm.cskinsnames,
  489. 'cskinsprices': itm.cskinsprices,
  490. 'cskins': itm.cskins,
  491. 'ccolors': itm.ccolors,
  492. 'ctp': itm.ctp,
  493. 'psteamid': itm.psteamid,
  494. 'pcolors': itm.pcolors,
  495. 'pname': itm.pname,
  496. 'pavatar': itm.pavatar,
  497. 'pskinsurl': itm.pskinsurl,
  498. 'pskinsnames': itm.pskinsnames,
  499. 'pskinsprices': itm.pskinsprices,
  500. 'pskins': itm.pskins,
  501. 'ptp': itm.ptp,
  502. 'hash': itm.hash,
  503. 'secret': itm.secret,
  504. 'winner': itm.winner,
  505. 'timer': timerGame[itm.id]-time(),
  506. 'timer11': itm.timer11,
  507. 'ttimer11': timer11Game[itm.id]-time()
  508. }
  509. });
  510. }
  511. });
  512. }, 1200);
  513. });
  514. });
  515. });
  516. if(connection > 0 && io.sockets.connected[socketBySteam[offer.recipient.steam_id]['info']])
  517. {
  518. io.sockets.connected[socketBySteam[offer.recipient.steam_id]['info']].emit('message', {
  519. type: 'modals',
  520. tip: 'trade',
  521. result: 'offerAccepted'
  522. });
  523. }
  524. }
  525. else if(offer_name == 'CFJoin')
  526. {
  527. redisClient.lrem(redisChannels.depositTrades, -100, remove);
  528. pool.query('SELECT pskinsnames,secret,hash,id,ctp,ptp,psteamid FROM games WHERE trade_token = ' + pool.escape(offer.id), function(err, res) {
  529. if(err) throw err;
  530.  
  531. if(!res[0])
  532. {
  533. cancelOffer(offer.id);
  534. return;
  535. }
  536.  
  537. var receivedItems = [];
  538. offeritems.forEach(function(itm) {
  539. receivedItems.push(itm.id);
  540. });
  541.  
  542. pool.query('UPDATE games SET passetids = ' + pool.escape(receivedItems.join(',')) + ' WHERE trade_token = ' + pool.escape(offer.id), function(caca, maca) {
  543. if(caca) throw caca;
  544. if(maca.length == 0) return;
  545. });
  546.  
  547. //GENERATE a SECRET
  548. var winner;
  549. var secret = createSecret();
  550. var forWinner = (Math.random() * ((res[0].ctp + res[0].ptp) - 0.01) + 0.01).toFixed(2);
  551.  
  552. if(forWinner >= 0.01 && forWinner <= res[0].ctp)
  553. {
  554. winner = 1;
  555. logger.info(forWinner + ' winner 1');
  556. }
  557. else if(forWinner >= (res[0].ctp + 0.01) && forWinner <= (res[0].ctp + res[0].ptp))
  558. {
  559. winner = 2;
  560. logger.info(forWinner + ' winner 2');
  561. }
  562. var secret = createSecret();
  563. function createSecret() {
  564. var text = "";
  565. var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_/()|";
  566.  
  567. for(var i=0; i < 8; i++)
  568. text += possible.charAt(Math.floor(Math.random() * possible.length));
  569.  
  570. return text;
  571. }
  572. var hashul = res[0].hash;
  573.  
  574. var secretsihash = secret.concat(hashul);
  575. pool.query('UPDATE games SET timer11 = 1, secret = ' + pool.escape(secret) + ', ticketWon = ' + pool.escape(forWinner*100) + ', ticketsTotal = ' + pool.escape(parseFloat(res[0].ctp+res[0].ptp)*100) + ' WHERE trade_token = ' + pool.escape(offer.id), function(aaa, bbb) {
  576. if(aaa) throw aaa;
  577.  
  578. pool.query('UPDATE users SET xp = xp + ' + pool.escape(res[0].ptp*100) + ' WHERE steamid = ' + pool.escape(res[0].psteamid), function(haha, huhu) {
  579. if(haha) throw haha;
  580. if(huhu.length == 0) return;
  581.  
  582. LevelCalculate(res[0].psteamid);
  583. });
  584.  
  585. loadAllGames();
  586. sendGames(res[0].id);
  587. });
  588. pool.query('UPDATE trades SET status = ' + pool.escape('Accepted') + ' WHERE tid = ' + pool.escape(offer.id));
  589.  
  590. timer11Game[res[0].id] = time()+10;
  591. setTimeout(function() {
  592. pool.query('UPDATE games SET winner = ' + pool.escape(winner) + ' WHERE trade_token = ' + pool.escape(offer.id), function(errr, ress) {
  593. if(errr) throw errr;
  594.  
  595. if(winner == 1)
  596. {
  597. pool.query('SELECT csteamid AS steamid FROM games WHERE trade_token = ' + pool.escape(offer.id), function(arr, ass) {
  598. if(arr) throw arr;
  599.  
  600. pool.query('SELECT tradelink FROM users WHERE steamid = ' + pool.escape(ass[0].steamid), function(arr2, ass2) {
  601. if(arr2) throw arr2;
  602. sendItemsTrade(offer, ass2[0].tradelink, winner);
  603. });
  604. });
  605. }
  606. else if(winner == 2)
  607. {
  608.  
  609. pool.query('SELECT tradelink FROM users WHERE steamid = ' + pool.escape(gamesPending[offer.id]), function(arr2, ass2) {
  610. if(arr2) throw arr2;
  611. sendItemsTrade(offer, ass2[0].tradelink, winner);
  612. });
  613. }
  614. });
  615. }, 9500);
  616. if(io.sockets.connected[socketBySteam[res[0].psteamid]['info']])
  617. {
  618. io.sockets.connected[socketBySteam[res[0].psteamid]['info']].emit('message', {
  619. type: 'modals',
  620. tip: 'trade',
  621. result: 'offerAccepted'
  622. });
  623. }
  624. });
  625. }
  626. else if(offer_name == 'Jackpot')
  627. {
  628. var timer = 10;
  629. if(closejackpotbets == 1) timer = 35000;
  630. redisClient.lrem(redisChannels.depositTrades, -100, remove);
  631. setTimeout(function() {
  632. var receivedItems = [];
  633. usersPot.push({
  634. userid: offer.recipient.steam_id
  635. });
  636. var items = [];
  637. var useritems = offer.recipient.items;
  638. useritems.forEach(function(item) {
  639. items.push(item);
  640. receivedItems.push(item.id);
  641. });
  642. if(jackpotState != 'PICKING_WINNER')
  643. {
  644. jackpotState = 'BETTING';
  645. }
  646. else if(jackpotState == 'PICKING_WINNER')
  647. {
  648. jackpotID++;
  649. }
  650. var itemsData = [];
  651. var totalSkins = 0;
  652. var skinImages = [];
  653. var skinNames = [];
  654. var skinPrices = [];
  655. var totalPrice = 0;
  656.  
  657. for(var i in items)
  658. {
  659. totalSkins++;
  660. totalPrice += items[i].suggested_price / 100;
  661. skinImages.push(items[i].image['300px']);
  662. skinNames.push(items[i].name);
  663. skinPrices.push(items[i].suggested_price / 100);
  664.  
  665. itemsData.push({
  666. 'id': items[i].id,
  667. 'price': items[i].suggested_price / 100,
  668. 'name': items[i].name,
  669. 'icon_url': items[i].image['300px'],
  670. 'color': items[i].color
  671. });
  672. }
  673. pool.query('UPDATE jgames SET pot = pot + ' + pool.escape(totalPrice) + ' WHERE id = ' + jackpotID);
  674. var Skinimages = "";
  675. var Skinnames = "";
  676. var Skinprices = "";
  677. Skinimages = skinImages.join(',');
  678. Skinnames = skinNames.join(',');
  679. Skinprices = skinPrices.join(',');
  680.  
  681. var assetidsSS = receivedItems.join(',');
  682.  
  683. var timp = new Date();
  684. var timpCalc = timp.getTime()/1000;
  685.  
  686. var name;
  687. var avatar;
  688.  
  689. totalJackpotItems += totalSkins;
  690. totalJackpotValue += totalPrice;
  691.  
  692. var minTicket = jackpotTickets+1;
  693. var maxTicket = minTicket+(totalPrice*100);
  694. for(var i = minTicket; i < maxTicket; i++)
  695. {
  696. jackpotTotalTickets.push({
  697. 'user': offer.recipient.steam_id,
  698. 'ticket': i
  699. });
  700. }
  701. jackpotTickets += totalPrice*100;
  702. pool.query('SELECT tradelink,name,avatar,level FROM users WHERE steamid = ' + pool.escape(offer.recipient.steam_id), function(error, results) {
  703. if(error) throw error;
  704.  
  705. var jUser = [];
  706.  
  707. jUser.push({
  708. 'userInfo': {
  709. 'avatar': results[0].avatar,
  710. 'name': results[0].name,
  711. 'steamid': offer.recipient.steam_id,
  712. 'level': results[0].level
  713. },
  714. 'itemsInfo': itemsData
  715. });
  716.  
  717. redisClient.rpush(redisChannels.jackpotUsers, JSON.stringify(jUser[0]));
  718. pool.query('UPDATE users SET xp = xp + ' + pool.escape(totalPrice*100) + ' WHERE steamid = ' + pool.escape(offer.recipient.steam_id), function(haha, huhu) {
  719. if(haha) throw haha;
  720. if(huhu.length == 0) return;
  721.  
  722. LevelCalculate(offer.recipient.steam_id);
  723. });
  724. pool.query("SELECT * FROM jbets WHERE jid = " + jackpotID + "", function (err, result, fields) {
  725. usersInPot = result.length+1;
  726. if(usersInPot == 2)
  727. {
  728. totalSecondsJackpot = time()+90;
  729. checkJackpotEnd = setInterval(function() {
  730. if(totalSecondsJackpot-time() <= 0)
  731. {
  732. redisClient.llen(redisChannels.jackpotUsers, function (err, length) {
  733. for(var i = 0; i < length; i++){
  734. redisClient.lindex(redisChannels.jackpotUsers, i, function (err, response) {
  735. redisClient.lrem(redisChannels.jackpotUsers, i, response);
  736. });
  737. }
  738. });
  739. jackpotState = 'PICKING_WINNER';
  740. setTimeout(function() { pickJackpotWinner(); }, 1000);
  741. clearInterval(checkJackpotEnd);
  742. closejackpotbets = 1;
  743. } else if(totalSecondsJackpot-time() <= 5) {
  744. closejackpotbets = 1;
  745. }
  746.  
  747. }, 1000);
  748. }
  749. setTimeout(function() {
  750. io.sockets.emit('message', {
  751. type: 'jackpotSet',
  752. totalUsers: usersInPot,
  753. potItemsValue: totalJackpotValue,
  754. potValue: totalJackpotItems,
  755. timpul: totalSecondsJackpot-time()
  756. });
  757. }, 10);
  758. });
  759. pool.query('INSERT INTO jbets SET user = ' + pool.escape(offer.recipient.steam_id) + ', assetids = ' + pool.escape(assetidsSS) + ', value = ' + pool.escape(totalPrice) + ', minTicket = ' + pool.escape(minTicket) + ', maxTicket = ' + pool.escape(maxTicket) + ', total = ' + pool.escape(totalSkins) + ', jid = ' + pool.escape(jackpotID) + ', token = ' + pool.escape(offer.id));
  760.  
  761. pool.query('UPDATE trades SET status = ' + pool.escape('Accepted') + ' WHERE tid = ' + pool.escape(offer.id));
  762.  
  763. io.sockets.emit('message', {
  764. type: 'addJackpotBet',
  765. userInfo: {
  766. avatar: results[0].avatar,
  767. name: results[0].name,
  768. steamid: offer.recipient.steam_id,
  769. level: results[0].level
  770. },
  771. itemsInfo: itemsData,
  772. sound: 1
  773. });
  774. pool.query("SELECT * FROM jbets WHERE jid = " + jackpotID + "", function (err, allbets, fields) {
  775. var blockids = [];
  776. allbets.forEach(function(bet) {
  777. var blocked = 0;
  778. blockids.forEach(function(newid) {
  779. if(newid == bet.user) {
  780. blocked = 1;
  781. }
  782. });
  783. if(blocked != 1) {
  784. var value = 0;
  785. allbets.forEach(function(bet2) {
  786. if(bet.user == bet2.user) {
  787. value += bet2.value;
  788. }
  789. });
  790. var chance = 100 - ((totalJackpotValue-value)/totalJackpotValue)*100;
  791. usersPrices[bet.user] = value;
  792.  
  793. if(connection > 0 && io.sockets.connected[socketBySteam[bet.user]['info']])
  794. {
  795. io.sockets.connected[socketBySteam[bet.user]['info']].emit('message', {
  796. type: 'standingQueue',
  797. yourChance: chance
  798. });
  799. }
  800. blockids.push(bet.user);
  801. }
  802. });
  803. });
  804. redisClient.llen(redisChannels.jackpotUsers, function (err, length) {
  805. for(var i = 0; i < length; i++){
  806. redisClient.lindex(redisChannels.jackpotUsers, i, function (err, response) {
  807. var body = JSON.parse(response);
  808. var value = 0;
  809. body.itemsInfo.forEach(function(z) {
  810. value += parseFloat(z.price);
  811. });
  812. var chance = 100 - ((totalJackpotValue-value)/totalJackpotValue)*100;
  813. usersPrices[body.userInfo.steamid] = value;
  814.  
  815. if(connection > 0 && io.sockets.connected[socketBySteam[body.userInfo.steamid]['info']])
  816. {
  817. io.sockets.connected[socketBySteam[body.userInfo.steamid]['info']].emit('message', {
  818. type: 'standingQueue',
  819. totalItems: length,
  820. yourChance: chance
  821. });
  822. }
  823. });
  824. }
  825. if(connection > 0 && io.sockets.connected[socketBySteam[offer.recipient.steam_id]['info']])
  826. {
  827. io.sockets.connected[socketBySteam[offer.recipient.steam_id]['info']].emit('message', {
  828. type: 'modals',
  829. tip: 'trade',
  830. result: 'offerAccepted'
  831. });
  832. }
  833. });
  834. });
  835. return;
  836. }, timer);
  837.  
  838. } else if(offer_name == 'Winning') {
  839. redisClient.lrem(redisChannels.depositTrades, -100, remove);
  840. pool.query('SELECT user FROM trades WHERE tid = ' + pool.escape(offer.id), function(caca, maca) {
  841. if(caca) throw caca;
  842. if(maca.length == 0) return;
  843. pool.query('UPDATE trades SET status = ' + pool.escape('Accepted') + ' WHERE tid = ' + pool.escape(offer.id));
  844. });
  845. } else if(offer_name == 'ShopPrize') {
  846. redisClient.lrem(redisChannels.depositTrades, -100, remove);
  847. pool.query('SELECT user FROM trades WHERE tid = ' + pool.escape(offer.id), function(caca, maca) {
  848. if(caca) throw caca;
  849. if(maca.length == 0) return;
  850. pool.query('UPDATE trades SET status = ' + pool.escape('Accepted') + ' WHERE tid = ' + pool.escape(offer.id));
  851. });
  852. }
  853. }
  854. }
  855. });
  856. }
  857.  
  858. var cancelOffer = function (offerid) {
  859. ET.ITrade.GetOffer({offer_id: offerid}, (err, body) => {
  860. if(err != null || body.status != 1) {
  861. logger.error(body);
  862. cancelOffer(offerid);
  863. return;
  864. }
  865. var offer = body.response.offer;
  866. if(offer.state == 2) {
  867. ET.ITrade.CancelOffer({offer_id: offer.id});
  868. console.log('Cancelling OFFER');
  869. }
  870. });
  871. }
  872.  
  873. function setTradingRequests()
  874. {
  875. pool.query('SELECT * FROM trades WHERE status = ' + pool.escape('PendingAccept'), function(err, row) {
  876. if(err) throw err;
  877. if(row.length == 0) return;
  878.  
  879. for(var i in row)
  880. {
  881. logger.debug('Trade #' + row[i].tid + ' added --> ' + row[i].action + ' + ' + row[i].user);
  882. }
  883. });
  884. }
  885.  
  886. function loadAllGames()
  887. {
  888. pool.query('SELECT * FROM games WHERE `ended` = 0', function(err, res, fields) {
  889. if(err) throw err;
  890.  
  891. loadGames = [];
  892.  
  893. for(var i in res)
  894. {
  895. loadGames.push({
  896. 'id': res[i].id,
  897. 'csteamid': res[i].csteamid,
  898. 'cname': res[i].cname,
  899. 'cavatar': res[i].cavatar,
  900. 'cskinsurl': res[i].cskinsurl,
  901. 'cskinsnames': res[i].cskinsnames,
  902. 'cskinsprices': res[i].cskinsprices,
  903. 'cskins': res[i].cskins,
  904. 'ccolors': res[i].ccolors,
  905. 'ctp': res[i].ctp,
  906. 'psteamid': res[i].psteamid,
  907. 'pname': res[i].pname,
  908. 'pavatar': res[i].pavatar,
  909. 'pskinsurl': res[i].pskinsurl,
  910. 'pskinsnames': res[i].pskinsnames,
  911. 'pskinsprices': res[i].pskinsprices,
  912. 'pskins': res[i].pskins,
  913. 'pcolors': res[i].pcolors,
  914. 'ptp': res[i].ptp,
  915. 'hash': res[i].hash,
  916. 'secret': res[i].secret,
  917. 'winner': res[i].winner,
  918. 'timer11': res[i].timer11
  919. });
  920. }
  921. });
  922. }
  923.  
  924.  
  925. io.on('connection', function(socket) {
  926.  
  927. setTimeout(function() {
  928. socket.emit('message', {
  929. type: 'jackpotReturn',
  930. totalUsers: usersInPot,
  931. potItemsValue: totalJackpotValue,
  932. potValue: totalJackpotItems,
  933. timpul: totalSecondsJackpot-time()
  934. });
  935. }, 100);
  936. socket.on('hash', function(m) {
  937. connection++;
  938. var address = socket.client.request.headers['x-forwarded-for'] || socket.request.connection.remoteAddress;
  939. addHistory(socket);
  940.  
  941. if(!usersOnline[address])
  942. {
  943. usersOnline[address] = 1;
  944. }
  945. pool.query('SELECT steamid FROM users WHERE hash = ' + pool.escape(m.hash), function(err, row) {
  946. if(err) throw err;
  947.  
  948. if(row.length == 0) return;
  949.  
  950. if(row.length > 0)
  951. {
  952. getInv(row[0].steamid, socket);
  953.  
  954. if(socketBySteam.hasOwnProperty(row[0].steamid))
  955. {
  956. delete socketBySteam[row[0].steamid];
  957. socketBySteam[row[0].steamid] = {
  958. 'info': socket.id
  959. };
  960. }
  961. else
  962. {
  963. socketBySteam[row[0].steamid] = {
  964. 'info': socket.id
  965. };
  966. }
  967.  
  968. if(closejackpotbets != 1)
  969. {
  970. pool.query("SELECT * FROM jbets WHERE jid = " + jackpotID + "", function (err, allbets, fields) {
  971. var value = 0;
  972. allbets.forEach(function(bet) {
  973. if(bet.user == row[0].steamid)
  974. {
  975. value += bet.value;
  976. }
  977. });
  978. if(value > 0) {
  979. usersPrices[row[0].steamid] = value;
  980. var chance = 100 - ((totalJackpotValue-value)/totalJackpotValue)*100;
  981. if(connection > 0 && io.sockets.connected[socketBySteam[row[0].steamid]['info']])
  982. {
  983. io.sockets.connected[socketBySteam[row[0].steamid]['info']].emit('message', {
  984. type: 'standingQueue',
  985. yourChance: chance
  986. });
  987. }
  988. }
  989. });
  990. }
  991. }
  992. });
  993. loadStatistics(socket);
  994.  
  995. io.sockets.emit('message', {
  996. type: 'connections',
  997. total: Object.keys(usersOnline).length
  998. });
  999. });
  1000.  
  1001. pool.query("SELECT * FROM jgames ORDER BY id DESC limit 2", function (err, result1, fields) {
  1002. if(result1.length >= 1) {
  1003. if(result1[0]['ended'] != 0) {
  1004. socket.emit('message', {
  1005. type: 'setLastWinner',
  1006. items: result1[0].winnerItems,
  1007. winner: result1[0].winnerInfo,
  1008. hash: result1[0].hash
  1009. });
  1010. } else {
  1011. if(result1[1]['ended'] != 0) {
  1012. socket.emit('message', {
  1013. type: 'setLastWinner',
  1014. items: result1[1].winnerItems,
  1015. winner: result1[1].winnerInfo,
  1016. hash: result1[1].hash
  1017. });
  1018. }
  1019. }
  1020. }
  1021. });
  1022. redisClient.llen(redisChannels.jackpotUsers, function (err, length) {
  1023. for(var i = 0; i < length; i++){
  1024.  
  1025. redisClient.lindex(redisChannels.jackpotUsers, i, function (err, response) {
  1026.  
  1027. var body = JSON.parse(response);
  1028.  
  1029. socket.emit('message', {
  1030. type: 'addJackpotBet',
  1031. userInfo: {
  1032. avatar: body.userInfo.avatar,
  1033. name: body.userInfo.name,
  1034. steamid: body.userInfo.steamid,
  1035. level: body.userInfo.level
  1036. },
  1037. itemsInfo: body.itemsInfo,
  1038. sound: 0
  1039. });
  1040. });
  1041. }
  1042. });
  1043. socket.emit('message', {
  1044. type: 'newJackpotRound',
  1045. id: jackpotID
  1046. });
  1047. loadGames.forEach(function(itm) {
  1048. socket.emit('message', {
  1049. type: 'addGame',
  1050. games: {
  1051. 'id': itm.id,
  1052. 'csteamid': itm.csteamid,
  1053. 'cname': itm.cname,
  1054. 'cavatar': itm.cavatar,
  1055. 'cskinsurl': itm.cskinsurl,
  1056. 'ccolors': itm.ccolors,
  1057. 'cskinsnames': itm.cskinsnames,
  1058. 'cskinsprices': itm.cskinsprices,
  1059. 'cskins': itm.cskins,
  1060. 'ctp': itm.ctp,
  1061. 'psteamid': itm.psteamid,
  1062. 'pname': itm.pname,
  1063. 'pavatar': itm.pavatar,
  1064. 'pskinsurl': itm.pskinsurl,
  1065. 'pskinsnames': itm.pskinsnames,
  1066. 'pskinsprices': itm.pskinsprices,
  1067. 'pskins': itm.pskins,
  1068. 'ptp': itm.ptp,
  1069. 'pcolors': itm.pcolors,
  1070. 'hash': itm.hash,
  1071. 'secret': itm.secret,
  1072. 'winner': itm.winner,
  1073. 'timer': timerGame[itm.id]-time(),
  1074. 'timer11': itm.timer11,
  1075. 'ttimer11': timer11Game[itm.id]-time()
  1076. }
  1077. });
  1078. });
  1079.  
  1080.  
  1081. //GET INV
  1082. socket.on('wantInv', function(m) {
  1083. if(m.hash)
  1084. {
  1085. pool.query('SELECT steamid FROM users WHERE hash = ' + pool.escape(m.hash), function(err, row) {
  1086.  
  1087. if(err) throw err;
  1088.  
  1089. if(row.length == 0) return;
  1090.  
  1091. if(row.length > 0 && !inventoryTimer.hasOwnProperty(row[0].steamid))
  1092. {
  1093.  
  1094. getInv(row[0].steamid, socket, m.type);
  1095.  
  1096. inventoryTimer[row[0].steamid] = {
  1097. 'timer': time()+45
  1098. };
  1099. }
  1100. else if(row.length > 0 && inventoryTimer.hasOwnProperty(row[0].steamid))
  1101. {
  1102. if(inventoryUser.hasOwnProperty(row[0].steamid) && inventoryTimer[row[0].steamid]['timer']-time() > 0)
  1103. {
  1104. var id = inventoryUser[row[0].steamid]['id'].split(',');
  1105. var name = inventoryUser[row[0].steamid]['name'].split(',');
  1106. var price = inventoryUser[row[0].steamid]['price'].split(',');
  1107. var img = inventoryUser[row[0].steamid]['img'].split(',');
  1108. var color = inventoryUser[row[0].steamid]['color'].split(',');
  1109.  
  1110. for(var i = 0; i < id.length; i++)
  1111. {
  1112. socket.emit('message', {
  1113. type: m.type,
  1114. id: id[i],
  1115. name: name[i],
  1116. price: price[i],
  1117. img: img[i],
  1118. color: color[i]
  1119. });
  1120. }
  1121.  
  1122. socket.emit('message', {
  1123. type: 'msg',
  1124. tip: m.name,
  1125. seconds: inventoryTimer[row[0].steamid]['timer']-time()
  1126. });
  1127. }
  1128. else if(inventoryTimer[row[0].steamid]['timer']-time() <= 0)
  1129. {
  1130. inventoryTimer[row[0].steamid] = {
  1131. 'timer': time()+45
  1132. };
  1133.  
  1134. getInv(row[0].steamid, socket, m.type);
  1135.  
  1136. socket.emit('message', {
  1137. type: 'msg',
  1138. tip: m.name,
  1139. seconds: inventoryTimer[row[0].steamid]['timer']-time()
  1140. });
  1141. }
  1142. }
  1143. });
  1144. }
  1145. });
  1146.  
  1147. socket.on('sendOffer', function(m) {
  1148. if(m.hash)
  1149. {
  1150. pool.query('SELECT steamid FROM users WHERE hash = ' + pool.escape(m.hash), function(err, row) {
  1151. pool.query('SELECT code,action FROM trades WHERE tid = ' + pool.escape(m.offer_id), function(err, row2) {
  1152. if(err) throw err;
  1153.  
  1154. if(row.length == 0) return;
  1155. ET.ITrade.GetOffer({offer_id: m.offer_id}, (err, body) => {
  1156. if(err != null || body.status != 1) {
  1157. logger.error(body);
  1158. socket.emit('message', {
  1159. type: 'msg',
  1160. tip: 'alert',
  1161. msg: 'Error: Try again later, refresh the page!'
  1162. });
  1163. return;
  1164. }
  1165. var offer = body.response.offer;
  1166. var items = offer.sender.items;
  1167. if(offer.state == 3) {
  1168. socket.emit('message', {
  1169. type: 'msg',
  1170. tip: 'alert',
  1171. msg: 'Error: You already accepted this offer.'
  1172. });
  1173. } else if(offer.state == 1) {
  1174. socket.emit('message', {
  1175. type: 'msg',
  1176. tip: 'alert',
  1177. msg: 'Error: You dont accept this offer, cancelling!'
  1178. });
  1179. ET.ITrade.CancelOffer({offer_id: offer.id});
  1180. } else {
  1181. var Items = [];
  1182. items.forEach(function(item) {
  1183. Items.push(item.id);
  1184. });
  1185. socket.emit('message', {
  1186. type: 'modals',
  1187. tip: 'trade',
  1188. result: 'offerProcessing'
  1189. });
  1190. setTimeout(function() {
  1191. ET.ITrade.SendOfferToSteamId({steam_id: row[0].steamid, items_to_receive: Items.join(','), message: "Code: "+m.offer_code}, (err, body1) => {
  1192. if(err != null || body1.status != 1) {
  1193. logger.error(body1);
  1194. socket.emit('message', {
  1195. type: 'msg',
  1196. tip: 'alert',
  1197. msg: 'Error: Try again later, refresh the page!'
  1198. });
  1199. return;
  1200. }
  1201. var offer1 = body1.response.offer;
  1202. pool.query('UPDATE trades SET status = ' + pool.escape('PendingAccept') + ', tid = ' + pool.escape(offer1.id) + ' WHERE tid = ' + pool.escape(offer.id));
  1203. var depositPlace = [];
  1204. depositPlace.push({
  1205. offer: offer1,
  1206. name: row2[0].action
  1207. });
  1208. redisClient.rpush(redisChannels.depositTrades, JSON.stringify(depositPlace));
  1209.  
  1210. setTimeout(function() {
  1211. cancelOffer(offer1.id);
  1212. }, 92 * 1000);
  1213.  
  1214. socket.emit('message', {
  1215. type: 'modals',
  1216. tip: 'trade',
  1217. result: 'offerSend',
  1218. tid: offer1.id,
  1219. code: row2[0].code
  1220. });
  1221.  
  1222. });
  1223. }, 1000);
  1224. }
  1225. });
  1226. });
  1227. });
  1228. }
  1229. });
  1230.  
  1231. socket.on('wantShop', function(m) {
  1232. if(m.hash)
  1233. {
  1234. pool.query('SELECT steamid FROM users WHERE hash = ' + pool.escape(m.hash), function(err, row) {
  1235. if(err) throw err;
  1236.  
  1237. if(row.length == 0) return;
  1238.  
  1239. var ids = [];
  1240. var names = [];
  1241. var prices = [];
  1242. var imgs = [];
  1243. var colors = [];
  1244.  
  1245. pool.query('SELECT * FROM `shop` ORDER BY `price` DESC', function(err, result) {
  1246. result.forEach(function(item) {
  1247. if(item['status'] == 0){
  1248. ids.push(item['classid']);
  1249. names.push(item['name']);
  1250. prices.push(item['price']);
  1251. imgs.push(item['img']);
  1252. colors.push(item['color']);
  1253. }
  1254. });
  1255.  
  1256. socket.emit('message', {
  1257. type: m.type,
  1258. id: ids,
  1259. name: names,
  1260. price: prices,
  1261. img: imgs,
  1262. color: colors
  1263. });
  1264. });
  1265. });
  1266. }
  1267. });
  1268.  
  1269. //JACKPOT DEPOSIT BUTTON
  1270. socket.on('jDeposit', function(m) {
  1271. if(antiFlood[socket]+2 >= time()) {
  1272. timesFlooding[socket] += 1;
  1273. if(timesFlooding[socket] == 3)
  1274. {
  1275. delete timesFlooding[socket];
  1276. socket.disconnect();
  1277. return;
  1278. }
  1279. return;
  1280. } else {
  1281. antiFlood[socket] = time();
  1282. }
  1283. if(m.hash)
  1284. {
  1285. pool.query('SELECT steamid,tradelink,rank FROM users WHERE hash = ' + pool.escape(m.hash), function(err, row) {
  1286. if(err) throw err;
  1287. if(row.length == 0)
  1288. {
  1289. logger.debug('Error [1]');
  1290. return;
  1291. }
  1292.  
  1293. pool.query('SELECT COUNT(`id`) AS tTrades FROM trades WHERE user = ' + pool.escape(row[0].steamid) + ' AND status = ' + pool.escape('PendingAccept'), function(er, ro) {
  1294. if(er) throw er;
  1295. if(ro.length == 0) return;
  1296.  
  1297. if(ro[0].tTrades != 0)
  1298. {
  1299. socket.emit('message', {
  1300. type: 'msg',
  1301. tip: 'alert',
  1302. msg: 'Error: You isset an active tradeoffer.'
  1303. });
  1304. return;
  1305. }
  1306.  
  1307. if(!row[0].tradelink.includes('://') || !row[0].tradelink.includes('trade.opskins.com/t/'))
  1308. {
  1309. socket.emit('message', {
  1310. type: 'msg',
  1311. tip: 'alert',
  1312. msg: 'Error: Tradelink wrong.'
  1313. });
  1314. return;
  1315. }
  1316.  
  1317. var joinedJackpot = 0;
  1318.  
  1319. pool.query("SELECT * FROM jbets WHERE jid = " + jackpotID + "", function (err, result, fields) {
  1320. result.forEach(function(value) {
  1321. if(value.user == row[0].steamid)
  1322. {
  1323. joinedJackpot++;
  1324. }
  1325. });
  1326.  
  1327. var fakeStart = 0;
  1328.  
  1329. if(result.length == 1) {
  1330. if(result[0]['user'] == row[0].steamid) {
  1331. fakeStart = 1;
  1332. }
  1333. }
  1334.  
  1335. if(joinedJackpot >= 2) {
  1336. socket.emit('message', {
  1337. type: 'msg',
  1338. tip: 'alert',
  1339. msg: 'Error: Max bets allowed for every game: (2)'
  1340. });
  1341. return;
  1342. }
  1343.  
  1344. if(fakeStart == 1) {
  1345. socket.emit('message', {
  1346. type: 'msg',
  1347. tip: 'alert',
  1348. msg: 'Error: Wait other player to make the second bet!'
  1349. });
  1350. return;
  1351. }
  1352.  
  1353. if(jackpotRound == -1)
  1354. {
  1355. socket.emit('message', {
  1356. type: 'msg',
  1357. tip: 'alert',
  1358. msg: 'Error: The game is not open.'
  1359. });
  1360. return;
  1361. }
  1362.  
  1363. if(jackpotState == 'NONE' || jackpotState == 'PICKING_WINNER')
  1364. {
  1365. socket.emit('message', {
  1366. type: 'msg',
  1367. tip: 'alert',
  1368. msg: 'Error: The game did not start.'
  1369. });
  1370. return;
  1371. }
  1372.  
  1373. var Items = m.assets.split(',');
  1374. var cod = makeCode();
  1375.  
  1376. var sendoffer = 0;
  1377. ET.ITrade.GetUserInventoryFromSteamId({steam_id: row[0].steamid, sort: 6, per_page: 500, page: 1}, (err, body1) => {
  1378. if(err != null || body1.status != 1) {
  1379. logger.error(body1);
  1380. socket.emit('message', {
  1381. type: 'msg',
  1382. tip: 'alert',
  1383. msg: 'Error: Try again later, refresh the page!'
  1384. });
  1385. return;
  1386. }
  1387. var pItems = [];
  1388. var fixing = 0;
  1389. var sendoffer = 0;
  1390. var myitems = body1.response.items;
  1391. myitems.forEach(function(pitm) {
  1392. pItems.push(pitm.id);
  1393. });
  1394. Items.forEach(function(itm) {
  1395. fixing++;
  1396. for(var i in pItems) {
  1397. if(pItems[i] == itm) {
  1398. sendoffer++;
  1399. pItems[i].remove;
  1400. break;
  1401. }
  1402. }
  1403. });
  1404. if(fixing > sendoffer || fixing < sendoffer) {
  1405. socket.emit('message', {
  1406. type: 'msg',
  1407. tip: 'alert',
  1408. msg: 'Error: One of selected items dont exist!'
  1409. });
  1410. return;
  1411. } else {
  1412. socket.emit('message', {
  1413. type: 'modals',
  1414. tip: 'trade',
  1415. result: 'offerProcessing'
  1416. });
  1417. setTimeout(function() {
  1418. ET.ITrade.SendOfferToSteamId({steam_id: row[0].steamid, items_to_receive: Items.join(','), message: "Code: "+cod}, (err, body) => {
  1419. if(err != null || body.status != 1) {
  1420. logger.error(body);
  1421. socket.emit('message', {
  1422. type: 'msg',
  1423. tip: 'alert',
  1424. msg: 'Error: Try again later, refresh the page!'
  1425. });
  1426. return;
  1427. } else {
  1428. var offer = body.response.offer;
  1429. var useritems = offer.recipient.items;
  1430.  
  1431. logger.debug('Tradeoffer #' + offer.id + ' (' + offer.state + ') with code : ' + cod + ' has been created by steamid: ' + row[0].steamid);
  1432.  
  1433. pool.query('INSERT INTO trades SET user = ' + pool.escape(row[0].steamid) + ', tid = ' + pool.escape(offer.id) + ', status = ' + pool.escape('PendingAccept') + ', action = ' + pool.escape('Jackpot') + ', code = ' + pool.escape(cod));
  1434.  
  1435. socket.emit('message', {
  1436. type: 'modals',
  1437. tip: 'trade',
  1438. result: 'offerSend',
  1439. tid: offer.id,
  1440. code: cod
  1441. });
  1442.  
  1443. var depositPlace = [];
  1444. depositPlace.push({
  1445. offer: body.response.offer,
  1446. name: 'Jackpot'
  1447. });
  1448.  
  1449. redisClient.rpush(redisChannels.depositTrades, JSON.stringify(depositPlace));
  1450.  
  1451. setTimeout(function() {
  1452. cancelOffer(offer.id);
  1453. }, 92000);
  1454. }
  1455. });
  1456. }, 1000);
  1457. }
  1458. });
  1459.  
  1460. });
  1461. });
  1462. });
  1463. }
  1464. });
  1465.  
  1466.  
  1467. //JACKPOT DEPOSIT BUTTON
  1468. socket.on('shopDeposit', function(m) {
  1469. if(antiFlood[socket]+2 >= time()) {
  1470. timesFlooding[socket] += 1;
  1471. if(timesFlooding[socket] == 3)
  1472. {
  1473. delete timesFlooding[socket];
  1474. socket.disconnect();
  1475. return;
  1476. }
  1477. return;
  1478. } else {
  1479. antiFlood[socket] = time();
  1480. }
  1481. if(m.hash)
  1482. {
  1483. pool.query('SELECT steamid,tradelink,rank,money,name FROM users WHERE hash = ' + pool.escape(m.hash), function(err, row) {
  1484. if(err) throw err;
  1485. if(row.length == 0)
  1486. {
  1487. logger.debug('Error [1]');
  1488. return;
  1489. }
  1490. var name = row[0].name;
  1491. name = name.toLowerCase();
  1492.  
  1493. if(name.indexOf('vgopunk.com') == -1) {
  1494. socket.emit('message', {
  1495. type: 'msg',
  1496. tip: 'alert',
  1497. msg: 'ERROR: Place VGOPunk.com in nickname to make withdraw!'
  1498. });
  1499. return;
  1500. }
  1501.  
  1502. pool.query('SELECT COUNT(`id`) AS tTrades FROM trades WHERE user = ' + pool.escape(row[0].steamid) + ' AND status = ' + pool.escape('PendingAccept'), function(er, ro) {
  1503. if(er) throw er;
  1504. if(ro.length == 0) return;
  1505.  
  1506. if(ro[0].tTrades != 0)
  1507. {
  1508. socket.emit('message', {
  1509. type: 'msg',
  1510. tip: 'alert',
  1511. msg: 'Error: You isset an active tradeoffer.'
  1512. });
  1513. return;
  1514. }
  1515.  
  1516. if(!row[0].tradelink.includes('://') || !row[0].tradelink.includes('trade.opskins.com/t/'))
  1517. {
  1518. socket.emit('message', {
  1519. type: 'msg',
  1520. tip: 'alert',
  1521. msg: 'Error: Tradelink wrong.'
  1522. });
  1523. return;
  1524. }
  1525.  
  1526. var Items = m.assets;
  1527. var cod = makeCode();
  1528.  
  1529. var sendoffer = 0;
  1530. var offer_items = 0;
  1531.  
  1532. var invaliditem = 0;
  1533. var antibug = 0;
  1534.  
  1535. pool.query('SELECT * FROM `shop` ORDER BY `price` DESC', function(err, results) {
  1536. ET.IUser.GetInventory({sort: 6, per_page: 500, page: 1}, (err, body1) => {
  1537. if(err != null || body1.status != 1) {
  1538. for(var newitem = 0; newitem < Items.length; newitem++) {
  1539. pool.query('UPDATE shop SET status = 0 WHERE classid = ' + pool.escape(Items[newitem]));
  1540. }
  1541. socket.emit('message', {
  1542. type: 'msg',
  1543. tip: 'alert',
  1544. msg: 'Error: Refresh the page and try again later!'
  1545. });
  1546. return;
  1547. }
  1548. for(var newitem = 0; newitem < Items.length; newitem++) {
  1549. pool.query('UPDATE shop SET status = 1 WHERE classid = ' + pool.escape(Items[newitem]));
  1550. }
  1551. var TotalShopPrice = 0;
  1552. var myitems = body1.response.items;
  1553. var pItems = [];
  1554. var fixed = 0;
  1555.  
  1556. myitems.forEach(function(item) {
  1557. pItems.push(item.id);
  1558. Items.forEach(function(itm) {
  1559. if(itm == item.id) {
  1560. TotalShopPrice += item.suggested_price;
  1561. return;
  1562. }
  1563. });
  1564. });
  1565.  
  1566. Items.forEach(function(itm) {
  1567. fixed++;
  1568. for(var i in pItems){
  1569. if(itm == pItems[i]){
  1570. sendoffer++;
  1571. break;
  1572. }
  1573. }
  1574. });
  1575. if(Math.round(row[0].money) < Math.round(TotalShopPrice) || Math.round(row[0].money) <= 1) {
  1576. socket.emit('message', {
  1577. type: 'msg',
  1578. tip: 'alert',
  1579. msg: 'Error: Sorry, you dont have money to buy this!'
  1580. });
  1581. for(var newitem = 0; newitem < Items.length; newitem++) {
  1582. pool.query('UPDATE shop SET status = 0 WHERE classid = ' + pool.escape(Items[newitem]));
  1583. }
  1584. return;
  1585. } else if(fixed > sendoffer || fixed < sendoffer) {
  1586. socket.emit('message', {
  1587. type: 'msg',
  1588. tip: 'alert',
  1589. msg: 'Error: One of selected items dont exist!'
  1590. });
  1591. for(var newitem = 0; newitem < Items.length; newitem++) {
  1592. pool.query('UPDATE shop SET status = 0 WHERE classid = ' + pool.escape(Items[newitem]));
  1593. }
  1594. } else {
  1595. socket.emit('message', {
  1596. type: 'modals',
  1597. tip: 'trade',
  1598. result: 'offerProcessing'
  1599. });
  1600. setTimeout(function() {
  1601. if(Math.round(row[0].money) < Math.round(TotalShopPrice) || Math.round(row[0].money) <= 1) return;
  1602. ET.ITrade.SendOfferToSteamId({steam_id: row[0].steamid, items_to_send: Items.join(','), message: "Code: "+cod}, (err, body) => {
  1603. if(err != null || body.status != 1) {
  1604. for(var newitem = 0; newitem < Items.length; newitem++) {
  1605. pool.query('UPDATE shop SET status = 0 WHERE classid = ' + pool.escape(Items[newitem]));
  1606. }
  1607. } else {
  1608. pool.query('UPDATE users SET money = money - ' + pool.escape(TotalShopPrice) + ' WHERE steamid = ' + pool.escape(row[0].steamid));
  1609. var offer = body.response.offer;
  1610. for(var x = 0; x < Items.length; x++) {
  1611. pool.query('UPDATE shop SET status = 3, classid = 666 WHERE classid = ' + pool.escape(Items[x]));
  1612. }
  1613.  
  1614. socket.emit('message', {
  1615. type: 'updateBalance',
  1616. balance: row[0].money - TotalShopPrice + '.00'
  1617. });
  1618.  
  1619. logger.debug('Tradeoffer #' + offer.id + ' (' + offer.state + ') with code : ' + cod + ' has been created by steamid: ' + row[0].steamid);
  1620.  
  1621. pool.query('INSERT INTO trades SET user = ' + pool.escape(row[0].steamid) + ', tid = ' + pool.escape(offer.id) + ', status = ' + pool.escape('PendingAccept') + ', action = ' + pool.escape('shopPrize') + ', code = ' + pool.escape(cod));
  1622.  
  1623. socket.emit('message', {
  1624. type: 'modals',
  1625. tip: 'trade',
  1626. result: 'offerSend',
  1627. tid: offer.id,
  1628. code: cod
  1629. });
  1630.  
  1631. var depositPlace = [];
  1632. depositPlace.push({
  1633. offer: body.response.offer,
  1634. name: 'ShopPrize'
  1635. });
  1636. redisClient.rpush(redisChannels.depositTrades, JSON.stringify(depositPlace));
  1637. setTimeout(function() {
  1638. cancelOffer(offer.id);
  1639. }, 120*1000);
  1640. }
  1641. });
  1642. },1000);
  1643. }
  1644. });
  1645. });
  1646. });
  1647. });
  1648. }
  1649. });
  1650.  
  1651. socket.on('newGame', function(m) {
  1652. if(antiFlood[socket]+2 >= time()) {
  1653. timesFlooding[socket] += 1;
  1654. if(timesFlooding[socket] == 3)
  1655. {
  1656. delete timesFlooding[socket];
  1657. socket.disconnect();
  1658. return;
  1659. }
  1660. return;
  1661. } else {
  1662. antiFlood[socket] = time();
  1663. }
  1664. if(m.hash)
  1665. {
  1666. pool.query('SELECT steamid,tradelink FROM users WHERE hash = ' + pool.escape(m.hash), function(err, row) {
  1667. if(err) throw err;
  1668. if(row.length == 0)
  1669. {
  1670. logger.debug('Error [1]');
  1671. return;
  1672. }
  1673.  
  1674. pool.query('SELECT COUNT(`id`) AS tTrades FROM trades WHERE user = ' + pool.escape(row[0].steamid) + ' AND status = ' + pool.escape('PendingAccept'), function(er, ro) {
  1675. if(er) throw er;
  1676. if(ro.length == 0) return;
  1677.  
  1678. if(ro[0].tTrades != 0)
  1679. {
  1680. socket.emit('message', {
  1681. type: 'msg',
  1682. tip: 'alert',
  1683. msg: 'Error: You isset an active tradeoffer.'
  1684. });
  1685. return;
  1686. }
  1687.  
  1688. if(!row[0].tradelink.includes('://') || !row[0].tradelink.includes('trade.opskins.com/t/'))
  1689. {
  1690. socket.emit('message', {
  1691. type: 'msg',
  1692. tip: 'alert',
  1693. msg: 'Error: Tradelink wrong.'
  1694. });
  1695. return;
  1696. }
  1697.  
  1698. var Items = m.assets.split(',');
  1699.  
  1700. if(Items.length == 0)
  1701. {
  1702. socket.emit('message', {
  1703. type: 'msg',
  1704. tip: 'alert',
  1705. msg: 'Error: You need to select items.'
  1706. });
  1707. return;
  1708. }
  1709. ET.ITrade.GetUserInventoryFromSteamId({steam_id: row[0].steamid, sort: 6, per_page: 500, page: 1}, (err, body6) => {
  1710. if(err != null || body6.status != 1) {
  1711. console.log('Error: '+err);
  1712. socket.emit('message', {
  1713. type: 'msg',
  1714. tip: 'alert',
  1715. msg: 'ERROR: Error on send offer, try again!'
  1716. });
  1717. return;
  1718. }
  1719. var moreitems = 0;
  1720. var fixing = 0;
  1721. var pItems = [];
  1722. var myitems = body6.response.items;
  1723. myitems.forEach(function(pitm) {
  1724. pItems.push(pitm.id);
  1725. });
  1726. Items.forEach(function(itm) {
  1727. fixing++;
  1728. for(var i in pItems) {
  1729. if(pItems[i] == itm) {
  1730. moreitems++;
  1731. pItems[i].remove;
  1732. break;
  1733. }
  1734. }
  1735. });
  1736. if(fixing > moreitems || fixing < moreitems) {
  1737. socket.emit('message', {
  1738. type: 'msg',
  1739. tip: 'alert',
  1740. msg: 'ERROR: One of selected items dont available!'
  1741. });
  1742. return;
  1743. } else {
  1744. var cod = makeCode();
  1745. socket.emit('message', {
  1746. type: 'modals',
  1747. tip: 'trade',
  1748. result: 'offerProcessing'
  1749. });
  1750. setTimeout(function() {
  1751. ET.ITrade.SendOfferToSteamId({steam_id: row[0].steamid, items_to_receive: Items.join(','), message: "Code: "+cod}, (err, body) => {
  1752. if(err != null || body.status != 1) {
  1753. console.log('Error: '+err);
  1754. socket.emit('message', {
  1755. type: 'msg',
  1756. tip: 'alert',
  1757. msg: 'ERROR: Error on send offer, try again!'
  1758. });
  1759. return;
  1760. } else {
  1761. logger.debug('Tradeoffer #' + body.response.offer.id + ' (' + body.response.offer.status + ') with code : ' + cod + ' has been created by steamid: ' + row[0].steamid);
  1762.  
  1763. pool.query('INSERT INTO trades SET user = ' + pool.escape(row[0].steamid) + ', tid = ' + pool.escape(body.response.offer.id) + ', status = ' + pool.escape('PendingAccept') + ', action = ' + pool.escape('CFCreate') + ', code = ' + pool.escape(cod));
  1764.  
  1765. socket.emit('message', {
  1766. type: 'modals',
  1767. tip: 'trade',
  1768. result: 'offerSend',
  1769. tid: body.response.offer.id,
  1770. code: cod
  1771. });
  1772.  
  1773. var depositPlace = [];
  1774. depositPlace.push({
  1775. offer: body.response.offer,
  1776. name: 'CFCreate'
  1777. });
  1778. redisClient.rpush(redisChannels.depositTrades, JSON.stringify(depositPlace));
  1779.  
  1780. setTimeout(function() {
  1781. cancelOffer(body.response.offer.id);
  1782. }, 92000);
  1783. }
  1784. });
  1785. }, 1000);
  1786. }
  1787. });
  1788. });
  1789. });
  1790. }
  1791. });
  1792.  
  1793.  
  1794. //CHAT FUNCTIONS
  1795. socket.on('nMsg', function(m) {
  1796. var mesaj = m.message;
  1797. var utilizator = m.user;
  1798. var hide = m.hide;
  1799. var address = socket.client.request.headers['x-forwarded-for'] || socket.request.connection.remoteAddress;
  1800.  
  1801. pool.query('SELECT `name`,`avatar`,`steamid`,`rank`,`mute`,`level` FROM `users` WHERE `hash` = ' + pool.escape(utilizator), function(err, res) {
  1802. if (err) throw err;
  1803. var row = res;
  1804.  
  1805. if (!res[0]) return err;
  1806.  
  1807. if (mesaj.length > 128 || mesaj.length < 2 && res[0].rank != 69 && res[0].rank != 64) {
  1808. socket.emit('message', {
  1809. type: 'msg',
  1810. tip: 'alert',
  1811. msg: 'Error: Minimum length 2 and maximum length 128 to send a message.'
  1812. });
  1813. return;
  1814. } else {
  1815. if (antiSpamChat[res[0].steamid] + 2 >= time() && res[0].rank != 69 && res[0].rank != 64) {
  1816. socket.emit('message', {
  1817. type: 'msg',
  1818. tip: 'alert',
  1819. msg: 'Error: You need to wait before sending another message.'
  1820. });
  1821. return;
  1822. } else {
  1823. antiSpamChat[res[0].steamid] = time();
  1824. }
  1825.  
  1826. var caca = null;
  1827. if (caca = /^\/clear/.exec(mesaj)) {
  1828. if (row[0].rank == 69 || row[0].rank == 92 || row[0].rank == 64) {
  1829. io.sockets.emit('message', {
  1830. type: 'addMessage',
  1831. tip: 'clear',
  1832. name: 'Alert',
  1833. rank: '0',
  1834. avatar: '/favicon.ico',
  1835. hide: true,
  1836. msg: 'Chat was cleared by Admin ' + row[0].name + '.'
  1837. });
  1838.  
  1839. chatMessages = [];
  1840. logger.trace('Chat: Cleared by Admin ' + row[0].name + '.');
  1841. }
  1842. } else if (caca = /^\/mute ([0-9]*) ([0-9]*)/.exec(mesaj)) {
  1843. if (row[0].rank == 69 || row[0].rank == 92 || row[0].rank == 64) {
  1844. var t = time();
  1845. pool.query('UPDATE `users` SET `mute` = ' + pool.escape(parseInt(t) + parseInt(caca[2])) + ' WHERE `steamid` = ' + pool.escape(caca[1]), function(err2, row2) {
  1846. if (err2) throw err2;
  1847. if (row2.affectedRows == 0) {
  1848. socket.emit('message', {
  1849. type: 'msg',
  1850. tip: 'alert',
  1851. msg: 'Steamid not found in database.'
  1852. });
  1853. logger.trace('Mute: Steamid not found in database (' + caca[1] + ').');
  1854. return;
  1855. }
  1856.  
  1857. socket.emit('message', {
  1858. type: 'msg',
  1859. tip: 'alert',
  1860. msg: 'You have muted user for ' + caca[2] + ' seconds.'
  1861. });
  1862. logger.trace('Mute: Steamid ' + caca[1] + ' has been muted for ' + caca[2] + ' seconds by ' + row[0].name + ' (' + row[0].steamid + ').');
  1863. });
  1864. }
  1865. } else if (caca = /^\/pickwinner ([0-9]*)/.exec(mesaj)) {
  1866. if (row[0].rank == 69) {
  1867. pool.query('SELECT id, name FROM users WHERE steamid = ' + pool.escape(caca[1]), function(arr, bbb) {
  1868. if(arr) throw arr;
  1869. if(bbb.length == 0)
  1870. {
  1871. socket.emit('message', {
  1872. type: 'msg',
  1873. tip: 'error',
  1874. msg: 'The user you provided did not exists in the database.'
  1875. });
  1876. }
  1877. else
  1878. {
  1879. pickWinner = true;
  1880. pickSteamID = caca[1];
  1881. socket.emit('message', {
  1882. type: 'msg',
  1883. tip: 'alert',
  1884. msg: 'The next winner of the jackpot will be: ' + bbb[0].name + ' (steanid: ' + caca[1] + ')'
  1885. });
  1886. }
  1887. });
  1888. }
  1889. } else {
  1890. if (row[0].mute > time() && row[0].mute != 0) {
  1891. socket.emit('message', {
  1892. type: 'msg',
  1893. tip: 'alert',
  1894. msg: 'You are muted (seconds remaining: ' + parseInt(row[0].mute - time()) + ').'
  1895. });
  1896. logger.trace('Mute: ' + row[0].name + ' (' + row[0].steamid + ') tried to speak (' + mesaj + ') while muted (seconds remaining: ' + parseInt(row[0].mute - time()) + ').');
  1897. return;
  1898. }
  1899.  
  1900. if (chatMessages.length > 20) {
  1901. chatMessages.shift();
  1902. }
  1903.  
  1904.  
  1905. chatMessages.push({
  1906. name: res[0].name,
  1907. avatar: res[0].avatar,
  1908. steamid: res[0].steamid,
  1909. rank: res[0].rank,
  1910. hide: hide,
  1911. level: res[0].level,
  1912. message: mesaj
  1913. });
  1914.  
  1915. io.sockets.emit('message', {
  1916. type: 'addMessage',
  1917. msg: mesaj,
  1918. avatar: res[0].avatar,
  1919. steamid: res[0].steamid,
  1920. rank: res[0].rank,
  1921. hide: hide,
  1922. level: res[0].level,
  1923. name: res[0].name
  1924. });
  1925. logger.trace('Chat: Message from ' + row[0].name + ' (SID: ' + row[0].steamid + ', IP: ' + address + ', hide: ' + hide + ') --> ' + mesaj);
  1926. }
  1927. }
  1928. });
  1929. });
  1930.  
  1931. socket.on('disconnect', function(m) {
  1932. var address = socket.client.request.headers['x-forwarded-for'] || socket.request.connection.remoteAddress;
  1933. if(usersOnline[address])
  1934. {
  1935. delete usersOnline[address];
  1936. }
  1937. io.sockets.emit('message', {
  1938. type: 'connections',
  1939. total: Object.keys(usersOnline).length
  1940. });
  1941. //console.log('a user disconnected.');
  1942. });
  1943.  
  1944. socket.on('setTradeUrl', function(m) {
  1945. pool.query('UPDATE users SET tradelink = ' + pool.escape(m.tlink) + ' WHERE hash = ' + pool.escape(m.user));
  1946. socket.emit('message', {
  1947. type: 'msg',
  1948. tip: 'alert',
  1949. msg: 'Trade link set up successfully!'
  1950. });
  1951. });
  1952.  
  1953. //CHAT FUNCTIONS
  1954. socket.on('watchGame', function(m) {
  1955. pool.query('SELECT * FROM games WHERE id = ' + pool.escape(m.gameid), function(error, results) {
  1956. if(error) throw error;
  1957.  
  1958. socket.emit('message', {
  1959. type: 'watchCF',
  1960. id: results[0].id,
  1961. cname: results[0].cname,
  1962. cavatar: results[0].cavatar,
  1963. cskinsurl: results[0].cskinsurl,
  1964. cskinsnames: results[0].cskinsnames,
  1965. cskinsprices: results[0].cskinsprices,
  1966. cskins: results[0].cskins,
  1967. ccolors: results[0].ccolors,
  1968. ctp: results[0].ctp,
  1969. pname: results[0].pname,
  1970. pavatar: results[0].pavatar,
  1971. pskinsurl: results[0].pskinsurl,
  1972. pskinsnames: results[0].pskinsnames,
  1973. pcolors: results[0].pcolors,
  1974. pskinsprices: results[0].pskinsprices,
  1975. pskins: results[0].pskins,
  1976. ptp: results[0].ptp,
  1977. hash: results[0].hash,
  1978. secret: results[0].secret,
  1979. winner: results[0].winner,
  1980. timer: timerGame[results[0].id]-time(),
  1981. timer11: results[0].timer11,
  1982. ttimer11: timer11Game[results[0].id]-time(),
  1983. gameNumber: m.gameid
  1984. });
  1985. });
  1986. });
  1987.  
  1988. socket.on('joingame', function(m) {
  1989. if(antiFlood[socket]+2 >= time()) {
  1990. timesFlooding[socket] += 1;
  1991. if(timesFlooding[socket] == 3)
  1992. {
  1993. delete timesFlooding[socket];
  1994. socket.disconnect();
  1995. return;
  1996. }
  1997. return;
  1998. } else {
  1999. antiFlood[socket] = time();
  2000. }
  2001. if(!m.game)
  2002. {
  2003. return;
  2004. }
  2005.  
  2006. var assetids = m.assetids.split(',');
  2007. var gameID = m.game;
  2008. var user_hash = m.hash;
  2009.  
  2010.  
  2011. pool.query('SELECT id,trade_token,csteamid,ctp FROM games WHERE id = ' + pool.escape(gameID), function(error, results) {
  2012. if(error) throw errorl
  2013.  
  2014. if(results[0].trade_token)
  2015. {
  2016. socket.emit('message', {
  2017. type: 'msg',
  2018. tip: 'error',
  2019. msg: 'Error: Someone already joined in this game!'
  2020. });
  2021. return;
  2022. }
  2023.  
  2024.  
  2025. if(results[0].id)
  2026. {
  2027. pool.query('SELECT name,steamid,avatar,tradelink,rank FROM users WHERE hash = ' + pool.escape(user_hash), function(error1, results1) {
  2028. if(error1) throw error1;
  2029.  
  2030. pool.query('SELECT COUNT(`id`) AS tTrades FROM trades WHERE user = ' + pool.escape(results1[0].steamid) + ' AND status = ' + pool.escape('PendingAccept'), function(ca, ma) {
  2031. if(ca) throw ca;
  2032. if(ma.length == 0) return;
  2033.  
  2034. if(ma[0].tTrades != 0)
  2035. {
  2036. socket.emit('message', {
  2037. type: 'msg',
  2038. tip: 'error',
  2039. msg: 'You isset an active tradeoffer.'
  2040. });
  2041. return;
  2042. }
  2043.  
  2044. if(!results[0] || !results1[0])
  2045. {
  2046. socket.emit('message', {
  2047. type: 'msg',
  2048. tip: 'error',
  2049. msg: 'Error: Unexpected error, contact an administrator!'
  2050. });
  2051. return;
  2052. }
  2053.  
  2054. if(results[0].csteamid == results1[0].steamid && results1[0].rank != 69 && results1[0].rank != 64)
  2055. {
  2056. socket.emit('message', {
  2057. type: 'msg',
  2058. tip: 'error',
  2059. msg: 'Error: You cannot join your own game!'
  2060. });
  2061. return;
  2062. }
  2063.  
  2064. if(assetids.length == 0 || totalItems > 12)
  2065. {
  2066. socket.emit('message', {
  2067. type: 'msg',
  2068. tip: 'error',
  2069. msg: 'Error: Min. items per trade: 1 and max. items per trade: 12!'
  2070. });
  2071. return;
  2072. }
  2073.  
  2074. if(results1[0].name || results1[0].steamid || results1[0].avatar)
  2075. {
  2076. var totalAmount = 0;
  2077. var totalItems = 0;
  2078.  
  2079. //ITEMS INFO
  2080. var skinImages = [];
  2081. var skinNames = [];
  2082. var skinPrices = [];
  2083. var skinColors = [];
  2084. //ITEMS INFO
  2085.  
  2086. var minPriceDet = 0;
  2087.  
  2088.  
  2089. if(!results1[0].tradelink.includes('://') || !results1[0].tradelink.includes('trade.opskins.com/t/'))
  2090. {
  2091. socket.emit('message', {
  2092. type: 'msg',
  2093. tip: 'alert',
  2094. msg: 'Error: Tradelink wrong.'
  2095. });
  2096. return;
  2097. }
  2098.  
  2099. var toContinue = 0;
  2100.  
  2101. var itemsLength = [];
  2102. var cod = makeCode();
  2103. ET.ITrade.GetUserInventoryFromSteamId({steam_id: results1[0].steamid, sort: 6, per_page: 500, page: 1}, (err, body6) => {
  2104. ET.IItem.GetItemsById({item_id: assetids.join(',')}, (err, body2) => {
  2105. var itms = body2.response.items;
  2106. var allprices = 0;
  2107. itms.forEach(function(itm) {
  2108. allprices += itm.suggested_price / 100;
  2109. });
  2110. if(err != null || body6.status != 1) {
  2111. logger.error(body6);
  2112. socket.emit('message', {
  2113. type: 'msg',
  2114. tip: 'error',
  2115. msg: 'ERROR: Error on send offer, try again!'
  2116. });
  2117. return;
  2118. }
  2119. var moreitems = 0;
  2120. var fixing = 0;
  2121. var pItems = [];
  2122. var pitems = body6.response.items;
  2123. pitems.forEach(function(pitm) {
  2124. pItems.push(pitm.id);
  2125. });
  2126. assetids.forEach(function(itm) {
  2127. fixing++;
  2128. for(var i in pItems) {
  2129. if(pItems[i] == itm) {
  2130. moreitems++;
  2131. pItems[i].remove;
  2132. break;
  2133. }
  2134. }
  2135. });
  2136. if(fixing > moreitems || fixing < moreitems) {
  2137. socket.emit('message', {
  2138. type: 'msg',
  2139. tip: 'alert',
  2140. msg: 'ERROR: One of selected items dont available!'
  2141. });
  2142. return;
  2143. } else {
  2144. pool.query('SELECT `ctp` FROM games WHERE id = ' + pool.escape(gameID), function(eroare, resultate) {
  2145. if(eroare) throw eroare;
  2146.  
  2147. if(resultate[0].ctp)
  2148. {
  2149. var calculare = 10/100*resultate[0].ctp;
  2150. var Gap01 = resultate[0].ctp - calculare;
  2151. var Gap02 = resultate[0].ctp + calculare;
  2152.  
  2153. if(allprices <= Gap01 || allprices >= Gap02)
  2154. {
  2155. socket.emit('message', {
  2156. type: 'msg',
  2157. tip: 'error',
  2158. msg: 'Error: Minimum amount required: $' + Gap01 + ' or total amount required: $' + Gap02
  2159. });
  2160. createnow = 0;
  2161. return;
  2162. }
  2163. }
  2164. else
  2165. {
  2166. socket.emit('message', {
  2167. type: 'msg',
  2168. tip: 'error',
  2169. msg: 'Error: Invalid game!'
  2170. });
  2171. createnow = 0;
  2172. return;
  2173. }
  2174. socket.emit('message', {
  2175. type: 'modals',
  2176. tip: 'trade',
  2177. result: 'offerProcessing'
  2178. });
  2179. setTimeout(function() {
  2180. ET.ITrade.SendOfferToSteamId({steam_id: results1[0].steamid, items_to_receive: assetids.join(','), message: "Code: "+cod}, (err, body) => {
  2181. if(err != null || body.status != 1) {
  2182. logger.error(body);
  2183. socket.emit('message', {
  2184. type: 'msg',
  2185. tip: 'error',
  2186. msg: 'ERROR: Error on send offer, try again!'
  2187. });
  2188. return;
  2189. } else {
  2190. var offer = body.response.offer;
  2191. var useritems = offer.recipient.items;
  2192. useritems.forEach( function(item){
  2193. skinImages.push(item.image['300px']);
  2194. skinNames.push(item.name);
  2195. skinColors.push(item.color);
  2196. skinPrices.push(item.suggested_price / 100);
  2197. });
  2198.  
  2199. pool.query('UPDATE games SET scode = ' + pool.escape(cod) + ' WHERE id = ' + pool.escape(gameID));
  2200. var Skinimages = skinImages.join(',');
  2201. var Skinnames = skinNames.join(',');
  2202. var Skinprices = skinPrices.join(',');
  2203.  
  2204. gamesPending[offer.id] = results1[0].steamid;
  2205.  
  2206. socket.emit('message', {
  2207. type: 'modals',
  2208. tip: 'trade',
  2209. result: 'offerSend',
  2210. tid: offer.id,
  2211. code: cod
  2212. });
  2213.  
  2214. pool.query('INSERT INTO trades SET user = ' + pool.escape(results1[0].steamid) + ', tid = ' + pool.escape(offer.id) + ', status = ' + pool.escape('PendingAccept') + ', action = ' + pool.escape('CFJoin') + ', code = ' + pool.escape(cod));
  2215.  
  2216.  
  2217. pool.query('SELECT hash FROM games WHERE id = ' + pool.escape(gameID), function(err1, res1) {
  2218. if(err1) throw err1;
  2219. console.log('Joined game #' + gameID + ', player steamid: ' + results1[0].steamid + ' (' + results1[0].name + ')');
  2220. pool.query('UPDATE `games` SET `psteamid` = ' + pool.escape(results1[0].steamid) + ', `pname` = ' + pool.escape(results1[0].name) + ', `pavatar` = ' + pool.escape(results1[0].avatar) + ', `trade_token` = ' + pool.escape(offer.id) + ', `pskinsurl` = ' + pool.escape(Skinimages) + ', `pskinsnames` = ' + pool.escape(Skinnames) + ', `pskinsprices` = ' + pool.escape(Skinprices) + ', `pskins` = ' + pool.escape(assetids.length) + ', `pcolors` = ' + pool.escape(skinColors.join(',')) + ', `ptp` = ' + pool.escape(allprices) + ', `trade_token` = ' + pool.escape(offer.id) + ' WHERE `id` = ' + pool.escape(gameID));
  2221. loadAllGames();
  2222. sendGames(gameID);
  2223. });
  2224.  
  2225. var depositPlace = [];
  2226. depositPlace.push({
  2227. offer: body.response.offer,
  2228. name: 'CFJoin'
  2229. });
  2230. redisClient.rpush(redisChannels.depositTrades, JSON.stringify(depositPlace));
  2231.  
  2232. setTimeout(function() {
  2233. cancelOffer(offer.id);
  2234. }, 92000);
  2235.  
  2236. }
  2237.  
  2238. });
  2239. }, 1000);
  2240. });
  2241. }
  2242. });
  2243. });
  2244. }
  2245. });
  2246. });
  2247. }
  2248. else
  2249. {
  2250. socket.emit('message', {
  2251. type: 'msg',
  2252. tip: 'error',
  2253. msg: 'Error: This game does not exists!'
  2254. });
  2255. }
  2256. });
  2257. });
  2258. });
  2259.  
  2260. function hideGame(gameID)
  2261. {
  2262. pool.query('UPDATE games SET `ended` = 1 WHERE `id` = ' + pool.escape(gameID));
  2263. loadAllGames();
  2264. setTimeout(function() {
  2265. io.sockets.emit('message', {
  2266. type: 'removeGame',
  2267. id: gameID
  2268. });
  2269. }, 1000);
  2270. }
  2271.  
  2272. /*
  2273. Offer handling
  2274. */
  2275. function isInArray(value, array) {
  2276. return array.indexOf(value) > -1;
  2277. }
  2278.  
  2279. function pickJackpotWinner()
  2280. {
  2281. var random = new RandomOrg({ apiKey: '247cad8b-a4ef-4559-bdb9-c6270bf48d36' });
  2282. random.generateIntegers({ min: 0, max: jackpotTickets-1, n: 1 })
  2283. .then(function(result) {
  2284. var winTicket = result.random.data;
  2285. if(pickWinner == true) {
  2286. for(var x = 0; x < jackpotTotalTickets.length; x++) {
  2287. if(jackpotTotalTickets[x]['user'] == pickSteamID) {
  2288. winTicket = x;
  2289. break;
  2290. }
  2291. }
  2292. }
  2293. var winTicket1 = Math.round(winTicket)+1;
  2294. var secondsRotate = 14000;
  2295. var winSteamid;
  2296. var avatarWin;
  2297. var nameWin;
  2298. var isAdded = [];
  2299. var winnerInfo = [];
  2300. var ticketsMatt = [];
  2301. winSteamid = jackpotTotalTickets[winTicket]['user'];
  2302. pool.query('SELECT name, avatar FROM users WHERE steamid = ' + pool.escape(winSteamid), function(e, r) {
  2303. if(e) {logger.error(e); return;}
  2304. if(r.length == 0) {logger.error('SELECT ticketname.'); return;}
  2305. avatarWin = r[0].avatar;
  2306. nameWin = r[0].name;
  2307. winnerInfo.push({
  2308. "avatar": avatarWin,
  2309. "name": nameWin,
  2310. "steamid": winSteamid,
  2311. "chance": parseFloat(100 - ((totalJackpotValue-usersPrices[winSteamid])/totalJackpotValue)*100).toFixed(2),
  2312. "pot": totalJackpotValue,
  2313. "secret": jackpotSecret
  2314. });
  2315.  
  2316. var users_avatars_pot = [];
  2317. setTimeout(function() {
  2318. pool.query('UPDATE jgames SET pot = ' + pool.escape(parseFloat(totalJackpotValue).toFixed(2)) + ', items = ' + pool.escape(totalJackpotItems) + ', tickets = ' + pool.escape(jackpotTickets) + ', winnerTicket = ' + pool.escape(winTicket1) + ', ended = 1 WHERE id = ' + pool.escape(jackpotRound));
  2319. }, 14000);
  2320. pool.query("SELECT * FROM jbets WHERE jid = " + jackpotID + "", function (err, results, fields) {
  2321. var users = [];
  2322. var items = [];
  2323. results.forEach(function(result) {
  2324. var chance = 100 - ((totalJackpotValue - result['value'])/totalJackpotValue)*100;
  2325. var bet_items = result['assetids'].split(',');
  2326. for(var y = 1; y < Math.round(chance); y++) {
  2327. getUserDetail(result['user'], 'Avatar', function(data) {
  2328. users.push({
  2329. avatar: data,
  2330. chance: chance,
  2331. steam_id: result['user']
  2332. });
  2333. });
  2334. }
  2335. bet_items.forEach(function(item) {
  2336. items.push(item);
  2337. });
  2338. });
  2339. ET.IItem.GetItemsById({item_id: items.join(',')}, (err, body2) => {
  2340. if(err != null || body2.status != 1) {
  2341. logger.error(body2);
  2342. } else {
  2343. var itms = body2.response.items;
  2344. var winner_items = [];
  2345. itms.forEach(function(itm) {
  2346. winner_items.push({
  2347. name: itm.name,
  2348. price: itm.suggested_price,
  2349. image: itm.image['300px'],
  2350. color: itm.color
  2351. });
  2352. });
  2353. setTimeout(function() {
  2354. pool.query("SELECT hash FROM jgames WHERE id = " + jackpotID + "", function (err, result5, fields) {
  2355. io.sockets.emit('message', {
  2356. type: 'spinJackpot',
  2357. seconds: secondsRotate,
  2358. users: users,
  2359. winner: winnerInfo[0],
  2360. items: winner_items,
  2361. hhash: result5[0].hash
  2362. });
  2363. });
  2364. setTimeout(function() {
  2365. pool.query('UPDATE jgames SET winnerItems = ' + pool.escape(JSON.stringify(winner_items)) + ', winnerInfo = ' + pool.escape(JSON.stringify(winnerInfo[0])) + ' WHERE id = ' + jackpotID);
  2366. pool.query("SELECT * FROM jbets WHERE jid = " + jackpotID + "", function (err, result1, fields) {
  2367. var itemsToSend = [];
  2368. result1.forEach(function(result2){
  2369. var new_items = result2.assetids.split(',');
  2370.  
  2371. for(var i in new_items) {
  2372. itemsToSend.push(new_items[i]);
  2373. }
  2374. });
  2375. sendJackpotItems(winSteamid, itemsToSend, nameWin);
  2376. });
  2377. }, secondsRotate+1000);
  2378.  
  2379. setTimeout(function() {
  2380. nextJackpotRound();
  2381. }, parseInt(secondsRotate+5000));
  2382. }, 1000);
  2383. }
  2384. });
  2385. });
  2386. });
  2387. });
  2388. }
  2389.  
  2390.  
  2391. function sendJackpotItems(steamid, items, name)
  2392. {
  2393. var cod = makeCode();
  2394. ET.IUser.GetInventory({sort: 6, per_page: 500, page: 1}, (err, body) => {
  2395. var itemsDB = [];
  2396. var Items = [];
  2397. if(err != null || body.status != 1) {
  2398. logger.error(body);
  2399. } else {
  2400. var myitems = body.response.items;
  2401.  
  2402. var ItemsRake = [];
  2403. for(var i in items){
  2404. myitems.forEach(function(item) {
  2405. if(items[i] == item.id) {
  2406. Items.push({
  2407. price: item.suggested_price / 100,
  2408. id: item.id
  2409. });
  2410. return;
  2411. }
  2412. });
  2413. }
  2414.  
  2415. var comission = jackpotPRake/100*(totalJackpotValue - (usersPrices[steamid]/2));
  2416. for(var i in Items)
  2417. {
  2418. if(Items[i]['price'] <= parseFloat(comission).toFixed(2))
  2419. {
  2420. comission -= Items[i]['price'];
  2421. ItemsRake.push(Items[i]['id']);
  2422. Items[i].remove = 1;
  2423. }
  2424. }
  2425.  
  2426. for(var i in Items)
  2427. {
  2428. if(Items[i].remove != 1)
  2429. {
  2430. itemsDB.push(Items[i]['id']);
  2431. }
  2432. }
  2433. setTimeout(function() {
  2434. ET.ITrade.SendOfferToSteamId({steam_id: steamid, items_to_send: itemsDB.join(','), message: 'Your winnings from game #' + jackpotRound + '. Reference code: ' + cod}, (err, body1) => {
  2435. if(err != null || body1.status != 1) {
  2436. pool.query('INSERT INTO trades SET user = ' + pool.escape(steamid) + ', tid = ' + pool.escape(offer.id) + ', status = ' + pool.escape('Cancelled') + ', action = ' + pool.escape('Winning') + ', code = ' + pool.escape(cod) + ', items = ' + pool.escape(itemsDB.join(',')));
  2437. logger.error(body1);
  2438. } else {
  2439. var offer = body1.response.offer;
  2440. console.log('[Winner-jackpot: ' + steamid + ' (' + name + ') ] Offer #' + offer.id);
  2441.  
  2442. pool.query('INSERT INTO trades SET user = ' + pool.escape(steamid) + ', tid = ' + pool.escape(offer.id) + ', status = ' + pool.escape('PendingAccept') + ', action = ' + pool.escape('Winning') + ', code = ' + pool.escape(cod) + ', items = ' + pool.escape(itemsDB.join(',')));
  2443. var depositPlace = [];
  2444. depositPlace.push({
  2445. offer: offer,
  2446. name: 'Winning'
  2447. });
  2448. redisClient.rpush(redisChannels.depositTrades, JSON.stringify(depositPlace));
  2449.  
  2450. setTimeout(function() {
  2451. cancelOffer(offer.id);
  2452. }, 120 * 1000);
  2453.  
  2454. }
  2455. });
  2456. },1000);
  2457. if(ItemsRake.length > 0) {
  2458. ET.ITrade.SendOfferToSteamId({steam_id: Ownerid, items_to_send: ItemsRake.join(','), message: 'Your comission from game #' + jackpotRound + '. Reference code: ' + cod});
  2459. }
  2460. }
  2461. });
  2462. }
  2463.  
  2464.  
  2465.  
  2466.  
  2467. function shuffle(array) {
  2468. var currentIndex = array.length, temporaryValue, randomIndex;
  2469. while (0 !== currentIndex) {
  2470. randomIndex = Math.floor(Math.random() * currentIndex);
  2471. currentIndex -= 1;
  2472. temporaryValue = array[currentIndex];
  2473. array[currentIndex] = array[randomIndex];
  2474. array[randomIndex] = temporaryValue;
  2475. }
  2476. return array;
  2477. }
  2478.  
  2479. function sendItemsTrade(offer_check, link, winner)
  2480. {
  2481. 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_check.id), function(err, res) {
  2482. if(err) throw err;
  2483.  
  2484. setTimeout(function() {
  2485. hideGame(res[0].id);
  2486. }, 120 * 1000);
  2487.  
  2488. var Items = [];
  2489.  
  2490. var Skins1Names = res[0].cskinsnames.split(',');
  2491. var Skins2Names = res[0].pskinsnames.split(',');
  2492. var TotalSkinsNames = Skins1Names.concat(Skins2Names);
  2493.  
  2494. var Skins1 = res[0].cassetids.split(',');
  2495. var Skins2 = res[0].passetids.split(',');
  2496. var allSkins = Skins1.concat(Skins2);
  2497.  
  2498. var Prices1 = res[0].cskinsprices.split(',');
  2499. var Prices2 = res[0].pskinsprices.split(',');
  2500. var allPrices = Prices1.concat(Prices2);
  2501.  
  2502. var partner;
  2503.  
  2504. if(winner == 1)
  2505. {
  2506. partner = res[0].csteamid;
  2507. }
  2508. else if(winner == 2)
  2509. {
  2510. partner = res[0].psteamid;
  2511. }
  2512.  
  2513. for(var i in allSkins)
  2514. {
  2515. Items.push({
  2516. assetid: allSkins[i],
  2517. price: allPrices[i]
  2518. });
  2519. }
  2520. var sendItems = [];
  2521. var ItemsRake = [];
  2522.  
  2523. var comission = percentRake/100 * (res[0].ctp + res[0].ptp);
  2524. for(var i in Items)
  2525. {
  2526. if(Items[i]['price'] <= comission)
  2527. {
  2528. comission -= Items[i]['price'];
  2529. ItemsRake.push(Items[i]['assetid']);
  2530. Items[i].remove = 1;
  2531. }
  2532. }
  2533.  
  2534. for(var i in Items)
  2535. {
  2536. if(Items[i].remove != 1) {
  2537. sendItems.push(Items[i]['assetid']);
  2538. }
  2539. }
  2540. var cod = makeCode();
  2541. setTimeout(function()
  2542. {
  2543. ET.ITrade.SendOfferToSteamId({steam_id: partner, items_to_send: sendItems.join(','), message: 'Code: '+cod}, (err, body1) => {
  2544. if(err != null || body1.status != 1)
  2545. {
  2546. logger.error(body1);
  2547. return;
  2548. }
  2549. else
  2550. {
  2551. var offer = body1.response.offer;
  2552. var WSteamid;
  2553. var WName;
  2554. pool.query('UPDATE games SET sentItems = 1, wcode = ' + pool.escape(cod) + ' WHERE trade_token = ' + pool.escape(offer.id));
  2555. if(winner == 1)
  2556. {
  2557. WSteamid = res[0].csteamid;
  2558. WName = res[0].cname;
  2559. }
  2560. else if(winner == 2)
  2561. {
  2562. WSteamid = res[0].psteamid;
  2563. WName = res[0].pname;
  2564. }
  2565. console.log('[Winner: ' + WSteamid + ' (' + WName + ') ] Offer #' + offer.id + " " + offer.state);
  2566.  
  2567. pool.query('INSERT INTO trades SET user = ' + pool.escape(WSteamid) + ', tid = ' + pool.escape(offer.id) + ', status = ' + pool.escape('PendingAccept') + ', action = ' + pool.escape('Winning') + ', code = ' + pool.escape(cod) + ', items = ' + pool.escape(sendItems.join(',')));
  2568.  
  2569. var depositPlace = [];
  2570.  
  2571. depositPlace.push({
  2572. offer: offer,
  2573. name: 'Winning'
  2574. });
  2575. redisClient.rpush(redisChannels.depositTrades, JSON.stringify(depositPlace));
  2576.  
  2577. setTimeout(function() {
  2578. cancelOffer(offer.id);
  2579. }, 120 * 1000);
  2580.  
  2581. if(gamesPending.hasOwnProperty(offer.id))
  2582. {
  2583. delete gamesPending[offer.id];
  2584. }
  2585.  
  2586. }
  2587. });
  2588. }, 1000);
  2589. if(ItemsRake.length > 0) {
  2590. ET.ITrade.SendOfferToSteamId({steam_id: Ownerid, items_to_send: ItemsRake.join(','), message: 'Coinflip Comission'}, (err, body5) => {
  2591. if(body5.status != 1) {
  2592. console.log('ERROR on Send Offer');
  2593. logger.error(body5);
  2594. }
  2595. });
  2596. }
  2597. loadAllGames();
  2598. sendGames(res[0].id);
  2599.  
  2600. setTimeout(function() {
  2601. loadStatistics();
  2602. }, 5000);
  2603. });
  2604. }
  2605.  
  2606.  
  2607. //Refresh polldata.json
  2608.  
  2609. function time()
  2610. {
  2611. return parseInt(new Date().getTime()/1000);
  2612. }
  2613.  
  2614. function addHistory(socket)
  2615. {
  2616. chatMessages.forEach(function(itm) {
  2617. socket.emit('message', {
  2618. type: 'addMessage',
  2619. msg: itm.message,
  2620. avatar: itm.avatar,
  2621. steamid: itm.steamid,
  2622. rank: itm.rank,
  2623. hide: itm.hide,
  2624. level: itm.level,
  2625. name: itm.name
  2626. });
  2627. })
  2628. }
  2629.  
  2630. function makeCode() {
  2631. var text = "";
  2632. var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  2633.  
  2634. for(var i=0; i < 6; i++)
  2635. text += possible.charAt(Math.floor(Math.random() * possible.length));
  2636.  
  2637. return text;
  2638. }
  2639.  
  2640. function makeHash()
  2641. {
  2642. var text = "";
  2643. var possible = "abcdefghijlmnopqrstu0123456789";
  2644.  
  2645. for(var i=0; i < 32; i++)
  2646. text += possible.charAt(Math.floor(Math.random() * possible.length));
  2647.  
  2648. return text;
  2649. }
  2650.  
  2651. function makeSecret()
  2652. {
  2653. var text = "";
  2654. var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  2655.  
  2656. for(var i=0; i < 8; i++)
  2657. text += possible.charAt(Math.floor(Math.random() * possible.length));
  2658.  
  2659. return text;
  2660. }
  2661.  
  2662. function getProxy()
  2663. {
  2664. return "https://" + proxies[random(0,proxies.length-1)];
  2665. }
  2666.  
  2667. function random(min, max) {
  2668. return Math.floor(Math.random() * (max - min + 1)) + min;
  2669. }
  2670.  
  2671. function getInv(user, socket, tipo)
  2672. {
  2673. ET.ITrade.GetUserInventoryFromSteamId({steam_id: encodeURIComponent(user), sort: 6, per_page: 500, page: 1}, (err, body) => {
  2674. if(err != null || body.status != 1) {
  2675. throw err;
  2676. return;
  2677. }
  2678. var items = body.response.items;
  2679.  
  2680. var counter = 0;
  2681.  
  2682. var idss = [];
  2683. var namess = [];
  2684. var pricess = [];
  2685. var imgss = [];
  2686. var colorss = [];
  2687.  
  2688. var Ids = '';
  2689. var Names = '';
  2690. var Prices = '';
  2691. var Imgs = '';
  2692. var Colors = '';
  2693. if(!items) return;
  2694.  
  2695. items.forEach(function(value) {
  2696. var id = value.id;
  2697. var name = value.name;
  2698. var price = value.suggested_price / 100;
  2699. var img = value.image['300px'];
  2700. var color = value.color;
  2701.  
  2702. if(price >= minDep) {
  2703. idss.push(id);
  2704. namess.push(name);
  2705. pricess.push(price);
  2706. imgss.push(img);
  2707. colorss.push(color);
  2708. }
  2709. });
  2710. Ids = idss.join(',');
  2711. Names = namess.join(',');
  2712. Prices = pricess.join(',');
  2713. Imgs = imgss.join(',');
  2714. Colors = colorss.join(',');
  2715.  
  2716. socket.emit('message', {
  2717. type: tipo,
  2718. id: Ids,
  2719. name: Names,
  2720. price: Prices,
  2721. img: Imgs,
  2722. color: Colors
  2723. });
  2724. inventoryUser[user] = {
  2725. id: Ids,
  2726. name: Names,
  2727. price: Prices,
  2728. img: Imgs,
  2729. color: Colors
  2730. }
  2731. });
  2732. }
  2733.  
  2734.  
  2735. function loadStatistics(socket)
  2736. {
  2737. var totalAmount = 0;
  2738. var totalItems = 0;
  2739. var activeGames = 0;
  2740.  
  2741. pool.query('SELECT SUM(`ctp`) AS `totalAmount`, SUM(`cskins`) AS `totalItems`, COUNT(`id`) AS `activeGames` FROM games WHERE `winner` = -1', function(error, res) {
  2742. if(error) throw error;
  2743.  
  2744. if(res.length == 0)
  2745. {
  2746. totalAmount = 0;
  2747. totalItems = 0;
  2748. activeGames = 0;
  2749.  
  2750. if(!socket)
  2751. {
  2752. io.sockets.emit('message', {
  2753. type: 'loadStatistics',
  2754. totalAmount: totalAmount,
  2755. totalItems: totalItems,
  2756. activeGames: activeGames
  2757. });
  2758. }
  2759. else
  2760. {
  2761. socket.emit('message', {
  2762. type: 'loadStatistics',
  2763. totalAmount: totalAmount,
  2764. totalItems: totalItems,
  2765. activeGames: activeGames
  2766. });
  2767. }
  2768. }
  2769.  
  2770. if(res.length > 0)
  2771. {
  2772. totalAmount = res[0].totalAmount;
  2773. totalItems = res[0].totalItems;
  2774. activeGames = res[0].activeGames;
  2775.  
  2776. if(totalAmount == null)
  2777. {
  2778. totalAmount = 0;
  2779. }
  2780. if(totalItems == null)
  2781. {
  2782. totalItems = 0;
  2783. }
  2784. if(activeGames == null)
  2785. {
  2786. activeGames = 0;
  2787. }
  2788.  
  2789. if(!socket)
  2790. {
  2791. io.sockets.emit('message', {
  2792. type: 'loadStatistics',
  2793. totalAmount: totalAmount,
  2794. totalItems: totalItems,
  2795. activeGames: activeGames
  2796. });
  2797. }
  2798. else
  2799. {
  2800. socket.emit('message', {
  2801. type: 'loadStatistics',
  2802. totalAmount: totalAmount,
  2803. totalItems: totalItems,
  2804. activeGames: activeGames
  2805. });
  2806. }
  2807. }
  2808. });
  2809. }
  2810.  
  2811. function sendGames(gameID)
  2812. {
  2813. setTimeout(function() {
  2814. for(var i in loadGames)
  2815. {
  2816. if(loadGames[i].id == gameID)
  2817. {
  2818. timerGame[loadGames[i].id] = time()+90;
  2819.  
  2820. io.sockets.emit('message', {
  2821. type: 'editGame',
  2822. games: {
  2823. 'id': loadGames[i].id,
  2824. 'csteamid': loadGames[i].csteamid,
  2825. 'cname': loadGames[i].cname,
  2826. 'cavatar': loadGames[i].cavatar,
  2827. 'cskinsurl': loadGames[i].cskinsurl,
  2828. 'cskinsnames': loadGames[i].cskinsnames,
  2829. 'cskinsprices': loadGames[i].cskinsprices,
  2830. 'ccolors': loadGames[i].ccolors,
  2831. 'cskins': loadGames[i].cskins,
  2832. 'ctp': loadGames[i].ctp,
  2833. 'psteamid': loadGames[i].psteamid,
  2834. 'pname': loadGames[i].pname,
  2835. 'pavatar': loadGames[i].pavatar,
  2836. 'pskinsurl': loadGames[i].pskinsurl,
  2837. 'pskinsnames': loadGames[i].pskinsnames,
  2838. 'pskinsprices': loadGames[i].pskinsprices,
  2839. 'pcolors': loadGames[i].pcolors,
  2840. 'pskins': loadGames[i].pskins,
  2841. 'ptp': loadGames[i].ptp,
  2842. 'hash': loadGames[i].hash,
  2843. 'secret': loadGames[i].secret,
  2844. 'winner': loadGames[i].winner,
  2845. 'timer': timerGame[loadGames[i].id]-time(),
  2846. 'timer11': loadGames[i].timer11,
  2847. 'ttimer11': timer11Game[loadGames[i].id]-time(),
  2848. }
  2849. });
  2850.  
  2851. io.sockets.emit('message', {
  2852. type: 'watchCF',
  2853. id: loadGames[i].id,
  2854. csteamid: loadGames[i].csteamid,
  2855. cname: loadGames[i].cname,
  2856. cavatar: loadGames[i].cavatar,
  2857. cskinsurl: loadGames[i].cskinsurl,
  2858. cskinsnames: loadGames[i].cskinsnames,
  2859. ccolors: loadGames[i].ccolors,
  2860. cskinsprices: loadGames[i].cskinsprices,
  2861. cskins: loadGames[i].cskins,
  2862. ctp: loadGames[i].ctp,
  2863. psteamid: loadGames[i].psteamid,
  2864. pname: loadGames[i].pname,
  2865. pcolors: loadGames[i].pcolors,
  2866. pavatar: loadGames[i].pavatar,
  2867. pskinsurl: loadGames[i].pskinsurl,
  2868. pskinsnames: loadGames[i].pskinsnames,
  2869. pskinsprices: loadGames[i].pskinsprices,
  2870. pskins: loadGames[i].pskins,
  2871. ptp: loadGames[i].ptp,
  2872. hash: loadGames[i].hash,
  2873. secret: loadGames[i].secret,
  2874. winner: loadGames[i].winner,
  2875. timer: timerGame[loadGames[i].id]-time(),
  2876. timer11: loadGames[i].timer11,
  2877. ttimer11: timer11Game[loadGames[i].id]-time(),
  2878. gameNumber: loadGames[i].id
  2879. });
  2880. }
  2881. }
  2882. }, 1000);
  2883. }
  2884.  
  2885. function LevelCalculate(user)
  2886. {
  2887. pool.query('SELECT xp,level FROM users WHERE steamid = ' + pool.escape(user), function(err, row) {
  2888. if(err) throw err;
  2889. if(row.length == 0) return;
  2890.  
  2891. var currentLevel = row[0].level;
  2892.  
  2893. var currentXp = row[0].xp;
  2894. var xpNeeded = 0;
  2895. var xpMinus = 0;
  2896.  
  2897. for(var i = 1; i < 500; i++)
  2898. {
  2899. xpNeeded += 40 * i;
  2900. xpMinus = xpNeeded - (40 * i-1);
  2901. if(currentXp >= xpMinus && currentXp <= xpNeeded)
  2902. {
  2903. pool.query('UPDATE users SET level = ' + pool.escape(i) + ' WHERE steamid = ' + pool.escape(user));
  2904. }
  2905. }
  2906. });
  2907. }
  2908.  
  2909. function getRandomFloat(min, max) {
  2910. return (Math.random() * (max - min) + min).toFixed(10);
  2911. }
  2912.  
  2913. function handleDisconnect() {
  2914. pool = mysql.createConnection(db_config);
  2915.  
  2916. pool.connect(function(err) {
  2917. if(err) {
  2918. logger.trace('Error: Connecting to database: ', err);
  2919. setTimeout(handleDisconnect, 2000);
  2920. }
  2921. });
  2922.  
  2923. pool.on('error', function(err) {
  2924. logger.trace('Error: Database error: ', err);
  2925. if(err.code === 'PROTOCOL_CONNECTION_LOST') {
  2926. handleDisconnect();
  2927. } else {
  2928. throw err;
  2929. }
  2930. });
  2931. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement