Пример #1
0
        private IEnumerator HandleRequestCashShopBuyRoutine(LiteNetLibMessageHandler messageHandler)
        {
            var connectionId = messageHandler.connectionId;
            var message      = messageHandler.ReadMessage <RequestCashShopBuyMessage>();
            var error        = ResponseCashShopBuyMessage.Error.None;
            var dataId       = message.dataId;
            var cash         = 0;
            BasePlayerCharacterEntity playerCharacter;
            UserCharacterData         userData;

            if (!playerCharacters.TryGetValue(connectionId, out playerCharacter) ||
                !usersById.TryGetValue(playerCharacter.Id, out userData))
            {
                error = ResponseCashShopBuyMessage.Error.UserNotFound;
            }
            else
            {
                // Request cash, reduce, send item info messages to map server
                var job = new GetCashJob(Database, userData.userId);
                job.Start();
                yield return(StartCoroutine(job.WaitFor()));

                cash = job.result;
                CashShopItem cashShopItem;
                if (!GameInstance.CashShopItems.TryGetValue(dataId, out cashShopItem))
                {
                    error = ResponseCashShopBuyMessage.Error.ItemNotFound;
                }
                else if (cash < cashShopItem.sellPrice)
                {
                    error = ResponseCashShopBuyMessage.Error.NotEnoughCash;
                }
                else
                {
                    var decreaseCashJob = new DecreaseCashJob(Database, userData.userId, cashShopItem.sellPrice);
                    decreaseCashJob.Start();
                    yield return(StartCoroutine(decreaseCashJob.WaitFor()));

                    cash = decreaseCashJob.result;
                    playerCharacter.Gold += cashShopItem.receiveGold;
                    foreach (var receiveItem in cashShopItem.receiveItems)
                    {
                        if (receiveItem.item == null)
                        {
                            continue;
                        }
                        var characterItem = CharacterItem.Create(receiveItem.item, 1, receiveItem.amount);
                        playerCharacter.NonEquipItems.Add(characterItem);
                    }
                }
            }
            var responseMessage = new ResponseCashShopBuyMessage();

            responseMessage.ackId        = message.ackId;
            responseMessage.responseCode = error == ResponseCashShopBuyMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error;
            responseMessage.error        = error;
            responseMessage.cash         = cash;
            ServerSendPacket(connectionId, SendOptions.ReliableOrdered, MsgTypes.CashShopBuy, responseMessage);
        }
Пример #2
0
        private IEnumerator HandleRequestCashPackageBuyValidationRoutine(LiteNetLibMessageHandler messageHandler)
        {
            long connectionId = messageHandler.connectionId;
            RequestCashPackageBuyValidationMessage message = messageHandler.ReadMessage <RequestCashPackageBuyValidationMessage>();

            // TODO: Validate purchasing at server side
            // Set response data
            ResponseCashPackageBuyValidationMessage.Error error = ResponseCashPackageBuyValidationMessage.Error.None;
            int dataId = message.dataId;
            int cash   = 0;
            BasePlayerCharacterEntity playerCharacter;
            UserCharacterData         userData;

            if (!playerCharacters.TryGetValue(connectionId, out playerCharacter) ||
                !usersById.TryGetValue(playerCharacter.Id, out userData))
            {
                error = ResponseCashPackageBuyValidationMessage.Error.UserNotFound;
            }
            else
            {
                // Get current cash will return this in case it cannot increase cash
                GetCashJob job = new GetCashJob(Database, userData.userId);
                job.Start();
                yield return(StartCoroutine(job.WaitFor()));

                cash = job.result;
                CashPackage cashPackage;
                if (!GameInstance.CashPackages.TryGetValue(dataId, out cashPackage))
                {
                    error = ResponseCashPackageBuyValidationMessage.Error.PackageNotFound;
                }
                else
                {
                    IncreaseCashJob increaseCashJob = new IncreaseCashJob(Database, userData.userId, cashPackage.cashAmount);
                    increaseCashJob.Start();
                    yield return(StartCoroutine(increaseCashJob.WaitFor()));

                    cash = increaseCashJob.result;
                }
            }
            // Send response message
            ResponseCashPackageBuyValidationMessage responseMessage = new ResponseCashPackageBuyValidationMessage();

            responseMessage.ackId        = message.ackId;
            responseMessage.responseCode = error == ResponseCashPackageBuyValidationMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error;
            responseMessage.error        = error;
            responseMessage.dataId       = dataId;
            responseMessage.cash         = cash;
            ServerSendPacket(connectionId, SendOptions.ReliableOrdered, MsgTypes.CashPackageBuyValidation, responseMessage);
        }
Пример #3
0
        private IEnumerator HandleRequestCashPackageInfoRoutine(LiteNetLibMessageHandler messageHandler)
        {
            long           connectionId = messageHandler.connectionId;
            BaseAckMessage message      = messageHandler.ReadMessage <BaseAckMessage>();

            // Set response data
            ResponseCashPackageInfoMessage.Error error = ResponseCashPackageInfoMessage.Error.None;
            int        cash           = 0;
            List <int> cashPackageIds = new List <int>();
            BasePlayerCharacterEntity playerCharacter;
            UserCharacterData         userData;

            if (!playerCharacters.TryGetValue(connectionId, out playerCharacter) ||
                !usersById.TryGetValue(playerCharacter.Id, out userData))
            {
                error = ResponseCashPackageInfoMessage.Error.UserNotFound;
            }
            else
            {
                GetCashJob job = new GetCashJob(Database, userData.userId);
                job.Start();
                yield return(StartCoroutine(job.WaitFor()));

                cash = job.result;
                foreach (int cashShopItemId in GameInstance.CashPackages.Keys)
                {
                    cashPackageIds.Add(cashShopItemId);
                }
            }
            // Send response message
            ResponseCashPackageInfoMessage responseMessage = new ResponseCashPackageInfoMessage();

            responseMessage.ackId          = message.ackId;
            responseMessage.responseCode   = error == ResponseCashPackageInfoMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error;
            responseMessage.error          = error;
            responseMessage.cash           = cash;
            responseMessage.cashPackageIds = cashPackageIds.ToArray();
            ServerSendPacket(connectionId, SendOptions.ReliableOrdered, MsgTypes.CashPackageInfo, responseMessage);
        }