public Character HumanCharacterFromEntities(CharacterEntity characterEntity, CharacterCustomVisualsEntity visualsEntity) { if (characterEntity == null) { throw new ArgumentNullException(nameof(characterEntity)); } if (visualsEntity == null) { throw new ArgumentNullException(nameof(visualsEntity)); } return(new HumanCharacter(characterEntity.CharacterId, _CharacterService) { IsValid = true, Name = characterEntity.CharacterName, LastKnownPosition = new Vec3f(characterEntity.PositionX, characterEntity.PositionY, characterEntity.PositionZ), Rotation = new Angles(0, characterEntity.Rotation, 0), Template = _NpcDefList.GetByCode(characterEntity.TemplateName), World = _WorldList.GetWorldOrFallback(characterEntity.WorldName), HumanVisuals = new HumanCharacterVisuals { BodyMesh = visualsEntity.BodyMesh, BodyTex = visualsEntity.BodyTex, HeadMesh = visualsEntity.HeadMesh, HeadTex = visualsEntity.HeadTex, BodyWidth = visualsEntity.BodyWidth, Fatness = visualsEntity.Fatness, Voice = visualsEntity.Voice, } }); }
public Task <CharacterCreationResult> CreateHumanPlayerCharacterAsync(CharCreationInfo creationInfo) { if (creationInfo == null) { throw new ArgumentNullException(nameof(creationInfo)); } return(_CharacterService.CheckCharacterExistsAsync(creationInfo.Name).ContinueWith <CharacterCreationResult>(task => { //Validate the creation info because it comes directly from the client(we do not trust the client). if (!creationInfo.Validate(out string invalidProperty, out object value)) { _Log.Warn($"Character creation failed due to invalid {nameof(CharCreationInfo)}, invalid attribute is '{invalidProperty}' with value '{value}'"); return new CharacterCreationFailed(CharacterCreationFailure.InvalidCreationInfo); } //Check whether the name can be used for a character. if (!_NameValidator.IsValid(creationInfo.Name)) { return (CharacterCreationResult) new CharacterCreationFailed(CharacterCreationFailure.NameIsInvalid); } //The character does already exist. if (task.Result) { return (CharacterCreationResult) new CharacterCreationFailed(CharacterCreationFailure .AlreadyExists); } //Create the new character try { using (CharacterManagementContext db = _ContextFactory.Create()) { SpawnPoint spawn = _SpawnPointProvider.GetSpawnPoint(); //Add the character var characterEntity = new CharacterEntity { CharacterName = creationInfo.Name, PositionX = spawn.Point.X, PositionY = spawn.Point.Y, PositionZ = spawn.Point.Z, Rotation = spawn.Rotation.Yaw, WorldName = spawn.World.Path, TemplateName = _CharacterTemplateSelector.GetTemplate(creationInfo) }; db.Characters.Add(characterEntity); //Add the visuals of the character. var customVisuals = new CharacterCustomVisualsEntity { OwnerCharacter = characterEntity, BodyMesh = creationInfo.BodyMesh, BodyTex = creationInfo.BodyTex, HeadMesh = creationInfo.HeadMesh, HeadTex = creationInfo.HeadTex, Voice = creationInfo.Voice, Fatness = creationInfo.Fatness, BodyWidth = creationInfo.BodyWidth }; db.CustomVisuals.Add(customVisuals); //Save the changes. db.SaveChanges(); Character character = _CharacterBuilder.HumanCharacterFromEntities(characterEntity, customVisuals); //Invoke the character creation event(later so we can inform the client first) _Dispatcher.EnqueueAction(() => { _CharacterService.OnCharacterCreated(new CharacterCreatedArgs(character)); }); //Return information about successful character creation. return new CharacterCreationSuccess(character); } } catch (Exception e) { throw new DatabaseRequestException("Something went wrong while adding the new character to the database.", e); } })); }