-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
215 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Slipstream.Shared; | ||
using System.Collections.Generic; | ||
|
||
namespace Slipstream.Components.IRacing.Events | ||
{ | ||
public class IRacingTowed : IEvent | ||
{ | ||
public string EventType => "IRacingTowed"; | ||
public bool ExcludeFromTxrx => false; | ||
public ulong Uptime { get; set; } | ||
public double SessionTime { get; set; } | ||
public double RemainingTowTime { get; set; } | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
return obj is IRacingTowed other && | ||
EventType == other.EventType && | ||
ExcludeFromTxrx == other.ExcludeFromTxrx && | ||
SessionTime == other.SessionTime && | ||
RemainingTowTime == other.RemainingTowTime; | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
int hashCode = 2084919435; | ||
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(EventType); | ||
hashCode = hashCode * -1521134295 + ExcludeFromTxrx.GetHashCode(); | ||
hashCode = hashCode * -1521134295 + SessionTime.GetHashCode(); | ||
hashCode = hashCode * -1521134295 + RemainingTowTime.GetHashCode(); | ||
return hashCode; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Slipstream.Components.IRacing.Plugins.Models; | ||
using Slipstream.Shared; | ||
using System.Diagnostics; | ||
|
||
#nullable enable | ||
|
||
namespace Slipstream.Components.IRacing.Plugins.Trackers | ||
{ | ||
internal class TowTracker : IIRacingDataTracker | ||
{ | ||
private readonly IIRacingEventFactory EventFactory; | ||
private readonly IEventBus EventBus; | ||
private bool BeingTowed = false; | ||
|
||
public TowTracker(IEventBus eventBus, IIRacingEventFactory eventFactory) | ||
{ | ||
EventBus = eventBus; | ||
EventFactory = eventFactory; | ||
} | ||
|
||
public void Handle(GameState.IState currentState, IRacingDataTrackerState state) | ||
{ | ||
var towTime = currentState.PlayerCarTowTime; | ||
|
||
if (towTime > 0) | ||
{ | ||
if (!BeingTowed) | ||
{ | ||
Debug.WriteLine($"TOW: {towTime}"); | ||
BeingTowed = true; | ||
|
||
EventBus.PublishEvent(EventFactory.CreateIRacingTowed( | ||
sessionTime: currentState.SessionTime, | ||
remainingTowTime: towTime | ||
)); | ||
} | ||
} | ||
else | ||
{ | ||
BeingTowed = false; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
Slipstream.UnitTests/Components/IRacing/Plugins/Trackers/TowTrackerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using Slipstream.Components.IRacing; | ||
using Slipstream.Components.IRacing.EventFactory; | ||
using Slipstream.Components.IRacing.Events; | ||
using Slipstream.Components.IRacing.Plugins.GameState; | ||
using Slipstream.Components.IRacing.Plugins.Models; | ||
using Slipstream.Components.IRacing.Plugins.Trackers; | ||
using Slipstream.UnitTests.TestData; | ||
using Xunit; | ||
|
||
namespace Slipstream.UnitTests.Components.IRacing.Plugins.Trackers | ||
{ | ||
public class TowTrackerTests | ||
{ | ||
private readonly TestEventBus EventBus; | ||
private readonly IIRacingEventFactory EventFactory; | ||
private readonly IRacingDataTrackerState TrackerState; | ||
private readonly GameStateBuilder Builder; | ||
|
||
public TowTrackerTests() | ||
{ | ||
EventBus = new TestEventBus(); | ||
EventFactory = new IRacingEventFactory(); | ||
TrackerState = new IRacingDataTrackerState(); | ||
Builder = new GameStateBuilder(); | ||
} | ||
|
||
[Fact] | ||
public void PublishEventIfBeginTowed() | ||
{ | ||
const float NOW = 1234.2f; | ||
const float TOW_TIME = 32.2f; | ||
|
||
// arrange | ||
Builder.AtSessionTime(NOW).Set(a => a.PlayerCarTowTime = TOW_TIME).Commit(); | ||
Builder.AtSessionTime(NOW + 1).Set(a => a.PlayerCarTowTime = TOW_TIME - 1).Commit(); | ||
Builder.AtSessionTime(NOW + 2).Set(a => a.PlayerCarTowTime = TOW_TIME - 2).Commit(); | ||
|
||
// act | ||
var sut = new TowTracker(EventBus, EventFactory); | ||
foreach (var s in Builder.States) | ||
sut.Handle(s, TrackerState); | ||
|
||
// assert | ||
Assert.True(EventBus.Events.Count == 1); | ||
|
||
var @event = EventBus.Events[0] as IRacingTowed; | ||
Assert.NotNull(@event); | ||
Assert.True(@event.SessionTime == NOW); | ||
Assert.True(@event.RemainingTowTime == TOW_TIME); | ||
} | ||
|
||
[Fact] | ||
public void PublishEventsIfPlayerGotAReallyShittyRace() | ||
{ | ||
const float NOW = 1234.2f; | ||
const float TOW_TIME = 32.2f; | ||
const float TOW2_TIME = 10f; | ||
|
||
// arrange | ||
// first two | ||
Builder.AtSessionTime(NOW).Set(a => a.PlayerCarTowTime = TOW_TIME).Commit(); | ||
Builder.AtSessionTime(NOW + 1).Set(a => a.PlayerCarTowTime = TOW_TIME - 1).Commit(); | ||
// all ok again | ||
Builder.AtSessionTime(NOW + 2).Set(a => a.PlayerCarTowTime = 0).Commit(); | ||
// another tow | ||
Builder.AtSessionTime(NOW + 3).Set(a => a.PlayerCarTowTime = TOW2_TIME).Commit(); | ||
|
||
// act | ||
var sut = new TowTracker(EventBus, EventFactory); | ||
foreach (var s in Builder.States) | ||
sut.Handle(s, TrackerState); | ||
|
||
// assert | ||
Assert.True(EventBus.Events.Count == 2); | ||
|
||
var @event = EventBus.Events[1] as IRacingTowed; | ||
Assert.NotNull(@event); | ||
Assert.True(@event.SessionTime == NOW + 3); | ||
Assert.True(@event.RemainingTowTime == TOW2_TIME); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters