Skip to content

Commit

Permalink
Fix auto tracker disconnect/reconnect loops
Browse files Browse the repository at this point in the history
  • Loading branch information
MattEqualsCoder committed Dec 25, 2024
1 parent 10ac181 commit 0d3a518
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public class AutoTrackerConfig : IMergeable<AutoTrackerConfig>
/// </summary>
public SchrodingersString? WhenDisconnected { get; init; }

/// <summary>
/// Gets the phrases to respond when the Auto Tracker has been stuck in a connect/disconnect loop and has
/// given up on trying to connect.
/// </summary>
public SchrodingersString? WhenDisconnectLimitReached { get; init; }

/// <summary>
/// Gets the phrases to respond with when the game is started.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ public bool IsSamusInArea(int minX, int maxX, int minY, int maxY)
/// Checks to make sure that the state is valid and fully loaded. There's a period upon first booting up that
/// all of these are 0s, but some of the memory in the location data can be screwy.
/// </summary>
public bool IsValid => Energy is > 0 and < 1999 && ReserveTanks is >= 0 and < 600 && (CurrentRoom != 0 ||
CurrentRegion != 0 || CurrentRoomInRegion != 0 || Energy != 0 ||
SamusX != 0 || SamusY != 0);
public bool IsValid => CurrentRoom != 0 || CurrentRegion != 0 || CurrentRoomInRegion != 0;

/// <summary>
/// Prints debug data for the state
Expand Down
50 changes: 44 additions & 6 deletions src/TrackerCouncil.Smz3.Tracking/AutoTracking/AutoTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public class AutoTracker : AutoTrackerBase
private bool _foundGTKey;
private ISnesConnectorService _snesConnectorService;
private bool _isEnabled;
private int _numDisconnects;
private bool _connectionValidated;

Check warning on line 33 in src/TrackerCouncil.Smz3.Tracking/AutoTracking/AutoTracker.cs

View workflow job for this annotation

GitHub Actions / build-mac

The field 'AutoTracker._connectionValidated' is never used

Check warning on line 33 in src/TrackerCouncil.Smz3.Tracking/AutoTracking/AutoTracker.cs

View workflow job for this annotation

GitHub Actions / build-mac

The field 'AutoTracker._connectionValidated' is never used

Check warning on line 33 in src/TrackerCouncil.Smz3.Tracking/AutoTracking/AutoTracker.cs

View workflow job for this annotation

GitHub Actions / build-mac

The field 'AutoTracker._connectionValidated' is never used

Check warning on line 33 in src/TrackerCouncil.Smz3.Tracking/AutoTracking/AutoTracker.cs

View workflow job for this annotation

GitHub Actions / build

The field 'AutoTracker._connectionValidated' is never used

Check warning on line 33 in src/TrackerCouncil.Smz3.Tracking/AutoTracking/AutoTracker.cs

View workflow job for this annotation

GitHub Actions / build

The field 'AutoTracker._connectionValidated' is never used

Check warning on line 33 in src/TrackerCouncil.Smz3.Tracking/AutoTracking/AutoTracker.cs

View workflow job for this annotation

GitHub Actions / build

The field 'AutoTracker._connectionValidated' is never used
private CancellationTokenSource? _validationCts;

/// <summary>
/// Constructor for Auto Tracker
Expand Down Expand Up @@ -62,20 +65,55 @@ public AutoTracker(ILogger<AutoTracker> logger,
snesConnectorService.Disconnected += SnesConnectorServiceOnDisconnected;
}

private void SnesConnectorServiceOnConnected(object? sender, EventArgs e)
private async void SnesConnectorServiceOnConnected(object? sender, EventArgs e)
{
_logger.LogInformation("Connector Connected");
TrackerBase.Say(x => x.AutoTracker.WhenConnected);
OnAutoTrackerConnected();
try
{
_logger.LogInformation("Connector connected");
_validationCts = new CancellationTokenSource();
await Task.Delay(TimeSpan.FromSeconds(2), _validationCts.Token);

if (!_validationCts.IsCancellationRequested)
{
_logger.LogInformation("Connection validated");
TrackerBase.Say(x => x.AutoTracker.WhenConnected);
OnAutoTrackerConnected();
_numDisconnects = 0;
_validationCts = null;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Connector connection failure");
}
}

private void SnesConnectorServiceOnDisconnected(object? sender, EventArgs e)
{
_logger.LogInformation("Connector Disconnected");
TrackerBase.Say(x => x.AutoTracker.WhenDisconnected);
OnAutoTrackerDisconnected();

if (_validationCts == null)
{
TrackerBase.Say(x => x.AutoTracker.WhenDisconnected);
OnAutoTrackerDisconnected();
}
else
{
_numDisconnects++;
_validationCts.Cancel();
}

HasStarted = false;
CurrentGame = Game.Neither;
_hasValidState = false;

if (_numDisconnects > 10)
{
_logger.LogInformation("Disconnect limit of 10 reached");
SetConnector(new SnesConnectorSettings(), SnesConnectorType.None);
_numDisconnects = 0;
TrackerBase.Say(x => x.AutoTracker.WhenDisconnectLimitReached);
}
}

public override void SetConnector(SnesConnectorSettings snesConnectorSettings,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
Expand All @@ -18,6 +19,8 @@ public class GameMonitor(TrackerBase tracker, ISnesConnectorService snesConnecto
{
private bool bIsCheckingGameStart;

private bool bIsFirstStart = true;

public override void Initialize()
{
// Check if the game has started or not
Expand Down Expand Up @@ -142,19 +145,24 @@ private void MarkAsStarted()

AutoTracker.HasStarted = true;

if (Tracker.World.Config.RomGenerator != RomGenerator.Cas)
{
Tracker.Say(x => x.AutoTracker.GameStartedNonCas);
}
else if (Tracker.World.Config.MultiWorld && worldQueryService.Worlds.Count > 1)
if (bIsFirstStart)
{
var worldCount = worldQueryService.Worlds.Count;
var otherPlayerName = worldQueryService.Worlds.Where(x => x != worldQueryService.World).Random(new Random())!.Config.PhoneticName;
Tracker.Say(x => x.AutoTracker.GameStartedMultiplayer, args: [worldCount, otherPlayerName]);
}
else
{
Tracker.Say(x => x.AutoTracker.GameStarted, args: [Tracker.Rom?.Seed]);
if (Tracker.World.Config.RomGenerator != RomGenerator.Cas)
{
Tracker.Say(x => x.AutoTracker.GameStartedNonCas);
}
else if (Tracker.World.Config.MultiWorld && worldQueryService.Worlds.Count > 1)
{
var worldCount = worldQueryService.Worlds.Count;
var otherPlayerName = worldQueryService.Worlds.Where(x => x != worldQueryService.World).Random(new Random())!.Config.PhoneticName;
Tracker.Say(x => x.AutoTracker.GameStartedMultiplayer, args: [worldCount, otherPlayerName]);
}
else
{
Tracker.Say(x => x.AutoTracker.GameStarted, args: [Tracker.Rom?.Seed]);
}

bIsFirstStart = false;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/TrackerCouncil.Smz3.UI/TrackerCouncil.Smz3.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<ApplicationManifest>app.manifest</ApplicationManifest>
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
<Version>9.9.0-beta.1</Version>
<Version>9.9.0-rc.1</Version>
<AssemblyName>SMZ3CasRandomizer</AssemblyName>
<ApplicationIcon>Assets\smz3.ico</ApplicationIcon>
<RootNamespace>$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace>
Expand Down

0 comments on commit 0d3a518

Please sign in to comment.