Skip to content

Commit

Permalink
building the state machine wrapper for the Valkyrie core
Browse files Browse the repository at this point in the history
  • Loading branch information
EarliestFall988 committed Nov 17, 2023
1 parent dd1c29d commit 18df4e8
Show file tree
Hide file tree
Showing 9 changed files with 411 additions and 103 deletions.
73 changes: 65 additions & 8 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
var apiKey = "some api key";
var valkApiKey = "some key";

StateMachinesController? stateMachinesController = new StateMachinesController();

long minuteCountWaitTime = 5;

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
Expand Down Expand Up @@ -48,7 +52,6 @@
return "incorrect api key";
}


if (string.IsNullOrEmpty(instructionId))
{
context.Response.StatusCode = 400;
Expand All @@ -60,16 +63,70 @@
ValkyrieAPIKey = valkApiKey
};



var response = await ValkyireServerController.TryGetInstructions(instructionId);

return response;
if (response.result == false)
{
context.Response.StatusCode = 500;
return response.content;
}

string id = Guid.NewGuid().ToString();

if (!stateMachinesController.IsTicking)
{
stateMachinesController.Boot();
}


//Debug.WriteLine(response.content);


stateMachinesController.AddMachine(id, response.content);

(bool complete, string result) status = (false, "Not completed - state machine took too long.");

long tick = 0;


//using (StreamReader reader = new StreamReader(context.Request.Body))
//{
// string body = await reader.ReadToEndAsync();
// context.Response.StatusCode = 200;
// return body;
//}
var totalTime = 100; //(60000 / 2) * minuteCountWaitTime;

while (!status.complete && tick < totalTime)
{

Debug.WriteLine("status: " + status.complete + " " + tick);

status = stateMachinesController.HandleStatus(id);

Thread.Sleep(100); // wait for the machine to complete
tick++;
}

if (stateMachinesController.GetMachines.Length == 0 && stateMachinesController.IsTicking)
{
stateMachinesController.Kill();
}

if (!status.complete && tick >= totalTime)
{
context.Response.StatusCode = 408;
stateMachinesController.KillStateMachineProcess(id); //remove the machine from the list of machines to process

return status.result;
}

if (status.complete)
{
context.Response.StatusCode = 200;
return status.result;
}
else
{
context.Response.StatusCode = 500;
return status.result;
}
});

app.MapGet("/weatherforecast", () =>
Expand Down
5 changes: 5 additions & 0 deletions State Machine/Function Definitions/ContinueFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public ContinueFunction()
protected override void DefineFunction()
{
Name = "Continue";
Function = () =>
{
Console.WriteLine(Name);
return 0;
};
}
}
}
7 changes: 4 additions & 3 deletions State Machine/Function Definitions/ExitQuestionFunction.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

Expand All @@ -22,13 +23,13 @@ protected override void DefineFunction()
Func<int> ExitQuestion = () =>
{

Console.WriteLine("Would you like to exit?");
string? answer = Console.ReadLine();
Debug.WriteLine("Would you like to exit?");
string? answer = "yes"; // Debug.ReadLine();

if (answer == null || answer.Trim() == "")
return 0;

// Console.WriteLine("You typed in " + number);
// Debug.WriteLine("You typed in " + number);

if (answer.Trim().ToLower() == "yes" || answer.Trim().ToLower() == "y") // exit the program
{
Expand Down
11 changes: 6 additions & 5 deletions State Machine/Function Definitions/GreetFunction.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

Expand Down Expand Up @@ -29,16 +30,16 @@ protected override void DefineFunction()
{

string? result = "";
Console.WriteLine("Hello!");
Debug.WriteLine("Hello!");

while (result == null || result == string.Empty)
{
Console.WriteLine("What is your Name?");
result = Console.ReadLine();
Debug.WriteLine("What is your Name?");
result = "John Doe"; // Console.ReadLine();

if (result == null || result == string.Empty)
{
Console.WriteLine("That's not your name!");
Debug.WriteLine("That's not your name!");
}

if (result?.ToLower().Trim() == "exit")
Expand All @@ -52,7 +53,7 @@ protected override void DefineFunction()

// if (!success)
// {
// Console.WriteLine("could not write to store");
// Debug.WriteLine("could not write to store");
// return -1;
// }
}
Expand Down
11 changes: 6 additions & 5 deletions State Machine/Function Definitions/GuessFunction.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

Expand Down Expand Up @@ -38,8 +39,8 @@ protected override void DefineFunction()
string name = Parameters["name"].GetText();
int guessNumber = Parameters["guess"].GetInt();

Console.WriteLine($"Okay {name}, Guess a number");
string? number = Console.ReadLine();
Debug.WriteLine($"Okay {name}, Guess a number");
string? number = guessNumber.ToString(); // Console.ReadLine();

if (number == null || number.Trim() == "")
return 0;
Expand All @@ -54,19 +55,19 @@ protected override void DefineFunction()

if (!result)
{
Console.WriteLine("That's not a number! Try again!");
Debug.WriteLine("That's not a number! Try again!");
return 0;
}


if (guessNumber == numberInt)
{
Console.WriteLine("Correct.");
Debug.WriteLine("Correct.");
return 1;
}
else
{
Console.WriteLine("Incorrect. Try Again");
Debug.WriteLine("Incorrect. Try Again");
return 0;
}
};
Expand Down
3 changes: 2 additions & 1 deletion State Machine/StateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void AddState(State state)
States.Add(state);
}

public string Result { get; set; } = "";

private void RunStateMachine()
{
Expand Down Expand Up @@ -273,7 +274,7 @@ public void Boot()
}

Console.WriteLine("\n\n\t>Exiting...");

}
}
}
Loading

0 comments on commit 18df4e8

Please sign in to comment.