private async UniTaskVoid HandleRequestCreateCharacterRoutine(LiteNetLibMessageHandler messageHandler) { long connectionId = messageHandler.connectionId; RequestCreateCharacterMessage message = messageHandler.ReadMessage <RequestCreateCharacterMessage>(); ResponseCreateCharacterMessage.Error error = ResponseCreateCharacterMessage.Error.None; string characterName = message.characterName; int dataId = message.dataId; int entityId = message.entityId; int factionId = message.factionId; CentralUserPeerInfo userPeerInfo; FindCharacterNameResp findCharacterNameResp = await DbServiceClient.FindCharacterNameAsync(new FindCharacterNameReq() { CharacterName = characterName }); if (findCharacterNameResp.FoundAmount > 0) { error = ResponseCreateCharacterMessage.Error.CharacterNameAlreadyExisted; } else if (!userPeers.TryGetValue(connectionId, out userPeerInfo)) { error = ResponseCreateCharacterMessage.Error.NotLoggedin; } else if (string.IsNullOrEmpty(characterName) || characterName.Length < minCharacterNameLength) { error = ResponseCreateCharacterMessage.Error.TooShortCharacterName; } else if (characterName.Length > maxCharacterNameLength) { error = ResponseCreateCharacterMessage.Error.TooLongCharacterName; } else if (!GameInstance.PlayerCharacters.ContainsKey(dataId) || !GameInstance.PlayerCharacterEntities.ContainsKey(entityId) || (GameInstance.Factions.Count > 0 && !GameInstance.Factions.ContainsKey(factionId))) { // If there is factions, it must have faction with the id stored in faction dictionary error = ResponseCreateCharacterMessage.Error.InvalidData; } else { string characterId = GenericUtils.GetUniqueId(); PlayerCharacterData characterData = new PlayerCharacterData(); characterData.Id = characterId; characterData.SetNewPlayerCharacterData(characterName, dataId, entityId); characterData.FactionId = factionId; DeserializeCreateCharacterExtra(characterData, messageHandler.reader); await DbServiceClient.CreateCharacterAsync(new CreateCharacterReq() { UserId = userPeerInfo.userId, CharacterData = characterData.ToByteString() }); } ResponseCreateCharacterMessage responseMessage = new ResponseCreateCharacterMessage(); responseMessage.ackId = message.ackId; responseMessage.responseCode = error == ResponseCreateCharacterMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error; responseMessage.error = error; ServerSendResponse(connectionId, MMOMessageTypes.ResponseCreateCharacter, responseMessage); }
private IEnumerator HandleRequestCreateCharacterRoutine(LiteNetLibMessageHandler messageHandler) { long connectionId = messageHandler.connectionId; RequestCreateCharacterMessage message = messageHandler.ReadMessage <RequestCreateCharacterMessage>(); ResponseCreateCharacterMessage.Error error = ResponseCreateCharacterMessage.Error.None; string characterName = message.characterName; int dataId = message.dataId; int entityId = message.entityId; int factionId = message.factionId; CentralUserPeerInfo userPeerInfo; FindCharacterNameJob findCharacterNameJob = new FindCharacterNameJob(Database, characterName); findCharacterNameJob.Start(); yield return(StartCoroutine(findCharacterNameJob.WaitFor())); if (findCharacterNameJob.result > 0) { error = ResponseCreateCharacterMessage.Error.CharacterNameAlreadyExisted; } else if (!userPeers.TryGetValue(connectionId, out userPeerInfo)) { error = ResponseCreateCharacterMessage.Error.NotLoggedin; } else if (string.IsNullOrEmpty(characterName) || characterName.Length < minCharacterNameLength) { error = ResponseCreateCharacterMessage.Error.TooShortCharacterName; } else if (characterName.Length > maxCharacterNameLength) { error = ResponseCreateCharacterMessage.Error.TooLongCharacterName; } else if (!GameInstance.PlayerCharacters.ContainsKey(dataId) || !GameInstance.PlayerCharacterEntities.ContainsKey(entityId) || (GameInstance.Factions.Count > 0 && !GameInstance.Factions.ContainsKey(factionId))) { // If there is factions, it must have faction with the id stored in faction dictionary error = ResponseCreateCharacterMessage.Error.InvalidData; } else { string characterId = GenericUtils.GetUniqueId(); PlayerCharacterData characterData = new PlayerCharacterData(); characterData.Id = characterId; characterData.SetNewPlayerCharacterData(characterName, dataId, entityId); characterData.FactionId = factionId; DeserializeCreateCharacterExtra(characterData, messageHandler.reader); CreateCharacterJob createCharacterJob = new CreateCharacterJob(Database, userPeerInfo.userId, characterData); createCharacterJob.Start(); yield return(StartCoroutine(createCharacterJob.WaitFor())); } ResponseCreateCharacterMessage responseMessage = new ResponseCreateCharacterMessage(); responseMessage.ackId = message.ackId; responseMessage.responseCode = error == ResponseCreateCharacterMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error; responseMessage.error = error; ServerSendPacket(connectionId, DeliveryMethod.ReliableOrdered, MMOMessageTypes.ResponseCreateCharacter, responseMessage); }
protected async UniTaskVoid HandleRequestCreateCharacter( RequestHandlerData requestHandler, RequestCreateCharacterMessage request, RequestProceedResultDelegate <ResponseCreateCharacterMessage> result) { long connectionId = requestHandler.ConnectionId; NetDataReader reader = requestHandler.Reader; ResponseCreateCharacterMessage.Error error = ResponseCreateCharacterMessage.Error.None; string characterName = request.characterName; int dataId = request.dataId; int entityId = request.entityId; int factionId = request.factionId; CentralUserPeerInfo userPeerInfo; FindCharacterNameResp findCharacterNameResp = await DbServiceClient.FindCharacterNameAsync(new FindCharacterNameReq() { CharacterName = characterName }); if (findCharacterNameResp.FoundAmount > 0) { error = ResponseCreateCharacterMessage.Error.CharacterNameAlreadyExisted; } else if (!userPeers.TryGetValue(connectionId, out userPeerInfo)) { error = ResponseCreateCharacterMessage.Error.NotLoggedin; } else if (string.IsNullOrEmpty(characterName) || characterName.Length < minCharacterNameLength) { error = ResponseCreateCharacterMessage.Error.TooShortCharacterName; } else if (characterName.Length > maxCharacterNameLength) { error = ResponseCreateCharacterMessage.Error.TooLongCharacterName; } else if (!GameInstance.PlayerCharacters.ContainsKey(dataId) || !GameInstance.PlayerCharacterEntities.ContainsKey(entityId) || (GameInstance.Factions.Count > 0 && !GameInstance.Factions.ContainsKey(factionId))) { // If there is factions, it must have faction with the id stored in faction dictionary error = ResponseCreateCharacterMessage.Error.InvalidData; } else { string characterId = GenericUtils.GetUniqueId(); PlayerCharacterData characterData = new PlayerCharacterData(); characterData.Id = characterId; characterData.SetNewPlayerCharacterData(characterName, dataId, entityId); characterData.FactionId = factionId; DeserializeCreateCharacterExtra(characterData, reader); await DbServiceClient.CreateCharacterAsync(new CreateCharacterReq() { UserId = userPeerInfo.userId, CharacterData = characterData.ToByteString() }); } // Response result.Invoke( error == ResponseCreateCharacterMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error, new ResponseCreateCharacterMessage() { error = error, }); }