Skip to content

Commit

Permalink
Changed racemode to boolean + added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarno458 committed Nov 23, 2024
1 parent 0d15540 commit faaab86
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 12 deletions.
64 changes: 62 additions & 2 deletions Archipelago.MultiClient.Net.Tests/DataStorageWrapperFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> Strings { get; set; }
public Options SlotOptions { get; set; }
// ReSharper restore UnusedAutoPropertyAccessor.Local
}
[Test]
public void GetSlotData_should_deserialize_custom_type_for_current_player_slot()
Expand All @@ -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,
Expand Down Expand Up @@ -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<IArchipelagoSocketHelper>();
var connectionInfo = Substitute.For<IConnectionInfoProvider>();
//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<GetPacket>(p => p.Keys.FirstOrDefault() == "_read_race_mode"));

Assert.IsTrue(raceMode);
}

[Test]
public void GetRaceModeAsync_should_return_race_mode()
{
var socket = Substitute.For<IArchipelagoSocketHelper>();
var connectionInfo = Substitute.For<IConnectionInfoProvider>();
//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<GetPacket>(p => p.Keys.FirstOrDefault() == "_read_race_mode"));

Assert.IsTrue(raceMode);
}

public static void ExecuteAsyncWithDelay(Action retrieve, Action raiseEvent)
{
Task.WaitAll(new[] {
Expand Down
29 changes: 19 additions & 10 deletions Archipelago.MultiClient.Net/Helpers/DataStorageWrappers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,24 @@ void TrackHints(Action<Hint[]> onHintsUpdated,
void TrackClientStatus(Action<ArchipelagoClientState> onStatusUpdated,
bool retrieveCurrentClientStatus = true, int? slot = null, int? team = null);

/// <summary>
/// Retrieves the server's race mode setting. false for disabled or unavailable, true for enabled
/// </summary>
/// <returns>The race mode setting. false for disabled or unavailable, true for enabled</returns>
bool GetRaceMode();

#if NET35
/// <summary>
/// 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
/// </summary>
/// <param name="onRaceModeRetrieved"> the method to call with the retrieved race mode setting</param>
void GetRaceModeAsync(Action<int> onRaceModeRetrieved);
void GetRaceModeAsync(Action<bool> onRaceModeRetrieved);
#else
/// <summary>
/// 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
/// </summary>
/// <param name="onRaceModeRetrieved"> the method to call with the retrieved race mode setting</param>
Task<int> GetRaceModeAsync();
/// <returns>The race mode setting. false for disabled or unavailable, true for enabled</returns>
Task<bool> GetRaceModeAsync();
#endif
}

Expand Down Expand Up @@ -317,15 +323,18 @@ public void TrackClientStatus(Action<ArchipelagoClientState> onStatusUpdated,
GetClientStatusAsync(slot, team).ContinueWith(t => onStatusUpdated(t.Result));
#endif
}


/// <inheritdoc />
public bool GetRaceMode() =>
(GetRaceModeElement().To<int?>() ?? 0) > 0;
#if NET35
/// <inheritdoc />
public void GetRaceModeAsync(Action<int> onRaceModeRetrieved) =>
GetRaceModeElement().GetAsync(t => onRaceModeRetrieved(t.ToObject<int?>() ?? 0));
public void GetRaceModeAsync(Action<bool> onRaceModeRetrieved) =>
GetRaceModeElement().GetAsync(t => onRaceModeRetrieved((t.ToObject<int?>() ?? 0) > 0));
#else
/// <inheritdoc />
public Task<int> GetRaceModeAsync() => GetRaceModeElement().GetAsync<int?>()
.ContinueWith(t => t.Result ?? 0);
public Task<bool> GetRaceModeAsync() => GetRaceModeElement().GetAsync<int?>()
.ContinueWith(t => (t.Result ?? 0) > 0);
#endif
}
}

0 comments on commit faaab86

Please sign in to comment.