marcelofoxes

Untitled

Sep 11th, 2017
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6.11 KB | None | 0 0
  1. use IO::Socket::INET;
  2. use Storable qw(thaw nfreeze);
  3. use Data::Dumper;
  4. use DBI;
  5. use DBD::mysql;
  6. use Digest::MD5 (md5_hex);
  7. use threads;
  8. # use Proc::Daemon;
  9. # Proc::Daemon::Init;
  10. system("color 5F");
  11. # system("echo -e '\e[0m'");
  12. print q{
  13.  ____  ___  _____  _  _  _____  __  __  _  _
  14. ( ___)/ __)(  _  )( \( )(  _  )(  \/  )( \/ )
  15.  )__)( (__  )(_)(  )  (  )(_)(  )    (  \  /
  16. (____)\___)(_____)(_)\_)(_____)(_/\/\_) (__) by Revok};
  17.  
  18. # auto-flush on socket
  19. $| = 1;
  20.  
  21. # creating a listening socket
  22. my $socket = new IO::Socket::INET (
  23.     LocalHost => 'localhost',
  24.     LocalPort => '7778',
  25.     Proto => 'tcp',
  26.     Listen => 5,
  27.     # Reuse => 1
  28. );
  29. die "cannot create socket $!\n" unless $socket;
  30. print "\n\nServer waiting for client connection on port 7777\n";
  31.  
  32.  
  33. my %db = (
  34.     'driver' => 'mysql',
  35.     'host' => '208.110.82.108',
  36.     # 'host' => 'ps.revok.com.br',
  37.     'user' => 'economy',
  38.     'password' => 'KabiFE3EXE6o2OvuYiPa66HePI64Ya',
  39.     'port' => 3306,
  40.     'database' => 'economy2'
  41. );
  42.  
  43. # my $conn = DBI->connect(
  44.         # 'dbi:'.$db{'driver'}.':'.$db{'database'}.':'.$db{'host'}.':'.$db{'port'},
  45.         # $db{'user'},
  46.         # $db{'password'},
  47.         # {mysql_enable_utf8 => 1,
  48.         # mysql_auto_reconnect => 1,
  49.         # mysql_connect_timeout => 20
  50.         # }
  51.     # ) or die (DBI->errstr);
  52.  
  53. # Establishing a connection with the DB
  54.  
  55. my @sof_queue = ();
  56.  
  57. # my $server = 'Asgard';
  58. my $old_reg_delete = time - 1;
  59. my $old_reg_delete_priority = time - 1;
  60.  
  61.  
  62. # print "Wait a new \n";
  63. # waiting for a new client connection
  64. while(my $client_socket = $socket->accept()) {
  65.     async(\&handle_con, $client_socket)->detach;
  66. }
  67.  
  68.  
  69. sub handle_con {
  70.     my $conn = DBI->connect(
  71.         'dbi:'.$db{'driver'}.':'.$db{'database'}.':'.$db{'host'}.':'.$db{'port'},
  72.         $db{'user'},
  73.         $db{'password'},
  74.         {mysql_enable_utf8 => 1,
  75.         mysql_auto_reconnect => 1
  76.         # , mysql_connect_timeout =>
  77.         }
  78.     ) or die (DBI->errstr);
  79.      my $client_socket = shift;
  80.     # get information about a newly connected client
  81.     my $client_address = $client_socket->peerhost();
  82.     my $client_port = $client_socket->peerport();
  83.     print "Unregistered connection from $client_address:$client_port\n";
  84.    
  85.     my $to_receive;
  86.     my $buffer;
  87.    
  88.     my $server;
  89.    
  90.     while (my $data = <$client_socket>) {
  91.         if (!$to_receive) {
  92.             $buffer = undef;
  93.             ($to_receive) = $data =~ /^(\d+).*/;
  94.             $to_receive++;
  95.             $data =~ s/^(\d+)//;
  96.         }
  97.        
  98.         $to_receive -= length($data);
  99.         $buffer .= $data;
  100.        
  101.         if (!$to_receive) {
  102.             chomp $buffer;
  103.             my $real_data = substr($buffer, 3);
  104.             my $command = substr($buffer, 0, 3);
  105.             my $udata = thaw $real_data;
  106.             if ($command eq 'ADD') {
  107.                     next if (!$udata->{items});
  108.                    
  109.                     my $store_md5 = md5_hex(nfreeze $udata->{items});
  110.                     next if ($udata->{vender_id} == 4611374);
  111.                     my $oldmd5;
  112.                     # my $query = "SELECT md5 FROM vendors WHERE server = ? AND vendor_id = ? LIMIT 1;";
  113.                     # my $stmt = $conn->prepare($query);
  114.                     # $stmt->execute($udata->{vender_id}, $server);
  115.                     # $stmt->bind_columns(\$oldmd5);
  116.                     # $stmt->fetch();
  117.                     # print "MD5: [ $oldmd5 -> $store_md5 ]\n";
  118.                     #my $query = "INSERT INTO vendors (vendor_id, vendor_name, shop_title, location_map, location_x, location_y, server) VALUES (?,?,?,?,?,?,?);";
  119.                     $query = "INSERT INTO vendors (vendor_id, vendor_name, shop_title, location_map, location_x, location_y, md5, server, time) VALUES (?,?,?,?,?,?,?,?, now()) ON DUPLICATE KEY UPDATE vendor_name = ?, shop_title = ?, location_map = ?, location_x = ?, location_y = ?, md5 = ?, time = now(), online = 1;";
  120.                     $stmt = $conn->prepare($query);
  121.                     $stmt->execute($udata->{vender_id}, $udata->{vender_name}, $udata->{title}, $udata->{location}->{map}, $udata->{location}->{x}, $udata->{location}->{y}, $store_md5, $server,
  122.                         $udata->{vender_name}, $udata->{title}, $udata->{location}->{map}, $udata->{location}->{x}, $udata->{location}->{y}, $store_md5);
  123.                     if ($stmt->errstr) {
  124.                         next;
  125.                     }
  126.                     #if ($oldmd5 != $store_md5) {
  127.                         print "[$server] UPDATING ".$udata->{vender_name}."'s shop \n";
  128.                         my $query1 = "DELETE FROM items WHERE owner_id = ? AND server = ?;";
  129.                         my $stmt1 = $conn->prepare($query1);
  130.                         $stmt1->execute($udata->{vender_id}, $server);
  131.                         if ($stmt->errstr) {
  132.                             next;
  133.                         }
  134.                         foreach my $item (@{$udata->{items}}) {
  135.                             my $query = "INSERT INTO items (item_id, owner_id, name, price, amount, server) VALUES (?,?,?,?,?,?);";
  136.                             my $stmt = $conn->prepare($query);
  137.                             $stmt->execute($item->{nameID}, $udata->{vender_id}, $item->{full_name}, $item->{price}, $item->{amount}, $server);
  138.                         }
  139.                     #}
  140.             } elsif ($command eq 'SOF') {
  141.                 print "[$server] QUEUEING SOF\n";
  142.                 push (@sof_queue, { min_x => $udata->{x} - 14, max_x => $udata->{x} + 14, min_y => $udata->{y} - 14, max_y => $udata->{y} + 14, map_name => $udata->{map}, server => $server, time => time });
  143.                
  144.                 #print Data::Dumper::Dumper(@sof_queue);           
  145.             } elsif ($command eq 'IDD') {
  146.                 $server = $udata->{server};
  147.                 print sprintf("[$server] Registering user %s to server %s\n", $udata->{username}, $udata->{server});
  148.                
  149.                
  150.                 # print "QUEUEING SOF\n";
  151.                 # push (@sof_queue, { min_x => $udata->{x} - 14, max_x => $udata->{x} + 14, min_y => $udata->{y} - 14, max_y => $udata->{y} + 14, map_name => $udata->{map}, server => $server, time => time });
  152.                
  153.                 #print Data::Dumper::Dumper(@sof_queue);           
  154.             }
  155.             while ($sof_queue[0] && time > ($sof_queue[0]->{time} + 30)) { # prevent autovivification
  156.                 print "[$server] TRIGGER ".($sof_queue[0]->{time} + 30)." end ".$sof_queue[0]->{time} ." and ".time."\n";
  157.                 print "[$server] UPDATING OFFLINE MERCHS... \n";
  158.                 my $query = "UPDATE vendors SET online=0 WHERE online = ? AND location_x >= ? AND location_x <= ? AND location_y >= ? AND location_y <= ? AND location_map = ? AND server = ? AND time < ADDDATE(NOW(), INTERVAL -5 MINUTE);";
  159.                 my $stmt = $conn->prepare($query);
  160.                 $stmt->execute(1, $sof_queue[0]->{min_x}, $sof_queue[0]->{max_x}, $sof_queue[0]->{min_y}, $sof_queue[0]->{max_y}, $sof_queue[0]->{map_name}, $server);
  161.                
  162.                 unless ($stmt->errstr) {
  163.                     shift(@sof_queue);
  164.                 }
  165.             }
  166.            
  167.         }
  168.     }
  169.     $conn->disconnect();
  170.     $socket->close();
  171. }
Advertisement
Add Comment
Please, Sign In to add comment