Skip to content

Commit

Permalink
Platform: Flow Id equals project Id, Simulator: default button variab…
Browse files Browse the repository at this point in the history
…le name fix
  • Loading branch information
NizamLZ committed Sep 8, 2017
1 parent 2d5c154 commit ddc4343
Show file tree
Hide file tree
Showing 2 changed files with 708 additions and 699 deletions.
243 changes: 124 additions & 119 deletions ANAConversationPlatform/Helpers/ChatFlowBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,133 +11,138 @@

namespace ANAConversationPlatform.Helpers
{
public static class ChatFlowBuilder
{
public static ILogger Logger { get; set; }
public static class ChatFlowBuilder
{
public static ILogger Logger { get; set; }

public static List<ChatNode> Build(ChatFlowPack chatFlow)
{
try
{
var filter = Builders<ChatFlowPack>.Filter;
public static List<ChatNode> Build(ChatFlowPack chatFlow)
{
try
{
var filter = Builders<ChatFlowPack>.Filter;

var nodesList = new ConcurrentBag<ChatNode>(chatFlow.ChatNodes);
var content = new ConcurrentBag<BaseContent>(chatFlow.ChatContent);
Parallel.ForEach(nodesList, chatNode =>
{
try
{
foreach (Section section in chatNode.Sections)
FillSectionWithContent(section, content);
foreach (Button button in chatNode.Buttons)
button.ButtonText = content.GetFor(button)?.ButtonText;
}
catch (Exception ex)
{
Logger.LogError(new EventId((int)LoggerEventId.MONGO_HELPER_ERROR), ex, "RetrieveRecordsFromChatNode Error: {0}", ex.Message);
}
});
var nodesList = new ConcurrentBag<ChatNode>(chatFlow.ChatNodes);
var content = new ConcurrentBag<BaseContent>(chatFlow.ChatContent);
Parallel.ForEach(nodesList, chatNode =>
{
try
{
chatNode.FlowId = chatFlow.ProjectId;
foreach (Section section in chatNode.Sections)
FillSectionWithContent(section, content);
foreach (Button button in chatNode.Buttons)
{
button.ButtonText = content.GetFor(button)?.ButtonText;
if (string.IsNullOrWhiteSpace(button.ButtonName))
button.ButtonName = button.ButtonText; //For backward comparability
}
}
catch (Exception ex)
{
Logger.LogError(new EventId((int)LoggerEventId.MONGO_HELPER_ERROR), ex, "RetrieveRecordsFromChatNode Error: {0}", ex.Message);
}
});

var startNode = nodesList.FirstOrDefault(x => x.IsStartNode);
if (startNode != null) //If start chat node is present, move it up
{
var result = nodesList.Where(x => x != startNode).ToList();
result.Insert(0, startNode);
return result;
}
return nodesList.ToList();
}
catch (Exception ex)
{
Logger.LogError(new EventId((int)LoggerEventId.MONGO_HELPER_ERROR), ex, "RetrieveRecordsFromChatNode Error: {0}", ex.Message);
}
return null;
}
var startNode = nodesList.FirstOrDefault(x => x.IsStartNode);
if (startNode != null) //If start chat node is present, move it up
{
var result = nodesList.Where(x => x != startNode).ToList();
result.Insert(0, startNode);
return result;
}
return nodesList.ToList();
}
catch (Exception ex)
{
Logger.LogError(new EventId((int)LoggerEventId.MONGO_HELPER_ERROR), ex, "RetrieveRecordsFromChatNode Error: {0}", ex.Message);
}
return null;
}

private static readonly Random rand = new Random();
private static void FillSectionWithContent(Section emptySection, ConcurrentBag<BaseContent> contents)
{
switch (emptySection.SectionType)
{
case SectionTypeEnum.Image:
case SectionTypeEnum.Gif:
var imgSection = emptySection as ImageSection;
var imgContent = contents.GetFor(imgSection) as TitleCaptionSectionContent;
if (imgContent != null)
{
imgSection.Title = imgContent.Title;
imgSection.Caption = imgContent.Caption;
}
break;
private static readonly Random rand = new Random();
private static void FillSectionWithContent(Section emptySection, ConcurrentBag<BaseContent> contents)
{
switch (emptySection.SectionType)
{
case SectionTypeEnum.Image:
case SectionTypeEnum.Gif:
var imgSection = emptySection as ImageSection;
var imgContent = contents.GetFor(imgSection) as TitleCaptionSectionContent;
if (imgContent != null)
{
imgSection.Title = imgContent.Title;
imgSection.Caption = imgContent.Caption;
}
break;

case SectionTypeEnum.Text:
var textSection = emptySection as TextSection;
var textContent = contents.GetFor(textSection) as TextSectionContent;
if (textContent != null)
{
textSection.Text = textContent.SectionText;
if (textSection.DelayInMs == 0)
textSection.DelayInMs = Math.Min(Utils.Settings.MaxCapTimeTakenToType, textSection.Text.Length * (Utils.Settings.BaseTimeTakenToTypePerChar + rand.Next(0, Utils.Settings.VariableTimeTakenToTypePerChar)));
}
break;
case SectionTypeEnum.Text:
var textSection = emptySection as TextSection;
var textContent = contents.GetFor(textSection) as TextSectionContent;
if (textContent != null)
{
textSection.Text = textContent.SectionText;
if (textSection.DelayInMs == 0)
textSection.DelayInMs = Math.Min(Utils.Settings.MaxCapTimeTakenToType, textSection.Text.Length * (Utils.Settings.BaseTimeTakenToTypePerChar + rand.Next(0, Utils.Settings.VariableTimeTakenToTypePerChar)));
}
break;

case SectionTypeEnum.Audio:
var audioSection = emptySection as AudioSection;
var audioContent = contents.GetFor(audioSection) as TitleCaptionSectionContent;
if (audioContent != null)
{
audioSection.Title = audioContent.Title;
audioSection.Caption = audioContent.Caption;
}
break;
case SectionTypeEnum.Audio:
var audioSection = emptySection as AudioSection;
var audioContent = contents.GetFor(audioSection) as TitleCaptionSectionContent;
if (audioContent != null)
{
audioSection.Title = audioContent.Title;
audioSection.Caption = audioContent.Caption;
}
break;

case SectionTypeEnum.Video:
var videoSection = emptySection as VideoSection;
var videoContent = contents.GetFor(videoSection) as TitleCaptionSectionContent;
if (videoContent != null)
{
videoSection.Title = videoContent.Title;
videoSection.Caption = videoContent.Caption;
}
break;
case SectionTypeEnum.Video:
var videoSection = emptySection as VideoSection;
var videoContent = contents.GetFor(videoSection) as TitleCaptionSectionContent;
if (videoContent != null)
{
videoSection.Title = videoContent.Title;
videoSection.Caption = videoContent.Caption;
}
break;

case SectionTypeEnum.EmbeddedHtml:
var embeddedHtmlSection = emptySection as EmbeddedHtmlSection;
var embeddedHtmlContent = contents.GetFor(embeddedHtmlSection) as TitleCaptionSectionContent;
if (embeddedHtmlContent != null)
{
embeddedHtmlSection.Title = embeddedHtmlContent.Title;
embeddedHtmlSection.Caption = embeddedHtmlContent.Caption;
}
break;
case SectionTypeEnum.EmbeddedHtml:
var embeddedHtmlSection = emptySection as EmbeddedHtmlSection;
var embeddedHtmlContent = contents.GetFor(embeddedHtmlSection) as TitleCaptionSectionContent;
if (embeddedHtmlContent != null)
{
embeddedHtmlSection.Title = embeddedHtmlContent.Title;
embeddedHtmlSection.Caption = embeddedHtmlContent.Caption;
}
break;

case SectionTypeEnum.Carousel:
var carouselSection = emptySection as CarouselSection;
var carContent = contents.GetFor(carouselSection) as TitleCaptionSectionContent;
if (carContent != null)
{
carouselSection.Title = carContent.Title;
carouselSection.Caption = carContent.Caption;
}
if (carouselSection.Items != null)
foreach (var carItem in carouselSection.Items)
{
var content = contents.GetFor(carItem);
if (content != null)
{
carItem.Title = content.Title;
carItem.Caption = content.Caption;
}
if (carItem.Buttons != null)
foreach (var carBtn in carItem.Buttons)
{
var carBtnContent = contents.GetFor(carBtn);
carBtn.Text = carBtnContent?.ButtonText;
}
}
break;
}
}
case SectionTypeEnum.Carousel:
var carouselSection = emptySection as CarouselSection;
var carContent = contents.GetFor(carouselSection) as TitleCaptionSectionContent;
if (carContent != null)
{
carouselSection.Title = carContent.Title;
carouselSection.Caption = carContent.Caption;
}
if (carouselSection.Items != null)
foreach (var carItem in carouselSection.Items)
{
var content = contents.GetFor(carItem);
if (content != null)
{
carItem.Title = content.Title;
carItem.Caption = content.Caption;
}
if (carItem.Buttons != null)
foreach (var carBtn in carItem.Buttons)
{
var carBtnContent = contents.GetFor(carBtn);
carBtn.Text = carBtnContent?.ButtonText;
}
}
break;
}
}

}
}
}
Loading

0 comments on commit ddc4343

Please sign in to comment.