Advertisement
Guest User

Untitled

a guest
Oct 12th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.48 KB | None | 0 0
  1. var app = require('express')();
  2. var http = require('http').Server(app);
  3. var io = require('socket.io')(http);
  4. var config = require('config.json')('./config.json');
  5. var spawn = require('child_process').spawn;
  6. var xssEscape = require('xss-escape');
  7. var request = require("request");
  8. var SteamCommunity = require('steamcommunity');
  9. var community = new SteamCommunity();
  10.  
  11. var bots = [];
  12. var allClients = {};
  13. var admin = 0;
  14. var end = 1;
  15. var jackpot_data = '';
  16.  
  17. var mysql = require('mysql');
  18. var connection = mysql.createConnection({
  19. host : config.mysql.host,
  20. user : config.mysql.user,
  21. password : config.mysql.pass,
  22. database : config.mysql.db
  23. });
  24.  
  25. app.get('/', function(req, res){
  26. res.send('');
  27. });
  28.  
  29. http.listen(3000, function(){
  30. console.log('listening on *:3000');
  31. });
  32.  
  33. for(var i=0; i<config.bots.length; i++){
  34. start(i);
  35. }
  36.  
  37. var pobiera = false;
  38. setInterval(function(){
  39. if(pobiera == false){
  40. pobiera = true;
  41. request({
  42. uri: "http://luckyshots.pl/ajax.php?s=jackpot1",
  43. }, function(error, response, body){
  44. if (!error) {
  45. if(body.length > 3){
  46. console.log(body);
  47. }
  48. }
  49. pobiera = false;
  50. });
  51. }else{
  52. console.log('Troche za szybko');
  53. }
  54. }, 1001);
  55.  
  56. setInterval(function(){
  57. connection.query('SELECT * FROM `skins` ORDER BY `time` LIMIT 1', function(err, rows, fields){
  58. if (err) throw err;
  59. rows.forEach(function(row, callback){
  60. community.getMarketItem(730, row.market_hash_name, function(err, item){
  61. if(err){
  62. console.log(row.market_hash_name+" "+err);
  63. connection.query("UPDATE `skins` SET `time`=UNIX_TIMESTAMP(NOW()) WHERE `market_hash_name`=?", row.market_hash_name, function(err, rows, fields){
  64. if (err) throw err;
  65. });
  66. }else{
  67. var suma = [];
  68. if(item.medianSalePrices != undefined){
  69. for(var f=0; f<item.medianSalePrices.length; f++){
  70. suma.push(item.medianSalePrices[f].price);
  71. }
  72. //var price = (suma / item.medianSalePrices.length).toFixed(2);
  73. var price = median(suma)
  74. connection.query("UPDATE `skins` SET `price`='"+price+"', `time`=UNIX_TIMESTAMP(NOW()) WHERE `market_hash_name`=?", row.market_hash_name, function(err, rows, fields){
  75. if (err) throw err;
  76. });
  77. }else{
  78. connection.query("UPDATE `skins` SET `time`=UNIX_TIMESTAMP(NOW()) WHERE `market_hash_name`=?", row.market_hash_name, function(err, rows, fields){
  79. if (err) throw err;
  80. });
  81. }
  82. }
  83. });
  84. });
  85. });
  86. }, 15000);
  87. function median(values) {
  88. values.sort( function(a,b) {return a - b;} );
  89. var half = Math.floor(values.length/2);
  90. if(values.length % 2)
  91. return values[half];
  92. else
  93. return (values[half-1] + values[half]) / 2.0;
  94. }
  95. var chat_msg_count = 0;
  96. setInterval(function(){
  97. connection.query("INSERT INTO `czat` SET `user_id`='0', `msg`='"+config.chat_msg.msg[chat_msg_count]+"', `time`=UNIX_TIMESTAMP(NOW())", function(err, rows, fields){
  98. if (err) throw err;
  99. });
  100. chat_msg_count++;
  101. if(chat_msg_count >= config.chat_msg.msg.length){
  102. chat_msg_count = 0;
  103. }
  104. }, config.chat_msg.interval*1000);
  105.  
  106.  
  107. var ips_connected = [];
  108. var connected_users = 0;
  109. io.on('connection', function(socket){
  110.  
  111. var ip_address = socket.handshake.address;
  112. if (ips_connected.hasOwnProperty(ip_address)){
  113. ips_connected[ip_address]++;
  114. }else{
  115. ips_connected[ip_address] = 1;
  116. connected_users++;
  117. }
  118.  
  119. io.emit('online', connected_users);
  120.  
  121. socket.on('disconnect', function () {
  122. if (ips_connected.hasOwnProperty(ip_address)) {
  123. ips_connected[ip_address]--;
  124. if (ips_connected[ip_address] <= 0) {
  125. delete ips_connected[ip_address];
  126. connected_users--;
  127. }
  128. }
  129. });
  130.  
  131. socket.on('online', function () {
  132. io.emit('online', connected_users);
  133. });
  134.  
  135. socket.on('jackpot reload', function(data){
  136. if(data == 'me'){
  137. get_jackpot(true, socket);
  138. }else{
  139. get_jackpot(false);
  140. }
  141. if(data == 'shot'){
  142. io.emit('new_items');
  143. io.emit('shot');
  144. }
  145. });
  146.  
  147. socket.on('chat', function(){
  148. io.emit('chat');
  149. });
  150.  
  151. socket.on('create offer', function(data){
  152. io.emit('create offer', data);
  153. });
  154.  
  155. socket.on('refresh_offer', function(data){
  156. io.emit('refresh_offer', data);
  157. });
  158.  
  159. socket.on('reload_giveaway', function(){
  160. io.emit('reload_giveaway');
  161. });
  162.  
  163. socket.on('refresh_offer_return', function(data){
  164. io.emit('refresh_offer_return', data);
  165. });
  166.  
  167. socket.on('user-info', function(data){
  168. io.emit('user-info', data);
  169. });
  170.  
  171. socket.on('new_items', function(data){
  172. io.emit('new_items', data);
  173. });
  174.  
  175. socket.on('get_my_items', function (data) {
  176. console.log('Ladowanie przedmiotow gracza ' + data);
  177. io.emit('close', data);
  178. request({
  179. uri: "http://luckyshots.pl/ajax.php?s=load_depozyt&num=-1&user="+data,
  180. }, function (error, response, body) {
  181. if (error) {
  182. console.log(error);
  183. } else {
  184. json = {};
  185.  
  186. try {
  187. json = JSON.parse(body.trim());
  188. } catch (e) {}
  189.  
  190. var ile = parseInt(json.count);
  191. var last = json.lastItemId;
  192. var wszystko = [];
  193.  
  194. console.log('Mamy ' + ile + ' itemow do pobrania');
  195. console.log('ID ostatniego itemu: ' + last);
  196.  
  197. for (var now = 0; now <= ile; now += 12) {
  198.  
  199. request({
  200. uri: "http://luckyshots.pl/ajax.php?s=load_depozyt&num=" + now + "&user=" + data,
  201. }, function (error, response, bodyq) {
  202. if (error){
  203. console.log(error);
  204. } else {
  205. if (bodyq[0] != '<') {
  206. var p = eval( "(" + bodyq + ")" )
  207. for (var key in p) {
  208. if (p.hasOwnProperty(key)) {
  209. wszystko.push({
  210. 'key': key,
  211. 'val': p[key]
  212. });
  213. if (key == last) {
  214. console.log('Send data to client');
  215. socket.emit('get_my_items_return', wszystko);
  216. }
  217. }
  218. }
  219. } else {
  220. console.log('Error while parsing items.');
  221. }
  222. }
  223. });
  224. }
  225. /**
  226. var tim = setInterval(function () {
  227. console.log('Wysylanie danych do uzytkownika');
  228. // console.log(wszystko);
  229. console.log(wszystko.length + ' :: ' + ile);
  230.  
  231. if (wszystko.length == ile) {
  232. socket.emit('get_my_items_return', wszystko);
  233. clearInterval(tim);
  234. }
  235. }, 100);
  236. */
  237. }
  238. });
  239. });
  240.  
  241. socket.on('offer state', function(data){
  242. io.emit('offer state', data);
  243. });
  244.  
  245. var user = '';
  246. var admin = false;
  247.  
  248. socket.on('online', function(msg){
  249. if(allClients[msg] !== undefined){
  250. user = msg;
  251. }else{
  252. allClients[msg] = 1;
  253. user = msg;
  254. }
  255. });
  256.  
  257. io.emit('chat clients', Object.keys(allClients).length);
  258. //io.emit('new_items');
  259.  
  260. io.emit('admins', admin);
  261.  
  262. socket.on('support message', function(msg){
  263. connection.query('SELECT * FROM `users` WHERE `login_hash`=\''+xssEscape(msg.user)+'\' LIMIT 1', function(err, rows, fields){
  264. if (err) throw err;
  265. rows.forEach(function(row, callback){
  266. connection.query("INSERT INTO `support` SET `user_id`='"+row.id+"', `msg`='"+xssEscape(msg.msg)+"', `admin`='0', `read`='0', `time`=UNIX_TIMESTAMP(NOW())", function(err, rows, fields){
  267. if (err) throw err;
  268. socket.emit('support reaload', '');
  269. io.emit('reload admin support', '');
  270. io.emit('user-info', {msg: 'Nowa wiadomość w supporcie!!',
  271. admin: true,
  272. type: 'info',
  273. dimiss: true,
  274. delay: 30});
  275. });
  276. });
  277. });
  278. });
  279.  
  280. socket.on('support admin message', function(msg){
  281. connection.query("INSERT INTO `support` SET `user_id`='"+xssEscape(msg.user)+"', `msg`='"+xssEscape(msg.msg)+"', `admin`='1', `read`='0', `time`=UNIX_TIMESTAMP(NOW())", function(err, rows, fields){
  282. if (err) throw err;
  283. io.emit('support reaload', '');
  284. io.emit('reload admin support', '');
  285. });
  286. });
  287.  
  288. var temp_admin = false;
  289. socket.on('login admin', function(msg){
  290. temp_admin = true;
  291. admin++;
  292. io.emit('admins', admin);
  293. });
  294.  
  295. socket.on('disconnect', function(){
  296. if(temp_admin == true){
  297. admin--;
  298. io.emit('admins', admin);
  299. }
  300. delete allClients[user];
  301. io.emit('chat clients', Object.keys(allClients).length);
  302. });
  303.  
  304. });
  305.  
  306. function start(bot_id){
  307. var proc = spawn('node', ['bot.js', bot_id]);
  308.  
  309. proc.stdout.on('data', function (data) {
  310. console.log("BOT "+bot_id+": "+data.toString());
  311. });
  312.  
  313. proc.stderr.on('data', function (data) {
  314. console.log("BOT "+bot_id+": "+data.toString());
  315. });
  316.  
  317. proc.on('exit', function (code){
  318. console.log("BOT "+bot_id+" ZOSTAŁ ZATRZYMANY: "+ code);
  319. delete(proc);
  320. setTimeout(start(bot_id), 300000);
  321. });
  322. }
  323.  
  324. function get_jackpot(me, socket){
  325. request({
  326. uri: "http://luckyshots.pl/ajax.php?s=jackpot2",
  327. }, function(error, response, body){
  328. if(error){
  329. console.log(error);
  330. }else{
  331. jackpot_data = body;
  332. if(me == true){
  333. socket.emit('reload jackpot', jackpot_data);
  334. }else{
  335. io.emit('reload jackpot', jackpot_data);
  336. }
  337. }
  338. });
  339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement