public async UniTaskVoid HandleRequestCashPackageBuyValidation( RequestHandlerData requestHandler, RequestCashPackageBuyValidationMessage request, RequestProceedResultDelegate <ResponseCashPackageBuyValidationMessage> result) { #if UNITY_STANDALONE && !CLIENT_BUILD // TODO: Validate purchasing at server side // Set response data UITextKeys message = UITextKeys.NONE; int dataId = request.dataId; int cash = 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 }); cash = getCashResp.Cash; CashPackage cashPackage; if (!GameInstance.CashPackages.TryGetValue(dataId, out cashPackage)) { // Cannot find package message = UITextKeys.UI_ERROR_CASH_PACKAGE_NOT_FOUND; } else { // Increase cash amount CashResp changeCashResp = await DbServiceClient.ChangeCashAsync(new ChangeCashReq() { UserId = playerCharacter.UserId, ChangeAmount = cashPackage.cashAmount }); cash = changeCashResp.Cash; playerCharacter.UserCash = cash; } } // Send response message result.Invoke( message == UITextKeys.NONE ? AckResponseCode.Success : AckResponseCode.Error, new ResponseCashPackageBuyValidationMessage() { message = message, dataId = dataId, cash = cash, }); #endif await UniTask.Yield(); }
public async UniTaskVoid HandleRequestCashPackageInfo( RequestHandlerData requestHandler, EmptyMessage request, RequestProceedResultDelegate <ResponseCashPackageInfoMessage> result) { #if UNITY_STANDALONE && !CLIENT_BUILD // Set response data UITextKeys message = UITextKeys.NONE; int cash = 0; List <int> cashPackageIds = new List <int>(); 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 }); cash = getCashResp.Cash; // Set cash package ids cashPackageIds.AddRange(GameInstance.CashPackages.Keys); } // Send response message result.Invoke( message == UITextKeys.NONE ? AckResponseCode.Success : AckResponseCode.Error, new ResponseCashPackageInfoMessage() { message = message, cash = cash, cashPackageIds = cashPackageIds.ToArray(), }); #endif await UniTask.Yield(); }
private async UniTaskVoid SetPlayerReadyRoutine(long connectionId, string userId, string selectCharacterId) { CharacterResp characterResp = await DbServiceClient.ReadCharacterAsync(new ReadCharacterReq() { UserId = userId, CharacterId = selectCharacterId }); PlayerCharacterData playerCharacterData = characterResp.CharacterData; // If data is empty / cannot find character, disconnect user if (playerCharacterData == null) { if (LogError) { Logging.LogError(LogTag, "Cannot find select character: " + selectCharacterId + " for user: "******"Cannot find player character with entity Id: " + playerCharacterData.EntityId); } Transport.ServerDisconnect(connectionId); } else { // Prepare saving location for this character string savingCurrentMapName = playerCharacterData.CurrentMapName; Vector3 savingCurrentPosition = playerCharacterData.CurrentPosition; if (IsInstanceMap()) { playerCharacterData.CurrentPosition = MapInstanceWarpToPosition; if (MapInstanceWarpOverrideRotation) { playerCharacterData.CurrentRotation = MapInstanceWarpToRotation; } } // Spawn character entity and set its data Quaternion characterRotation = Quaternion.identity; if (CurrentGameInstance.DimensionType == DimensionType.Dimension3D) { characterRotation = Quaternion.Euler(playerCharacterData.CurrentRotation); } GameObject spawnObj = Instantiate(entityPrefab.gameObject, playerCharacterData.CurrentPosition, characterRotation); BasePlayerCharacterEntity playerCharacterEntity = spawnObj.GetComponent <BasePlayerCharacterEntity>(); playerCharacterData.CloneTo(playerCharacterEntity); Assets.NetworkSpawn(spawnObj, 0, connectionId); // Set currencies // Gold GoldResp getGoldResp = await DbServiceClient.GetGoldAsync(new GetGoldReq() { UserId = userId }); playerCharacterEntity.UserGold = getGoldResp.Gold; // Cash CashResp getCashResp = await DbServiceClient.GetCashAsync(new GetCashReq() { UserId = userId }); playerCharacterEntity.UserCash = getCashResp.Cash; // Prepare saving location for this character if (IsInstanceMap()) { instanceMapCurrentLocations.TryAdd(playerCharacterEntity.ObjectId, new KeyValuePair <string, Vector3>(savingCurrentMapName, savingCurrentPosition)); } // Set user Id playerCharacterEntity.UserId = userId; // Load user level GetUserLevelResp getUserLevelResp = await DbServiceClient.GetUserLevelAsync(new GetUserLevelReq() { UserId = userId }); playerCharacterEntity.UserLevel = getUserLevelResp.UserLevel; // Load party data, if this map-server does not have party data if (playerCharacterEntity.PartyId > 0) { if (!ServerPartyHandlers.ContainsParty(playerCharacterEntity.PartyId)) { await LoadPartyRoutine(playerCharacterEntity.PartyId); } PartyData party; if (ServerPartyHandlers.TryGetParty(playerCharacterEntity.PartyId, out party)) { ServerGameMessageHandlers.SendSetPartyData(playerCharacterEntity.ConnectionId, party); ServerGameMessageHandlers.SendAddPartyMembersToOne(playerCharacterEntity.ConnectionId, party); } else { playerCharacterEntity.ClearParty(); } } // Load guild data, if this map-server does not have guild data if (playerCharacterEntity.GuildId > 0) { if (!ServerGuildHandlers.ContainsGuild(playerCharacterEntity.GuildId)) { await LoadGuildRoutine(playerCharacterEntity.GuildId); } GuildData guild; if (ServerGuildHandlers.TryGetGuild(playerCharacterEntity.GuildId, out guild)) { playerCharacterEntity.GuildName = guild.guildName; playerCharacterEntity.GuildRole = guild.GetMemberRole(playerCharacterEntity.Id); ServerGameMessageHandlers.SendSetGuildData(playerCharacterEntity.ConnectionId, guild); ServerGameMessageHandlers.SendAddGuildMembersToOne(playerCharacterEntity.ConnectionId, guild); ServerGameMessageHandlers.SendSetGuildMessage(playerCharacterEntity.ConnectionId, guild); ServerGameMessageHandlers.SendSetGuildRolesToOne(playerCharacterEntity.ConnectionId, guild); ServerGameMessageHandlers.SendSetGuildMemberRolesToOne(playerCharacterEntity.ConnectionId, guild); ServerGameMessageHandlers.SendSetGuildSkillLevelsToOne(playerCharacterEntity.ConnectionId, guild); ServerGameMessageHandlers.SendSetGuildGold(playerCharacterEntity.ConnectionId, guild); ServerGameMessageHandlers.SendSetGuildLevelExpSkillPoint(playerCharacterEntity.ConnectionId, guild); } else { playerCharacterEntity.ClearGuild(); } } // Summon saved summons for (int i = 0; i < playerCharacterEntity.Summons.Count; ++i) { CharacterSummon summon = playerCharacterEntity.Summons[i]; summon.Summon(playerCharacterEntity, summon.Level, summon.summonRemainsDuration, summon.Exp, summon.CurrentHp, summon.CurrentMp); playerCharacterEntity.Summons[i] = summon; } // Summon saved mount entity if (GameInstance.VehicleEntities.ContainsKey(playerCharacterData.MountDataId)) { playerCharacterEntity.Mount(GameInstance.VehicleEntities[playerCharacterData.MountDataId]); } // Force make caches, to calculate current stats to fill empty slots items playerCharacterEntity.ForceMakeCaches(); playerCharacterEntity.FillEmptySlots(); // Notify clients that this character is spawn or dead if (!playerCharacterEntity.IsDead()) { playerCharacterEntity.CallAllOnRespawn(); } else { playerCharacterEntity.CallAllOnDead(); } // Register player character entity to the server RegisterPlayerCharacter(connectionId, playerCharacterEntity); } } }
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(); }