Skip to content

Commit

Permalink
replacing the keytypedef class with a proper type handling system. Al…
Browse files Browse the repository at this point in the history
…so adding some requested metadata (like var IO)
  • Loading branch information
EarliestFall988 committed Dec 4, 2023
1 parent 341650a commit 7b7b7f4
Show file tree
Hide file tree
Showing 19 changed files with 533 additions and 220 deletions.
35 changes: 19 additions & 16 deletions State Machine/Function Definitions/GreetFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ protected override void DefineFunction()

Name = "GreetUser";

ExpectedParameters = new Dictionary<string, ReferenceTuple>()
ExpectedParameters = new Dictionary<string, Parameter>()
{
{ "name", new ReferenceTuple(StateMachineVariableType.Text, false) }
{ "name", new Parameter(StateMachineVariableType.Text) }
};

Func<int> GreetUser = () =>
Expand All @@ -37,25 +37,28 @@ protected override void DefineFunction()
Debug.WriteLine("What is your Name?");
result = "John Doe"; // Console.ReadLine();

if (result == null || result == string.Empty)
{
Debug.WriteLine("That's not your name!");
}
var nameV = Parameters["name"];

if (result?.ToLower().Trim() == "exit")
if (nameV is VariableDefinition<string> name)
{
return -1;
}

// bool success = stateMachine.WriteValue(0, 0, result?.Replace(',', '\'') ?? "");
if (result == null || result == string.Empty)
{
Debug.WriteLine("That's not your name!");
}

if (result?.ToLower().Trim() == "exit")
{
return -1;
}

Parameters["name"].SetValue(result?.Replace(',', '\'') ?? "");
name.Value = result?.Replace(',', '\'') ?? "";

// if (!success)
// {
// Debug.WriteLine("could not write to store");
// return -1;
// }
}
else
{
return -1;
}
}

return 1;
Expand Down
17 changes: 12 additions & 5 deletions State Machine/Function Definitions/GuessFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,25 @@ protected override void DefineFunction()
// StateMachineVariableType.Integer // guess
// };

ExpectedParameters = new Dictionary<string, ReferenceTuple>()
ExpectedParameters = new Dictionary<string, Parameter>()
{
{ "name", new ReferenceTuple(StateMachineVariableType.Text, false) },
{ "guess", new ReferenceTuple(StateMachineVariableType.Integer, false) }
{ "name", new Parameter(StateMachineVariableType.Text) },
{ "guess", new Parameter(StateMachineVariableType.Integer) }
};

Name = "Guess";

Function = () =>
{

string name = Parameters["name"].GetText();
int guessNumber = Parameters["guess"].GetInt();
VariableDefinition<string>? nameVarCheck = Parameters["name"] as VariableDefinition<string>;
VariableDefinition<int>? guessNumberVarCheck = Parameters["guess"] as VariableDefinition<int>;

if (nameVarCheck == null || guessNumberVarCheck == null)
return -1;

var name = nameVarCheck.Value;
var guessNumber = guessNumberVarCheck.Value;

Debug.WriteLine($"Okay {name}, Guess a number");
string? number = guessNumber.ToString(); // Console.ReadLine();
Expand Down Expand Up @@ -70,6 +76,7 @@ protected override void DefineFunction()
Debug.WriteLine("Incorrect. Try Again");
return 0;
}

};
}
}
Expand Down
26 changes: 17 additions & 9 deletions State Machine/Function Definitions/Math/AddNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public AddNumber()

void Setup()
{
ExpectedParameters = new Dictionary<string, ReferenceTuple>()
ExpectedParameters = new Dictionary<string, Parameter>()
{
{ "a", new ReferenceTuple(StateMachineVariableType.Decimal, false) },
{ "b", new ReferenceTuple(StateMachineVariableType.Decimal, false) },
{ "out", new ReferenceTuple(StateMachineVariableType.Decimal, false, VariableIO.Out) }
{ "a", new Parameter(StateMachineVariableType.Decimal) },
{ "b", new Parameter(StateMachineVariableType.Decimal) },
{ "out", new Parameter(StateMachineVariableType.Decimal, VariableIO.Out) }
};
}

Expand All @@ -26,14 +26,22 @@ protected override void DefineFunction()
Name = nameof(AddNumber);
Function = () =>
{
var a = Parameters["a"].GetDecimal();
var b = Parameters["b"].GetDecimal();
var x = Parameters["a"];
var y = Parameters["b"];

Debug.WriteLine($"{a} + {b} = {a + b}");
var z = Parameters["out"];

Parameters["out"].SetValue(a + b);
if (x is VariableDefinition<decimal> a && y is VariableDefinition<decimal> b && z is VariableDefinition<decimal> result)
{

return 1;
Debug.WriteLine($"{a} + {b} = {a.Value + b.Value}");

result.Value = a.Value + b.Value;

return 1;
}

return -1;
};
}

Expand Down
36 changes: 23 additions & 13 deletions State Machine/Function Definitions/Math/DivideNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public DivideNumber()

void Setup()
{
ExpectedParameters = new Dictionary<string, ReferenceTuple>()
ExpectedParameters = new Dictionary<string, Parameter>()
{
{ "a", new ReferenceTuple(StateMachineVariableType.Decimal, false) },
{ "b", new ReferenceTuple(StateMachineVariableType.Decimal, false) },
{ "out", new ReferenceTuple(StateMachineVariableType.Decimal, false, VariableIO.Out) }
{ "a", new Parameter(StateMachineVariableType.Decimal) },
{ "b", new Parameter(StateMachineVariableType.Decimal) },
{ "out", new Parameter(StateMachineVariableType.Decimal, VariableIO.Out) }
};
}

Expand All @@ -30,20 +30,30 @@ protected override void DefineFunction()
Name = nameof(DivideNumber);
Function = () =>
{
var a = Parameters["a"].GetDecimal();
var b = Parameters["b"].GetDecimal();
var x = Parameters["a"];
var y = Parameters["b"];

if (b == 0)
var z = Parameters["out"];

if (x is VariableDefinition<decimal> aV && y is VariableDefinition<decimal> bV && z is VariableDefinition<decimal> resultV)
{
Debug.WriteLine("Cannot divide by zero");
return -1;
}
var b = bV.Value;
var a = aV.Value;

if (b == 0)
{
Debug.WriteLine("Cannot divide by zero");
return -1;
}

Debug.WriteLine($"{a} / {b} = {a / b}");
Debug.WriteLine($"{a} / {b} = {a / b}");

Parameters["out"].SetValue(a / b);
resultV.Value = a / b;

return 1;
}

return 1;
return -1;
};
}

Expand Down
28 changes: 19 additions & 9 deletions State Machine/Function Definitions/Math/MultiplyNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public MultiplyNumber()

void Setup()
{
ExpectedParameters = new Dictionary<string, ReferenceTuple>()
ExpectedParameters = new Dictionary<string, Parameter>()
{
{ "a", new ReferenceTuple(StateMachineVariableType.Decimal, false) },
{ "b", new ReferenceTuple(StateMachineVariableType.Decimal, false) },
{ "out", new ReferenceTuple(StateMachineVariableType.Decimal, false, VariableIO.Out) }
{ "a", new Parameter(StateMachineVariableType.Decimal) },
{ "b", new Parameter(StateMachineVariableType.Decimal) },
{ "out", new Parameter(StateMachineVariableType.Decimal, VariableIO.Out) }
};
}

Expand All @@ -29,14 +29,24 @@ protected override void DefineFunction()
Name = nameof(MultiplyNumber);
Function = () =>
{
var a = Parameters["a"].GetDecimal();
var b = Parameters["b"].GetDecimal();
var x = Parameters["a"];
var y = Parameters["b"];

Debug.WriteLine($"{a} * {b} = {a * b}");
var z = Parameters["out"];

Parameters["out"].SetValue(a * b);
if (x is VariableDefinition<decimal> aV && y is VariableDefinition<decimal> bV && z is VariableDefinition<decimal> resultV)
{
var b = bV.Value;
var a = aV.Value;

return 1;
Debug.WriteLine($"{a} * {b} = {a * b}");

resultV.Value = a * b;

return 1;
}

return -1;
};
}
}
Expand Down
31 changes: 21 additions & 10 deletions State Machine/Function Definitions/Math/Pow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public PowNumber()

void Setup()
{
ExpectedParameters = new Dictionary<string, ReferenceTuple>()
ExpectedParameters = new Dictionary<string, Parameter>()
{
{ "a", new ReferenceTuple(StateMachineVariableType.Decimal, false) },
{ "b", new ReferenceTuple(StateMachineVariableType.Decimal, false) },
{ "out", new ReferenceTuple(StateMachineVariableType.Decimal, false, VariableIO.Out) }
{ "a", new Parameter(StateMachineVariableType.Decimal) },
{ "b", new Parameter(StateMachineVariableType.Decimal) },
{ "out", new Parameter(StateMachineVariableType.Decimal, VariableIO.Out) }
};
}

Expand All @@ -29,16 +29,27 @@ protected override void DefineFunction()
Name = nameof(PowNumber);
Function = () =>
{
var a = Parameters["a"].GetDecimal();
var b = Parameters["b"].GetDecimal();
var x = Parameters["a"];
var y = Parameters["b"];

var pow = Math.Pow((double)a, (double)b);
var z = Parameters["out"];

Debug.WriteLine($"{a} to the power of {b} = {pow}");
if (x is VariableDefinition<decimal> aV && y is VariableDefinition<decimal> bV && z is VariableDefinition<decimal> resultV)
{

Parameters["out"].SetValue((decimal)pow);
var b = bV.Value;
var a = aV.Value;

var powResult = Math.Pow((double)a, (double)b);

Debug.WriteLine($"{a} to the power of {b} = {powResult}");

resultV.Value = (decimal)powResult;
return 1;
}

return -1;

return 1;
};
}
}
Expand Down
29 changes: 20 additions & 9 deletions State Machine/Function Definitions/Math/SubtractNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public SubtractNumber()

void Setup()
{
ExpectedParameters = new Dictionary<string, ReferenceTuple>()
ExpectedParameters = new Dictionary<string, Parameter>()
{
{ "a", new ReferenceTuple(StateMachineVariableType.Decimal, false) },
{ "b", new ReferenceTuple(StateMachineVariableType.Decimal, false) },
{ "out", new ReferenceTuple(StateMachineVariableType.Decimal, false, VariableIO.Out) }
{ "a", new Parameter(StateMachineVariableType.Decimal) },
{ "b", new Parameter(StateMachineVariableType.Decimal) },
{ "out", new Parameter(StateMachineVariableType.Decimal, VariableIO.Out) }
};
}

Expand All @@ -29,14 +29,25 @@ protected override void DefineFunction()
Name = nameof(SubtractNumber);
Function = () =>
{
var a = Parameters["a"].GetDecimal();
var b = Parameters["b"].GetDecimal();
var x = Parameters["a"];
var y = Parameters["b"];

Debug.WriteLine($"{a} + {b} = {a - b}");
var z = Parameters["out"];

Parameters["out"].SetValue(a - b);
if (x is VariableDefinition<decimal> aV && y is VariableDefinition<decimal> bV && z is VariableDefinition<decimal> resultV)
{

var b = bV.Value;
var a = aV.Value;

Debug.WriteLine($"{a} + {b} = {a - b}");

resultV.Value = a - b;
return 1;
}

return -1;

return 1;
};
}
}
Expand Down
34 changes: 17 additions & 17 deletions State Machine/Function Definitions/SetRandomNumberFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,43 @@

namespace Avalon
{
/// <summary>
/// Set a random number
/// </summary>
public sealed class SetRandomNumberFunction : FunctionDefinition
{

public SetRandomNumberFunction()
{
Setup();
DefineFunction();
}

protected override void DefineFunction()
void Setup()
{

Name = "SetRandomNumber";
Description = "Set a random number within the range of min and max";

ExpectedParameters = new Dictionary<string, ReferenceTuple>()
ExpectedParameters = new Dictionary<string, Parameter>()
{
{ "valueToChange", new ReferenceTuple(StateMachineVariableType.Integer, false) },
{ "min", new ReferenceTuple(StateMachineVariableType.Integer, false) },
{ "max", new ReferenceTuple(StateMachineVariableType.Integer, false) }
{ "valueToChange", new Parameter(StateMachineVariableType.Integer) },
{ "min", new Parameter(StateMachineVariableType.Integer) },
{ "max", new Parameter(StateMachineVariableType.Integer) }
};
}

protected override void DefineFunction()
{

Func<int> func = () =>
{
int valueToChange = Parameters["valueToChange"].GetInt();

int min = Parameters["min"].GetInt();

int max = Parameters["max"].GetInt();

Random random = new Random();
int randomNumber = random.Next(Get<int>("min"), Get<int>("max"));
bool result = TrySet("valueToChange", randomNumber);

int randomNumber = random.Next(min, max);

// Console.WriteLine($"The random number is {randomNumber}");

Parameters["valueToChange"].SetValue(randomNumber.ToString());

return 1;
return result == true ? 1 : -1;
};

Function = func;
Expand Down
Loading

0 comments on commit 7b7b7f4

Please sign in to comment.