public async Task <RetrieveEntityResult <IReadOnlyList <Transformation> > > GetTransformationsByPartAndSpeciesAsync ( Bodypart bodypart, Species species ) { var bodyparts = new List <Bodypart>(); if (bodypart.IsComposite()) { bodyparts.AddRange(bodypart.GetComposingParts()); } else { bodyparts.Add(bodypart); } var transformations = await _database.Transformations.AsQueryable() .Where(tf => bodyparts.Contains(tf.Part) && tf.Species.Name.ToLower().Equals(species.Name.ToLower())) .ToListAsync(); if (!transformations.Any()) { return(RetrieveEntityResult <IReadOnlyList <Transformation> > .FromError ( "No transformation found for that combination." )); } return(RetrieveEntityResult <IReadOnlyList <Transformation> > .FromSuccess(transformations)); }
/// <summary> /// Decomposes and removes the given composite bodypart. /// </summary> /// <param name="bodypart">The bodypart.</param> /// <returns>A shifting result which may or may not have succeeded.</returns> private async Task <Result <ShiftBodypartResult> > RemoveCompositeBodypartAsync(Bodypart bodypart) { var composingParts = bodypart.GetComposingParts(); var currentParagraphLength = 0; var messageBuilder = new StringBuilder(); void InsertRemovalMessage(string message) { messageBuilder.Append(message); if (!message.EndsWith(" ")) { messageBuilder.Append(' '); } if (currentParagraphLength > 240) { messageBuilder.AppendLine(); messageBuilder.AppendLine(); currentParagraphLength = 0; } currentParagraphLength += message.Length; } foreach (var composingPart in composingParts) { if (composingPart.IsComposite()) { var shiftResult = await RemoveCompositeBodypartAsync(composingPart); if (!shiftResult.IsSuccess || shiftResult.Entity.Action == ShiftBodypartAction.Nothing) { continue; } InsertRemovalMessage(shiftResult.Entity.ShiftMessage); continue; } if (composingPart.IsChiral()) { var performLeftShift = await RemoveBodypartAsync(composingPart, Chirality.Left); if (!performLeftShift.IsSuccess) { return(performLeftShift); } var performRightShift = await RemoveBodypartAsync(composingPart, Chirality.Right); if (!performRightShift.IsSuccess) { return(performRightShift); } var leftShift = performLeftShift.Entity; var rightShift = performRightShift.Entity; switch (leftShift.Action) { // There's a couple of cases here for us to deal with. // 1: both parts were removed // 2: one part was removed // 3: no changes were made case ShiftBodypartAction.Nothing when rightShift.Action == ShiftBodypartAction.Nothing: { // No change, keep moving continue; } case ShiftBodypartAction.Remove when rightShift.Action == ShiftBodypartAction.Remove: { var uniformShiftMessage = await GetUniformRemoveMessageAsync(composingPart); InsertRemovalMessage(uniformShiftMessage); continue; } } if (leftShift.Action != ShiftBodypartAction.Nothing) { InsertRemovalMessage ( await BuildMessageFromResultAsync(leftShift, composingPart, Chirality.Left) ); } if (rightShift.Action != ShiftBodypartAction.Nothing) { InsertRemovalMessage ( await BuildMessageFromResultAsync(rightShift, composingPart, Chirality.Right) ); } } else { var performSimpleShift = await RemoveBodypartAsync ( composingPart, Chirality.Center ); if (!performSimpleShift.IsSuccess) { return(performSimpleShift); } var simpleShift = performSimpleShift.Entity; if (simpleShift.Action != ShiftBodypartAction.Nothing) { InsertRemovalMessage ( await BuildMessageFromResultAsync(simpleShift, composingPart, Chirality.Center) ); } } } if (messageBuilder.Length == 0) { return(new ShiftBodypartResult ( await GetNoChangeMessageAsync(bodypart), ShiftBodypartAction.Nothing )); } return(new ShiftBodypartResult(messageBuilder.ToString(), ShiftBodypartAction.Shift)); }
private async Task <ShiftBodypartResult> ShiftCompositeBodypartAsync(Bodypart bodypart) { var composingParts = bodypart.GetComposingParts(); var currentParagraphLength = 0; var messageBuilder = new StringBuilder(); void InsertShiftMessage(string message) { messageBuilder.Append(message); if (!message.EndsWith(" ")) { messageBuilder.Append(" "); } if (currentParagraphLength > 240) { messageBuilder.AppendLine(); messageBuilder.AppendLine(); currentParagraphLength = 0; } currentParagraphLength += message.Length; } foreach (var composingPart in composingParts) { if (composingPart.IsComposite()) { var shiftResult = await ShiftCompositeBodypartAsync(composingPart); if (!shiftResult.IsSuccess || shiftResult.Action == ShiftBodypartAction.Nothing) { continue; } InsertShiftMessage(shiftResult.ShiftMessage); continue; } if (composingPart.IsChiral()) { var leftShift = await ShiftBodypartAsync(composingPart, Chirality.Left); var rightShift = await ShiftBodypartAsync(composingPart, Chirality.Right); // There's a couple of cases here for us to deal with. // 1: both parts were shifted // 2: one part was shifted // 3: one part was shifted and one was added // 4: both parts were added // 5: no changes were made if (leftShift.Action == ShiftBodypartAction.Nothing && rightShift.Action == ShiftBodypartAction.Nothing) { // No change, keep moving continue; } if (leftShift.Action == ShiftBodypartAction.Shift && rightShift.Action == ShiftBodypartAction.Shift) { var uniformShiftMessage = await GetUniformShiftMessageAsync(composingPart); InsertShiftMessage(uniformShiftMessage); continue; } if (leftShift.Action == ShiftBodypartAction.Add && rightShift.Action == ShiftBodypartAction.Add) { var uniformGrowMessage = await GetUniformAddMessageAsync(composingPart); InsertShiftMessage(uniformGrowMessage); continue; } if (leftShift.Action != ShiftBodypartAction.Nothing) { InsertShiftMessage ( await BuildMessageFromResultAsync(leftShift, composingPart, Chirality.Left) ); } if (rightShift.Action != ShiftBodypartAction.Nothing) { InsertShiftMessage ( await BuildMessageFromResultAsync(rightShift, composingPart, Chirality.Right) ); } } else { var simpleShiftResult = await ShiftBodypartAsync ( composingPart, Chirality.Center ); if (simpleShiftResult.Action != ShiftBodypartAction.Nothing) { InsertShiftMessage ( await BuildMessageFromResultAsync(simpleShiftResult, composingPart, Chirality.Center) ); } } } if (messageBuilder.Length == 0) { return(ShiftBodypartResult.FromSuccess ( await GetNoChangeMessageAsync(bodypart), ShiftBodypartAction.Nothing )); } return(ShiftBodypartResult.FromSuccess(messageBuilder.ToString(), ShiftBodypartAction.Shift)); }