From faaab86ffd3c558d3aeb498922840a9693f9778c Mon Sep 17 00:00:00 2001 From: Jarno Westhof Date: Sat, 23 Nov 2024 14:30:42 +0100 Subject: [PATCH] Changed racemode to boolean + added unit tests --- .../DataStorageWrapperFixture.cs | 64 ++++++++++++++++++- .../Helpers/DataStorageWrappers.cs | 29 ++++++--- 2 files changed, 81 insertions(+), 12 deletions(-) diff --git a/Archipelago.MultiClient.Net.Tests/DataStorageWrapperFixture.cs b/Archipelago.MultiClient.Net.Tests/DataStorageWrapperFixture.cs index da50d06..4e8bc59 100644 --- a/Archipelago.MultiClient.Net.Tests/DataStorageWrapperFixture.cs +++ b/Archipelago.MultiClient.Net.Tests/DataStorageWrapperFixture.cs @@ -322,19 +322,21 @@ public void GetSlotData_should_return_slot_data_for_current_player_slot() Assert.That(slotData["Two"], Is.EqualTo(2)); } - private class CustomSlotDataModel + class CustomSlotDataModel { public class Options { public bool IsXRandomized { get; set; } public bool IsYRandomized { get; set; } } + // ReSharper disable UnusedAutoPropertyAccessor.Local public int One { get; set; } public long Two { get; set; } public uint? Three { get; set; } [JsonProperty("ListOfStuff")] public List Strings { get; set; } public Options SlotOptions { get; set; } + // ReSharper restore UnusedAutoPropertyAccessor.Local } [Test] public void GetSlotData_should_deserialize_custom_type_for_current_player_slot() @@ -351,7 +353,7 @@ public void GetSlotData_should_deserialize_custom_type_for_current_player_slot() { { "One", 1 }, { "Two", 2 }, - { "ListOfStuff", new string[] { "A", "B", "C" } }, + { "ListOfStuff", new [] { "A", "B", "C" } }, { "SlotOptions", new CustomSlotDataModel.Options { IsXRandomized = true, @@ -943,6 +945,64 @@ public void GetClientStatusAsync_should_return_unknown_for_non_existing_slot() Assert.That(statusBySlotAndTeam, Is.EqualTo(ArchipelagoClientState.ClientUnknown)); } + [Test] + public void GetRaceMode_should_return_race_mode() + { + var socket = Substitute.For(); + var connectionInfo = Substitute.For(); + //connectionInfo.Slot.Returns(8); + //connectionInfo.Team.Returns(2); + + var sut = new DataStorageHelper(socket, connectionInfo); + + bool raceMode = false; + + ExecuteAsyncWithDelay( + () => { raceMode = sut.GetRaceMode(); }, + () => RaiseRetrieved(socket, "_read_race_mode", new JValue(true))); + + socket.Received().SendPacketAsync(Arg.Is(p => p.Keys.FirstOrDefault() == "_read_race_mode")); + + Assert.IsTrue(raceMode); + } + + [Test] + public void GetRaceModeAsync_should_return_race_mode() + { + var socket = Substitute.For(); + var connectionInfo = Substitute.For(); + //connectionInfo.Slot.Returns(7); + //connectionInfo.Team.Returns(3); + + var sut = new DataStorageHelper(socket, connectionInfo); + + bool raceMode = false; + +#if NET471 + bool statusCallbackReceived = false; + + ExecuteAsyncWithDelay( + () => { + sut.GetRaceModeAsync(t => { + statusCallbackReceived = true; + raceMode = t; + }); + }, + () => RaiseRetrieved(socket, "_read_race_mode", new JValue(true))); + + while (!statusCallbackReceived) + Thread.Sleep(10); +#else + ExecuteAsyncWithDelay( + () => { raceMode = sut.GetRaceModeAsync().Result; }, + () => RaiseRetrieved(socket, "_read_race_mode", new JValue(true))); +#endif + + socket.Received().SendPacketAsync(Arg.Is(p => p.Keys.FirstOrDefault() == "_read_race_mode")); + + Assert.IsTrue(raceMode); + } + public static void ExecuteAsyncWithDelay(Action retrieve, Action raiseEvent) { Task.WaitAll(new[] { diff --git a/Archipelago.MultiClient.Net/Helpers/DataStorageWrappers.cs b/Archipelago.MultiClient.Net/Helpers/DataStorageWrappers.cs index 668035c..152100a 100644 --- a/Archipelago.MultiClient.Net/Helpers/DataStorageWrappers.cs +++ b/Archipelago.MultiClient.Net/Helpers/DataStorageWrappers.cs @@ -184,18 +184,24 @@ void TrackHints(Action onHintsUpdated, void TrackClientStatus(Action onStatusUpdated, bool retrieveCurrentClientStatus = true, int? slot = null, int? team = null); + /// + /// Retrieves the server's race mode setting. false for disabled or unavailable, true for enabled + /// + /// The race mode setting. false for disabled or unavailable, true for enabled + bool GetRaceMode(); + #if NET35 /// - /// Retrieves the server's race mode setting. 0 for disabled, 1 for enabled + /// Retrieves the server's race mode setting. false for disabled or unavailable, true for enabled /// /// the method to call with the retrieved race mode setting - void GetRaceModeAsync(Action onRaceModeRetrieved); + void GetRaceModeAsync(Action onRaceModeRetrieved); #else /// - /// Retrieves the server's race mode setting. 0 for disabled, 1 for enabled + /// Retrieves the server's race mode setting. false for disabled or unavailable, true for enabled /// - /// the method to call with the retrieved race mode setting - Task GetRaceModeAsync(); + /// The race mode setting. false for disabled or unavailable, true for enabled + Task GetRaceModeAsync(); #endif } @@ -317,15 +323,18 @@ public void TrackClientStatus(Action onStatusUpdated, GetClientStatusAsync(slot, team).ContinueWith(t => onStatusUpdated(t.Result)); #endif } - + + /// + public bool GetRaceMode() => + (GetRaceModeElement().To() ?? 0) > 0; #if NET35 /// - public void GetRaceModeAsync(Action onRaceModeRetrieved) => - GetRaceModeElement().GetAsync(t => onRaceModeRetrieved(t.ToObject() ?? 0)); + public void GetRaceModeAsync(Action onRaceModeRetrieved) => + GetRaceModeElement().GetAsync(t => onRaceModeRetrieved((t.ToObject() ?? 0) > 0)); #else /// - public Task GetRaceModeAsync() => GetRaceModeElement().GetAsync() - .ContinueWith(t => t.Result ?? 0); + public Task GetRaceModeAsync() => GetRaceModeElement().GetAsync() + .ContinueWith(t => (t.Result ?? 0) > 0); #endif } }