Advertisement
Guest User

Untitled

a guest
Oct 17th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.23 KB | None | 0 0
  1. var SteamApi = require('steam-api');
  2. var user = new SteamApi.User('MYAPIKEY');
  3. var server = require('http').createServer();
  4. var io = require('socket.io')(server);
  5. server.listen(3000);
  6. var request = require('request');
  7.  
  8. var mysql = require('mysql');
  9. var connection = mysql.createConnection({
  10. host : 'localhost',
  11. user : 'root',
  12. password : 'MYPASSWORD:P',
  13. database : 'CSGOMind',
  14. charset : 'utf8_general_ci'
  15. });
  16.  
  17. connection.connect();
  18.  
  19. var sitepath = "CSGOMind.net"; // Path to your website, without www or http:// | Example: csgoresorts.com
  20. var JackpotTimer=120;
  21. var playersRequired=2;
  22. var endtimer = -1;
  23. var disablecredits = -1;
  24. var allowdeposits=1;
  25. var mindeposit=25;
  26. var maxdeposit=10000;
  27. var rsecret='MYSECRET:P'; // Change this to the same thing you have in your Endround.php!
  28.  
  29. function DisableCreditBets()
  30. {
  31. allowdeposits=0;
  32. io.emit('disablecredit');
  33. }
  34.  
  35. function addslashes(str)
  36. {
  37. str=str.replace(/\\/g,'\\\\');
  38. str=str.replace(/\'/g,'\\\'');
  39. str=str.replace(/\"/g,'\\"');
  40. str=str.replace(/\0/g,'\\0');
  41. return str;
  42. }
  43.  
  44. function randomString(length, chars)
  45. {
  46. var mask = '';
  47. if (chars.indexOf('a') > -1) mask += 'abcdefghijklmnopqrstuvwxyz';
  48. if (chars.indexOf('A') > -1) mask += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  49. if (chars.indexOf('#') > -1) mask += '0123456789';
  50. if (chars.indexOf('!') > -1) mask += '~`!@#$%^&*()_+-={}[]:";\'<>?,./|\\';
  51. var result = '';
  52. for (var i = length; i > 0; --i) result += mask[Math.floor(Math.random() * mask.length)];
  53. return result;
  54. }
  55.  
  56. function EndGame()
  57. {
  58. endtimer = -1;
  59. proceedWinners();
  60. setTimeout(function()
  61. {
  62. io.emit('enablecredit');
  63. allowdeposits=1;
  64. },12000);
  65.  
  66. }
  67.  
  68. function ResumeJackpot()
  69. {
  70. connection.query('SELECT `starttime` FROM `jackpotgames` GROUP BY `id` DESC', function(errs, rowss, fieldss)
  71. {
  72. if(errs)
  73. {
  74. return;
  75. }
  76. var timeleft;
  77. if(rowss[0].starttime == 2147483647)
  78. {
  79. timeleft = JackpotTimer;
  80. }
  81. else
  82. {
  83. var unixtime = Math.round(new Date().getTime()/1000.0);
  84. timeleft = rowss[0].starttime+JackpotTimer-unixtime;
  85. if(timeleft < 0)
  86. {
  87. timeleft = 0;
  88. }
  89. }
  90. if(timeleft != JackpotTimer)
  91. {
  92. endtimer = setTimeout(EndGame,timeleft*1000);
  93. console.log('[SERVER] Restoring the latest game with '+timeleft+' seconds left!');
  94. }
  95. });
  96. }
  97. ResumeJackpot();
  98.  
  99. function proceedWinners()
  100. {
  101. console.log('[SERVER] Ending current game & choosing winner.');
  102. var url = 'http://'+sitepath+'/endround.php?secret='+rsecret+'';
  103. request(url, function(error, response, body)
  104. {
  105. if(error)
  106. {
  107. console.log('Couldn\'t end round, error: '+error);
  108. return;
  109. }
  110. if(response)
  111. {
  112. io.emit('jackpotanimation');
  113. }
  114. });
  115. }
  116.  
  117.  
  118. io.on('connection', function (socket)
  119. {
  120.  
  121. console.log('connection');
  122.  
  123.  
  124. socket.on('jackpotanimation',function(status)
  125. {
  126. io.emit('jackpotanimation',status);
  127. });
  128.  
  129. socket.on('showchat',function(status)
  130. {
  131. var data;
  132. try
  133. {
  134. data = JSON.parse(status);
  135. }
  136. catch (e)
  137. {
  138. return console.error(e);
  139. }
  140. status= JSON.parse(status);
  141. if(status.messageid && status.room>-1)
  142. {
  143.  
  144. messageid=status.messageid;
  145. roomid=status.room;
  146. if(messageid>0 && roomid>-1)
  147. {
  148. connection.query('SELECT * FROM `chat` WHERE `id`='+connection.escape(messageid)+' ', function(err, row, fields)
  149. {
  150. if(row.length!=0)
  151. {
  152. part=row[0].PlayerID;
  153. var timenow=Math.round(new Date().getTime()/1000);
  154. started=row[0].time;
  155. since=timenow-started;
  156.  
  157. if(since<5)
  158. {
  159. console.log('showchat');
  160. io.emit('showthechat',{ messageid: messageid, roomid: roomid });
  161. }
  162. else
  163. {
  164. console.log('timer');
  165. }
  166.  
  167. }
  168. else
  169. {
  170. console.log('row lenght');
  171. }
  172. });
  173. }
  174. else
  175. {
  176. console.log('missing room message');
  177. }
  178. }
  179. else
  180. {
  181. console.log('missing room or f message');
  182. }
  183. });
  184.  
  185. socket.on('showmessages',function(status)
  186. {
  187. socket.emit('showthemessages',status);
  188. });
  189. socket.on('processdeposit',function(status)
  190. {
  191.  
  192. if(allowdeposits==0)
  193. {
  194. return;
  195. }
  196. var data=status;
  197. data.steamid=addslashes(data.steamid);
  198. data.amount=addslashes(data.amount);
  199. data.secret=addslashes(data.secret);
  200. if(data.amount>=mindeposit && data.amount<=maxdeposit)
  201. {
  202. connection.query('SELECT * FROM `users` WHERE `steamid`="'+data.steamid+'"', function(err, row, fields)
  203. {
  204. var timenow=Math.round(new Date().getTime()/1000);
  205. updated=timenow+1;
  206. lastaction=row[0].lastaction;
  207. if(timenow<lastaction)
  208. {
  209. return;
  210. }
  211. connection.query('UPDATE `users` SET `lastaction`="'+updated+'" WHERE `steamid`="'+data.steamid+'"', function(err, rows, fields)
  212. {
  213. if(row.length!=0)
  214. {
  215. var as=row[0].account_secret;
  216. var ban=row[0].ban;
  217. var credits=row[0].credits;
  218. var steamname=row[0].name;
  219. if(steamname)
  220. {
  221. steamname=addslashes(steamname);
  222. }
  223. var avatar=row[0].avatar;
  224. console.log(as);
  225. console.log(data.secret);
  226. if(as==data.secret)
  227. {
  228. if(ban==0)
  229. {
  230. if(credits>=data.amount)
  231. {
  232. connection.query('SELECT * FROM `jackpotgames` ORDER BY ID DESC LIMIT 1', function(err, row, fields)
  233. {
  234. var unixtime = Math.round(new Date().getTime()/1000.0);
  235. current=row[0].id;
  236. var timeleft;
  237. if(row[0].starttime == 2147483647)
  238. {
  239. timeleft = JackpotTimer;
  240. }
  241. else
  242. {
  243. timeleft = row[0].starttime+JackpotTimer-unixtime;
  244. if(timeleft < 0)
  245. {
  246. timeleft = 0;
  247. }
  248. }
  249. if(timeleft>5)
  250. {
  251. connection.query('UPDATE `users` SET `credits`=credits-'+data.amount+' WHERE `steamid`="'+data.steamid+'"', function(err, row, fields)
  252. {
  253. if(!err)
  254. {
  255. key=randomString(32, '#aA');
  256. connection.query('INSERT INTO `jackpotdeposits` (gameid,userid,username,useravatar,skin,cost,date,assetid,offerid) VALUES ("'+current+'","'+data.steamid+'","'+steamname+'","'+avatar+'","Credits","'+data.amount+'","'+unixtime+'","4961","'+key+'")', function(err, row, fields, result)
  257. {
  258. if(!err)
  259. {
  260. connection.query('UPDATE `jackpotgames` SET `value`=value+'+data.amount+', `skins`=skins+1 WHERE `id`="'+current+'"', function(err, row, fields)
  261. {
  262.  
  263. });
  264. connection.query('SELECT COUNT(DISTINCT userid) AS playersCount FROM `jackpotdeposits` WHERE `gameid`=\''+current+'\'', function(err, rows)
  265. {
  266. players = rows[0].playersCount;
  267. if(players == playersRequired && endtimer==-1)
  268. {
  269. console.log('[SERVER] Starting the countdown for Game #'+current+'');
  270. endtimer = setTimeout(EndGame,JackpotTimer*1000);
  271. disablecredits = setTimeout(DisableCreditBets,115000);
  272. connection.query('UPDATE `jackpotgames` SET `starttime`=UNIX_TIMESTAMP() WHERE `id` = \'' + current + '\'', function(err, row, fields) {});
  273. io.emit('updategameinfo','');
  274. }
  275. else
  276. {
  277. io.emit('showthedeposit',''+data.steamid+'/'+key+'');
  278. }
  279. });
  280. }
  281. else
  282. {
  283. console.log(err);
  284. return;
  285. }
  286.  
  287.  
  288. });
  289. }
  290. });
  291. }
  292. else
  293. {
  294.  
  295. }
  296. });
  297.  
  298.  
  299. }
  300. }
  301. }
  302. }
  303. });
  304. });
  305. }
  306. });
  307. socket.on('showdeposit',function(status)
  308. {
  309. var array=status;
  310. io.emit('showthedeposit',status);
  311. });
  312. socket.on('updatecredits',function(status)
  313. {
  314. socket.emit('updatethecredits',status);
  315. });
  316.  
  317. });
  318.  
  319.  
  320. function inArray(needle, haystack)
  321. {
  322. var length = haystack.length;
  323. for(var i = 0; i < length; i++)
  324. {
  325. if(haystack[i] == needle)
  326. {
  327. return true;
  328. }
  329. }
  330. return false;
  331. }
  332.  
  333. setInterval(function ()
  334. {
  335. connection.query('SELECT 1');
  336. }, 3600000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement