Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Bazaar Merchant Script
- ### Purpose: Allow players to set up items on this merchant to sell whilst they are offline or out of zone.
- ### Stores items in a custom database table, allowing for custom searches and buy/sell (add/remove from table)
- ### Written by: Vallik
- ###############################################################################################################################
- %items_h = ();
- sub EVENT_SPAWN
- {
- Check_Owner();
- Check_Items();
- Check_CustomParams();
- $npc->SetEntityVariable("Update", 0);
- $npc->SetEntityVariable("ChangeRace", 0);
- $npc->SetEntityVariable("ChangeGender", 0);
- $npc->SetEntityVariable("ChangeName", 0);
- $npc->SetEntityVariable("ChangeSize", 0);
- }
- sub EVENT_SAY
- {
- if ($client->AccountID() == $npc->GetEntityVariable("owner"))
- {
- my $list = quest::saylink("list", 1, "List Items");
- my $uprice = quest::saylink("update", 1, "Update Prices");
- my $remove = quest::saylink("remove", 1, "Remove Items");
- my $cashout = quest::saylink("cashout", 1, "Collect Income");
- my $params = quest::saylink("params", 1, "Change My Appearance");
- my $fire = quest::saylink("fire", 1, "Fire Me");
- if ($text =~ /hail/i) {
- plugin::Whisper("Greetings $name. What action would you like to perform?");
- plugin::Whisper("== $list == $uprice == $remove == $cashout == $params == $fire ==");
- }
- elsif ($text =~ /list/i) {
- foreach (sort keys %items_h) {
- plugin::Whisper("Item Index: " . $_ . " | Item: " . quest::varlink($items_h{$_}[1]) . " | Quantity: " . $items_h{$_}[2] . " | Price: " . $items_h{$_}[3]);
- }
- }
- elsif ($text =~ /update/i) {
- my $message = "Update Price For Item : ";
- foreach (sort keys %items_h) {
- $message = $message . quest::saylink("Item_" . $items_h{$_}[0], 1, $items_h{$_}[0]) . " || ";
- }
- plugin::Whisper($message);
- }
- elsif ($text =~ /^Item_/i) {
- my $index = substr($text, 5);
- if (!exists($items_h{$index})) {
- return;
- } else {
- plugin::Whisper("Item $index (" . quest::varlink($items_h{$index}[0]) . ") is currently set to " . $items_h{$index}[2] . "pp. What value would you like?");
- $npc->SetEntityVariable("Update", $index);
- }
- }
- elsif ($text =~ /^[0-9]+$/ && $npc->GetEntityVariable("Update") > 0) {
- if ($text > 0 && $text < 1000000) {
- my $index = $npc->GetEntityVariable("Update");
- my $it = $items_h{$index}[0];
- plugin::Whisper("Very well. I will now sell " . quest::varlink($it) . " for " . $text . "pp.");
- UpdatePrice($index, $text);
- } else {
- plugin::Whisper("Price must be over 0 and less than 1,000,000 platinum! Please try again.");
- }
- }
- elsif ($text =~ /remove/i) {
- my $message = "Remove Item : ";
- foreach (sort keys %items_h) {
- $message = $message . quest::saylink("Delete_" . $items_h{$_}[0], 1, $items_h{$_}[0]) . " || ";
- }
- plugin::Whisper($message);
- }
- elsif ($text =~ /^Delete_/i) {
- Check_Items();
- my $index = substr($text, 7);
- if (!exists($items_h{$index})) {
- plugin::Whisper("That item no longer exists! I probably just sold it for you.");
- } else {
- my $it = $items_h{$index}[0];
- plugin::Whisper("Ok. I will no longer sell " . quest::varlink($it) .", you can have it back.");
- RemoveItem($index);
- }
- }
- elsif ($text =~ /cashout/i) {
- my @sold = ();
- my $cash = Cashout($client->AccountID);
- if ($cash > 0) {
- plugin::Whisper("I sold the following items for you, totalling " . $cash . "pp.");
- foreach my $v (@sold) {
- plugin::Whisper($v[1] . " x " . quest::varlink($v[0]) . " (Worth: " . $v[2] . "each.");
- }
- $client->AddMoneyToPP(0, 0, 0, $cash, 1);
- } else {
- plugin::Whisper("I am sorry, I have not sold anything for you yet.");
- }
- }
- elsif ($text =~ /params/i) {
- my $mobname = quest::saylink("mobname", 1, "Name");
- my $raceid = quest::saylink("race", 1, "Race");
- my $genderid = quest::saylink("gender", 1, "Gender");
- my $appear = quest::saylink("appear", 1, "Randomize Features");
- my $mobsize = quest::saylink("size", 1, "Size");
- plugin::Whisper("Please select what you wish to change.");
- plugin::Whisper("== $mobname == $raceid == $genderid == $appear == $mobsize ==");
- }
- elsif ($text =~ /mobname/i) {
- 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>.");
- $npc->SetEntityVariable("ChangeName", 1);
- }
- elsif ($text =~ /^<.>/i && $npc->GetEntityVariable("ChangeName") == 1) {
- my $newname = substr($text, 1, -1);
- if ($newname =~ /[a-zA-Z]+$/)
- {
- $npc->SetEntityVariable("Name", $newname);
- $npc->SetEntityVariable("ChangeName", 0);
- $npc->TempName($newname);
- SaveNPC();
- } else {
- plugin::Whisper("You can't call me that!");
- }
- }
- elsif ($text =~ /race/i) {
- plugin::Whisper("What race would you like me to transform into?");
- plugin::Whisper(quest::saylink("Race_1", 1, "Human"));
- plugin::Whisper(quest::saylink("Race_2", 1, "Barbarian"));
- plugin::Whisper(quest::saylink("Race_3", 1, "Erudite"));
- plugin::Whisper(quest::saylink("Race_4", 1, "Wood Elf"));
- plugin::Whisper(quest::saylink("Race_5", 1, "High Elf"));
- plugin::Whisper(quest::saylink("Race_6", 1, "Dark Elf"));
- plugin::Whisper(quest::saylink("Race_7", 1, "Half Elf"));
- plugin::Whisper(quest::saylink("Race_8", 1, "Dwarf"));
- plugin::Whisper(quest::saylink("Race_9", 1, "Troll"));
- plugin::Whisper(quest::saylink("Race_10", 1, "Ogre"));
- plugin::Whisper(quest::saylink("Race_11", 1, "Halfling"));
- plugin::Whisper(quest::saylink("Race_12", 1, "Gnome"));
- plugin::Whisper(quest::saylink("Race_128", 1, "Iksar"));
- plugin::Whisper(quest::saylink("Race_130", 1, "Vah Shir"));
- plugin::Whisper(quest::saylink("Race_330", 1, "Froglok"));
- plugin::Whisper(quest::saylink("Race_522", 1, "Drakkin"));
- $npc->SetEntityVariable("ChangeRace", 1);
- }
- elsif ($text =~ /^Race_/i && $npc->GetEntityVariable("ChangeRace") == 1) {
- my $mobrace = substr($text, 5);
- if ($mobrace <= 12 || $mobrace == 128 || $mobrace == 130 || $mobrace == 330 || $mobrace == 522)
- {
- $mob->SetRace($mobrace);
- $npc->SetEntityVariable("ChangeRace", 0);
- SaveNPC();
- }
- }
- elsif ($text =~ /gender/i) {
- plugin::Whisper("Do you want me to be " . quest::saylink("Sex_0", 1, "male") . " or " . quest::saylink("Sex_1", 1, "female") . "?");
- $npc->SetEntityVariable("ChangeGender", 1);
- }
- elsif ($text =~ /^Sex_/i && $npc->GetEntityVariable("ChangeGender") == 1) {
- my $gender = substr($text, 4);
- if ($gender <= 1) {
- $mob->SetGender($gender);
- $npc->SetEntityVariable("ChangeGender", 0);
- SaveNPC();
- }
- }
- elsif ($text =~ /appear/i) {
- plugin::Whisper("Randomizing my features!");
- plugin::RandomFeatures($mob);
- SaveNPC();
- }
- elsif ($text =~ /size/i) {
- 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}.");
- $npc->SetEntityVariable("ChangeSize", 1);
- }
- elsif ($text =~ /^{.}/ && $npc->GetEntityVariable("ChangeSize") == 1) {
- my $newsize = substr($text, 1, -1);
- if ($newsize =~ /[3-8]/)
- {
- $mob->ChangeSize($newsize);
- $npc->SetEntityVariable("ChangeSize", 0);
- } else {
- plugin::Whisper("That's not a size between 3 and 8!");
- }
- }
- elsif ($text =~ /fire/i) {
- my $count = keys %items_h;
- if ($count != 0){
- plugin::Whisper("I still have items I am selling for you! You need to sell or remove those first boss.");
- } else {
- my $cash = Cashout($client->AccountID);
- if ($cash > 0) {
- plugin::Whisper("But I still owe you money boss! You should $cashout first!");
- } else {
- plugin::Whisper("Ok boss, until next time!");
- CleanBazaarTables();
- quest::depop();
- }
- }
- }
- }
- else
- {
- quest::emote("smiles at you. Would you like to buy something?");
- }
- }
- sub EVENT_ITEM
- {
- if ($client->AccountID() == $npc->GetEntityVariable("owner"))
- {
- my %new_items;
- if ($item1 > 0) { $new_items{0}[0] = $item1; $new_items{0}[1] = $item1_charges; }
- if ($item2 > 0) { $new_items{1}[0] = $item2; $new_items{1}[1] = $item2_charges; }
- if ($item3 > 0) { $new_items{2}[0] = $item3; $new_items{2}[1] = $item3_charges; }
- if ($item4 > 0) { $new_items{3}[0] = $item4; $new_items{3}[1] = $item4_charges; }
- AddNewItems();
- plugin::Whisper("I have added your new items to my list. Don't forget to update their selling price!");
- } else {
- plugin::return_items(\%itemcount);
- plugin::Whisper("I do not need your items, $name.");
- }
- }
- sub Check_Owner
- {
- my $connect = plugin::LoadMysql();
- my $query = "SELECT DISTINCT seller FROM `bazaar_merchant` WHERE npcid = '?'";
- my $query_handle = $connect->prepare($query);
- $query_handle->execute($mobid);
- my ($owner) = $query_handle->fetchrow_array();
- $npc->SetEntityVariable("owner", $owner);
- }
- sub Check_Items
- {
- my $connect = plugin::LoadMysql();
- my $query = "SELECT itemindex, itemid, charges, price FROM `bazaar_merchant` WHERE npcid = '?' AND itemid > 0 AND sold != 1 ORDER BY itemindex";
- my $query_handle = $connect->prepare($query);
- $query_handle->execute($mobid);
- my @items = $query_handle->fetchrow_array();
- %items_h = map { $_ => 1 } @items;
- }
- sub Check_CustomParams
- {
- my $connect = plugin::LoadMysql();
- my $query = "SELECT * FROM `bazaar_npc` WHERE npcid = '?'";
- my $query_handle = $connect->prepare($query);
- $query_handle->execute($mobid);
- my @appearance = $query_handle->fetchrow_array();
- $npc->TempName($appearance[1]);
- $npc->SetEntityVariable("Name", $appearance[1]);
- $npc->ChangeLastName("");
- $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]);
- }
- sub UpdatePrice
- {
- my $index = $_[0];
- my $price = $_[1];
- my $connect = plugin::LoadMysql();
- my $query = "UPDATE `bazaar_merchant` SET price = '?' WHERE npcid = '?' AND itemindex = '?'";
- my $query_handle = $connect->prepare($query);
- $query_handle->execute($price, $mobid, $index);
- $items_h{$index}[2] = $price;
- }
- sub RemoveItem
- {
- my $index = $_[0];
- my $connect = plugin::LoadMysql();
- my $query = "DELETE itemid, charges, itemtype, slot, price, sold, tags FROM `bazaar_merchant` WHERE npcid = '?' AND itemindex = '?'";
- my $query_handle = $connect->prepare($query);
- $query_handle->execute($mobid, $index);
- $client->SummonItem($items_h{$index}[0], $items_h{$index}[1]);
- delete $items_h{$index};
- quest::MerchantSetItem($mobid, $items_h{$index}[0], 0);
- }
- sub Cashout
- {
- my $acct = $_[0];
- my $cash = 0;
- my $connect = plugin::LoadMysql();
- my $query = "SELECT sold_item, quantity, price FROM `bazaar_income` WHERE seller = '?'";
- my $query_handle = $connect->prepare($query);
- $query_handle->execute($acct);
- @sold = $query_handle->fetchrow_array();
- foreach my $c (@sold) {
- my $income = $c[1] * $c[2];
- $cash = $cash + $income;
- }
- if ($cash > 0) {
- ClearIncome($acct);
- ClearMerchant($acct);
- }
- return $cash;
- }
- sub ClearIncome
- {
- my $acct = $_[0];
- my $connect = plugin::LoadMysql;
- my $query = "DELETE * FROM `bazaar_income` WHERE seller = ?";
- my $query_handle = $connect->prepare($query);
- $query_handle->execute($acct);
- }
- sub ClearMerchant
- {
- my $acct = $_[0];
- my $connect = plugin::LoadMysql();
- my $query = "DELETE * FROM `bazaar_merchant` WHERE seller = '?' AND npcid = '?' AND sold = '1'";
- my $query_handle = $connect->prepare($query);
- $query_handle->execute($acct, $mobid);
- }
- sub SaveNPC
- {
- my $connect = plugin::LoadMysql();
- my $query = "REPLACE INTO `bazaar_npc` VALUES (" . $mobid .
- ", " . $npc->GetEntityVariable("Name") .
- ", " . $mob->GetRace() .
- ", " . $mob->GetGender() .
- ", " . $mob->GetTexture() .
- ", " . $mob->GetHelmTexture() .
- ", " . $mob->GetLuclinFace() .
- ", " . $mob->GetHairStyle() .
- ", " . $mob->GetHairColor() .
- ", " . $mob->GetBeard() .
- ", " . $mob->GetBeardColor() .
- ", " . $mob->GetDrakkinHeritage() .
- ", " . $mob->GetDrakkinTattoo() .
- ", " . $mob->GetDrakkinDetails() .
- ", " . $mob->GetSize() .
- ")";
- my $query_handle = $connect->prepare($query);
- $query_handle->execute();
- }
- sub CleanBazaarTables
- {
- my $connect = plugin::LoadMysql();
- my $query = "DELETE * FROM `bazaar_npc` WHERE npcid = '?'";
- my $query_handle = $connect->prepare($query);
- $query_handle->execute($mobid);
- }
- sub AddNewItems
- {
- my $connect = plugin::LoadMysql();
- my $query = "SELECT itemindex, itemid FROM `bazaar_merchant` WHERE npcid ='?'";
- my $query_handle = $connect->prepare($query);
- $query_handle->execute($mobid);
- my @index = $query_handle->fetchrow_array();
- my @index_free = ();
- foreach $v (@index) {
- if ($index[1] > 0) {
- next;
- } else {
- push @index_free, $index[0];
- }
- }
- foreach(sort keys %new_items) {
- $query = "SELECT itemtype, slots, price FROM `items` WHERE id = '?'";
- $query_handle = $connect->prepare($query);
- $query_handle->execute($new_items{$_}[0]);
- my @item = $query_handle->fetchrow_array();
- my $slot = 0;
- if (@index_free) {
- $slot = shift @index_free;
- } else {
- $slot = @index;
- }
- $query = "REPLACE INTO `bazaar_merchant` VALUES (" . $client->AccountID .
- ", " . $slot .
- ", " . $mobid .
- ", " . $new_items{$_}[0] .
- ", " . $new_items{$_}[1] .
- ", " . $item[0] .
- ", " . $item[1] .
- ", " . $item[2] .
- ", 0, ''";
- $query_handle = $connect->prepare($query);
- $query_handle->execute();
- $items_h{$slot}[0] = $new_items{$_}[0];
- $items_h{$slot}[1] = $new_items{$_}[1];
- $items_h{$slot}[2] = $item[2];
- quest::MerchantSetItem($mobid, $new_items{$_}[0], $new_items{$_}[1]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement