From d0a0c0abbb5ea875d6b0b542d3c9fac3dc8396d0 Mon Sep 17 00:00:00 2001 From: Kalakoi Date: Wed, 11 Jun 2025 14:58:28 -0400 Subject: [PATCH] Continued integration and testing of true randomness. --- osrs-toolbox/APIs/Random/Random.cs | 43 ++++++++++++++ .../APIs/Random/RandomIntegerRequest.cs | 56 +++++++++++++++++++ osrs-toolbox/Random.cs | 14 ----- osrs-toolbox/ViewModels/APITestViewModel.cs | 8 ++- osrs-toolbox/osrs-toolbox.csproj | 4 -- 5 files changed, 106 insertions(+), 19 deletions(-) create mode 100644 osrs-toolbox/APIs/Random/Random.cs create mode 100644 osrs-toolbox/APIs/Random/RandomIntegerRequest.cs delete mode 100644 osrs-toolbox/Random.cs diff --git a/osrs-toolbox/APIs/Random/Random.cs b/osrs-toolbox/APIs/Random/Random.cs new file mode 100644 index 0000000..0fee2f3 --- /dev/null +++ b/osrs-toolbox/APIs/Random/Random.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; +using System.Windows; + +namespace osrs_toolbox +{ + public static class Random + { + private static string BaseEndpoint = @"https://api.random.org/json-rpc/4/invoke"; + private static string APIKey = APIKeys.RANDOM; + + public static async Task GetRandomIntegersAsync(int Minimum, int Maximum, int Count, bool AllowDuplicates) + { + RandomIntegerRequest Request = new RandomIntegerRequest() + { + parameters = new() + { + apiKey = APIKey, + n = Count, + min = Minimum, + max = Maximum, + replacement = AllowDuplicates + } + }; + string TestString = string.Empty; + TestString += string.Format("{0}: {1}\n", nameof(Request.jsonrpc), Request.jsonrpc); + TestString += string.Format("{0}: {1}\n", nameof(Request.method), Request.method); + string RequestJson = Request.GetJson(); + TestString += string.Format("{0}\n", RequestJson); + string ResponseJson = await RestServices.GetPostResponseAsync(new Uri(BaseEndpoint), RequestJson, APIKey).ConfigureAwait(false); + TestString += string.Format("{0}", ResponseJson); + MessageBox.Show(TestString); + RandomIntegerRequestResponse response = JsonSerializer.Deserialize(ResponseJson); + return response.result.random.data; + //return TestString; + //return ResponseJson; + } + } +} diff --git a/osrs-toolbox/APIs/Random/RandomIntegerRequest.cs b/osrs-toolbox/APIs/Random/RandomIntegerRequest.cs new file mode 100644 index 0000000..91d5e24 --- /dev/null +++ b/osrs-toolbox/APIs/Random/RandomIntegerRequest.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Data.SqlTypes; +using System.Linq; +using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace osrs_toolbox +{ + public class RandomIntegerRequest + { + public string jsonrpc { get; set; } = "2.0"; + public string method { get; set; } = "generateIntegers"; + [JsonPropertyName("params")] + public RandomIntegerRequestParameters parameters { get; set; } = new(); + public long id { get; set; } = DateTime.Now.Ticks; + + public string GetJson() + { + return JsonSerializer.Serialize(this); + } + } + + public class RandomIntegerRequestParameters + { + public string apiKey { get; set; } + public int n { get; set; } + public int min { get; set; } + public int max { get; set; } + public bool replacement { get; set; } + } + + public class RandomIntegerRequestResponse + { + public string jsonrpc; + public RandomIntegerResult result; + public int id; + } + + public class RandomIntegerResult + { + public RandomIntegerData random; + public int bitsUsed; + public int bitsLeft; + public int requestsLeft; + public int advisoryDelay; + } + + public class RandomIntegerData + { + public int[] data; + public DateTime completionTime; + } +} diff --git a/osrs-toolbox/Random.cs b/osrs-toolbox/Random.cs deleted file mode 100644 index 1a4fb5e..0000000 --- a/osrs-toolbox/Random.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace osrs_toolbox -{ - public static class Random - { - private static string BaseEndpoint = @"https://api.randdom.org/json-rpc/4/invoke"; - private static string APIKey = APIKeys.RANDOM; - } -} diff --git a/osrs-toolbox/ViewModels/APITestViewModel.cs b/osrs-toolbox/ViewModels/APITestViewModel.cs index 88ed587..b0c753c 100644 --- a/osrs-toolbox/ViewModels/APITestViewModel.cs +++ b/osrs-toolbox/ViewModels/APITestViewModel.cs @@ -16,7 +16,13 @@ namespace osrs_toolbox private async void DoTest(object obj) { - TestOutput = await Wiki.TestAsync(); + int[] test = await Random.GetRandomIntegersAsync(0, 36, 8, false); + string TestString = string.Empty; + foreach(int i in test) + { + TestString += string.Format("{0}\n", i); + } + TestOutput = TestString; } } } diff --git a/osrs-toolbox/osrs-toolbox.csproj b/osrs-toolbox/osrs-toolbox.csproj index 703ed70..8714d4a 100644 --- a/osrs-toolbox/osrs-toolbox.csproj +++ b/osrs-toolbox/osrs-toolbox.csproj @@ -85,8 +85,4 @@ - - - -