Пример #1
0
        void c_ClientTradePlayerCommandsInfo(object sender, ClientTradePlayerCommandsEventArgs e)
        {
            Client c = (Client)sender;
            TradePlayerCommandsInfo i = e.Info;
            Client p = null;

            if (i.CharacterID != c.MyCharacter.CharacterId)
                p = GetClientByCharacterID(i.CharacterID);

            if (i.Error == TradePlayerCommands.Accept)
            {
                c.MyCharacter.TradeWindow.Accepted = true;

                if (c.MyCharacter.TradeWindow.Accepted && p.MyCharacter.TradeWindow.Accepted)
                {
                    // do item stuff here
                    BaseItem[] Player1FakeTradeItems = null;
                    BaseItem[] Player2FakeTradeItems = null;

                    Bag[] Player1FakeBags = new Bag[]
                    {
                        new Bag(c.MyCharacter.Bags[0].Items.Select(x=>new BaseItem { ItemID = x.ItemID, SizeX = x.SizeX, SizeY = x.SizeY, Slot = x.Slot, Bag = x.Bag }).ToList()),
                        new Bag(c.MyCharacter.Bags[1].Items.Select(x=>new BaseItem { ItemID = x.ItemID, SizeX = x.SizeX, SizeY = x.SizeY, Slot = x.Slot, Bag = x.Bag }).ToList())
                    };

                    Bag[] Player2FakeBags = new Bag[]
                    {
                        new Bag(p.MyCharacter.Bags[0].Items.Select(x=>new BaseItem { ItemID = x.ItemID, SizeX = x.SizeX, SizeY = x.SizeY, Slot = x.Slot, Bag = x.Bag }).ToList()),
                        new Bag(p.MyCharacter.Bags[1].Items.Select(x=>new BaseItem { ItemID = x.ItemID, SizeX = x.SizeX, SizeY = x.SizeY, Slot = x.Slot, Bag = x.Bag }).ToList())
                    };

                    if (c.MyCharacter.TradeWindow.Items.Length > 0)
                    {
                        Player1FakeTradeItems = c.MyCharacter.TradeWindow.Items.Select(x => new BaseItem { ItemID = x.ItemID, SizeX = x.SizeX, SizeY = x.SizeY, Bag = x.Bag, Slot = x.Slot }).ToArray();
                        for (int j = 0; j < Player1FakeTradeItems.Length; j++)
                        {
                            BaseItem itemToRemove = Player1FakeBags[Player1FakeTradeItems[j].Bag - 1].Items.Where(x => x.ItemID == Player1FakeTradeItems[j].ItemID).FirstOrDefault();
                            if (itemToRemove != null)
                                Player1FakeBags[Player1FakeTradeItems[j].Bag - 1].RemoveItem(itemToRemove);
                        }
                    }
                    if (p.MyCharacter.TradeWindow.Items.Length > 0)
                    {
                        Player2FakeTradeItems = p.MyCharacter.TradeWindow.Items.Select(x => new BaseItem { ItemID = x.ItemID, SizeX = x.SizeX, SizeY = x.SizeY, Bag = x.Bag, Slot = x.Slot }).ToArray();
                        for (int j = 0; j < Player2FakeTradeItems.Length; j++)
                        {
                            BaseItem itemToRemove = Player2FakeBags[Player2FakeTradeItems[j].Bag - 1].Items.Where(x => x.ItemID == Player2FakeTradeItems[j].ItemID).FirstOrDefault();
                            if (itemToRemove != null)
                                Player2FakeBags[Player2FakeTradeItems[j].Bag - 1].RemoveItem(itemToRemove);
                        }
                    }

                    try
                    {
                        if (Player1FakeTradeItems != null)
                        {
                            for (int j = 0; j < Player1FakeTradeItems.Length; j++)
                            {
                                BagSlot newBagSlot = gameEngine.TryPickToBags(Player2FakeBags, Player1FakeTradeItems[j]);
                                Player1FakeTradeItems[j].Bag = newBagSlot.Bag;
                                Player1FakeTradeItems[j].Slot = newBagSlot.Slot;
                            }
                        }
                        if (Player2FakeTradeItems != null)
                        {
                            for (int j = 0; j < Player2FakeTradeItems.Length; j++)
                            {
                                BagSlot newBagSlot = gameEngine.TryPickToBags(Player1FakeBags, Player2FakeTradeItems[j]);
                                Player2FakeTradeItems[j].Bag = newBagSlot.Bag;
                                Player2FakeTradeItems[j].Slot = newBagSlot.Slot;
                            }
                        }

                        if (Player1FakeTradeItems != null)
                        {
                            if (Player2FakeTradeItems != null)
                            {
                                for (int j = 0; j < p.MyCharacter.TradeWindow.Items.Length; j++)
                                {
                                    p.MyCharacter.Bags[p.MyCharacter.TradeWindow.Items[j].Bag - 1].RemoveItem(p.MyCharacter.TradeWindow.Items[j]);

                                }
                            }

                            for (int j = 0; j < c.MyCharacter.TradeWindow.Items.Length; j++)
                            {

                                BaseItem tradedItem = c.MyCharacter.TradeWindow.Items[j];
                                byte oldPlayerBag = tradedItem.Bag;
                                byte oldPlayerSlot = tradedItem.Slot;
                                BagSlot newBagSlot = gameEngine.TryPickToBags(p.MyCharacter.Bags.ToArray(), tradedItem);

                                c.MyCharacter.Bags[oldPlayerBag - 1].RemoveItem(tradedItem);
                                byte[] SendRemoveFromBag = PacketManager.SendRemoveItem(oldPlayerBag, oldPlayerSlot);
                                c.Send(SendRemoveFromBag);

                                tradedItem.Bag = newBagSlot.Bag;
                                tradedItem.Slot = newBagSlot.Slot;
                                tradedItem.OwnerID = p.MyCharacter.CharacterId;

                                itemDataManager.UpdateItem(tradedItem);

                                byte[] SendAddItemToBag = PacketManager.SendAddItem(tradedItem);
                                p.Send(SendAddItemToBag);
                            }
                        }
                        if (Player2FakeTradeItems != null)
                        {
                            if (Player1FakeTradeItems != null)
                            {
                                for (int j = 0; j < c.MyCharacter.TradeWindow.Items.Length; j++)
                                {
                                    c.MyCharacter.Bags[c.MyCharacter.TradeWindow.Items[j].Bag - 1].RemoveItem(c.MyCharacter.TradeWindow.Items[j]);
                                }
                            }

                            for (int j = 0; j < p.MyCharacter.TradeWindow.Items.Length; j++)
                            {
                                BaseItem tradedItem = p.MyCharacter.TradeWindow.Items[j];
                                byte oldPlayerBag = tradedItem.Bag;
                                byte oldPlayerSlot = tradedItem.Slot;
                                BagSlot newBagSlot = gameEngine.TryPickToBags(c.MyCharacter.Bags.ToArray(), tradedItem);

                                p.MyCharacter.Bags[oldPlayerBag - 1].RemoveItem(tradedItem);
                                byte[] SendRemoveFromBag = PacketManager.SendRemoveItem(oldPlayerBag, oldPlayerSlot);
                                p.Send(SendRemoveFromBag);

                                tradedItem.Bag = newBagSlot.Bag;
                                tradedItem.Slot = newBagSlot.Slot;
                                tradedItem.OwnerID = c.MyCharacter.CharacterId;

                                itemDataManager.UpdateItem(tradedItem);
                                //c adds
                                byte[] SendAddItemToBag = PacketManager.SendAddItem(tradedItem);
                                c.Send(SendAddItemToBag);
                            }

                        }
                    }
                    catch (BagIsFullException)
                    {
                        //Send no room in bag and cancel trade
                        c.MyCharacter.TradeWindow = null;
                        p.MyCharacter.TradeWindow = null;
                        c.Send(PacketManager.SendTradeAcceptFinished(1));
                        p.Send(PacketManager.SendTradeAcceptFinished(1));
                        c.Send(PacketManager.SendTradePlayerError((byte)TradePlayerError.Cancel, p.MyCharacter.CharacterId));
                        p.Send(PacketManager.SendTradePlayerError((byte)TradePlayerError.Cancel, c.MyCharacter.CharacterId));
                        return;
                    };

                    c.MyCharacter.Money -= c.MyCharacter.TradeWindow.Money;
                    c.MyCharacter.Money += p.MyCharacter.TradeWindow.Money;

                    p.MyCharacter.Money -= p.MyCharacter.TradeWindow.Money;
                    p.MyCharacter.Money += c.MyCharacter.TradeWindow.Money;

                    c.MyCharacter.TradeWindow = null;
                    p.MyCharacter.TradeWindow = null;

                    c.Send(PacketManager.SendMoneyLeft(c.MyCharacter.Money));
                    p.Send(PacketManager.SendMoneyLeft(p.MyCharacter.Money));

                    c.Send(PacketManager.SendTradeAcceptFinished(0));
                    p.Send(PacketManager.SendTradeAcceptFinished(0));
                }
            }

            if (i.Error == TradePlayerCommands.Cancel)
            {
                c.MyCharacter.TradeWindow.Active = false;
                p.MyCharacter.TradeWindow.Active = false;
                c.MyCharacter.TradeWindow = null;
                p.MyCharacter.TradeWindow = null;
            }

            c.Send(PacketManager.SendTradePlayerError((byte)i.Error, p.MyCharacter.CharacterId));
            p.Send(PacketManager.SendTradePlayerError((byte)i.Error, c.MyCharacter.CharacterId));
        }
Пример #2
0
        public BagSlot TryPickToBags(Bag[] playerBags, BaseItem item)
        {
            BagSlot bagSlot = new BagSlot();

            bool added = false;

            for (int i = 0; i < playerBags.Length; i++)
            {
                if (!added)
                {
                    if (playerBags[i].PickItem(item, bagSlot))
                    {
                        added = true;
                        bagSlot.Bag = (byte)(i + 1);
                        break;
                    }
                }
            }

            if (!added)
                throw new BagIsFullException(Messages.BAGISFULLEXCEPTION);
            return bagSlot;
        }