public async UniTaskVoid HandleRequestCashShopBuy( RequestHandlerData requestHandler, RequestCashShopBuyMessage request, RequestProceedResultDelegate <ResponseCashShopBuyMessage> result) { #if UNITY_STANDALONE && !CLIENT_BUILD IPlayerCharacterData playerCharacter; if (!GameInstance.ServerUserHandlers.TryGetPlayerCharacter(requestHandler.ConnectionId, out playerCharacter)) { result.InvokeError(new ResponseCashShopBuyMessage() { message = UITextKeys.UI_ERROR_NOT_LOGGED_IN, }); return; } if (request.amount <= 0) { result.InvokeError(new ResponseCashShopBuyMessage() { message = UITextKeys.UI_ERROR_INVALID_DATA, }); return; } CashShopItem cashShopItem; if (!GameInstance.CashShopItems.TryGetValue(request.dataId, out cashShopItem)) { result.InvokeError(new ResponseCashShopBuyMessage() { message = UITextKeys.UI_ERROR_ITEM_NOT_FOUND, }); return; } if ((request.currencyType == CashShopItemCurrencyType.CASH && cashShopItem.SellPriceCash <= 0) || (request.currencyType == CashShopItemCurrencyType.GOLD && cashShopItem.SellPriceGold <= 0)) { result.InvokeError(new ResponseCashShopBuyMessage() { message = UITextKeys.UI_ERROR_INVALID_ITEM_DATA, }); return; } int characterGold = playerCharacter.Gold; int userCash = playerCharacter.UserCash; int priceGold = 0; int priceCash = 0; int changeCharacterGold = 0; int changeUserCash = 0; // Validate cash if (request.currencyType == CashShopItemCurrencyType.CASH) { priceCash = cashShopItem.SellPriceCash * request.amount; if (userCash < priceCash) { result.InvokeError(new ResponseCashShopBuyMessage() { message = UITextKeys.UI_ERROR_NOT_ENOUGH_CASH, }); return; } changeUserCash -= priceCash; } // Validate gold if (request.currencyType == CashShopItemCurrencyType.GOLD) { priceGold = cashShopItem.SellPriceGold * request.amount; if (characterGold < priceGold) { result.InvokeError(new ResponseCashShopBuyMessage() { message = UITextKeys.UI_ERROR_NOT_ENOUGH_GOLD, }); return; } changeCharacterGold -= priceGold; } // Increase gold if (cashShopItem.ReceiveGold > 0) { changeCharacterGold += cashShopItem.ReceiveGold * request.amount; } // Increase items List <RewardedItem> rewardItems = new List <RewardedItem>(); if (cashShopItem.ReceiveItems != null && cashShopItem.ReceiveItems.Length > 0) { foreach (ItemAmount itemAmount in cashShopItem.ReceiveItems) { for (int i = 0; i < request.amount; ++i) { rewardItems.Add(new RewardedItem() { item = itemAmount.item, level = 1, amount = itemAmount.amount, randomSeed = (short)Random.Range(short.MinValue, short.MaxValue), }); } } if (playerCharacter.IncreasingItemsWillOverwhelming(rewardItems)) { result.InvokeError(new ResponseCashShopBuyMessage() { message = UITextKeys.UI_ERROR_WILL_OVERWHELMING, }); return; } } // Increase custom currencies List <CharacterCurrency> customCurrencies = new List <CharacterCurrency>(); if (cashShopItem.ReceiveCurrencies != null && cashShopItem.ReceiveCurrencies.Length > 0) { foreach (CurrencyAmount currencyAmount in cashShopItem.ReceiveCurrencies) { for (int i = 0; i < request.amount; ++i) { customCurrencies.Add(CharacterCurrency.Create(currencyAmount.currency, currencyAmount.amount)); } } } // Update currency characterGold += changeCharacterGold; if (request.currencyType == CashShopItemCurrencyType.CASH) { AsyncResponseData <CashResp> changeCashResp = await DbServiceClient.ChangeCashAsync(new ChangeCashReq() { UserId = playerCharacter.UserId, ChangeAmount = -priceCash }); if (!changeCashResp.IsSuccess) { result.InvokeError(new ResponseCashShopBuyMessage() { message = UITextKeys.UI_ERROR_INTERNAL_SERVER_ERROR, }); return; } userCash = changeCashResp.Response.Cash; } playerCharacter.Gold = characterGold; playerCharacter.UserCash = userCash; playerCharacter.IncreaseItems(rewardItems); playerCharacter.IncreaseCurrencies(customCurrencies); playerCharacter.FillEmptySlots(); // Response to client result.InvokeSuccess(new ResponseCashShopBuyMessage() { dataId = request.dataId, rewardGold = cashShopItem.ReceiveGold, rewardItems = rewardItems, }); #endif }
public async UniTaskVoid HandleRequestCashShopBuy( RequestHandlerData requestHandler, RequestCashShopBuyMessage request, RequestProceedResultDelegate<ResponseCashShopBuyMessage> result) { #if UNITY_STANDALONE && !CLIENT_BUILD // Set response data UITextKeys message = UITextKeys.NONE; int dataId = request.dataId; int userCash = 0; int userGold = 0; IPlayerCharacterData playerCharacter; if (!GameInstance.ServerUserHandlers.TryGetPlayerCharacter(requestHandler.ConnectionId, out playerCharacter)) { // Cannot find user message = UITextKeys.UI_ERROR_NOT_LOGGED_IN; } else { // Get user cash amount CashResp getCashResp = await DbServiceClient.GetCashAsync(new GetCashReq() { UserId = playerCharacter.UserId }); userCash = getCashResp.Cash; // Get user gold amount GoldResp getGoldResp = await DbServiceClient.GetGoldAsync(new GetGoldReq() { UserId = playerCharacter.UserId }); userGold = getGoldResp.Gold; CashShopItem cashShopItem; if (!GameInstance.CashShopItems.TryGetValue(dataId, out cashShopItem)) { // Cannot find item message = UITextKeys.UI_ERROR_ITEM_NOT_FOUND; } else if (userCash < cashShopItem.sellPriceCash) { // Not enough cash message = UITextKeys.UI_ERROR_NOT_ENOUGH_CASH; } else if (userGold < cashShopItem.sellPriceGold) { // Not enough cash message = UITextKeys.UI_ERROR_NOT_ENOUGH_GOLD; } else if (playerCharacter.IncreasingItemsWillOverwhelming(cashShopItem.receiveItems)) { // Cannot carry all rewards message = UITextKeys.UI_ERROR_WILL_OVERWHELMING; } else { // Decrease cash amount CashResp changeCashResp = await DbServiceClient.ChangeCashAsync(new ChangeCashReq() { UserId = playerCharacter.UserId, ChangeAmount = -cashShopItem.sellPriceCash }); userCash = changeCashResp.Cash; playerCharacter.UserCash = userCash; // Decrease gold amount GoldResp changeGoldResp = await DbServiceClient.ChangeGoldAsync(new ChangeGoldReq() { UserId = playerCharacter.UserId, ChangeAmount = -cashShopItem.sellPriceGold }); userGold = changeGoldResp.Gold; playerCharacter.UserGold = userGold; // Increase character gold playerCharacter.Gold = playerCharacter.Gold.Increase(cashShopItem.receiveGold); // Increase currencies playerCharacter.IncreaseCurrencies(cashShopItem.receiveCurrencies); // Increase character item if (cashShopItem.receiveItems != null && cashShopItem.receiveItems.Length > 0) { foreach (ItemAmount receiveItem in cashShopItem.receiveItems) { if (receiveItem.item == null || receiveItem.amount <= 0) continue; playerCharacter.AddOrSetNonEquipItems(CharacterItem.Create(receiveItem.item, 1, receiveItem.amount)); } playerCharacter.FillEmptySlots(); } } } // Send response message result.Invoke( message == UITextKeys.NONE ? AckResponseCode.Success : AckResponseCode.Error, new ResponseCashShopBuyMessage() { message = message, dataId = dataId, userCash = userCash, userGold = userGold, }); #endif await UniTask.Yield(); }
private IEnumerator HandleRequestCashShopBuyRoutine(LiteNetLibMessageHandler messageHandler) { long connectionId = messageHandler.connectionId; RequestCashShopBuyMessage message = messageHandler.ReadMessage <RequestCashShopBuyMessage>(); // Set response data ResponseCashShopBuyMessage.Error error = ResponseCashShopBuyMessage.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 = ResponseCashShopBuyMessage.Error.UserNotFound; } else { // Request cash, reduce, send item info messages to map server GetCashJob 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 { DecreaseCashJob decreaseCashJob = new DecreaseCashJob(Database, userData.userId, cashShopItem.sellPrice); decreaseCashJob.Start(); yield return(StartCoroutine(decreaseCashJob.WaitFor())); cash = decreaseCashJob.result; playerCharacter.Gold += cashShopItem.receiveGold; foreach (ItemAmount receiveItem in cashShopItem.receiveItems) { if (receiveItem.item == null) { continue; } CharacterItem characterItem = CharacterItem.Create(receiveItem.item, 1, receiveItem.amount); playerCharacter.NonEquipItems.Add(characterItem); } } } // Send response message ResponseCashShopBuyMessage responseMessage = new ResponseCashShopBuyMessage(); responseMessage.ackId = message.ackId; responseMessage.responseCode = error == ResponseCashShopBuyMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error; responseMessage.error = error; responseMessage.dataId = dataId; responseMessage.cash = cash; ServerSendPacket(connectionId, SendOptions.ReliableOrdered, MsgTypes.CashShopBuy, responseMessage); }