Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var app = require('express')();
- var http = require('http').Server(app);
- var io = require('socket.io')(http);
- var config = require('config.json')('./config.json');
- var spawn = require('child_process').spawn;
- var xssEscape = require('xss-escape');
- var request = require("request");
- var SteamCommunity = require('steamcommunity');
- var community = new SteamCommunity();
- var bots = [];
- var allClients = {};
- var admin = 0;
- var end = 1;
- var jackpot_data = '';
- var mysql = require('mysql');
- var connection = mysql.createConnection({
- host : config.mysql.host,
- user : config.mysql.user,
- password : config.mysql.pass,
- database : config.mysql.db
- });
- app.get('/', function(req, res){
- res.send('');
- });
- http.listen(3000, function(){
- console.log('listening on *:3000');
- });
- for(var i=0; i<config.bots.length; i++){
- start(i);
- }
- var pobiera = false;
- setInterval(function(){
- if(pobiera == false){
- pobiera = true;
- request({
- uri: "http://luckyshots.pl/ajax.php?s=jackpot1",
- }, function(error, response, body){
- if (!error) {
- if(body.length > 3){
- console.log(body);
- }
- }
- pobiera = false;
- });
- }else{
- console.log('Troche za szybko');
- }
- }, 1001);
- setInterval(function(){
- connection.query('SELECT * FROM `skins` ORDER BY `time` LIMIT 1', function(err, rows, fields){
- if (err) throw err;
- rows.forEach(function(row, callback){
- community.getMarketItem(730, row.market_hash_name, function(err, item){
- if(err){
- console.log(row.market_hash_name+" "+err);
- connection.query("UPDATE `skins` SET `time`=UNIX_TIMESTAMP(NOW()) WHERE `market_hash_name`=?", row.market_hash_name, function(err, rows, fields){
- if (err) throw err;
- });
- }else{
- var suma = [];
- if(item.medianSalePrices != undefined){
- for(var f=0; f<item.medianSalePrices.length; f++){
- suma.push(item.medianSalePrices[f].price);
- }
- //var price = (suma / item.medianSalePrices.length).toFixed(2);
- var price = median(suma)
- connection.query("UPDATE `skins` SET `price`='"+price+"', `time`=UNIX_TIMESTAMP(NOW()) WHERE `market_hash_name`=?", row.market_hash_name, function(err, rows, fields){
- if (err) throw err;
- });
- }else{
- connection.query("UPDATE `skins` SET `time`=UNIX_TIMESTAMP(NOW()) WHERE `market_hash_name`=?", row.market_hash_name, function(err, rows, fields){
- if (err) throw err;
- });
- }
- }
- });
- });
- });
- }, 15000);
- function median(values) {
- values.sort( function(a,b) {return a - b;} );
- var half = Math.floor(values.length/2);
- if(values.length % 2)
- return values[half];
- else
- return (values[half-1] + values[half]) / 2.0;
- }
- var chat_msg_count = 0;
- setInterval(function(){
- 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){
- if (err) throw err;
- });
- chat_msg_count++;
- if(chat_msg_count >= config.chat_msg.msg.length){
- chat_msg_count = 0;
- }
- }, config.chat_msg.interval*1000);
- var ips_connected = [];
- var connected_users = 0;
- io.on('connection', function(socket){
- var ip_address = socket.handshake.address;
- if (ips_connected.hasOwnProperty(ip_address)){
- ips_connected[ip_address]++;
- }else{
- ips_connected[ip_address] = 1;
- connected_users++;
- }
- io.emit('online', connected_users);
- socket.on('disconnect', function () {
- if (ips_connected.hasOwnProperty(ip_address)) {
- ips_connected[ip_address]--;
- if (ips_connected[ip_address] <= 0) {
- delete ips_connected[ip_address];
- connected_users--;
- }
- }
- });
- socket.on('online', function () {
- io.emit('online', connected_users);
- });
- socket.on('jackpot reload', function(data){
- if(data == 'me'){
- get_jackpot(true, socket);
- }else{
- get_jackpot(false);
- }
- if(data == 'shot'){
- io.emit('new_items');
- io.emit('shot');
- }
- });
- socket.on('chat', function(){
- io.emit('chat');
- });
- socket.on('create offer', function(data){
- io.emit('create offer', data);
- });
- socket.on('refresh_offer', function(data){
- io.emit('refresh_offer', data);
- });
- socket.on('reload_giveaway', function(){
- io.emit('reload_giveaway');
- });
- socket.on('refresh_offer_return', function(data){
- io.emit('refresh_offer_return', data);
- });
- socket.on('user-info', function(data){
- io.emit('user-info', data);
- });
- socket.on('new_items', function(data){
- io.emit('new_items', data);
- });
- socket.on('get_my_items', function (data) {
- console.log('Ladowanie przedmiotow gracza ' + data);
- io.emit('close', data);
- request({
- uri: "http://luckyshots.pl/ajax.php?s=load_depozyt&num=-1&user="+data,
- }, function (error, response, body) {
- if (error) {
- console.log(error);
- } else {
- json = {};
- try {
- json = JSON.parse(body.trim());
- } catch (e) {}
- var ile = parseInt(json.count);
- var last = json.lastItemId;
- var wszystko = [];
- console.log('Mamy ' + ile + ' itemow do pobrania');
- console.log('ID ostatniego itemu: ' + last);
- for (var now = 0; now <= ile; now += 12) {
- request({
- uri: "http://luckyshots.pl/ajax.php?s=load_depozyt&num=" + now + "&user=" + data,
- }, function (error, response, bodyq) {
- if (error){
- console.log(error);
- } else {
- if (bodyq[0] != '<') {
- var p = eval( "(" + bodyq + ")" )
- for (var key in p) {
- if (p.hasOwnProperty(key)) {
- wszystko.push({
- 'key': key,
- 'val': p[key]
- });
- if (key == last) {
- console.log('Send data to client');
- socket.emit('get_my_items_return', wszystko);
- }
- }
- }
- } else {
- console.log('Error while parsing items.');
- }
- }
- });
- }
- /**
- var tim = setInterval(function () {
- console.log('Wysylanie danych do uzytkownika');
- // console.log(wszystko);
- console.log(wszystko.length + ' :: ' + ile);
- if (wszystko.length == ile) {
- socket.emit('get_my_items_return', wszystko);
- clearInterval(tim);
- }
- }, 100);
- */
- }
- });
- });
- socket.on('offer state', function(data){
- io.emit('offer state', data);
- });
- var user = '';
- var admin = false;
- socket.on('online', function(msg){
- if(allClients[msg] !== undefined){
- user = msg;
- }else{
- allClients[msg] = 1;
- user = msg;
- }
- });
- io.emit('chat clients', Object.keys(allClients).length);
- //io.emit('new_items');
- io.emit('admins', admin);
- socket.on('support message', function(msg){
- connection.query('SELECT * FROM `users` WHERE `login_hash`=\''+xssEscape(msg.user)+'\' LIMIT 1', function(err, rows, fields){
- if (err) throw err;
- rows.forEach(function(row, callback){
- 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){
- if (err) throw err;
- socket.emit('support reaload', '');
- io.emit('reload admin support', '');
- io.emit('user-info', {msg: 'Nowa wiadomość w supporcie!!',
- admin: true,
- type: 'info',
- dimiss: true,
- delay: 30});
- });
- });
- });
- });
- socket.on('support admin message', function(msg){
- 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){
- if (err) throw err;
- io.emit('support reaload', '');
- io.emit('reload admin support', '');
- });
- });
- var temp_admin = false;
- socket.on('login admin', function(msg){
- temp_admin = true;
- admin++;
- io.emit('admins', admin);
- });
- socket.on('disconnect', function(){
- if(temp_admin == true){
- admin--;
- io.emit('admins', admin);
- }
- delete allClients[user];
- io.emit('chat clients', Object.keys(allClients).length);
- });
- });
- function start(bot_id){
- var proc = spawn('node', ['bot.js', bot_id]);
- proc.stdout.on('data', function (data) {
- console.log("BOT "+bot_id+": "+data.toString());
- });
- proc.stderr.on('data', function (data) {
- console.log("BOT "+bot_id+": "+data.toString());
- });
- proc.on('exit', function (code){
- console.log("BOT "+bot_id+" ZOSTAŁ ZATRZYMANY: "+ code);
- delete(proc);
- setTimeout(start(bot_id), 300000);
- });
- }
- function get_jackpot(me, socket){
- request({
- uri: "http://luckyshots.pl/ajax.php?s=jackpot2",
- }, function(error, response, body){
- if(error){
- console.log(error);
- }else{
- jackpot_data = body;
- if(me == true){
- socket.emit('reload jackpot', jackpot_data);
- }else{
- io.emit('reload jackpot', jackpot_data);
- }
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement