Esempio n. 1
0
 public void OnItemBuy(CM_ITEM_BUY p)
 {
     if (map.GetActor(p.ActorID) is ActorNPC npc)
     {
         if (NPC.NPCStoreFactory.Instance.Stores.ContainsKey(npc.BaseData.StoreID))
         {
             NPC.NPCStore store = NPC.NPCStoreFactory.Instance[npc.BaseData.StoreID];
             foreach (KeyValuePair <byte, ushort> i in p.Items)
             {
                 if (i.Key < store.Items.Length)
                 {
                     Common.Item.Item item = ItemFactory.Instance.CreateNewItem(store.Items[i.Key]);
                     if (item != null)
                     {
                         int price = (int)(item.BaseData.Price * store.BuyRate) * i.Value;
                         if (chara.Gold > price)
                         {
                             Interlocked.Add(ref chara.Gold, -price);
                             item.Count = i.Value;
                             AddItem(item);
                             SendPlayerGold();
                         }
                     }
                 }
             }
         }
     }
 }
Esempio n. 2
0
        public void OnItemBuyBack(CM_ITEM_BUYBACK p)
        {
            if (map.GetActor(p.ActorID) is ActorNPC npc)
            {
                if (NPC.NPCStoreFactory.Instance.Stores.ContainsKey(npc.BaseData.StoreID))
                {
                    NPC.NPCStore store = NPC.NPCStoreFactory.Instance[npc.BaseData.StoreID];
                    if (p.Slot < chara.Inventory.SoldItems.Count)
                    {
                        Common.Item.Item item = chara.Inventory.SoldItems[p.Slot];
                        if (item != null)
                        {
                            int price = (int)(item.BaseData.Price * store.BuyBackRate) * item.Count;
                            if (chara.Gold > price)
                            {
                                Interlocked.Add(ref chara.Gold, -price);
                                chara.Inventory.SoldItems.RemoveAt(p.Slot);
                                OperationResults res = chara.Inventory.AddItem(Containers.Inventory, item, out List <Common.Item.Item> affected);
                                if (affected.Count > 0)
                                {
                                    CharacterSession.Instance.SaveItem(affected);
                                }

                                if (res != OperationResults.FAILED)
                                {
                                    if (item.Count > 0)
                                    {
                                        affected.Add(item);
                                    }

                                    switch (res)
                                    {
                                    case OperationResults.NEW_INDEX:
                                        SendItemUpdate(ItemUpdateMethod.Add, affected);
                                        break;

                                    case OperationResults.STACK_UPDATE:
                                        SendItemUpdate(ItemUpdateMethod.Update, affected);
                                        break;
                                    }
                                    if (res == OperationResults.NEW_INDEX && item.Count > 0)
                                    {
                                        CharacterSession.Instance.SaveItem(item);
                                    }
                                    if (item.Count == 0)
                                    {
                                        List <Common.Item.Item> remove = new List <Common.Item.Item>();
                                        remove.Add(item);
                                        CharacterSession.Instance.DeleteItem(remove);
                                    }
                                }
                                SendPlayerGold();
                                SendItemBuyBackList();
                            }
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        public void OnItemBuy(CM_ITEM_EXCHANGE p)
        {
            if (map.GetActor(p.ActorID) is ActorNPC npc)
            {
                if (NPC.NPCStoreFactory.Instance.StoresByItem.ContainsKey(npc.BaseData.StoreByItemID))
                {
                    NPC.NPCStore store = NPC.NPCStoreFactory.Instance.StoresByItem[npc.BaseData.StoreByItemID];
                    if (store != null)
                    {
                        Common.Item.Item item = ItemFactory.Instance.CreateNewItem(store.Items[p.BuySlot]);
                        Dictionary <Common.Item.Item, ushort> items = new Dictionary <Common.Item.Item, ushort>();
                        ushort total = 0;
                        if (item != null)
                        {
                            Common.Item.Item add;
                            foreach (KeyValuePair <ushort, ushort> i in p.ExchangeItems)
                            {
                                add = Character.Inventory.Container[Containers.Inventory][i.Key];
                                if (add != null)
                                {
                                    if (add.Count >= i.Value && add.ItemID == store.Materials[p.BuySlot])
                                    {
                                        items.Add(add, i.Value);
                                        total += add.Count;
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }

                            if (total >= p.BuyCount * store.MaterialCounts[p.BuySlot])
                            {
                                foreach (KeyValuePair <Common.Item.Item, ushort> i in items)
                                {
                                    RemoveItemSlot(i.Key.SlotID, i.Value);
                                }
                                AddItem(item);
                            }
                        }
                    }
                }
            }
        }