Skip to content

Commit faaab86

Browse files
committed
Changed racemode to boolean + added unit tests
1 parent 0d15540 commit faaab86

File tree

2 files changed

+81
-12
lines changed

2 files changed

+81
-12
lines changed

Archipelago.MultiClient.Net.Tests/DataStorageWrapperFixture.cs

+62-2
Original file line numberDiff line numberDiff line change
@@ -322,19 +322,21 @@ public void GetSlotData_should_return_slot_data_for_current_player_slot()
322322
Assert.That(slotData["Two"], Is.EqualTo(2));
323323
}
324324

325-
private class CustomSlotDataModel
325+
class CustomSlotDataModel
326326
{
327327
public class Options
328328
{
329329
public bool IsXRandomized { get; set; }
330330
public bool IsYRandomized { get; set; }
331331
}
332+
// ReSharper disable UnusedAutoPropertyAccessor.Local
332333
public int One { get; set; }
333334
public long Two { get; set; }
334335
public uint? Three { get; set; }
335336
[JsonProperty("ListOfStuff")]
336337
public List<string> Strings { get; set; }
337338
public Options SlotOptions { get; set; }
339+
// ReSharper restore UnusedAutoPropertyAccessor.Local
338340
}
339341
[Test]
340342
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()
351353
{
352354
{ "One", 1 },
353355
{ "Two", 2 },
354-
{ "ListOfStuff", new string[] { "A", "B", "C" } },
356+
{ "ListOfStuff", new [] { "A", "B", "C" } },
355357
{ "SlotOptions", new CustomSlotDataModel.Options
356358
{
357359
IsXRandomized = true,
@@ -943,6 +945,64 @@ public void GetClientStatusAsync_should_return_unknown_for_non_existing_slot()
943945
Assert.That(statusBySlotAndTeam, Is.EqualTo(ArchipelagoClientState.ClientUnknown));
944946
}
945947

948+
[Test]
949+
public void GetRaceMode_should_return_race_mode()
950+
{
951+
var socket = Substitute.For<IArchipelagoSocketHelper>();
952+
var connectionInfo = Substitute.For<IConnectionInfoProvider>();
953+
//connectionInfo.Slot.Returns(8);
954+
//connectionInfo.Team.Returns(2);
955+
956+
var sut = new DataStorageHelper(socket, connectionInfo);
957+
958+
bool raceMode = false;
959+
960+
ExecuteAsyncWithDelay(
961+
() => { raceMode = sut.GetRaceMode(); },
962+
() => RaiseRetrieved(socket, "_read_race_mode", new JValue(true)));
963+
964+
socket.Received().SendPacketAsync(Arg.Is<GetPacket>(p => p.Keys.FirstOrDefault() == "_read_race_mode"));
965+
966+
Assert.IsTrue(raceMode);
967+
}
968+
969+
[Test]
970+
public void GetRaceModeAsync_should_return_race_mode()
971+
{
972+
var socket = Substitute.For<IArchipelagoSocketHelper>();
973+
var connectionInfo = Substitute.For<IConnectionInfoProvider>();
974+
//connectionInfo.Slot.Returns(7);
975+
//connectionInfo.Team.Returns(3);
976+
977+
var sut = new DataStorageHelper(socket, connectionInfo);
978+
979+
bool raceMode = false;
980+
981+
#if NET471
982+
bool statusCallbackReceived = false;
983+
984+
ExecuteAsyncWithDelay(
985+
() => {
986+
sut.GetRaceModeAsync(t => {
987+
statusCallbackReceived = true;
988+
raceMode = t;
989+
});
990+
},
991+
() => RaiseRetrieved(socket, "_read_race_mode", new JValue(true)));
992+
993+
while (!statusCallbackReceived)
994+
Thread.Sleep(10);
995+
#else
996+
ExecuteAsyncWithDelay(
997+
() => { raceMode = sut.GetRaceModeAsync().Result; },
998+
() => RaiseRetrieved(socket, "_read_race_mode", new JValue(true)));
999+
#endif
1000+
1001+
socket.Received().SendPacketAsync(Arg.Is<GetPacket>(p => p.Keys.FirstOrDefault() == "_read_race_mode"));
1002+
1003+
Assert.IsTrue(raceMode);
1004+
}
1005+
9461006
public static void ExecuteAsyncWithDelay(Action retrieve, Action raiseEvent)
9471007
{
9481008
Task.WaitAll(new[] {

Archipelago.MultiClient.Net/Helpers/DataStorageWrappers.cs

+19-10
Original file line numberDiff line numberDiff line change
@@ -184,18 +184,24 @@ void TrackHints(Action<Hint[]> onHintsUpdated,
184184
void TrackClientStatus(Action<ArchipelagoClientState> onStatusUpdated,
185185
bool retrieveCurrentClientStatus = true, int? slot = null, int? team = null);
186186

187+
/// <summary>
188+
/// Retrieves the server's race mode setting. false for disabled or unavailable, true for enabled
189+
/// </summary>
190+
/// <returns>The race mode setting. false for disabled or unavailable, true for enabled</returns>
191+
bool GetRaceMode();
192+
187193
#if NET35
188194
/// <summary>
189-
/// Retrieves the server's race mode setting. 0 for disabled, 1 for enabled
195+
/// Retrieves the server's race mode setting. false for disabled or unavailable, true for enabled
190196
/// </summary>
191197
/// <param name="onRaceModeRetrieved"> the method to call with the retrieved race mode setting</param>
192-
void GetRaceModeAsync(Action<int> onRaceModeRetrieved);
198+
void GetRaceModeAsync(Action<bool> onRaceModeRetrieved);
193199
#else
194200
/// <summary>
195-
/// Retrieves the server's race mode setting. 0 for disabled, 1 for enabled
201+
/// Retrieves the server's race mode setting. false for disabled or unavailable, true for enabled
196202
/// </summary>
197-
/// <param name="onRaceModeRetrieved"> the method to call with the retrieved race mode setting</param>
198-
Task<int> GetRaceModeAsync();
203+
/// <returns>The race mode setting. false for disabled or unavailable, true for enabled</returns>
204+
Task<bool> GetRaceModeAsync();
199205
#endif
200206
}
201207

@@ -317,15 +323,18 @@ public void TrackClientStatus(Action<ArchipelagoClientState> onStatusUpdated,
317323
GetClientStatusAsync(slot, team).ContinueWith(t => onStatusUpdated(t.Result));
318324
#endif
319325
}
320-
326+
327+
/// <inheritdoc />
328+
public bool GetRaceMode() =>
329+
(GetRaceModeElement().To<int?>() ?? 0) > 0;
321330
#if NET35
322331
/// <inheritdoc />
323-
public void GetRaceModeAsync(Action<int> onRaceModeRetrieved) =>
324-
GetRaceModeElement().GetAsync(t => onRaceModeRetrieved(t.ToObject<int?>() ?? 0));
332+
public void GetRaceModeAsync(Action<bool> onRaceModeRetrieved) =>
333+
GetRaceModeElement().GetAsync(t => onRaceModeRetrieved((t.ToObject<int?>() ?? 0) > 0));
325334
#else
326335
/// <inheritdoc />
327-
public Task<int> GetRaceModeAsync() => GetRaceModeElement().GetAsync<int?>()
328-
.ContinueWith(t => t.Result ?? 0);
336+
public Task<bool> GetRaceModeAsync() => GetRaceModeElement().GetAsync<int?>()
337+
.ContinueWith(t => (t.Result ?? 0) > 0);
329338
#endif
330339
}
331340
}

0 commit comments

Comments
 (0)