Skip to content

Commit

Permalink
Enabled async methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ydinkov committed Aug 12, 2023
1 parent 2870989 commit bb3e64a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 31 deletions.
84 changes: 62 additions & 22 deletions Dialogative/DialogueTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,46 @@ public DialogueTree(string sourceString, Func<ICollection<string>> mutations, Ac
return null;
}

public Line? Talk(Option? option = null)
{
if (_reset)
{
_reset = false;
return GetCurrentLine();
}

var shouldHaveChosenAnOption = CurrentBeat!.Predicate.Bool(() => Declarations, Mutations)
? CurrentBeat.Success.Options?.Any() ?? false
: CurrentBeat.Failure.Options?.Any() ?? false;

if (shouldHaveChosenAnOption && option is null)
{
Reset();
//Exit
return null;
}
else
{
if (option != null)
{
if (string.IsNullOrWhiteSpace(option.Next)) return null; //if option doesnt have next, then break
CurrentBeat = Model.Scenes[option.Next]?.Beats.First(); // else change beat to next
}
else
{
CurrentBeat =
CurrentBeat?.GetNext(() => Declarations, Mutations,
Model.Scenes)!; //otherwise keep getting next line
}
}

//If you have something to say, say it
var somethingToSay = GetCurrentLine();
if (somethingToSay != null) return somethingToSay;
//otherwise reset and end the conversation
Reset();
return null;
}
private async Task<Line?> GetCurrentLineAsync()
{
if (CurrentBeat is null)return null;
Expand All @@ -101,28 +141,28 @@ public DialogueTree(string sourceString, Func<ICollection<string>> mutations, Ac
};
}

//private Line? GetCurrentLine()
//{
// if (CurrentBeat is null)return null;
// var lineModel = CurrentBeat?.GetLine(() => Declarations, Mutations)!;
// var q = new ConcurrentQueue<Option>();
//
// foreach (var x in lineModel.Options)
// {
// var predicate = x?.Predicate ?? string.Empty;
// var shouldShowOption = predicate.Bool(() => Declarations, Mutations);
// if (x != null && (shouldShowOption || string.IsNullOrWhiteSpace(predicate) ) )
// q.Enqueue(x);
// }
// _onEventTrigger(lineModel.Trigger);
// return new Line
// {
// Text = lineModel.Text.Choice(_randomBarkChooser),
// Mood = lineModel.Mood,
// Sound = lineModel.Sound,
// Options = q.ToArray()
// };
//}
private Line? GetCurrentLine()
{
if (CurrentBeat is null)return null;
var lineModel = CurrentBeat?.GetLine(() => Declarations, Mutations)!;
var q = new ConcurrentQueue<Option>();

foreach (var x in lineModel.Options)
{
var predicate = x?.Predicate ?? string.Empty;
var shouldShowOption = predicate.Bool(() => Declarations, Mutations);
if (x != null && (shouldShowOption || string.IsNullOrWhiteSpace(predicate) ) )
q.Enqueue(x);
}
_onEventTrigger(lineModel.Trigger);
return new Line
{
Text = lineModel.Text.Choice(_randomBarkChooser),
Mood = lineModel.Mood,
Sound = lineModel.Sound,
Options = q.ToArray()
};
}

private void Reset()
{
Expand Down
18 changes: 9 additions & 9 deletions Dialogative/models/BeatModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class BeatModel
internal async Task<LineModel> GetLineAsync(Func<ICollection<string>> delarations,Func<ICollection<string>> mutations) =>
await Predicate.BoolAsync(delarations,mutations) ? Success : Failure;

//internal LineModel GetLine(Func<ICollection<string>> delarations,Func<ICollection<string>> mutations) =>
// Predicate.Bool(delarations,mutations) ? Success : Failure;
internal LineModel GetLine(Func<ICollection<string>> delarations,Func<ICollection<string>> mutations) =>
Predicate.Bool(delarations,mutations) ? Success : Failure;

internal async Task<BeatModel?> GetNextAsync(Func<ICollection<string>> delarations,Func<ICollection<string>> mutations, Dictionary<string, SceneModel> scenes)
{
Expand All @@ -24,13 +24,13 @@ internal async Task<LineModel> GetLineAsync(Func<ICollection<string>> delaration
return nextInLine == null ? GetNextInScene() : scenes[nextInLine].Beats.First();
}

//internal BeatModel? GetNext(Func<ICollection<string>> delarations,Func<ICollection<string>> mutations, Dictionary<string, SceneModel> scenes)
//{
// var nextInLine = (GetLine(delarations,mutations)).Next;
// return nextInLine == "exit" ? null :
// //If no next line, then get next in scene
// scenes[nextInLine].Beats.First();
//}
internal BeatModel? GetNext(Func<ICollection<string>> delarations,Func<ICollection<string>> mutations, Dictionary<string, SceneModel> scenes)
{
var nextInLine = (GetLine(delarations,mutations)).Next;
return nextInLine == "exit" ? null :
//If no next line, then get next in scene
scenes[nextInLine].Beats.First();
}

private BeatModel? GetNextInScene()
{
Expand Down

0 comments on commit bb3e64a

Please sign in to comment.