[Fix] OpenNos Trade bug

10/09/2018 16:20 Aruko7#1
Hey,
After long researching, i just can't fix the trade on OpenNos.
Im using the Ciapa Cracked SRC, but i just can't fix the trade.
I please u guys to help me, that would be super kind!

PS: Please don't write "LEARN TO CODE" - It's called learning by doing.. ._.
10/09/2018 16:52 erixor#2
Coding is learning by doing :rollsafe:
10/09/2018 18:41 SxManuxS#3
send you discord for you help :) !
10/09/2018 18:54 Marshmello*#4
Quote:
Originally Posted by SxManuxS View Post
send you discord for you help :) !
the new dude which says all time send discord for you help lmao
10/10/2018 01:11 0Lucifer0#5
Quote:
Originally Posted by Aruko7 View Post
Hey,
After long researching, i just can't fix the trade on OpenNos.
Im using the Ciapa Cracked SRC, but i just can't fix the trade.
I please u guys to help me, that would be super kind!

PS: Please don't write "LEARN TO CODE" - It's called learning by doing.. ._.
This is not doing... when you try to learn math do you ask what’s 2+2 ? No. Also this project is too difficult for you to start with. If you have 0 knowledge try on something really basics. Try to understand the logic behind development. Always start with simple things. If your goal is to fix stuff fast for your server. It won’t happen. It’s like asking please guy I want to pass a PhD tomorrow in quantum physics can you tell me how...
10/10/2018 01:59 MANUEL PERES#6

Code:
  [Packet("exc_list")]
        public void ExchangeList(string packet)
        {
            string[] packetsplit = packet.Split(' ');
            if (!long.TryParse(packetsplit[2], out long gold))
            {
                return;
            }
            if (!long.TryParse(packetsplit[3], out long bankgold))
            {
                return;
            }
            byte[] type = new byte[10], qty = new byte[10];
            short[] slot = new short[10];
            var packetList = string.Empty;

            if (gold < 0 || gold > Session.Character.Gold || bankgold < 0 || bankgold > Session.Account.BankGold || Session.Character.ExchangeInfo == null || Session.Character.ExchangeInfo.ExchangeList.Any())
            {
                return;
            }

            var targetSession = ServerManager.Instance.GetSessionByCharacterId(Session.Character.ExchangeInfo.TargetCharacterId);
            if (Session.Character.HasShopOpened || targetSession != null && targetSession.Character.HasShopOpened)
            {
                closeExchange(Session, targetSession);
                return;
            }

            for (int j = 7, i = 0; j <= packetsplit.Length && i < 10; j += 3, i++)
            {
                byte.TryParse(packetsplit[j - 3], out type[i]);
                short.TryParse(packetsplit[j - 2], out slot[i]);
                byte.TryParse(packetsplit[j - 1], out qty[i]);
                if ((InventoryType)type[i] == InventoryType.Bazaar)
                {
                    closeExchange(Session, targetSession);
                    return;
                }

                var item = Session.Character.Inventory.LoadBySlotAndType(slot[i], (InventoryType)type[i]);
                if (item == null)
                {
                    return;
                }

                if (qty[i] <= 0 || item.Amount < qty[i])
                {
                    return;
                }

                var it = item.DeepCopy();
                if (it.Item.IsTradable && !it.IsBound)
                {
                    it.Amount = qty[i];
                    Session.Character.ExchangeInfo.ExchangeList.Add(it);
                    if (type[i] != 0)
                    {
                        packetList += $"{i}.{type[i]}.{it.ItemVNum}.{qty[i]} ";
                    }
                    else
                    {
                        packetList += $"{i}.{type[i]}.{it.ItemVNum}.{it.Rare}.{it.Upgrade} ";
                    }
                }
                else if (it.IsBound)
                {
                    Session.SendPacket("exc_close 0");
                    Session.CurrentMapInstance?.Broadcast(Session, "exc_close 0", ReceiverType.OnlySomeone,
                        string.Empty, Session.Character.ExchangeInfo.TargetCharacterId);

                    if (targetSession != null)
                    {
                        targetSession.Character.ExchangeInfo = null;
                    }
                    Session.Character.ExchangeInfo = null;
                    return;
                }
            }

            Session.Character.ExchangeInfo.Gold = gold;
            Session.Character.ExchangeInfo.BankGold = bankgold;
            Session.CurrentMapInstance?.Broadcast(Session, $"exc_list 1 {Session.Character.CharacterId} {gold} {bankgold} {packetList}", ReceiverType.OnlySomeone, string.Empty, Session.Character.ExchangeInfo.TargetCharacterId);
            Session.Character.ExchangeInfo.Validate = true;
        }

        /// <summary>
        /// req_exc packet
        /// </summary>
        /// <param name="exchangeRequestPacket"></param>
        public void ExchangeRequest(ExchangeRequestPacket exchangeRequestPacket)
        {
            if (exchangeRequestPacket.CharacterId != 0 && Session.Character.MapInstanceId != ServerManager.Instance.GetProperty<Guid>(exchangeRequestPacket.CharacterId, nameof(Character.MapInstanceId)))
            {
                ServerManager.Instance.SetProperty(exchangeRequestPacket.CharacterId, nameof(Character.ExchangeInfo), null);
                Session.Character.ExchangeInfo = null;
            }
            else
            {
                switch (exchangeRequestPacket.RequestType)
                {
                    case RequestExchangeType.Requested:
                        if (!Session.HasCurrentMapInstance)
                        {
                            return;
                        }

                        var targetSession = Session.CurrentMapInstance.GetSessionByCharacterId(exchangeRequestPacket.CharacterId);
                        if (targetSession == null)
                        {
                            return;
                        }

                        if (targetSession.Character.Group != null && targetSession.Character.Group?.GroupType != GroupType.Group)
                        {
                            Session.SendPacket(UserInterfaceHelper.GenerateMsg(Language.Instance.GetMessageFromKey("EXCHANGE_NOT_ALLOWED_IN_RAID"), 0));
                            return;
                        }

                        if (Session.Character.Group != null && Session.Character.Group?.GroupType != GroupType.Group)
                        {
                            Session.SendPacket(UserInterfaceHelper.GenerateMsg(Language.Instance.GetMessageFromKey("EXCHANGE_NOT_ALLOWED_WITH_RAID_MEMBER"), 0));
                            return;
                        }

                        if (Session.Character.IsBlockedByCharacter(exchangeRequestPacket.CharacterId))
                        {
                            Session.SendPacket(UserInterfaceHelper.GenerateInfo(Language.Instance.GetMessageFromKey("BLACKLIST_BLOCKED")));
                            return;
                        }

                        if (Session.Character.Speed == 0 || targetSession.Character.Speed == 0)
                        {
                            Session.Character.ExchangeBlocked = true;
                        }

                        if (targetSession.Character.LastSkillUse.AddSeconds(20) > DateTime.Now || targetSession.Character.LastDefence.AddSeconds(20) > DateTime.Now)
                        {
                            Session.SendPacket(UserInterfaceHelper.GenerateInfo(string.Format(Language.Instance.GetMessageFromKey("PLAYER_IN_BATTLE"), targetSession.Character.Name)));
                            return;
                        }

                        if (Session.Character.LastSkillUse.AddSeconds(20) > DateTime.Now || Session.Character.LastDefence.AddSeconds(20) > DateTime.Now)
                        {
                            Session.SendPacket(UserInterfaceHelper.GenerateInfo(Language.Instance.GetMessageFromKey("IN_BATTLE")));
                            return;
                        }

                        if (Session.Character.HasShopOpened || targetSession.Character.HasShopOpened)
                        {
                            Session.SendPacket(UserInterfaceHelper.GenerateMsg(Language.Instance.GetMessageFromKey("HAS_SHOP_OPENED"), 10));
                            return;
                        }

                        if (targetSession.Character.ExchangeBlocked)
                        {
                            Session.SendPacket(Session.Character.GenerateSay(Language.Instance.GetMessageFromKey("TRADE_BLOCKED"), 11));
                        }
                        else
                        {
                            if (Session.Character.InExchangeOrTrade || targetSession.Character.InExchangeOrTrade)
                            {
                                Session.SendPacket(UserInterfaceHelper.GenerateModal(Language.Instance.GetMessageFromKey("ALREADY_EXCHANGE"), 0));
                            }
                            else
                            {
                                Session.SendPacket(UserInterfaceHelper.GenerateModal(string.Format(Language.Instance.GetMessageFromKey("YOU_ASK_FOR_EXCHANGE"), targetSession.Character.Name), 0));
                                Session.Character.TradeRequests.Add(targetSession.Character.CharacterId);
                                targetSession.SendPacket(UserInterfaceHelper.GenerateDialog($"#req_exc^2^{Session.Character.CharacterId} #req_exc^5^{Session.Character.CharacterId} {string.Format(Language.Instance.GetMessageFromKey("INCOMING_EXCHANGE"), Session.Character.Name)}"));
                            }
                        }

                        break;

                    case RequestExchangeType.Confirmed: // click Trade button in exchange window
                        if (Session.HasCurrentMapInstance && Session.HasSelectedCharacter
                            && Session.Character.ExchangeInfo != null && Session.Character.ExchangeInfo.TargetCharacterId != Session.Character.CharacterId)
                        {
                            if (!Session.HasCurrentMapInstance)
                            {
                                return;
                            }

                            targetSession = Session.CurrentMapInstance.GetSessionByCharacterId(Session.Character.ExchangeInfo.TargetCharacterId);

                            if (targetSession == null)
                            {
                                return;
                            }

                            if (Session.Character.Group != null && Session.Character.Group?.GroupType != GroupType.Group)
                            {
                                Session.SendPacket(UserInterfaceHelper.GenerateMsg(Language.Instance.GetMessageFromKey("EXCHANGE_NOT_ALLOWED_IN_RAID"), 0));
                                return;
                            }

                            if (targetSession.Character.Group != null && targetSession.Character.Group?.GroupType != GroupType.Group)
                            {
                                Session.SendPacket(UserInterfaceHelper.GenerateMsg(Language.Instance.GetMessageFromKey("EXCHANGE_NOT_ALLOWED_WITH_RAID_MEMBER"), 0));
                                return;
                            }

                            if (Session.IsDisposing || targetSession.IsDisposing)
                            {
                                closeExchange(Session, targetSession);
                                return;
                            }

                            lock (targetSession.Character.Inventory)
                            {
                                lock (Session.Character.Inventory)
                                {
                                    var targetExchange = targetSession.Character.ExchangeInfo;
                                    var inventory = targetSession.Character.Inventory;

                                    var gold = targetSession.Character.Gold;
                                    var bankgold = targetSession.Account.BankGold;
                                    var backpack = targetSession.Character.HaveBackpack() ? 1 : 0;
                                    var maxGold = ServerManager.Instance.MaxGold;
                                    var maxBankGold = ServerManager.Instance.MaxBankGold;

                                    if (targetExchange == null || Session.Character.ExchangeInfo == null)
                                    {
                                        return;
                                    }

                                    if (Session.Character.ExchangeInfo.Validate && targetExchange.Validate)
                                    {
                                        Session.Character.ExchangeInfo.Confirm = true;
                                        if (targetExchange.Confirm && Session.Character.ExchangeInfo.Confirm)
                                        {
                                            Session.SendPacket("exc_close 1");
                                            targetSession.SendPacket("exc_close 1");

                                            var [MENTION=1560910]Continue[/MENTION] = true;
                                            var goldmax = false;
                                            if (!Session.Character.Inventory.EnoughPlace(targetExchange.ExchangeList))
                                            {
                                                [MENTION=1560910]Continue[/MENTION] = false;
                                            }

                                             if (!inventory.EnoughPlace(Session.Character.ExchangeInfo.ExchangeList))
                                            {
                                                [MENTION=1560910]Continue[/MENTION] = false;
                                            }

                                            if (Session.Character.ExchangeInfo.Gold + gold > maxGold)
                                            {
                                                goldmax = false;
                                            }
                                            if (Session.Character.ExchangeInfo.BankGold + bankgold > maxBankGold)
                                                goldmax = false;
                                            if (Session.Character.ExchangeInfo.BankGold > Session.Account.BankGold)
                                            {
                                                return;
                                            }

                                            if (Session.Character.ExchangeInfo.Gold > Session.Character.Gold)
                                            {
                                                return;
                                            }



                                            if (targetExchange.BankGold + Session.Character.ExchangeInfo.BankGold > maxBankGold)
                                                goldmax = false;
                                            if (targetExchange.Gold + Session.Character.Gold > maxGold)
                                            {
                                                goldmax = false;
                                            }

                                            if ( [MENTION=1560910]Continue[/MENTION] || goldmax)
                                            {
                                                var message =  [MENTION=1560910]Continue[/MENTION] ? UserInterfaceHelper.GenerateMsg(Language.Instance.GetMessageFromKey("NOT_ENOUGH_PLACE"), 0)
                                                    : UserInterfaceHelper.GenerateMsg(Language.Instance.GetMessageFromKey("MAX_GOLD"), 0);
                                                Session.SendPacket(message);
                                                targetSession.SendPacket(message);
                                                closeExchange(Session, targetSession);
                                            }
                                            else
                                            {
                                                if (Session.Character.ExchangeInfo.ExchangeList.Any(ei => !(ei.Item.IsTradable || ei.IsBound)))
                                                {
                                                    Session.SendPacket(UserInterfaceHelper.GenerateMsg(Language.Instance.GetMessageFromKey("ITEM_NOT_TRADABLE"), 0));
                                                    closeExchange(Session, targetSession);
                                                }
                                                else // all items can be traded
                                                {
                                                    Session.Character.IsExchanging = targetSession.Character.IsExchanging = true;

                                                    // exchange all items from target to source
                                                    exchange(targetSession, Session);

                                                    // exchange all items from source to target
                                                    exchange(Session, targetSession);

                                                    Session.Character.IsExchanging = targetSession.Character.IsExchanging = false;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            Session.SendPacket(UserInterfaceHelper.GenerateInfo(string.Format(Language.Instance.GetMessageFromKey("IN_WAITING_FOR"), targetSession.Character.Name)));
                                        }
                                    }
                                }
                            }
                        }

                        break;

                    case RequestExchangeType.Cancelled: // cancel trade thru exchange window
                        if (Session.HasCurrentMapInstance && Session.Character.ExchangeInfo != null)
                        {
                            targetSession = Session.CurrentMapInstance.GetSessionByCharacterId(Session.Character.ExchangeInfo.TargetCharacterId);
                            closeExchange(Session, targetSession);
                        }

                        break;

                    case RequestExchangeType.List:
                        var otherInExchangeOrTrade = ServerManager.Instance.GetProperty<bool>(exchangeRequestPacket.CharacterId, nameof(Character.InExchangeOrTrade));
                        if (!Session.Character.InExchangeOrTrade || !otherInExchangeOrTrade)
                        {
                            var otherSession = ServerManager.Instance.GetSessionByCharacterId(exchangeRequestPacket.CharacterId);
                            if (exchangeRequestPacket.CharacterId == Session.Character.CharacterId || Session.Character.Speed == 0 || otherSession == null || otherSession.Character.TradeRequests.All(s => s != Session.Character.CharacterId))
                            {
                                return;
                            }

                            if (Session.Character.Group != null && Session.Character.Group?.GroupType != GroupType.Group)
                            {
                                Session.SendPacket(UserInterfaceHelper.GenerateMsg(Language.Instance.GetMessageFromKey("EXCHANGE_NOT_ALLOWED_IN_RAID"), 0));
                                return;
                            }

                            if (otherSession.Character.Group != null && otherSession.Character.Group?.GroupType != GroupType.Group)
                            {
                                Session.SendPacket(UserInterfaceHelper.GenerateMsg(Language.Instance.GetMessageFromKey("EXCHANGE_NOT_ALLOWED_WITH_RAID_MEMBER"), 0));
                                return;
                            }

                            Session.SendPacket($"exc_list 1 {exchangeRequestPacket.CharacterId} -1");
                            Session.SendPacket($"gbex {Session.Account.BankGold / 1000} {Session.Character.Gold} 0 0");
                            var exc = new ExchangeInfo
                            {
                                TargetCharacterId = exchangeRequestPacket.CharacterId,
                                Confirm = false
                            };
                            Session.Character.ExchangeInfo = exc;
                            ServerManager.Instance.SetProperty(exchangeRequestPacket.CharacterId, nameof(Character.ExchangeInfo), new ExchangeInfo { TargetCharacterId = Session.Character.CharacterId, Confirm = false });
                            Session.CurrentMapInstance?.Broadcast(Session, $"exc_list 1 {Session.Character.CharacterId} -1", ReceiverType.OnlySomeone, string.Empty, exchangeRequestPacket.CharacterId);
                            ClientSession test = ServerManager.Instance.GetSessionByCharacterId(exchangeRequestPacket.CharacterId);
                            test.SendPacket($"gbex {test.Account.BankGold / 1000} {test.Character.Gold} 0 0");
                        }
                        else
                        {
                            Session.CurrentMapInstance?.Broadcast(Session, UserInterfaceHelper.GenerateModal(Language.Instance.GetMessageFromKey("ALREADY_EXCHANGE"), 0), ReceiverType.OnlySomeone, string.Empty, exchangeRequestPacket.CharacterId);
                        }

                        break;

                    case RequestExchangeType.Declined:
                        ServerManager.Instance.GetProperty<string>(exchangeRequestPacket.CharacterId, nameof(Character.Name));
                        ServerManager.Instance.SetProperty(exchangeRequestPacket.CharacterId, nameof(Character.ExchangeInfo), null);
                        Session.Character.ExchangeInfo = null;
                        Session.SendPacket(Session.Character.GenerateSay(Language.Instance.GetMessageFromKey("YOU_REFUSED"), 10));
                        Session.CurrentMapInstance?.Broadcast(Session, Session.Character.GenerateSay(string.Format(Language.Instance.GetMessageFromKey("EXCHANGE_REFUSED"), Session.Character.Name), 10), ReceiverType.OnlySomeone, string.Empty, exchangeRequestPacket.CharacterId);
                        break;

                    default:
                        Logger.Log.Warn($"Exchange-Request-Type not implemented. RequestType: {exchangeRequestPacket.RequestType})");
                        break;
                }
            }
        }



Code:
using System.Collections.Generic;

namespace OpenNos.GameObject
{
    public class ExchangeInfo
    {
        #region Instantiation

        public ExchangeInfo()
        {
            Confirm = false;
            Gold = 0;
            BankGold = 0;
            TargetCharacterId = -1;
            ExchangeList = new List<ItemInstance>();
            Validate = false;
        }

        #endregion

        #region Properties

        public bool Confirm { get; set; }

        public List<ItemInstance> ExchangeList { get; set; }

        public long Gold { get; set; }

        public long BankGold { get; set; }

        public long TargetCharacterId { get; set; }

        public bool Validate { get; set; }

        #endregion
    }
}



Code:
 public void SetProperty(long charId, string property, object value)
        {
            var session = GetSessionByCharacterId(charId);
            if (session == null)
            {
                return;
            }

            var propertyinfo = session.Character.GetType().GetProperty(property);
            propertyinfo?.SetValue(session.Character, value, null);
        }

        public T GetProperty<T>(string charName, string property)
        {
            var session = Sessions.FirstOrDefault(s => s.Character != null && s.Character.Name.Equals(charName));
            if (session == null)
            {
                return default(T);
            }

            return (T)session.Character.GetType().GetProperties().Single(pi => pi.Name == property).GetValue(session.Character, null);
        }

        public T GetProperty<T>(long charId, string property)
        {
            var session = GetSessionByCharacterId(charId);
            if (session == null)
            {
                return default(T);
            }

            return (T)session.Character.GetType().GetProperties().Single(pi => pi.Name == property).GetValue(session.Character, null);
        }
:kappa: