Esempio n. 1
0
        public void Handle(Character character, InteractionConstants.InteractionCode code, Packet iPacket)
        {
            switch (code)
            {
            case InteractionConstants.InteractionCode.Invite:
            {
                int characterID = iPacket.ReadInt();

                if (!Owner.Map.Characters.Contains(characterID))
                {
                    // TODO: What does happen in case the invitee left?

                    return;
                }

                Character invitee = Owner.Map.Characters[characterID];

                if (invitee.Trade != null)
                {
                    using (Packet oPacket = new Packet(ServerOperationCode.PlayerInteraction))
                    {
                        oPacket
                        .WriteByte((byte)InteractionConstants.InteractionCode.Decline)
                        .WriteByte(2)
                        .WriteString(invitee.Name);

                        Owner.Client.Send(oPacket);
                    }
                }
                else
                {
                    invitee.Trade = this;
                    Visitor       = invitee;

                    using (Packet oPacket = new Packet(ServerOperationCode.PlayerInteraction))
                    {
                        oPacket
                        .WriteByte((byte)InteractionConstants.InteractionCode.Invite)
                        .WriteByte(3)
                        .WriteString(Owner.Name)
                        .WriteBytes(new byte[4] {
                                0xB7, 0x50, 0x00, 0x00
                            });

                        Visitor.Client.Send(oPacket);
                    }
                }
            }
            break;

            case InteractionConstants.InteractionCode.Decline:
            {
                using (Packet oPacket = new Packet(ServerOperationCode.PlayerInteraction))
                {
                    oPacket
                    .WriteByte((byte)InteractionConstants.InteractionCode.Decline)
                    .WriteByte(3)
                    .WriteString(character.Name);

                    Owner.Client.Send(oPacket);
                }

                Owner.Trade   = null;
                Visitor.Trade = null;
                Owner         = null;
                Visitor       = null;
            }
            break;

            case InteractionConstants.InteractionCode.Visit:
            {
                if (Owner == null)
                {
                    Visitor         = null;
                    character.Trade = null;

                    Character.Notify(character, "Trade has been closed.", ServerConstants.NoticeType.Popup);
                }
                else
                {
                    Started = true;

                    using (Packet oPacket = new Packet(ServerOperationCode.PlayerInteraction))
                    {
                        oPacket
                        .WriteByte((byte)InteractionConstants.InteractionCode.Visit)
                        .WriteByte(1)
                        .WriteBytes(Visitor.AppearanceToByteArray())
                        .WriteString(Visitor.Name);

                        Owner.Client.Send(oPacket);
                    }

                    using (Packet oPacket = new Packet(ServerOperationCode.PlayerInteraction))
                    {
                        oPacket
                        .WriteByte((byte)InteractionConstants.InteractionCode.Room)
                        .WriteByte(3)
                        .WriteByte(2)
                        .WriteByte(1)
                        .WriteByte(0)
                        .WriteBytes(Owner.AppearanceToByteArray())
                        .WriteString(Owner.Name)
                        .WriteByte(1)
                        .WriteBytes(Visitor.AppearanceToByteArray())
                        .WriteString(Visitor.Name)
                        .WriteByte(byte.MaxValue);

                        Visitor.Client.Send(oPacket);
                    }
                }
            }
            break;

            case InteractionConstants.InteractionCode.SetItems:
            {
                ItemConstants.ItemType type = (ItemConstants.ItemType)iPacket.ReadByte();
                short slot       = iPacket.ReadShort();
                short quantity   = iPacket.ReadShort();
                byte  targetSlot = iPacket.ReadByte();

                Item item = character.Items[type, slot];

                if (item.IsBlocked)
                {
                    return;
                }

                if (quantity > item.Quantity)
                {
                    return;
                }

                if (quantity < item.Quantity)
                {
                    item.Quantity -= quantity;
                    Item.UpdateItem(item);

                    item = new Item(item.MapleID, quantity);
                }
                else
                {
                    character.Items.RemoveItemFromInventory(item, true);
                }

                item.Slot = 0;

                using (Packet oPacket = new Packet(ServerOperationCode.PlayerInteraction))
                {
                    oPacket
                    .WriteByte((byte)InteractionConstants.InteractionCode.SetItems)
                    .WriteByte(0)
                    .WriteByte(targetSlot)
                    .WriteBytes(item.ToByteArray(true));

                    if (character == Owner)
                    {
                        OwnerItems.Add(item);

                        Owner.Client.Send(oPacket);
                    }
                    else
                    {
                        VisitorItems.Add(item);

                        Visitor.Client.Send(oPacket);
                    }
                }

                using (Packet oPacket = new Packet(ServerOperationCode.PlayerInteraction))
                {
                    oPacket
                    .WriteByte((byte)InteractionConstants.InteractionCode.SetItems)
                    .WriteByte(1)
                    .WriteByte(targetSlot)
                    .WriteBytes(item.ToByteArray(true));

                    if (character == Owner)
                    {
                        Visitor.Client.Send(oPacket);
                    }
                    else
                    {
                        Owner.Client.Send(oPacket);
                    }
                }
            }
            break;

            case InteractionConstants.InteractionCode.SetMeso:
            {
                int meso = iPacket.ReadInt();

                if (meso < 0 || meso > character.Stats.Meso)
                {
                    return;
                }

                // TODO: The meso written in this packet is the added meso amount.
                // The amount that should be written is the total amount.

                using (Packet oPacket = new Packet(ServerOperationCode.PlayerInteraction))
                {
                    oPacket
                    .WriteByte((byte)InteractionConstants.InteractionCode.SetMeso)
                    .WriteByte(0)
                    .WriteInt(meso);

                    if (character == Owner)
                    {
                        if (OwnerLocked)
                        {
                            return;
                        }

                        OwnerMeso        += meso;
                        Owner.Stats.Meso -= meso;

                        Owner.Client.Send(oPacket);
                    }
                    else
                    {
                        if (VisitorLocked)
                        {
                            return;
                        }

                        VisitorMeso        += meso;
                        Visitor.Stats.Meso -= meso;

                        Visitor.Client.Send(oPacket);
                    }
                }

                using (Packet oPacket = new Packet(ServerOperationCode.PlayerInteraction))
                {
                    oPacket
                    .WriteByte((byte)InteractionConstants.InteractionCode.SetMeso)
                    .WriteByte(1)
                    .WriteInt(meso);

                    if (Owner == character)
                    {
                        Visitor.Client.Send(oPacket);
                    }
                    else
                    {
                        oPacket.WriteInt(OwnerMeso);

                        Owner.Client.Send(oPacket);
                    }
                }
            }
            break;

            case InteractionConstants.InteractionCode.Exit:
            {
                if (Started)
                {
                    Cancel();

                    using (Packet oPacket = new Packet(ServerOperationCode.PlayerInteraction))
                    {
                        oPacket
                        .WriteByte((byte)InteractionConstants.InteractionCode.Exit)
                        .WriteByte(0)
                        .WriteByte(2);

                        Owner.Client.Send(oPacket);
                        Visitor.Client.Send(oPacket);
                    }

                    Owner.Trade   = null;
                    Visitor.Trade = null;
                    Owner         = null;
                    Visitor       = null;
                }
                else
                {
                    using (Packet oPacket = new Packet(ServerOperationCode.PlayerInteraction))
                    {
                        oPacket
                        .WriteByte((byte)InteractionConstants.InteractionCode.Exit)
                        .WriteByte(0)
                        .WriteByte(2);

                        Owner.Client.Send(oPacket);
                    }

                    Owner.Trade = null;
                    Owner       = null;
                }
            }
            break;

            case InteractionConstants.InteractionCode.Confirm:
            {
                using (Packet oPacket = new Packet(ServerOperationCode.PlayerInteraction))
                {
                    oPacket.WriteByte((byte)InteractionConstants.InteractionCode.Confirm);

                    if (character == Owner)
                    {
                        OwnerLocked = true;

                        Visitor.Client.Send(oPacket);
                    }
                    else
                    {
                        VisitorLocked = true;

                        Owner.Client.Send(oPacket);
                    }
                }

                if (OwnerLocked && VisitorLocked)
                {
                    Complete();

                    using (Packet oPacket = new Packet(ServerOperationCode.PlayerInteraction))
                    {
                        oPacket
                        .WriteByte((byte)InteractionConstants.InteractionCode.Exit)
                        .WriteByte(0)
                        .WriteByte(6);

                        Owner.Client.Send(oPacket);
                        Visitor.Client.Send(oPacket);
                    }

                    Owner.Trade   = null;
                    Visitor.Trade = null;
                    Owner         = null;
                    Visitor       = null;
                }
            }
            break;

            case InteractionConstants.InteractionCode.Chat:
            {
                string text = iPacket.ReadString();

                using (Packet oPacket = new Packet(ServerOperationCode.PlayerInteraction))
                {
                    oPacket
                    .WriteByte((byte)InteractionConstants.InteractionCode.Chat)
                    .WriteByte(8)
                    .WriteBool(Owner != character)
                    .WriteString("{0} : {1}", character.Name, text);

                    Owner.Client.Send(oPacket);
                    Visitor.Client.Send(oPacket);
                }
            }
            break;

            case InteractionConstants.InteractionCode.Create:
                break;

            case InteractionConstants.InteractionCode.Room:
                break;

            case InteractionConstants.InteractionCode.Open:
                break;

            case InteractionConstants.InteractionCode.TradeBirthday:
                break;

            case InteractionConstants.InteractionCode.AddItem:
                break;

            case InteractionConstants.InteractionCode.Buy:
                break;

            case InteractionConstants.InteractionCode.UpdateItems:
                break;

            case InteractionConstants.InteractionCode.RemoveItem:
                break;

            case InteractionConstants.InteractionCode.OpenStore:
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(code), code, null);
            }
        }
Esempio n. 2
0
        private void SetItems(PacketReader packet, GameCharacter gameCharacter)
        {
            var type       = (ItemType)packet.ReadByte();
            var slot       = packet.ReadShort();
            var quantity   = packet.ReadShort();
            var targetSlot = packet.ReadByte();

            var item = gameCharacter.Items[type, slot];

            if (item.IsBlocked)
            {
                return;
            }

            if (quantity > item.Quantity)
            {
                return;
            }

            if (quantity < item.Quantity)
            {
                item.Quantity -= quantity;
                item.Update();

                item = new Item(item.MapleId, quantity);
            }
            else
            {
                gameCharacter.Items.Remove(item, true);
            }

            item.Slot = 0;

            using var pw = new PacketWriter(ServerOperationCode.PlayerInteraction);
            pw.WriteByte(InteractionCode.SetItems);
            pw.WriteByte(0);
            pw.WriteByte(targetSlot);
            pw.WriteBytes(item.ToByteArray(true));

            if (gameCharacter == Owner)
            {
                OwnerItems.Add(item);

                Owner.Send(pw);
            }
            else
            {
                VisitorItems.Add(item);

                Visitor.Send(pw);
            }

            using var pw2 = new PacketWriter(ServerOperationCode.PlayerInteraction);
            pw2.WriteByte(InteractionCode.SetItems);
            pw2.WriteByte(1);
            pw2.WriteByte(targetSlot);
            pw2.WriteBytes(item.ToByteArray(true));

            if (gameCharacter == Owner)
            {
                Visitor.Send(pw2);
            }
            else
            {
                Owner.Send(pw2);
            }
        }