Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 3.44 KB | None | 0 0
  1. ### Randomly add items to Bazaar merchant each restart
  2. ### Stores items in merchantlist_temp
  3. ### Reads items from Bazaar.txt list
  4. ##################################################################################
  5. ##################################################################################
  6.  
  7. #::: Variables
  8. $merchant1 = 500000;
  9. $number_of_items = 5;
  10. $file = 'Bazaar.txt';
  11. @items = ();
  12.  
  13. #::: Script Body
  14. #::: Read database name, user and password from eqemu config file
  15. read_eqemu_config_xml();
  16. get_mysql_path();
  17.  
  18. #::: Read bazaar items from text file "Bazaar.txt"
  19. read_bazaar_items();
  20.  
  21. #::: Clear previous entries from merchantlist_temp table
  22. clear_existing_merchantlist_temp($merchant1);
  23.  
  24. #::: Add $number_of_items randomly to merchantlist_temp for merchant
  25. add_merchantlist_temp_items($merchant1);
  26.  
  27. sub read_eqemu_config_xml {
  28.     open (CONFIG, "eqemu_config.xml");
  29.     while (<CONFIG>){
  30.         chomp;
  31.         $o = $_;
  32.        
  33.         if($o=~/\<\!--/i){
  34.             next;
  35.         }
  36.        
  37.         if($o=~/database/i && $o=~/\<\//i){
  38.             $in_database_tag = 0;
  39.         }
  40.         if($o=~/database/i){
  41.             print "IN DATABASE TAG\n" if $debug;
  42.             $in_database_tag = 1;
  43.         }
  44.         if ($o=~/<longname>/i){
  45.             ($long_name) = $o =~ /<longname>(.*)<\/longname>/;
  46.             print "Long Name: '" . $long_name . "'/n" if $debug;
  47.         }
  48.         if($in_database_tag == 1){
  49.             @left = split (">", $o);
  50.             @right = split ("<", $left[1]);
  51.             $tag_data = trim($right[0]);
  52.            
  53.             if($o=~/<username>/i && $in_database_tag){
  54.                 $user = $tag_data;
  55.                 print "Database User: '" . $user . "'\n" if $debug;
  56.             }
  57.             if($o=~/<password>/i && $in_database_tag){
  58.                 $pass = $tag_data;
  59.                 print "Database Pass: '" . $pass . "'\n" if $debug;
  60.             }
  61.             if($o=~/<db>/i){
  62.                 $db = $tag_data;
  63.                 print "Database Name: '" . $db . "'\n" if $debug;
  64.             }
  65.             if($o=~/<host>/i){
  66.                 $host = $tag_data;
  67.                 print "Database Host: '" . $host . "'\n" if $debug;
  68.             }
  69.         }
  70.     }
  71.     close(CONFIG);
  72. }
  73.  
  74. sub get_mysql_path {
  75.     $has_mysql_path = `echo %PATH%`;
  76.     if($has_mysql_path=~/MySQL|MariaDB/i){
  77.         @mysql = split(';', $has_mysql_path);
  78.         foreach my $v (@mysql){
  79.             if($v=~/MySQL|MariaDB/i){
  80.                 $v =~s/\n//g;
  81.                 $path = trim($v) . "/mysql";
  82.                 last;
  83.             }
  84.         }
  85.     }
  86.    
  87.     #::: Path not found, error and exit
  88.     if($path eq ""){
  89.         print "[Error:Bazaar.pl] MySQL path not found, please add the path for automatic merchant script to continue... \n\n";
  90.         exit;
  91.     }  
  92. }
  93.  
  94. sub get_mysql_result {
  95.     my $run_query = $_[0];
  96.     if(!$db){ return; }
  97.     return `"$path" --host $host --user $user --password="$pass" $db -N -B -e "$run_query"`;
  98. }
  99.  
  100. sub clear_existing_merchantlist_temp {
  101.     my $merchant = $_[0];
  102.     get_mysql_result("DELETE FROM `merchantlist_temp` WHERE `npcid` = " . $merchant);
  103.    
  104.     print "Cleared Temp Merchantlist For NPC: '" . $merchant . "'\n" if $debug;
  105. }
  106.  
  107. sub read_bazaar_items {
  108.     open my $info, $file or die "Could not open $file: $!";
  109.    
  110.     while (my $line = <$info>){
  111.         push(@items, $line);
  112.     }
  113.    
  114.     close $info;
  115. }
  116.  
  117. sub add_merchantlist_temp_items {
  118.     my $merchant = $_[0];
  119.     my $slot = 0;
  120.     my @tmp_items = @items;
  121.     my @add_items = ();
  122.    
  123.     for (my $i = 0; $i < $number_of_items; $i++){
  124.         push(@add_items, splice(@tmp_items, rand @tmp_items, 1));
  125.     }
  126.    
  127.     while (@add_items){
  128.         my $item = pop @add_items;
  129.         print get_mysql_result("INSERT INTO `merchantlist_temp` VALUES ('" . $merchant . "', '" . ++$slot . "', '" . $item . "', '1');");
  130.     }
  131. }
  132.  
  133. sub trim {
  134.     my $string = $_[0];
  135.     $string =~ s/^\s+//;
  136.     $string =~ s/\s+$//;
  137.     return $string;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement