Example #1
0
        public async UniTaskVoid HandleRequestOpenGacha(RequestHandlerData requestHandler, RequestOpenGachaMessage request, RequestProceedResultDelegate <ResponseOpenGachaMessage> result)
        {
#if UNITY_STANDALONE && !CLIENT_BUILD
            IPlayerCharacterData playerCharacter;
            if (!GameInstance.ServerUserHandlers.TryGetPlayerCharacter(requestHandler.ConnectionId, out playerCharacter))
            {
                result.Invoke(AckResponseCode.Error, new ResponseOpenGachaMessage()
                {
                    message = UITextKeys.UI_ERROR_NOT_LOGGED_IN,
                });
                return;
            }

            Gacha gacha;
            if (!GameInstance.Gachas.TryGetValue(request.dataId, out gacha))
            {
                result.Invoke(AckResponseCode.Error, new ResponseOpenGachaMessage()
                {
                    message = UITextKeys.UI_ERROR_INVALID_DATA,
                });
                return;
            }

            int price = request.openMode == GachaOpenMode.Multiple ? gacha.MultipleModeOpenPrice : gacha.SingleModeOpenPrice;
            // Get user cash amount
            AsyncResponseData <CashResp> getCashResp = await DbServiceClient.GetCashAsync(new GetCashReq()
            {
                UserId = playerCharacter.UserId
            });

            if (!getCashResp.IsSuccess)
            {
                result.InvokeError(new ResponseOpenGachaMessage()
                {
                    message = UITextKeys.UI_ERROR_INTERNAL_SERVER_ERROR,
                });
                return;
            }
            int cash = getCashResp.Response.Cash;
            if (cash < price)
            {
                result.Invoke(AckResponseCode.Error, new ResponseOpenGachaMessage()
                {
                    message = UITextKeys.UI_ERROR_NOT_ENOUGH_CASH,
                });
                return;
            }

            int openCount = request.openMode == GachaOpenMode.Multiple ? gacha.MultipleModeOpenCount : 1;
            List <RewardedItem> rewardItems = gacha.GetRandomedItems(openCount);
            if (playerCharacter.IncreasingItemsWillOverwhelming(rewardItems))
            {
                result.Invoke(AckResponseCode.Error, new ResponseOpenGachaMessage()
                {
                    message = UITextKeys.UI_ERROR_WILL_OVERWHELMING,
                });
                return;
            }
            // Decrease cash amount
            AsyncResponseData <CashResp> changeCashResp = await DbServiceClient.ChangeCashAsync(new ChangeCashReq()
            {
                UserId       = playerCharacter.UserId,
                ChangeAmount = -price
            });

            if (!changeCashResp.IsSuccess)
            {
                result.InvokeError(new ResponseOpenGachaMessage()
                {
                    message = UITextKeys.UI_ERROR_INTERNAL_SERVER_ERROR,
                });
                return;
            }
            playerCharacter.UserCash = changeCashResp.Response.Cash;
            // Increase character items
            playerCharacter.IncreaseItems(rewardItems);
            playerCharacter.FillEmptySlots();
            // Send response message
            result.Invoke(AckResponseCode.Success, new ResponseOpenGachaMessage()
            {
                dataId      = request.dataId,
                rewardItems = rewardItems,
            });
#endif
        }