Exemplo n.º 1
0
        /// <summary>
        /// Occurs when your aggreeing with the trade.
        /// After the trade content has been agreed.
        /// </summary>
        private void CM_TRADECONFIRM(CMSG_TRADECONFIRM cpkt)
        {
            TradeSession session = this.character.TradeSession;

            if (session != null)
            {
                //OBTAIN THE ORGIN TARGET
                Character target;
                TradeSession.TradeItem[] Items;
                if (this.character.TradeSession.Source == this.character)
                {
                    target = session.Target;
                    Items  = session.TargetItem;
                }
                else
                {
                    target = session.Source;
                    Items  = session.SourceItem;
                }

                //Calculate required slots
                int slots = 0;
                for (int i = 0; i < 16; i++)
                {
                    if (Items[i] == null)
                    {
                        continue;
                    }
                    slots++;
                }

                if (slots > this.character.container.Capacity - this.character.container.Count)
                {
                    //Not enough space oponent
                    SMSG_TRADERESULT2 spkt = new SMSG_TRADERESULT2();
                    spkt.Reason    = (byte)TradeResult.TargetNotEnoughInventorySpace;
                    spkt.SessionId = target.id;
                    target.client.Send((byte[])spkt);

                    //Not enough space myself
                    SMSG_TRADERESULT2 spkt2 = new SMSG_TRADERESULT2();
                    spkt2.Reason    = (byte)TradeResult.NotEnoughIventorySpace;
                    spkt2.SessionId = this.character.id;
                    this.Send((byte[])spkt2);

                    //Set tradesession to null;
                    this.character.TradeSession = null;
                    target.TradeSession         = null;
                    return;
                }
                else
                {
                    if (session.Source == this.character)
                    {
                        session.SourceHasAgreed = true;
                    }
                    else
                    {
                        session.TargetHasAgreed = true;
                    }
                }

                if (session.TargetHasAgreed && session.SourceHasAgreed)
                {
                    target.ZENY         += session.ZenySource;
                    this.character.ZENY -= session.ZenySource;

                    target.ZENY         -= session.ZenyTarget;
                    this.character.ZENY += session.ZenyTarget;

                    List <Rag2Item> SourceList = new List <Rag2Item>();
                    List <Rag2Item> TargetList = new List <Rag2Item>();
                    for (int i = 0; i < 16; i++)
                    {
                        TradeSession.TradeItem item = session.SourceItem[i];
                        if (item == null)
                        {
                            continue;
                        }

                        Rag2Item ragitem = this.character.container[item.Slot];
                        if (ragitem.count - item.Count == 0)
                        {
                            SourceList.Add(ragitem);
                            this.character.container.RemoveAt(item.Slot);
                            SMSG_DELETEITEM spkt = new SMSG_DELETEITEM();
                            spkt.Container    = 2;
                            spkt.Index        = item.Slot;
                            spkt.SessionId    = this.character.id;
                            spkt.UpdateReason = (byte)ItemUpdateReason.SendToTrader;
                            this.Send((byte[])spkt);
                        }
                        else
                        {
                            SourceList.Add(ragitem.Clone(item.Count));
                            ragitem.count -= item.Count;
                            SMSG_UPDATEITEM spkt = new SMSG_UPDATEITEM();
                            spkt.Container    = 2;
                            spkt.Index        = item.Slot;
                            spkt.SessionId    = this.character.id;
                            spkt.UpdateReason = (byte)ItemUpdateReason.SendToTrader;
                            spkt.UpdateType   = 4;
                            this.Send((byte[])spkt);
                        }
                    }

                    for (int i = 0; i < 16; i++)
                    {
                        TradeSession.TradeItem item = session.TargetItem[i];
                        if (item == null)
                        {
                            continue;
                        }

                        Rag2Item ragitem = target.container[item.Slot];
                        if (ragitem.count - item.Count == 0)
                        {
                            TargetList.Add(ragitem);
                            this.character.container.RemoveAt(item.Slot);
                            SMSG_DELETEITEM spkt = new SMSG_DELETEITEM();
                            spkt.Container    = 2;
                            spkt.Index        = item.Slot;
                            spkt.SessionId    = target.id;
                            spkt.UpdateReason = (byte)ItemUpdateReason.SendToTrader;
                            target.client.Send((byte[])spkt);
                        }
                        else
                        {
                            TargetList.Add(ragitem.Clone(item.Count));
                            ragitem.count -= item.Count;
                            SMSG_UPDATEITEM spkt = new SMSG_UPDATEITEM();
                            spkt.Container    = 2;
                            spkt.Index        = item.Slot;
                            spkt.SessionId    = target.id;
                            spkt.UpdateReason = (byte)ItemUpdateReason.SendToTrader;
                            spkt.UpdateType   = 4;
                            target.client.Send((byte[])spkt);
                        }
                    }

                    for (int i = 0; i < SourceList.Count; i++)
                    {
                        Rag2Item     ragitem = SourceList[i];
                        int          index   = target.container.Add(ragitem);
                        SMSG_ADDITEM spkt    = new SMSG_ADDITEM();
                        spkt.Container    = 2;
                        spkt.UpdateReason = (byte)ItemUpdateReason.ReceiveFromTrade;
                        spkt.SessionId    = target.id;
                        spkt.SetItem(ragitem, index);
                        target.client.Send((byte[])spkt);
                    }

                    for (int i = 0; i < TargetList.Count; i++)
                    {
                        Rag2Item     ragitem = TargetList[i];
                        int          index   = this.character.container.Add(ragitem);
                        SMSG_ADDITEM spkt    = new SMSG_ADDITEM();
                        spkt.Container    = 2;
                        spkt.UpdateReason = (byte)ItemUpdateReason.ReceiveFromTrade;
                        spkt.SessionId    = this.character.id;
                        spkt.SetItem(ragitem, index);
                        this.Send((byte[])spkt);
                    }

                    //Update zeny yourself
                    SMSG_SENDZENY spkt4 = new SMSG_SENDZENY();
                    spkt4.SessionId = this.character.id;
                    spkt4.Zeny      = this.character.ZENY;
                    this.Send((byte[])spkt4);

                    //Update zeny opponent
                    SMSG_SENDZENY spkt5 = new SMSG_SENDZENY();
                    spkt5.SessionId = target.id;
                    spkt5.Zeny      = target.ZENY;
                    target.client.Send((byte[])spkt5);

                    //Set traderesult to succesfull
                    SMSG_TRADERESULT2 spkt3 = new SMSG_TRADERESULT2();
                    spkt3.SessionId = this.character.id;
                    this.Send((byte[])spkt3);

                    //Set traderesult successfull oponent
                    SMSG_TRADERESULT2 spkt2 = new SMSG_TRADERESULT2();
                    spkt2.SessionId = target.id;
                    target.client.Send((byte[])spkt2);

                    //Set the tradesession to null
                    this.character.TradeSession = null;
                    target.TradeSession         = null;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Occurs when a player selects a drop from the droplist.
        /// </summary>
        /// <param name="cpkt"></param>
        private void CM_DROPSELECT(CMSG_DROPSELECT cpkt)
        {
            try
            {
                MapObject      target = null;
                Rag2Collection list   = null;
                LootCollection loot   = this.character.Tag as LootCollection;

                //Corpse disappeared already
                if (!Regiontree.TryFind(cpkt.ActorID, this.character, out target))
                {
                    SMSG_NPCDROPLISTRESULT spkt = new SMSG_NPCDROPLISTRESULT();
                    spkt.ActorID   = cpkt.ActorID;
                    spkt.Result    = 0;
                    spkt.SessionId = this.character.id;
                    this.Send((byte[])spkt);
                    return;
                }
                //Uknown looting error (not implamented itseems)
                else if (loot == null)
                {
                    SMSG_NPCDROPLISTRESULT spkt = new SMSG_NPCDROPLISTRESULT();
                    spkt.ActorID   = cpkt.ActorID;
                    spkt.Result    = 4;
                    spkt.SessionId = this.character.id;
                    this.Send((byte[])spkt);
                    return;
                }
                else
                {
                    list = loot.Lootlist;
                }



                //OBTAIN THE REQUIRED ITEMS FROM THE MERCHANT
                Rag2Item item = list[cpkt.Index];
                if (item == null)
                {
                    return;
                }

                //TEMP HELPER VARIABLES
                int        nstacked     = 0;
                List <int> update_queue = new List <int>();

                //WALKTHROUGH EVERY ITEM AND CHECK IF IT CAN BE STACKED
                foreach (int index in this.character.container.FindAllItems(item.info.item))
                {
                    Rag2Item invItem = this.character.container[index];
                    nstacked += Math.Min(0, (item.info.max_stack - invItem.count));
                    if (invItem.count < item.info.max_stack)
                    {
                        update_queue.Add(index);
                    }
                }

                //CALCULATE THE AMOUNT OF NEW SLOTS REQUIRED
                int req_hslot = (int)item.count % (int)this.character.container.Capacity;
                int req_slots = item.count;
                //int max_stack = (item.info.max_stack == 0) ? 1 : item.info.max_stack;
                if (item.info.max_stack > 0)
                {
                    int div_rem  = (int)((item.count - nstacked) / item.info.max_stack);
                    int div_rem2 = (req_hslot > 0) ? 1 : 0;
                    req_slots = div_rem + div_rem2;
                }

                if (this.character.container.Count + req_slots > this.character.container.Capacity)
                {
                    SMSG_NPCDROPLISTRESULT spkt = new SMSG_NPCDROPLISTRESULT();
                    spkt.ActorID   = cpkt.ActorID;
                    spkt.Result    = 3;
                    spkt.SessionId = this.character.id;
                    this.Send((byte[])spkt);
                    return;
                }


                //AMOUNT USED IN DECREMENT CALCULATIONS
                int amount = (int)item.count;

                //ITERATE THROUGH ALL AVAILABLE ITEM THAT CAN BE PROCESSED FOR UPDATES
                foreach (int invIndex in update_queue)
                {
                    Rag2Item invItem  = this.character.container[invIndex];
                    int      leftover = item.info.max_stack - invItem.count;
                    leftover       = Math.Max(0, Math.Min(amount, leftover));
                    invItem.count += leftover;
                    amount        -= leftover;

                    SMSG_UPDATEITEM spkt = new SMSG_UPDATEITEM();
                    spkt.Index        = (byte)invIndex;
                    spkt.UpdateReason = (byte)ItemUpdateReason.Obtained;
                    spkt.UpdateType   = 4;
                    spkt.Container    = 2;
                    spkt.Amount       = (byte)invItem.count;
                    spkt.SessionId    = this.character.id;
                    this.Send((byte[])spkt);
                }

                //ITERATE THROUGH EVERY FREE INDEX AND PROCESS IT
                foreach (int invIndex in this.character.container.FindFreeIndexes())
                {
                    if (amount == 0)
                    {
                        break;
                    }
                    int      leftover = Math.Min(amount, item.info.max_stack);
                    Rag2Item invItem  = item.Clone(leftover);
                    this.character.container[invIndex] = invItem;
                    amount -= leftover;

                    SMSG_ADDITEM spkt = new SMSG_ADDITEM();
                    spkt.Container    = 2;
                    spkt.UpdateReason = (byte)ItemUpdateReason.Obtained;
                    spkt.SetItem(invItem, invIndex);
                    spkt.SessionId = this.character.id;
                    this.Send((byte[])spkt);
                }

                //CHECK THE LIST
                if (amount == 0)
                {
                    list.RemoveAt(cpkt.Index);
                }
                else
                {
                    item.count = amount;
                }

                //REFRESH THE INVENTORY
                loot.Open(this.character, target);
                QuestBase.UserObtainedItem(item.info.item, this.character);

                if (this.character.sessionParty != null)
                {
                    for (int i = 0; i < this.character.sessionParty._Characters.Count; i++)
                    {
                        Character partyTarget = this.character.sessionParty._Characters[i];
                        if (partyTarget.id == this.character.id)
                        {
                            continue;
                        }
                        SMSG_PARTYMEMBERLOOT spkt2 = new SMSG_PARTYMEMBERLOOT();
                        spkt2.SessionId = partyTarget.id;
                        spkt2.Index     = (byte)(i + 1);
                        spkt2.ActorId   = this.character.id;
                        spkt2.ItemId    = item.info.item;
                        partyTarget.client.Send((byte[])spkt2);
                    }
                }
            }
            //CATCH ALL UNKNOWN ERRORS
            catch (Exception e)
            {
                SMSG_NPCDROPLISTRESULT spkt = new SMSG_NPCDROPLISTRESULT();
                spkt.ActorID   = cpkt.ActorID;
                spkt.Result    = 5;
                spkt.SessionId = this.character.id;
                this.Send((byte[])spkt);
                Console.WriteLine(e);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Registers a item from your inventory onto the market.
        /// </summary>
        /// <param name="cpkt"></param>
        private void CM_MARKET_REGISTERITEM(CMSG_MARKETREGISTER cpkt)
        {
            SMSG_MARKETREGISTER spkt = new SMSG_MARKETREGISTER();
            byte result = 0;

            try
            {
                Rag2Item item = this.character.container[cpkt.Index];
                if (item == null)
                {
                    return;
                }
                uint requiredZeny = (uint)(50 * cpkt.Days);

                //NOT ENOUGH MONEY TO REGISTER ITEM
                if (requiredZeny > this.character.ZENY)
                {
                    result = 6;
                }
                //CHECK REGISTERED ITEM COUNT
                else if (Singleton.Database.GetOwnerItemCount(this.character) == 20)
                {
                    result = 8;
                }
                //EVERYTHING OKAY
                else
                {
                    //Create mail argument

                    Rag2Item           bitem    = item.Clone(cpkt.Count);
                    MarketItemArgument argument = new MarketItemArgument();
                    argument.item     = bitem;
                    argument.expires  = DateTime.Now.AddDays(cpkt.Days);
                    argument.cat      = (byte)item.info.categorie;
                    argument.price    = cpkt.Price;
                    argument.sender   = this.character.Name;
                    argument.itemname = item.info.name;
                    argument.id       = Singleton.Database.RegisterNewMarketItem(this.character, argument);

                    spkt.Item       = bitem;
                    spkt.AuctionID  = argument.id;
                    spkt.Zeny       = cpkt.Price;
                    spkt.ExpireDate = argument.expires;

                    this.character.ZENY -= requiredZeny;
                    CommonFunctions.UpdateZeny(this.character);

                    int newCount = item.count - cpkt.Count;
                    if (newCount > 0)
                    {
                        item.count = newCount;
                        SMSG_UPDATEITEM spkt2 = new SMSG_UPDATEITEM();
                        spkt2.Index        = cpkt.Index;
                        spkt2.UpdateReason = (byte)ItemUpdateReason.AuctionRegister;
                        spkt2.UpdateType   = 4;
                        spkt2.SessionId    = this.character.id;
                        spkt2.Amount       = (byte)item.count;
                        spkt2.Container    = 2;
                        this.Send((byte[])spkt2);
                    }
                    else
                    {
                        this.character.container.RemoveAt(cpkt.Index);
                        SMSG_DELETEITEM spkt2 = new SMSG_DELETEITEM();
                        spkt2.Index        = cpkt.Index;
                        spkt2.UpdateReason = (byte)ItemUpdateReason.AuctionRegister;
                        spkt2.UpdateReason = 4;
                        spkt2.SessionId    = this.character.id;
                        spkt2.Container    = 2;
                        this.Send((byte[])spkt2);
                    }
                }
            }
            //DATABASE ERROR
            catch (Exception e)
            {
                result = 1;
                Console.WriteLine(e);
            }
            finally
            {
                spkt.SessionId = this.character.id;
                spkt.Result    = result;
                this.Send((byte[])spkt);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Sends a new mail message
        /// </summary>
        private void CM_NEWMAILITEM(CMSG_SENDMAIL cpkt)
        {
            //HELPER VARIABLES
            byte     result      = 1;
            uint     req_zeny    = 0;
            Rag2Item item        = null;
            MailItem mailmessage = new MailItem();

            mailmessage.Content    = cpkt.Content;
            mailmessage.Recieptent = cpkt.Name;
            mailmessage.Topic      = cpkt.Topic;
            mailmessage.item       = item;

            if ((cpkt.HasItem & 2) == 2)
            {
                item = this.character.container[cpkt.Slot];
                if (item != null)
                {
                    req_zeny         = 10;
                    mailmessage.item = item.Clone(cpkt.StackCount);
                }
            }
            if ((cpkt.HasItem & 1) == 1)
            {
                req_zeny         = 10 + cpkt.Zeny;
                mailmessage.Zeny = cpkt.Zeny;
            }

            try
            {
                //RECIEVER DOES NOT EXISTS
                if (!Singleton.Database.VerifyNameExists(mailmessage.Recieptent))
                {
                    result = 2;
                }
                //NOT ENOUGH MONEY
                else if (this.character.ZENY < req_zeny)
                {
                    result = 3;
                }
                //CHECK ITEM INVENTORY
                else if (item != null && cpkt.StackCount > item.count)
                {
                    result = 5;
                }
                //CHECK IF OWNER OUTBOX IF FULL
                else if (Singleton.Database.GetInboxMailCount(this.character.Name) == 20)
                {
                    result = 6;
                }
                //CHECK IF SENDER INBOX IS FULL
                else if (Singleton.Database.GetInboxMailCount(mailmessage.Recieptent) == 20)
                {
                    result = 7;
                }
                //DATABASE ERROR
                else if (!Singleton.Database.InsertNewMailItem(this.character, mailmessage))
                {
                    result = 1;
                }
                //EVERYTHING IS OKAY
                else
                {
                    if (cpkt.HasItem > 0)
                    {
                        this.character.ZENY -= req_zeny;
                        CommonFunctions.UpdateZeny(this.character);
                    }

                    //UPDATE ITEM COUNT AS FORM OF A ATTACHMENT
                    if ((cpkt.HasItem & 2) == 2)
                    {
                        item.count -= cpkt.StackCount;
                        if (item.count > 0)
                        {
                            SMSG_UPDATEITEM spkt = new SMSG_UPDATEITEM();
                            spkt.Amount       = (byte)item.count;
                            spkt.UpdateReason = (byte)ItemUpdateReason.AttachmentReceived;
                            spkt.UpdateType   = 4;
                            spkt.Container    = 2;
                            spkt.SessionId    = this.character.id;
                            spkt.Index        = cpkt.Slot;
                            this.Send((byte[])spkt);
                        }
                        else
                        {
                            this.character.container.RemoveAt(cpkt.Slot);
                            SMSG_DELETEITEM spkt = new SMSG_DELETEITEM();
                            spkt.Container    = 2;
                            spkt.Index        = cpkt.Slot;
                            spkt.UpdateReason = (byte)ItemUpdateReason.AttachmentReceived;
                            spkt.SessionId    = this.character.id;
                            this.Send((byte[])spkt);
                        }
                    }

                    //EVERYTHING OKAY
                    result = 0;
                }
            }
            finally
            {
                SMSG_MAILSENDAWNSER spkt = new SMSG_MAILSENDAWNSER();
                spkt.Result    = result;
                spkt.SessionId = cpkt.SessionId;
                this.Send((byte[])spkt);
            }
        }