Skip to content

Commit

Permalink
- Upgraded buttplug.io library to V3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
RonSijm committed Jan 7, 2023
1 parent 49bf769 commit b2ec1fa
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 44 deletions.
18 changes: 9 additions & 9 deletions RonSijm.ButtFish.Tests/FENToAsciiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ public class FENToAsciiTest
[Fact]
public void Should_Be_Able_To_Convert_White_Board_To_Ascii()
{
var fenCode = "k7/q1pppp2/p1bP4/r1n1P3/n1r2P1P/b5Pp/P4RpP/RNBQKBNp w Q - 0 1";
var fenCode = "1bb2QQQ/bbbb1Q2/bbbbbQ2/bbbbbQKQ/bbbbbQ2/kbbb1Q2/rrr2Q2/rrr2Q2 w - - 0 1";
var boardModel = fenCode.ConvertToCharArray();
var whiteToModel = fenCode.IsWhiteToMove();

var boardAscii = BoardToAscii.ToAscii(boardModel, whiteToModel);

var expected = @" ╔═══╤═══╤═══╤═══╤═══╤═══╤═══╤═══╗
8 ║ k │   │   │   │   │   │   │   ║
8 ║   │ b │ b │   │   │ Q │ Q │ Q ║
╟───┼───┼───┼───┼───┼───┼───┼───╢
7 ║ q │   │ p │ p │ p │ p │   │   ║
7 ║ b │ b │ b │ b │   │ Q │   │   ║
╟───┼───┼───┼───┼───┼───┼───┼───╢
6 ║ p │   │ b │ P │   │   │   │   ║
6 ║ b │ b │ b │ b │ b │ Q │   │   ║
╟───┼───┼───┼───┼───┼───┼───┼───╢
5 ║ r │   │ n │   │ P │   │   │   ║
5 ║ b │ b │ b │ b │ b │ Q │ K │ Q ║
╟───┼───┼───┼───┼───┼───┼───┼───╢
4 ║ n │   │ r │   │   │ P │   │ P ║
4 ║ b │ b │ b │ b │ b │ Q │   │   ║
╟───┼───┼───┼───┼───┼───┼───┼───╢
3 ║ b │   │   │   │   │   │ P │ p ║
3 ║ k │ b │ b │ b │   │ Q │   │   ║
╟───┼───┼───┼───┼───┼───┼───┼───╢
2 ║ P │   │   │   │   │ R │ p │ P ║
2 ║ r │ r │ r │   │   │ Q │   │   ║
╟───┼───┼───┼───┼───┼───┼───┼───╢
1 ║ R │ N │ B │ Q │ K │ B │ N │ p ║
1 ║ r │ r │ r │   │   │ Q │   │   ║
╚═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╝
a b c d e f g h
";
Expand Down
2 changes: 1 addition & 1 deletion RonSijm.ButtFish/Ascii/BoardToAscii.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static string ToAscii(char[,] model, bool isWhiteToMove)

foreach (var horizontal in Loop.Between(0, 8, !isWhiteToMove))
{
bob.Append(" " + (8 - horizontal) + "");
bob.Append($" {(8 - horizontal)} \u2551");

foreach (var vertical in Loop.Between(0, 8, !isWhiteToMove))
{
Expand Down
57 changes: 40 additions & 17 deletions RonSijm.ButtFish/Connectors/ButtplugConnector.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Buttplug;
using Buttplug.Client;
using Buttplug.Client.Connectors.WebsocketConnector;
using RonSijm.ButtFish.Devices;

namespace RonSijm.ButtFish.Connectors;

Expand All @@ -8,29 +10,50 @@ public class ButtplugConnector

public async Task<List<IDeviceAbstraction>> GetDiscoveredDevices(string discoverAddress = null)
{
Console.WriteLine(discoverAddress == null ? "Starting to scan for ButtPlug Devices..." : $"Attempting to connect to {discoverAddress}...");

Console.WriteLine("Will return results after 5 seconds.", Color.Green);

if (discoverAddress == null)
{
await _client.ConnectAsync(new ButtplugEmbeddedConnectorOptions());
}
else
try
{
if (!discoverAddress.Contains("://"))
Console.WriteLine(discoverAddress == null ? "Starting to scan for ButtPlug Devices..." : $"Attempting to connect to {discoverAddress}...");

Console.WriteLine("Will return results after 5 seconds.", Color.Green);

if (discoverAddress == null)
{
await _client.ConnectAsync(new ButtplugWebsocketConnector(new Uri("ws://localhost:12345")));
}
else
{
discoverAddress = $"ws://{discoverAddress}";
if (!discoverAddress.Contains("://"))
{
discoverAddress = $"ws://{discoverAddress}";
}

await _client.ConnectAsync(new ButtplugWebsocketConnector(new Uri(discoverAddress)));
}

await _client.ConnectAsync(new ButtplugWebsocketConnectorOptions(new Uri(discoverAddress)));
await _client.StartScanningAsync();

// Wait 5 seconds on ButtplugClient to find results
await Task.Delay(5000);

return _client.Devices.Select(locatedDevice => new ButtDevice(locatedDevice)).Cast<IDeviceAbstraction>().ToList();
}
catch (ButtplugClientConnectorException e)
{
var innerException = e.InnerException;
Console.WriteLine($"ButtplugConnector error: {innerException?.Message.Replace(" More detailed information in inner exception.", string.Empty)}", Color.Red);

await _client.StartScanningAsync();
if (innerException?.InnerException?.Message != null)
{
Console.WriteLine($"Details: {innerException?.InnerException.Message}", Color.Red);
}

// Wait 5 seconds on ButtplugClient to find results
await Task.Delay(5000);
Console.WriteLine("Try manually configuring URI for Intiface Central instead", Color.Red);
}
catch (Exception e)
{
Console.WriteLine($"ButtplugConnector error: {e}", Color.Red);
}

return _client.Devices.Select(locatedDevice => new ButtDevice(locatedDevice)).Cast<IDeviceAbstraction>().ToList();
return new List<IDeviceAbstraction>();
}
}
4 changes: 3 additions & 1 deletion RonSijm.ButtFish/Connectors/DeviceConnector.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace RonSijm.ButtFish.Connectors;
using RonSijm.ButtFish.Devices;

namespace RonSijm.ButtFish.Connectors;

public class DeviceConnector
{
Expand Down
3 changes: 2 additions & 1 deletion RonSijm.ButtFish/Connectors/YeelightConnector.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using YeelightAPI;
using RonSijm.ButtFish.Devices;
using YeelightAPI;

namespace RonSijm.ButtFish.Connectors;

Expand Down
4 changes: 3 additions & 1 deletion RonSijm.ButtFish/DeviceBroadcaster.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace RonSijm.ButtFish;
using RonSijm.ButtFish.Devices;

namespace RonSijm.ButtFish;

public class DeviceBroadcaster
{
Expand Down
4 changes: 3 additions & 1 deletion RonSijm.ButtFish/DeviceDiscoveryManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace RonSijm.ButtFish;
using RonSijm.ButtFish.Devices;

namespace RonSijm.ButtFish;

public class DeviceDiscoveryManager
{
Expand Down
7 changes: 3 additions & 4 deletions RonSijm.ButtFish/Devices/ButtDevice.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Buttplug;
using Buttplug.Client;

namespace RonSijm.ButtFish.Devices;

Expand All @@ -16,14 +16,13 @@ public ButtDevice(ButtplugClientDevice device)

public async Task SendDuration(int time)
{
await _device.SendVibrateCmd(1);
await _device.VibrateAsync(1);
await Task.Delay(time);
await _device.SendVibrateCmd(0);
await _device.VibrateAsync(0);
}

public void Dispose()
{
_device.Dispose();
GC.SuppressFinalize(this);
}

Expand Down
2 changes: 1 addition & 1 deletion RonSijm.ButtFish/Devices/ConsoleOutputDevice.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace RonSijm.ButtFish.Models;
namespace RonSijm.ButtFish.Devices;

public class ConsoleOutputDevice : IDeviceAbstraction
{
Expand Down
2 changes: 1 addition & 1 deletion RonSijm.ButtFish/Devices/IDeviceAbstraction.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace RonSijm.ButtFish.Models;
namespace RonSijm.ButtFish.Devices;

public interface IDeviceAbstraction : IDisposable
{
Expand Down
10 changes: 9 additions & 1 deletion RonSijm.ButtFish/InputLoops/FENBasedLoop.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace RonSijm.ButtFish.InputLoops;
using RonSijm.ButtFish.Devices;

namespace RonSijm.ButtFish.InputLoops;

public class FENBasedLoop : IInputLoop
{
Expand Down Expand Up @@ -56,6 +58,12 @@ public async Task Start(IList<IDeviceAbstraction> devices)

var nextPosition = _iuciEngine.GetBestMove();

if (nextPosition == null)
{
Console.WriteLine("Engine was not able to find a next best move", Color.Orange);
continue;
}

if (_options.EndPositionOnly)
{
nextPosition = nextPosition.Substring(2, 2);
Expand Down
4 changes: 3 additions & 1 deletion RonSijm.ButtFish/InputLoops/IInputLoop.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace RonSijm.ButtFish.InputLoops;
using RonSijm.ButtFish.Devices;

namespace RonSijm.ButtFish.InputLoops;

public interface IInputLoop
{
Expand Down
4 changes: 3 additions & 1 deletion RonSijm.ButtFish/InputLoops/ManualInputLoop.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace RonSijm.ButtFish.InputLoops;
using RonSijm.ButtFish.Devices;

namespace RonSijm.ButtFish.InputLoops;

public class ManualInputLoop : IInputLoop
{
Expand Down
2 changes: 0 additions & 2 deletions RonSijm.ButtFish/Properties/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
global using Color = System.Drawing.Color;

global using RonSijm.ButtFish.Connectors;
global using RonSijm.ButtFish.Models;

global using RonSijm.ButtFish.Ascii;
global using RonSijm.ButtFish.Encoders;
global using RonSijm.UCIEngineInterop.Core;
Expand Down
3 changes: 2 additions & 1 deletion RonSijm.ButtFish/RonSijm.ButtFish.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Buttplug" Version="2.0.6" />
<PackageReference Include="Buttplug" Version="3.0.0" />
<PackageReference Include="Buttplug.Client.Connectors.WebsocketConnector" Version="3.0.0" />
<PackageReference Include="Colorful.Console" Version="1.2.15" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.1" />
Expand Down
2 changes: 1 addition & 1 deletion RonSijm.ButtFish/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

"TimeUnitInMS": 400,

"UseManualInput": true
"UseManualInput": false
}
17 changes: 17 additions & 0 deletions RonSijm.UCIEngineInterop/Core/Stockfish.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ private void GoTime(int time)
private List<string> ReadLineAsList()
{
var data = UCIEngineProcess.ReadLine();

if (data == null)
{
return null;
}

return data.Split(' ').ToList();
}

Expand All @@ -126,6 +132,12 @@ public string GetFenPosition()
}

var data = ReadLineAsList();

if (data == null)
{
return null;
}

if (data[0] == "Fen:")
{
return string.Join(" ", data.GetRange(1, data.Count - 1));
Expand Down Expand Up @@ -154,6 +166,11 @@ public string GetBestMove()

var data = ReadLineAsList();

if (data == null)
{
return null;
}

if (data[0] == "bestmove")
{
if (data[1] == "(none)")
Expand Down

0 comments on commit b2ec1fa

Please sign in to comment.