Skip to content

Commit

Permalink
added notifications, adjusted ip fetch to get ipv4, prevented actions…
Browse files Browse the repository at this point in the history
… from taking progression items
  • Loading branch information
TalicZealot committed Apr 10, 2021
1 parent 062cc8e commit eff386b
Show file tree
Hide file tree
Showing 15 changed files with 263 additions and 64 deletions.
Binary file added Resources/Images/SotnTextBox.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using BizHawk.Client.Common;
using NSubstitute;
using SotnApi.Interfaces;
using SotnRandoTools.Configuration;
using SotnRandoTools.Configuration.Interfaces;
using SotnRandoTools.RandoTracker.Interfaces;
using SotnRandoTools.Services;
Expand Down Expand Up @@ -56,11 +57,58 @@ public void ThrowArgumentNullException_WhenWatchlistServiceIsNull()
}

[Fact]
public void ThrowArgumentNullException_WhenRenderingApiIsNull()
{
//Arrange
var mockedGraphics = Substitute.For<IGraphics>();
var mockedToolConfig = Substitute.For<IToolConfig>();
var mockedwatchlistService = Substitute.For<IWatchlistService>();
var mockedRenderingApi = Substitute.For<IRenderingApi>();
var mockedGameApi = Substitute.For<IGameApi>();
var mockedAlucardApi = Substitute.For<IAlucardApi>();
//Act&Assert
Assert.Throws<ArgumentNullException>(() => new Tracker(mockedGraphics, mockedToolConfig, mockedwatchlistService, null, mockedGameApi, mockedAlucardApi));
}

[Fact]
public void ThrowArgumentNullException_WhenGameApiIsNull()
{
//Arrange
var mockedGraphics = Substitute.For<IGraphics>();
var mockedToolConfig = Substitute.For<IToolConfig>();
var mockedwatchlistService = Substitute.For<IWatchlistService>();
var mockedRenderingApi = Substitute.For<IRenderingApi>();
var mockedGameApi = Substitute.For<IGameApi>();
var mockedAlucardApi = Substitute.For<IAlucardApi>();
//Act&Assert
Assert.Throws<ArgumentNullException>(() => new Tracker(mockedGraphics, mockedToolConfig, mockedwatchlistService, mockedRenderingApi, null, mockedAlucardApi));
}

[Fact]
public void ThrowArgumentNullException_WhenAlucardApiIsNull()
{
//Arrange
var mockedGraphics = Substitute.For<IGraphics>();
var mockedToolConfig = Substitute.For<IToolConfig>();
var mockedwatchlistService = Substitute.For<IWatchlistService>();
var mockedRenderingApi = Substitute.For<IRenderingApi>();
var mockedGameApi = Substitute.For<IGameApi>();
var mockedAlucardApi = Substitute.For<IAlucardApi>();
//Act&Assert
Assert.Throws<ArgumentNullException>(() => new Tracker(mockedGraphics, mockedToolConfig, mockedwatchlistService, mockedRenderingApi, mockedGameApi, null));
}

[Fact(Skip = "Skip for now, too many file reads. Would be more of an integration test atm.")]
public void ReturnAnInstance_WhenParametersAreNotNull()
{
//Arrange
var mockedGraphics = Substitute.For<IGraphics>();
var mockedToolConfig = Substitute.For<IToolConfig>();
TrackerConfig stubTrackerConfig = new TrackerConfig();
stubTrackerConfig.Locations = false;
mockedToolConfig
.Tracker
.Returns<TrackerConfig>(stubTrackerConfig);
var mockedwatchlistService = Substitute.For<IWatchlistService>();
var mockedRenderingApi = Substitute.For<IRenderingApi>();
var mockedGameApi = Substitute.For<IGameApi>();
Expand Down
3 changes: 3 additions & 0 deletions SotnRandoTools/src/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,6 @@ dotnet_diagnostic.CS8602.severity = silent

# CS8618: Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
dotnet_diagnostic.CS8618.severity = suggestion

# CS8604: Possible null reference argument.
dotnet_diagnostic.CS8604.severity = suggestion
1 change: 1 addition & 0 deletions SotnRandoTools/src/Constants/Paths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static class Paths
public const string WarpsAndShortcutsWatchPath = "./ExternalTools/SotnRandoTools/Watches/WarpsAndShortcuts.wch";

public const string ImagesPath = "./ExternalTools/SotnRandoTools/Images/";
public const string TextboxImage = "./ExternalTools/SotnRandoTools/Images/SotnTextBox.png";

public const string CasualPresetPath = "./ExternalTools/SotnRandoTools/Presets/casual.json";
public const string SafePresetPath = "./ExternalTools/SotnRandoTools/Presets/safe.json";
Expand Down
18 changes: 11 additions & 7 deletions SotnRandoTools/src/Coop/CoopMessanger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class CoopMessanger : ICoopMessanger
private SimpleTcpServer? server;
private SimpleTcpClient? client;
private string connectedClientAddress = "";
private bool manualDisconnect = false;

public CoopMessanger(IToolConfig toolConfig, ICoopReceiver coopReceiver)
{
Expand All @@ -31,9 +32,6 @@ public bool Connect(string hostIp, int port)
client = new SimpleTcpClient(hostIp, port);
}

//Causes disconnects instead of keeping the connection up.
//client.Keepalive.EnableTcpKeepAlives = true;

try
{
client.ConnectWithRetries(4 * 1000);
Expand All @@ -55,6 +53,7 @@ public void Disconnect()
{
if (client is not null)
{
manualDisconnect = true;
client.Disconnect();
client.Dispose();
}
Expand All @@ -73,9 +72,6 @@ public bool StartServer(int port)
server.Events.DataReceived += DataReceived;
}

//Causes disconnects instead of keeping the connection up.
//server.Keepalive.EnableTcpKeepAlives = true;

server.Start();
string myIP = WebRequests.getExternalIP().Replace("\n", "");
System.Windows.Forms.Clipboard.SetText(myIP + ":" + port);
Expand Down Expand Up @@ -122,8 +118,16 @@ private void Connected(object sender, ClientConnectedEventArgs e)

private void Disconnected(object sender, ClientDisconnectedEventArgs e)
{
client.Dispose();
Console.WriteLine($"Disconnected from host.");
if (!manualDisconnect)
{
Console.WriteLine($"Attempting to reconnect...");
client.ConnectWithRetries(2 * 1000);
}
else
{
manualDisconnect = false;
}
}

private void ClientConnected(object sender, ClientConnectedEventArgs e)
Expand Down
23 changes: 11 additions & 12 deletions SotnRandoTools/src/Coop/CoopReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ public class CoopReceiver : ICoopReceiver
private readonly IToolConfig toolConfig;
private readonly IGameApi gameApi;
private readonly IAlucardApi alucardApi;
private readonly IGuiApi guiApi;
private readonly INotificationService notificationService;
private readonly IWatchlistService watchlistService;

public CoopReceiver(IToolConfig toolConfig, IGameApi gameApi, IAlucardApi alucardApi, IGuiApi guiApi, IWatchlistService watchlistService)
public CoopReceiver(IToolConfig toolConfig, IGameApi gameApi, IAlucardApi alucardApi, INotificationService notificationService, IWatchlistService watchlistService)
{
if (toolConfig is null) throw new ArgumentNullException(nameof(toolConfig));
if (gameApi is null) throw new ArgumentNullException(nameof(gameApi));
if (alucardApi is null) throw new ArgumentNullException(nameof(alucardApi));
if (guiApi is null) throw new ArgumentNullException(nameof(guiApi));
if (notificationService is null) throw new ArgumentNullException(nameof(notificationService));
if (watchlistService is null) throw new ArgumentNullException(nameof(watchlistService));
this.toolConfig = toolConfig;
this.gameApi = gameApi;
this.alucardApi = alucardApi;
this.guiApi = guiApi;
this.notificationService = notificationService;
this.watchlistService = watchlistService;
}

Expand All @@ -45,20 +45,20 @@ public void ProcessMessage(byte[] data)
alucardApi.GrantRelic((Relic) index);
watchlistService.UpdateWatchlist(watchlistService.CoopRelicWatches);
watchlistService.CoopRelicWatches.ClearChangeCounts();
guiApi.AddMessage($"Received relic: {(Relic) index}");
notificationService.DisplayMessage(((Relic) index).ToString());
Console.WriteLine($"Received relic: {(Relic) index}");
}
break;
case MessageType.Location:
gameApi.SetRoomToVisited(watchlistService.CoopLocationWatches[index].Address);
watchlistService.UpdateWatchlist(watchlistService.CoopLocationWatches);
watchlistService.CoopLocationWatches.ClearChangeCounts();
guiApi.AddMessage($"Received location: {watchlistService.CoopLocationWatches[index].Notes}");
notificationService.DisplayMessage(watchlistService.CoopLocationWatches[index].Notes);
Console.WriteLine($"Received location: {watchlistService.CoopLocationWatches[index].Notes}");
break;
case MessageType.Item:
alucardApi.GrantItemByName(Equipment.Items[index]);
guiApi.AddMessage($"Received item: {Equipment.Items[index]}");
notificationService.DisplayMessage(Equipment.Items[index]);
Console.WriteLine($"Received item: {Equipment.Items[index]}");
break;
case MessageType.Effect:
Expand All @@ -68,14 +68,14 @@ public void ProcessMessage(byte[] data)
alucardApi.GrantFirstCastleWarp((Warp) index);
watchlistService.UpdateWatchlist(watchlistService.WarpsAndShortcutsWatches);
watchlistService.WarpsAndShortcutsWatches.ClearChangeCounts();
guiApi.AddMessage($"Received warp: {(Warp) index}");
notificationService.DisplayMessage(((Warp) index).ToString());
Console.WriteLine($"Received warp: {(Warp) index}");
break;
case MessageType.WarpSecondCastle:
alucardApi.GrantSecondCastleWarp((Warp) index);
watchlistService.UpdateWatchlist(watchlistService.WarpsAndShortcutsWatches);
watchlistService.WarpsAndShortcutsWatches.ClearChangeCounts();
guiApi.AddMessage($"Received warp: Inverted {(Warp) index}");
notificationService.DisplayMessage(((Warp) index).ToString());
Console.WriteLine($"Received warp: Inverted {(Warp) index}");
break;
case MessageType.Shortcut:
Expand All @@ -85,7 +85,7 @@ public void ProcessMessage(byte[] data)
break;
case MessageType.Settings:
DecodeSettings((int) index);
guiApi.AddMessage($"Received co-op settings from host.");
notificationService.DisplayMessage("Connected");
Console.WriteLine($"Received co-op settings from host.");
break;
default:
Expand Down Expand Up @@ -244,7 +244,7 @@ private void DecodeShortcut(Shortcut shortcut)
return;
}

guiApi.AddMessage($"Received shortcut: {shortcut}");
notificationService.DisplayMessage(shortcut.ToString());
Console.WriteLine($"Received shortcut: {shortcut}");
}

Expand All @@ -261,7 +261,6 @@ private void DecodeAssist(string item)
return;
}

guiApi.AddMessage($"Received assist: {item}");
Console.WriteLine($"Received assist: {item}");
}
}
Expand Down
4 changes: 2 additions & 2 deletions SotnRandoTools/src/CoopForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public partial class CoopForm : Form
private readonly CoopMessanger coopMessanger;
private readonly CoopReceiver coopReceiver;

public CoopForm(IToolConfig toolConfig, IWatchlistService watchlistService, IGameApi gameApi, IAlucardApi alucardApi, IJoypadApi joypadApi, IGuiApi guiApi)
public CoopForm(IToolConfig toolConfig, IWatchlistService watchlistService, IGameApi gameApi, IAlucardApi alucardApi, IJoypadApi joypadApi, INotificationService notificationService)
{
if (toolConfig is null) throw new ArgumentNullException(nameof(toolConfig));
this.toolConfig = toolConfig;

this.coopReceiver = new CoopReceiver(toolConfig, gameApi, alucardApi, guiApi, watchlistService);
this.coopReceiver = new CoopReceiver(toolConfig, gameApi, alucardApi, notificationService, watchlistService);
this.coopMessanger = new CoopMessanger(toolConfig, coopReceiver);
this.coopSender = new CoopSender(toolConfig, watchlistService, gameApi, alucardApi, joypadApi, coopMessanger);
InitializeComponent();
Expand Down
Loading

0 comments on commit eff386b

Please sign in to comment.