Skip to content

Commit

Permalink
add /variables POST endpoint, and GSI functions to get them
Browse files Browse the repository at this point in the history
  • Loading branch information
Aytackydln committed Dec 24, 2024
1 parent e993a32 commit fef5829
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.IO;
using System.Net;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using AuroraRgb.Nodes;
using AuroraRgb.Profiles;
using Common.Utils;

Expand All @@ -31,6 +33,9 @@ public sealed partial class AuroraHttpListener
private readonly HttpListener _netListener;
private readonly int _port;
private readonly SingleConcurrentThread _readThread;
private readonly SingleConcurrentThread _readThread2;

private SingleConcurrentThread _nextReadThread;

public IGameState CurrentGameState
{
Expand Down Expand Up @@ -62,7 +67,10 @@ public AuroraHttpListener(int port)
_cancellationTokenSource = new CancellationTokenSource();
_cancellationToken = _cancellationTokenSource.Token;

_readThread = new SingleConcurrentThread("Http Read Thread", AsyncRead, ExceptionCallback);
_readThread = new SingleConcurrentThread("Http Read Thread 1", AsyncRead1, ExceptionCallback);
_readThread2 = new SingleConcurrentThread("Http Read Thread 2", AsyncRead2, ExceptionCallback);

_nextReadThread = _readThread;
}

private static void ExceptionCallback(object? sender, SingleThreadExceptionEventArgs eventArgs)
Expand Down Expand Up @@ -100,7 +108,24 @@ public bool Start()
return true;
}

private async Task AsyncRead()
private async Task AsyncRead1()
{
try
{
var context = await _netListener.GetContextAsync().WaitAsync(_cancellationToken);
if (!_cancellationToken.IsCancellationRequested)
{
_readThread2.Trigger();
}
ProcessContext(context);
}
catch (TaskCanceledException)
{
// stop
}
}

private async Task AsyncRead2()
{
try
{
Expand Down Expand Up @@ -155,6 +180,12 @@ private void ProcessContext(HttpListenerContext context)
return null;
}

if (path.StartsWith("/variables"))
{
ProcessVariables(json);
return null;
}

if (!path.StartsWith("/gameState/"))
{
return new NewtonsoftGameState(json);
Expand All @@ -177,6 +208,39 @@ private void ProcessContext(HttpListenerContext context)
return new NewtonsoftGameState(json, false);
}

private static void ProcessVariables(string json)
{
var jsonNode = JsonSerializer.Deserialize<JsonElement>(json);
switch (jsonNode.ValueKind)
{
case JsonValueKind.Object:
foreach (var property in jsonNode.EnumerateObject())
{
var key = property.Name;
var value = property.Value;
switch (value.ValueKind)
{
case JsonValueKind.String:
AuroraVariables.Instance.Strings[key] = value.GetString() ?? string.Empty;
break;
case JsonValueKind.Number:
AuroraVariables.Instance.Numbers[key] = value.GetDouble();
break;
case JsonValueKind.True:
case JsonValueKind.False:
AuroraVariables.Instance.Booleans[key] = value.GetBoolean();
break;
case JsonValueKind.Null:
AuroraVariables.Instance.Strings.Remove(key);
AuroraVariables.Instance.Numbers.Remove(key);
AuroraVariables.Instance.Booleans.Remove(key);
break;
}
}
break;
}
}

private static string ReadContent(HttpListenerContext context, out string path)
{
var request = context.Request;
Expand Down
12 changes: 12 additions & 0 deletions Project-Aurora/Project-Aurora/Nodes/AuroraVariables.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;

namespace AuroraRgb.Nodes;

public class AuroraVariables
{
public static readonly AuroraVariables Instance = new();

public Dictionary<string, bool> Booleans { get; } = new(8);
public Dictionary<string, double> Numbers { get; } = new(8);
public Dictionary<string, string> Strings { get; } = new(8);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using AuroraRgb.Nodes;
using AuroraRgb.Profiles;
using AuroraRgb.Utils;

namespace AuroraRgb.Settings.Overrides.Logic.Boolean;

[Evaluatable("Boolean Global Variable", category: EvaluatableCategory.Global)]
public class Boolean_Variable : Evaluatable<bool>
{
public Evaluatable<bool> DefaultValue { get; set; } = new BooleanConstant();
public Evaluatable<string> VariableName { get; set; } = new StringConstant();

public Boolean_Variable() { }

public Boolean_Variable(Evaluatable<string> variableName, Evaluatable<bool> defaultValue) {
VariableName = variableName;
DefaultValue = defaultValue;
}

protected override bool Execute(IGameState gameState)
{
var key = VariableName.Evaluate(gameState);
var defaultValue = DefaultValue.Evaluate(gameState);
return AuroraVariables.Instance.Booleans.GetValueOrDefault(key, defaultValue);
}

public override Visual GetControl()
{
return new StackPanel { Orientation = Orientation.Horizontal }
.WithChild(new TextBlock { Text = "Name", FontWeight = FontWeights.Bold, Margin = new Thickness(2, 0, 6, 0), VerticalAlignment = VerticalAlignment.Center })
.WithChild(new Control_EvaluatablePresenter { EvalType = typeof(string) }
.WithBinding(Control_EvaluatablePresenter.ExpressionProperty, new Binding(nameof(VariableName)) { Source = this, Mode = BindingMode.TwoWay }))
.WithChild(new TextBlock { Text = "Default Value", FontWeight = FontWeights.Bold, Margin = new Thickness(2, 0, 6, 0), VerticalAlignment = VerticalAlignment.Center })
.WithChild(new Control_EvaluatablePresenter { EvalType = typeof(bool) }
.WithBinding(Control_EvaluatablePresenter.ExpressionProperty, new Binding(nameof(DefaultValue)) { Source = this, Mode = BindingMode.TwoWay }));
}

public override Evaluatable<bool> Clone()
{
return new Boolean_Variable(VariableName, DefaultValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public EvaluatableAttribute(string name, EvaluatableCategory category = Evaluata
public EvaluatableCategory Category { get; }

/// <summary>Gets the description of the category as a string.</summary>
public string CategoryStr => EnumUtils.GetDescription(Category);
public string CategoryStr => Category.GetDescription();
}

public enum EvaluatableCategory {
Expand All @@ -35,5 +35,6 @@ public enum EvaluatableCategory {
[Description("Input")] Input,
[Description("Misc.")] Misc,
[Description("Maths")] Maths,
[Description("String")] String
[Description("String")] String,
[Description("Global Variable")] Global,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using AuroraRgb.Nodes;
using AuroraRgb.Profiles;
using AuroraRgb.Utils;

namespace AuroraRgb.Settings.Overrides.Logic.Number;

[Evaluatable("Number Global Variable", category: EvaluatableCategory.Global)]
public class Number_Variable : Evaluatable<double>
{
public Evaluatable<double> DefaultValue { get; set; } = new NumberConstant();
public Evaluatable<string> VariableName { get; set; } = new StringConstant();

public Number_Variable() { }

public Number_Variable(Evaluatable<string> variableName, Evaluatable<double> defaultValue) {
VariableName = variableName;
DefaultValue = defaultValue;
}

protected override double Execute(IGameState gameState)
{
var key = VariableName.Evaluate(gameState);
var defaultValue = DefaultValue.Evaluate(gameState);
return AuroraVariables.Instance.Numbers.GetValueOrDefault(key, defaultValue);
}

public override Visual GetControl()
{
return new StackPanel { Orientation = Orientation.Horizontal }
.WithChild(new TextBlock { Text = "Name", FontWeight = FontWeights.Bold, Margin = new Thickness(2, 0, 6, 0), VerticalAlignment = VerticalAlignment.Center })
.WithChild(new Control_EvaluatablePresenter { EvalType = typeof(string) }
.WithBinding(Control_EvaluatablePresenter.ExpressionProperty, new Binding(nameof(VariableName)) { Source = this, Mode = BindingMode.TwoWay }))
.WithChild(new TextBlock { Text = "Default Value", FontWeight = FontWeights.Bold, Margin = new Thickness(2, 0, 6, 0), VerticalAlignment = VerticalAlignment.Center })
.WithChild(new Control_EvaluatablePresenter { EvalType = typeof(double) }
.WithBinding(Control_EvaluatablePresenter.ExpressionProperty, new Binding(nameof(DefaultValue)) { Source = this, Mode = BindingMode.TwoWay }));
}

public override Evaluatable<double> Clone()
{
return new Number_Variable(VariableName, DefaultValue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using AuroraRgb.Nodes;
using AuroraRgb.Profiles;
using AuroraRgb.Utils;

namespace AuroraRgb.Settings.Overrides.Logic;

[Evaluatable("String Global Variable", category: EvaluatableCategory.Global)]
public class String_Variable : Evaluatable<string>
{
public Evaluatable<string> DefaultValue { get; set; } = new StringConstant();
public Evaluatable<string> VariableName { get; set; } = new StringConstant();

public String_Variable() { }

public String_Variable(Evaluatable<string> variableName, Evaluatable<string> defaultValue)
{
VariableName = variableName;
DefaultValue = defaultValue;
}

protected override string Execute(IGameState gameState)
{
var key = VariableName.Evaluate(gameState);
var defaultValue = DefaultValue.Evaluate(gameState);
return AuroraVariables.Instance.Strings.GetValueOrDefault(key, defaultValue);
}

public override Visual GetControl()
{
return new StackPanel { Orientation = Orientation.Horizontal }
.WithChild(new TextBlock { Text = "Name", FontWeight = FontWeights.Bold, Margin = new Thickness(2, 0, 6, 0), VerticalAlignment = VerticalAlignment.Center })
.WithChild(new Control_EvaluatablePresenter { EvalType = typeof(string) }
.WithBinding(Control_EvaluatablePresenter.ExpressionProperty, new Binding(nameof(VariableName)) { Source = this, Mode = BindingMode.TwoWay }))
.WithChild(new TextBlock { Text = "Default Value", FontWeight = FontWeights.Bold, Margin = new Thickness(2, 0, 6, 0), VerticalAlignment = VerticalAlignment.Center })
.WithChild(new Control_EvaluatablePresenter { EvalType = typeof(string) }
.WithBinding(Control_EvaluatablePresenter.ExpressionProperty, new Binding(nameof(DefaultValue)) { Source = this, Mode = BindingMode.TwoWay }));
}

public override Evaluatable<string> Clone()
{
return new String_Variable(VariableName, DefaultValue);
}
}

0 comments on commit fef5829

Please sign in to comment.