Advertisement
Kiporralixo

Untitled

Mar 6th, 2017
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.16 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Data;
  5. using System.Collections.Generic;
  6.  
  7. using Plus.HabboHotel.GameClients;
  8.  
  9. using Plus.HabboHotel.Items;
  10. using Plus.HabboHotel.Catalog.Marketplace;
  11. using Plus.Communication.Packets.Outgoing.Marketplace;
  12. using Plus.Communication.Packets.Outgoing.Inventory.Purse;
  13. using Plus.Communication.Packets.Outgoing.Inventory.Furni;
  14.  
  15. using Plus.Database.Interfaces;
  16.  
  17.  
  18. namespace Plus.Communication.Packets.Incoming.Marketplace
  19. {
  20.     class BuyOfferEvent : IPacketEvent
  21.     {
  22.         public void Parse(HabboHotel.GameClients.GameClient Session, ClientPacket Packet)
  23.         {
  24.             int OfferId = Packet.PopInt();
  25.  
  26.             DataRow Row = null;
  27.             using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
  28.             {
  29.                 dbClient.SetQuery("SELECT `state`,`timestamp`,`total_price`,`extra_data`,`item_id`,`furni_id`,`user_id`,`limited_number`,`limited_stack` FROM `catalog_marketplace_offers` WHERE `offer_id` = @OfferId LIMIT 1");
  30.                 dbClient.AddParameter("OfferId", OfferId);
  31.                 Row = dbClient.getRow();
  32.             }
  33.  
  34.             if (Row == null)
  35.             {
  36.                 this.ReloadOffers(Session);
  37.                 return;
  38.             }
  39.  
  40.             if (Convert.ToString(Row["state"]) == "2")
  41.             {
  42.                 Session.SendNotification("Opa, esta oferta não está mais disponível.");
  43.                 this.ReloadOffers(Session);
  44.                 return;
  45.             }
  46.  
  47.             if (PlusEnvironment.GetGame().GetCatalog().GetMarketplace().FormatTimestamp() > (Convert.ToDouble(Row["timestamp"])))
  48.             {
  49.                 Session.SendNotification("Opa, esta oferta expirou...");
  50.                 this.ReloadOffers(Session);
  51.                 return;
  52.             }
  53.  
  54.             ItemData Item = null;
  55.             if (!PlusEnvironment.GetGame().GetItemManager().GetItem(Convert.ToInt32(Row["item_id"]), out Item))
  56.             {
  57.                 Session.SendNotification("Item não está mais no hotel.");
  58.                 this.ReloadOffers(Session);
  59.                 return;
  60.             }
  61.             else
  62.             {
  63.                 if (Convert.ToInt32(Row["user_id"]) == Session.GetHabbo().Id)
  64.                 {
  65.                     Session.SendNotification("Você não pode comprar seus próprios produtos no mercado " + PlusEnvironment.NOMEHOTEL);
  66.                     return;
  67.                 }
  68.  
  69.                 if (Convert.ToInt32(Row["total_price"]) > Session.GetHabbo().Diamonds)
  70.                 {
  71.                     Session.SendNotification("Opa, você não tem "+ PlusEnvironment.NOMEMOEDA +" suficientes para comprar esta mobília.");
  72.                     return;
  73.                 }
  74.  
  75.                 Session.GetHabbo().Diamonds -= Convert.ToInt32(Row["total_price"]);
  76.                 Session.SendMessage(new HabboActivityPointNotificationComposer(Session.GetHabbo().Diamonds, Session.GetHabbo().Diamonds, 5));
  77.  
  78.  
  79.                 Item GiveItem = ItemFactory.CreateSingleItem(Item, Session.GetHabbo(), Convert.ToString(Row["extra_data"]), Convert.ToString(Row["extra_data"]), Convert.ToInt32(Row["furni_id"]), Convert.ToInt32(Row["limited_number"]), Convert.ToInt32(Row["limited_stack"]));
  80.                 if (GiveItem != null)
  81.                 {
  82.                     Session.GetHabbo().GetInventoryComponent().TryAddItem(GiveItem);
  83.                     Session.SendMessage(new FurniListNotificationComposer(GiveItem.Id, 1));
  84.  
  85.                     Session.SendMessage(new Plus.Communication.Packets.Outgoing.Catalog.PurchaseOKComposer());
  86.                     Session.SendMessage(new FurniListAddComposer(GiveItem));            
  87.                     Session.SendMessage(new FurniListUpdateComposer());
  88.                 }
  89.  
  90.  
  91.                 using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
  92.                 {
  93.                     dbClient.RunQuery("UPDATE `catalog_marketplace_offers` SET `state` = '2' WHERE `offer_id` = '" + OfferId + "' LIMIT 1");
  94.  
  95.                     int Id = 0;
  96.                     dbClient.SetQuery("SELECT `id` FROM catalog_marketplace_data WHERE sprite = " + Item.SpriteId + " LIMIT 1;");
  97.                     Id = dbClient.getInteger();
  98.  
  99.                     if (Id > 0)
  100.                         dbClient.RunQuery("UPDATE `catalog_marketplace_data` SET `sold` = `sold` + 1, `avgprice` = (avgprice + " + Convert.ToInt32(Row["total_price"]) + ") WHERE `id` = " + Id + " LIMIT 1;");
  101.                     else
  102.                         dbClient.RunQuery("INSERT INTO `catalog_marketplace_data` (sprite, sold, avgprice) VALUES ('" + Item.SpriteId + "', '1', '" + Convert.ToInt32(Row["total_price"]) + "')");
  103.  
  104.  
  105.                     if (PlusEnvironment.GetGame().GetCatalog().GetMarketplace().MarketAverages.ContainsKey(Item.SpriteId) && PlusEnvironment.GetGame().GetCatalog().GetMarketplace().MarketCounts.ContainsKey(Item.SpriteId))
  106.                     {
  107.                         int num3 = PlusEnvironment.GetGame().GetCatalog().GetMarketplace().MarketCounts[Item.SpriteId];
  108.                         int num4 = (PlusEnvironment.GetGame().GetCatalog().GetMarketplace().MarketAverages[Item.SpriteId] += Convert.ToInt32(Row["total_price"]));
  109.  
  110.                         PlusEnvironment.GetGame().GetCatalog().GetMarketplace().MarketAverages.Remove(Item.SpriteId);
  111.                         PlusEnvironment.GetGame().GetCatalog().GetMarketplace().MarketAverages.Add(Item.SpriteId, num4);
  112.                         PlusEnvironment.GetGame().GetCatalog().GetMarketplace().MarketCounts.Remove(Item.SpriteId);
  113.                         PlusEnvironment.GetGame().GetCatalog().GetMarketplace().MarketCounts.Add(Item.SpriteId, num3 + 1);
  114.                     }
  115.                     else
  116.                     {
  117.                         if (!PlusEnvironment.GetGame().GetCatalog().GetMarketplace().MarketAverages.ContainsKey(Item.SpriteId))
  118.                             PlusEnvironment.GetGame().GetCatalog().GetMarketplace().MarketAverages.Add(Item.SpriteId, Convert.ToInt32(Row["total_price"]));
  119.  
  120.                         if (!PlusEnvironment.GetGame().GetCatalog().GetMarketplace().MarketCounts.ContainsKey(Item.SpriteId))
  121.                             PlusEnvironment.GetGame().GetCatalog().GetMarketplace().MarketCounts.Add(Item.SpriteId, 1);
  122.                     }
  123.                 }
  124.             }
  125.  
  126.             this.ReloadOffers(Session);
  127.         }
  128.  
  129.  
  130.         private void ReloadOffers(GameClient Session)
  131.         {
  132.             int MinCost = -1;
  133.             int MaxCost = -1;
  134.             string SearchQuery = "";
  135.             int FilterMode = 1;
  136.  
  137.  
  138.             DataTable table = null;
  139.             StringBuilder builder = new StringBuilder();
  140.             string str = "";
  141.             builder.Append("WHERE state = '1' AND timestamp >= " + PlusEnvironment.GetGame().GetCatalog().GetMarketplace().FormatTimestampString());
  142.             if (MinCost >= 0)
  143.             {
  144.                 builder.Append(" AND total_price > " + MinCost);
  145.             }
  146.             if (MaxCost >= 0)
  147.             {
  148.                 builder.Append(" AND total_price < " + MaxCost);
  149.             }
  150.             switch (FilterMode)
  151.             {
  152.                 case 1:
  153.                     str = "ORDER BY asking_price DESC";
  154.                     break;
  155.  
  156.                 default:
  157.                     str = "ORDER BY asking_price ASC";
  158.                     break;
  159.             }
  160.  
  161.             using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
  162.             {
  163.  
  164.                 dbClient.SetQuery("SELECT `offer_id`,`item_type`,`sprite_id`,`total_price`,`limited_number`,`limited_stack` FROM `catalog_marketplace_offers` " + builder.ToString() + " " + str + " LIMIT 500");
  165.                 dbClient.AddParameter("search_query", "%" + SearchQuery + "%");
  166.                 if (SearchQuery.Length >= 1)
  167.                 {
  168.                     builder.Append(" AND `public_name` LIKE @search_query");
  169.                 }
  170.                 table = dbClient.getTable();
  171.             }
  172.  
  173.             PlusEnvironment.GetGame().GetCatalog().GetMarketplace().MarketItems.Clear();
  174.             PlusEnvironment.GetGame().GetCatalog().GetMarketplace().MarketItemKeys.Clear();
  175.             if (table != null)
  176.             {
  177.                 foreach (DataRow row in table.Rows)
  178.                 {
  179.                     if (!PlusEnvironment.GetGame().GetCatalog().GetMarketplace().MarketItemKeys.Contains(Convert.ToInt32(row["offer_id"])))
  180.                     {
  181.                         MarketOffer item = new MarketOffer(Convert.ToInt32(row["offer_id"]), Convert.ToInt32(row["sprite_id"]), Convert.ToInt32(row["total_price"]), int.Parse(row["item_type"].ToString()), Convert.ToInt32(row["limited_number"]), Convert.ToInt32(row["limited_stack"]));
  182.                         PlusEnvironment.GetGame().GetCatalog().GetMarketplace().MarketItemKeys.Add(Convert.ToInt32(row["offer_id"]));
  183.                         PlusEnvironment.GetGame().GetCatalog().GetMarketplace().MarketItems.Add(item);
  184.                     }
  185.                 }
  186.             }
  187.  
  188.             Dictionary<int, MarketOffer> dictionary = new Dictionary<int, MarketOffer>();
  189.             Dictionary<int, int> dictionary2 = new Dictionary<int, int>();
  190.  
  191.             foreach (MarketOffer item in PlusEnvironment.GetGame().GetCatalog().GetMarketplace().MarketItems)
  192.             {
  193.                 if (dictionary.ContainsKey(item.SpriteId))
  194.                 {
  195.                     if (dictionary[item.SpriteId].TotalPrice > item.TotalPrice)
  196.                     {
  197.                         dictionary.Remove(item.SpriteId);
  198.                         dictionary.Add(item.SpriteId, item);
  199.                     }
  200.  
  201.                     int num = dictionary2[item.SpriteId];
  202.                     dictionary2.Remove(item.SpriteId);
  203.                     dictionary2.Add(item.SpriteId, num + 1);
  204.                 }
  205.                 else
  206.                 {
  207.                     dictionary.Add(item.SpriteId, item);
  208.                     dictionary2.Add(item.SpriteId, 1);
  209.                 }
  210.             }
  211.  
  212.             Session.SendMessage(new MarketPlaceOffersComposer(MinCost, MaxCost, dictionary, dictionary2));
  213.         }
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement