Advertisement
Guest User

Untitled

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