Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 13.91 KB | None | 0 0
  1. ### Bazaar Merchant Script
  2. ### Purpose: Allow players to set up items on this merchant to sell whilst they are offline or out of zone.
  3. ### Stores items in a custom database table, allowing for custom searches and buy/sell (add/remove from table)
  4. ### Written by: Vallik
  5. ###############################################################################################################################
  6.  
  7. %items_h = ();
  8.  
  9. sub EVENT_SPAWN
  10. {
  11.     Check_Owner();
  12.     Check_Items();
  13.     Check_CustomParams();
  14.     $npc->SetEntityVariable("Update", 0);
  15.     $npc->SetEntityVariable("ChangeRace", 0);
  16.     $npc->SetEntityVariable("ChangeGender", 0);
  17.     $npc->SetEntityVariable("ChangeName", 0);
  18.     $npc->SetEntityVariable("ChangeSize", 0);
  19. }
  20.  
  21. sub EVENT_SAY
  22. {
  23.     if ($client->AccountID() == $npc->GetEntityVariable("owner"))
  24.     {
  25.         my $list = quest::saylink("list", 1, "List Items");
  26.         my $uprice = quest::saylink("update", 1, "Update Prices");
  27.         my $remove = quest::saylink("remove", 1, "Remove Items");
  28.         my $cashout = quest::saylink("cashout", 1, "Collect Income");
  29.         my $params = quest::saylink("params", 1, "Change My Appearance");
  30.         my $fire = quest::saylink("fire", 1, "Fire Me");
  31.    
  32.         if ($text =~ /hail/i) {
  33.             plugin::Whisper("Greetings $name. What action would you like to perform?");
  34.             plugin::Whisper("== $list == $uprice == $remove == $cashout == $params == $fire ==");
  35.         }
  36.        
  37.         elsif ($text =~ /list/i) {
  38.             foreach (sort keys %items_h) {
  39.                 plugin::Whisper("Item Index: " . $_ . " | Item: " . quest::varlink($items_h{$_}[1]) . " | Quantity: " . $items_h{$_}[2] . " | Price: " . $items_h{$_}[3]);
  40.             }
  41.         }
  42.        
  43.         elsif ($text =~ /update/i) {
  44.             my $message = "Update Price For Item : ";
  45.             foreach (sort keys %items_h) {
  46.                 $message = $message . quest::saylink("Item_" . $items_h{$_}[0], 1, $items_h{$_}[0]) . " || ";
  47.             }
  48.             plugin::Whisper($message);
  49.         }
  50.        
  51.         elsif ($text =~ /^Item_/i) {
  52.             my $index = substr($text, 5);
  53.             if (!exists($items_h{$index})) {
  54.                 return;
  55.             } else {
  56.                 plugin::Whisper("Item $index (" . quest::varlink($items_h{$index}[0]) . ") is currently set to " . $items_h{$index}[2] . "pp. What value would you like?");
  57.                 $npc->SetEntityVariable("Update", $index);
  58.             }
  59.         }
  60.        
  61.         elsif ($text =~ /^[0-9]+$/ && $npc->GetEntityVariable("Update") > 0) {
  62.             if ($text > 0 && $text < 1000000) {
  63.                 my $index = $npc->GetEntityVariable("Update");
  64.                 my $it = $items_h{$index}[0];
  65.                 plugin::Whisper("Very well. I will now sell " . quest::varlink($it) . " for " . $text . "pp.");
  66.                 UpdatePrice($index, $text);
  67.             } else {
  68.                 plugin::Whisper("Price must be over 0 and less than 1,000,000 platinum! Please try again.");
  69.             }
  70.         }
  71.        
  72.         elsif ($text =~ /remove/i) {
  73.             my $message = "Remove Item : ";
  74.             foreach (sort keys %items_h) {
  75.                 $message = $message . quest::saylink("Delete_" . $items_h{$_}[0], 1, $items_h{$_}[0]) . " || ";
  76.             }
  77.             plugin::Whisper($message);
  78.         }
  79.        
  80.         elsif ($text =~ /^Delete_/i) {
  81.             Check_Items();
  82.             my $index = substr($text, 7);
  83.             if (!exists($items_h{$index})) {
  84.                 plugin::Whisper("That item no longer exists! I probably just sold it for you.");
  85.             } else {
  86.                 my $it = $items_h{$index}[0];
  87.                 plugin::Whisper("Ok. I will no longer sell " . quest::varlink($it) .", you can have it back.");
  88.                 RemoveItem($index);
  89.             }
  90.         }
  91.        
  92.         elsif ($text =~ /cashout/i) {
  93.             my @sold = ();
  94.             my $cash = Cashout($client->AccountID);
  95.             if ($cash > 0) {
  96.                 plugin::Whisper("I sold the following items for you, totalling " . $cash . "pp.");
  97.                 foreach my $v (@sold) {
  98.                     plugin::Whisper($v[1] . " x " . quest::varlink($v[0]) . " (Worth: " . $v[2] . "each.");
  99.                 }
  100.                 $client->AddMoneyToPP(0, 0, 0, $cash, 1);
  101.             } else {
  102.                 plugin::Whisper("I am sorry, I have not sold anything for you yet.");
  103.             }
  104.         }
  105.        
  106.         elsif ($text =~ /params/i) {
  107.             my $mobname = quest::saylink("mobname", 1, "Name");
  108.             my $raceid = quest::saylink("race", 1, "Race");
  109.             my $genderid = quest::saylink("gender", 1, "Gender");
  110.             my $appear = quest::saylink("appear", 1, "Randomize Features");
  111.             my $mobsize = quest::saylink("size", 1, "Size");
  112.            
  113.             plugin::Whisper("Please select what you wish to change.");
  114.             plugin::Whisper("== $mobname == $raceid == $genderid == $appear == $mobsize ==");
  115.         }
  116.        
  117.         elsif ($text =~ /mobname/i) {
  118.             plugin::Whisper("Well, I guess you can call me whatever you want. you're the boss! Just say <name> and I will change my name to that. Just like <Frank> or <Penny>.");
  119.             $npc->SetEntityVariable("ChangeName", 1);
  120.         }
  121.        
  122.         elsif ($text =~ /^<.>/i && $npc->GetEntityVariable("ChangeName") == 1) {
  123.             my $newname = substr($text, 1, -1);
  124.             if ($newname =~ /[a-zA-Z]+$/)
  125.             {
  126.                 $npc->SetEntityVariable("Name", $newname);
  127.                 $npc->SetEntityVariable("ChangeName", 0);
  128.                 $npc->TempName($newname);
  129.                 SaveNPC();
  130.             } else {
  131.                 plugin::Whisper("You can't call me that!");
  132.             }
  133.         }
  134.        
  135.         elsif ($text =~ /race/i) {
  136.         plugin::Whisper("What race would you like me to transform into?");
  137.         plugin::Whisper(quest::saylink("Race_1", 1, "Human"));
  138.         plugin::Whisper(quest::saylink("Race_2", 1, "Barbarian"));
  139.         plugin::Whisper(quest::saylink("Race_3", 1, "Erudite"));
  140.         plugin::Whisper(quest::saylink("Race_4", 1, "Wood Elf"));
  141.         plugin::Whisper(quest::saylink("Race_5", 1, "High Elf"));
  142.         plugin::Whisper(quest::saylink("Race_6", 1, "Dark Elf"));
  143.         plugin::Whisper(quest::saylink("Race_7", 1, "Half Elf"));
  144.         plugin::Whisper(quest::saylink("Race_8", 1, "Dwarf"));
  145.         plugin::Whisper(quest::saylink("Race_9", 1, "Troll"));
  146.         plugin::Whisper(quest::saylink("Race_10", 1, "Ogre"));
  147.         plugin::Whisper(quest::saylink("Race_11", 1, "Halfling"));
  148.         plugin::Whisper(quest::saylink("Race_12", 1, "Gnome"));
  149.         plugin::Whisper(quest::saylink("Race_128", 1, "Iksar"));
  150.         plugin::Whisper(quest::saylink("Race_130", 1, "Vah Shir"));
  151.         plugin::Whisper(quest::saylink("Race_330", 1, "Froglok"));
  152.         plugin::Whisper(quest::saylink("Race_522", 1, "Drakkin"));
  153.         $npc->SetEntityVariable("ChangeRace", 1);
  154.         }
  155.        
  156.         elsif ($text =~ /^Race_/i && $npc->GetEntityVariable("ChangeRace") == 1) {
  157.             my $mobrace = substr($text, 5);
  158.             if ($mobrace <= 12 || $mobrace == 128 || $mobrace == 130 || $mobrace == 330 || $mobrace == 522)
  159.             {
  160.                 $mob->SetRace($mobrace);
  161.                 $npc->SetEntityVariable("ChangeRace", 0);
  162.                 SaveNPC();
  163.             }
  164.         }
  165.  
  166.         elsif ($text =~ /gender/i) {
  167.             plugin::Whisper("Do you want me to be " . quest::saylink("Sex_0", 1, "male") . " or " . quest::saylink("Sex_1", 1, "female") . "?");
  168.             $npc->SetEntityVariable("ChangeGender", 1);
  169.         }
  170.        
  171.         elsif ($text =~ /^Sex_/i && $npc->GetEntityVariable("ChangeGender") == 1) {
  172.             my $gender = substr($text, 4);
  173.             if ($gender <= 1) {
  174.                 $mob->SetGender($gender);
  175.                 $npc->SetEntityVariable("ChangeGender", 0);
  176.                 SaveNPC();
  177.             }
  178.         }
  179.        
  180.         elsif ($text =~ /appear/i) {
  181.             plugin::Whisper("Randomizing my features!");
  182.             plugin::RandomFeatures($mob);
  183.             SaveNPC();
  184.         }
  185.  
  186.         elsif ($text =~ /size/i) {
  187.             plugin::Whisper("Well, I could shrink or grow I guess. Just let me know what size by saying {size}, anywhere between 3 and 8. Just like {5} or {7}.");
  188.             $npc->SetEntityVariable("ChangeSize", 1);
  189.         }
  190.        
  191.         elsif ($text =~ /^{.}/ && $npc->GetEntityVariable("ChangeSize") == 1) {
  192.             my $newsize = substr($text, 1, -1);
  193.             if ($newsize =~ /[3-8]/)
  194.             {
  195.                 $mob->ChangeSize($newsize);
  196.                 $npc->SetEntityVariable("ChangeSize", 0);
  197.             } else {
  198.                 plugin::Whisper("That's not a size between 3 and 8!");
  199.             }
  200.         }
  201.        
  202.         elsif ($text =~ /fire/i) {
  203.             my $count = keys %items_h;
  204.             if ($count != 0){
  205.                 plugin::Whisper("I still have items I am selling for you! You need to sell or remove those first boss.");
  206.             } else {
  207.                 my $cash = Cashout($client->AccountID);
  208.                 if ($cash > 0) {
  209.                     plugin::Whisper("But I still owe you money boss! You should $cashout first!");
  210.                 } else {
  211.                     plugin::Whisper("Ok boss, until next time!");
  212.                     CleanBazaarTables();
  213.                     quest::depop();
  214.                 }
  215.             }
  216.         }
  217.     }
  218.     else
  219.     {
  220.         quest::emote("smiles at you. Would you like to buy something?");
  221.     }
  222. }
  223.  
  224. sub EVENT_ITEM
  225. {
  226.     if ($client->AccountID() == $npc->GetEntityVariable("owner"))
  227.     {
  228.         my %new_items;
  229.         if ($item1 > 0) { $new_items{0}[0] = $item1; $new_items{0}[1] = $item1_charges; }
  230.         if ($item2 > 0) { $new_items{1}[0] = $item2; $new_items{1}[1] = $item2_charges; }
  231.         if ($item3 > 0) { $new_items{2}[0] = $item3; $new_items{2}[1] = $item3_charges; }
  232.         if ($item4 > 0) { $new_items{3}[0] = $item4; $new_items{3}[1] = $item4_charges; }
  233.         AddNewItems();
  234.         plugin::Whisper("I have added your new items to my list. Don't forget to update their selling price!");
  235.     } else {
  236.         plugin::return_items(\%itemcount);
  237.         plugin::Whisper("I do not need your items, $name.");
  238.     }
  239. }
  240.  
  241. sub Check_Owner
  242. {
  243.     my $connect = plugin::LoadMysql();
  244.     my $query = "SELECT DISTINCT seller FROM `bazaar_merchant` WHERE npcid = '?'";
  245.     my $query_handle = $connect->prepare($query);
  246.     $query_handle->execute($mobid);
  247.     my ($owner) = $query_handle->fetchrow_array();
  248.     $npc->SetEntityVariable("owner", $owner);
  249. }
  250.  
  251. sub Check_Items
  252. {
  253.     my $connect = plugin::LoadMysql();
  254.     my $query = "SELECT itemindex, itemid, charges, price FROM `bazaar_merchant` WHERE npcid = '?' AND itemid > 0 AND sold != 1 ORDER BY itemindex";
  255.     my $query_handle = $connect->prepare($query);
  256.     $query_handle->execute($mobid);
  257.    
  258.     my @items = $query_handle->fetchrow_array();
  259.     %items_h = map { $_ => 1 } @items;
  260. }
  261.  
  262. sub Check_CustomParams
  263. {
  264.     my $connect = plugin::LoadMysql();
  265.     my $query = "SELECT * FROM `bazaar_npc` WHERE npcid = '?'";
  266.     my $query_handle = $connect->prepare($query);
  267.     $query_handle->execute($mobid);
  268.    
  269.     my @appearance = $query_handle->fetchrow_array();
  270.    
  271.     $npc->TempName($appearance[1]);
  272.     $npc->SetEntityVariable("Name", $appearance[1]);
  273.     $npc->ChangeLastName("");
  274.     $npc->SendIllusion($appearance[2], $appearance[3], $appearance[4], $appearance[5], $appearance[6], $appearance[7], $appearance[8], $appearance[9], $appearance[10], $appearance[11], $appearance[12], $appearance[13], $appearance[14]);
  275. }
  276.  
  277. sub UpdatePrice
  278. {
  279.     my $index = $_[0];
  280.     my $price = $_[1];
  281.     my $connect = plugin::LoadMysql();
  282.     my $query = "UPDATE `bazaar_merchant` SET price = '?' WHERE npcid = '?' AND itemindex = '?'";
  283.     my $query_handle = $connect->prepare($query);
  284.     $query_handle->execute($price, $mobid, $index);
  285.     $items_h{$index}[2] = $price;
  286. }
  287.  
  288. sub RemoveItem
  289. {
  290.     my $index = $_[0];
  291.     my $connect = plugin::LoadMysql();
  292.     my $query = "DELETE itemid, charges, itemtype, slot, price, sold, tags FROM `bazaar_merchant` WHERE npcid = '?' AND itemindex = '?'";
  293.     my $query_handle = $connect->prepare($query);
  294.     $query_handle->execute($mobid, $index);
  295.     $client->SummonItem($items_h{$index}[0], $items_h{$index}[1]);
  296.     delete $items_h{$index};
  297.     quest::MerchantSetItem($mobid, $items_h{$index}[0], 0);
  298. }
  299.  
  300. sub Cashout
  301. {
  302.     my $acct = $_[0];
  303.     my $cash = 0;
  304.     my $connect = plugin::LoadMysql();
  305.     my $query = "SELECT sold_item, quantity, price FROM `bazaar_income` WHERE seller = '?'";
  306.     my $query_handle = $connect->prepare($query);
  307.     $query_handle->execute($acct);
  308.    
  309.     @sold = $query_handle->fetchrow_array();
  310.    
  311.     foreach my $c (@sold) {
  312.         my $income = $c[1] * $c[2];
  313.         $cash = $cash + $income;
  314.     }
  315.    
  316.     if ($cash > 0) {
  317.         ClearIncome($acct);
  318.         ClearMerchant($acct);
  319.     }
  320.    
  321.     return $cash;
  322. }
  323.  
  324. sub ClearIncome
  325. {
  326.     my $acct = $_[0];
  327.     my $connect = plugin::LoadMysql;
  328.     my $query = "DELETE * FROM `bazaar_income` WHERE seller = ?";
  329.     my $query_handle = $connect->prepare($query);
  330.     $query_handle->execute($acct);
  331. }
  332.  
  333. sub ClearMerchant
  334. {
  335.     my $acct = $_[0];
  336.     my $connect = plugin::LoadMysql();
  337.     my $query = "DELETE * FROM `bazaar_merchant` WHERE seller = '?' AND npcid = '?' AND sold = '1'";
  338.     my $query_handle = $connect->prepare($query);
  339.     $query_handle->execute($acct, $mobid); 
  340. }
  341.  
  342. sub SaveNPC
  343. {
  344.     my $connect = plugin::LoadMysql();
  345.     my $query = "REPLACE INTO `bazaar_npc` VALUES (" . $mobid .
  346.         ", " . $npc->GetEntityVariable("Name") .
  347.         ", " . $mob->GetRace() .
  348.         ", " . $mob->GetGender() .
  349.         ", " . $mob->GetTexture() .
  350.         ", " . $mob->GetHelmTexture() .
  351.         ", " . $mob->GetLuclinFace() .
  352.         ", " . $mob->GetHairStyle() .
  353.         ", " . $mob->GetHairColor() .
  354.         ", " . $mob->GetBeard() .
  355.         ", " . $mob->GetBeardColor() .
  356.         ", " . $mob->GetDrakkinHeritage() .
  357.         ", " . $mob->GetDrakkinTattoo() .
  358.         ", " . $mob->GetDrakkinDetails() .
  359.         ", " . $mob->GetSize() .
  360.         ")";
  361.        
  362.     my $query_handle = $connect->prepare($query);
  363.     $query_handle->execute();
  364. }
  365.  
  366. sub CleanBazaarTables
  367. {
  368.     my $connect = plugin::LoadMysql();
  369.     my $query = "DELETE * FROM `bazaar_npc` WHERE npcid = '?'";
  370.     my $query_handle = $connect->prepare($query);
  371.     $query_handle->execute($mobid);
  372. }
  373.  
  374. sub AddNewItems
  375. {
  376.     my $connect = plugin::LoadMysql();
  377.     my $query = "SELECT itemindex, itemid FROM `bazaar_merchant` WHERE npcid ='?'";
  378.     my $query_handle = $connect->prepare($query);
  379.     $query_handle->execute($mobid);
  380.        
  381.     my @index = $query_handle->fetchrow_array();
  382.     my @index_free = ();
  383.    
  384.     foreach $v (@index) {
  385.         if ($index[1] > 0) {
  386.             next;
  387.         } else {
  388.             push @index_free, $index[0];
  389.         }
  390.     }
  391.  
  392.     foreach(sort keys %new_items) {
  393.         $query = "SELECT itemtype, slots, price FROM `items` WHERE id = '?'";
  394.         $query_handle = $connect->prepare($query);
  395.         $query_handle->execute($new_items{$_}[0]);
  396.        
  397.         my @item = $query_handle->fetchrow_array();
  398.         my $slot = 0;
  399.        
  400.         if (@index_free) {
  401.             $slot = shift @index_free;
  402.         } else {
  403.             $slot = @index;
  404.         }
  405.                                
  406.         $query = "REPLACE INTO `bazaar_merchant` VALUES (" . $client->AccountID .
  407.             ", " . $slot .
  408.             ", " . $mobid .
  409.             ", " . $new_items{$_}[0] .
  410.             ", " . $new_items{$_}[1] .
  411.             ", " . $item[0] .
  412.             ", " . $item[1] .
  413.             ", " . $item[2] .
  414.             ", 0, ''";
  415.         $query_handle = $connect->prepare($query);
  416.         $query_handle->execute();
  417.  
  418.         $items_h{$slot}[0] = $new_items{$_}[0];
  419.         $items_h{$slot}[1] = $new_items{$_}[1];
  420.         $items_h{$slot}[2] = $item[2];
  421.        
  422.         quest::MerchantSetItem($mobid, $new_items{$_}[0], $new_items{$_}[1]);
  423.     }
  424. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement